• Advertisement
Sign in to follow this  

[Lua] Store references to objects in hash table, or look up each time?

This topic is 2194 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Simplified version of what I'm doing:

Basic text adventure engine. Rooms and game objects are stored in hash tables. Rooms themselves are a table containing, among other things, a table indicating what their contents are.

The contents table could be two things:

1) A list of object names which could be used to look up the actual game objects from that hash table.
B) A list of references to said objects.

Which would be preferable?

Share this post


Link to post
Share on other sites
Advertisement
Personally, I would prefer the contents table have a list of object names or IDs that can be used to look the objects up. That would greatly simplify the tweaking process of editing and balancing items, allowing you to edit the item definition in only one place (the main lookup table), rather than going through all the room contents tables and editing the item definitions there.

Share this post


Link to post
Share on other sites
Benchmark if you doubt.

Without benchmark, I guess reference (if you mean Lua reference) has better performance because it uses integer keys rather than string?

Share this post


Link to post
Share on other sites

Personally, I would prefer the contents table have a list of object names or IDs that can be used to look the objects up.


As I said, all of the objects are stored in a hash table. The local contents tables in the rooms are just either going to hold strings that the actual object can be looked up from the hash table with, or a references to the objects in the hash table.

wqking, references SHOULD be faster -- from my understanding not because they use integer keys, but because... well, apparently local variables are faster to access in lua because they're stored in registers and not the global hash table, and register lookup is faster than table lookup, which I think is the case. However, table lookup in Lua is still supposed to be pretty fast -- tables are like, its main data structure, so it's optimized for those.

The problem with references is that, as data, they're a bit fiddlier to work with, and may or may not lead to a leakier abstraction than I'd have otherwise.

The other problem is I'm not entirely sure how the Lua garbage collector works, or how Lua handles references as a whole, so what I'm assuming are fairly permanent references might actually be not that permanent.

Share this post


Link to post
Share on other sites

The problem with references is that, as data, they're a bit fiddlier to work with, and may or may not lead to a leakier abstraction than I'd have otherwise.


Then wrap the operation of the reference management in some object.
The object may needs fiddly, but using the object should be easier.


The other problem is I'm not entirely sure how the Lua garbage collector works, or how Lua handles references as a whole, so what I'm assuming are fairly permanent references might actually be not that permanent.


If you hold a Lua reference, Lua won't GC the object that referenced. When you unref it, the object may be GCed.
That's to say, holding a Lua reference will make the object always live.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement