Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 MeshGearFox   Members   -  Reputation: 158

Like
0Likes
Like

Posted 20 January 2012 - 11:43 PM

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?

Sponsor:

#2 JTippetts   Moderators   -  Reputation: 8409

Like
0Likes
Like

Posted 21 January 2012 - 07:38 AM

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.

#3 wqking   Members   -  Reputation: 756

Like
-1Likes
Like

Posted 21 January 2012 - 09:43 AM

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?

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#4 MeshGearFox   Members   -  Reputation: 158

Like
0Likes
Like

Posted 21 January 2012 - 04:59 PM

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.

#5 wqking   Members   -  Reputation: 756

Like
-1Likes
Like

Posted 21 January 2012 - 09:34 PM

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.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS