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

Started by
3 comments, last by wqking 12 years, 3 months ago
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?
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.
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?

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.


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.

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.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

This topic is closed to new replies.

Advertisement