|
Jamie Webb wrote:
You are correct in thinking that version A will create and then immediately delete a key, but I'm not sure what you mean by fragmentation in the registry. AIUI all that will happen in net is that should the created key collide with another, the free pointer will be incremented, causing a rehash to happen sooner.
Sorry, I used the word 'fragmentation' wrongly. I meant the issues you described.
lua_rawset() doesn't behave 'intelligently', but you'll probably have to do some benchmarks to find out which is actually faster. The tradeoff is an extra lookup for every key you delete versus the possibility of a rehash. It probably depends on how many keys you expect to be nonexistent in relation the the total keys you're dealing with and the total size of the registry.
Hmm. I skimmed throught the ltable.c lapi.c and saw that lua_rawget() is pretty cheap. On the other hand I won't call lua_rawset() expensive, but lua_rawset() isn't very cheap. It has to do much more work as lua_rawget().
I suspect in most cases you'll find there's very little difference.
I decided to use lua_rawget() first to check if the key exists. In a relativ short living programm the run time difference will be not measurably in practice, IMHO.
In my case, we are talking about 8 keys for each handle to my wrapper. If somebody only obtain one handle, and use it as long as possible, it doesn't matters. But for example, if somebody obtains and release a handle to my wrapper in a long living loop, for examples 1000 times, this will create upto 8000 unused keys in the registry. IMHO 8000 keys in the registry are not so nice.
IIRC, I read somewhere, that lua tables are pretty fast if they don't contain to much keys. So the idea is to trade some very small amount of CPU time (cheap lua_rawget) to keep the overall performace of the registry.
Attachment:
signature.asc
Description: OpenPGP digital signature