lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Rici Lake wrote:

On 10-Oct-05, at 1:31 PM, Chris Marrin wrote:

Adam D. Moss wrote:

Posti Laatikko wrote:

Since lua is intepreted language i was wondering does variables & funtions
names effect execution speed if names are long?

Nope - they're just strings which are interned and then
compared/hashed by pointer.


But technically a long string takes longer to do the initial hash, right? So I can imagine a situation where a string must be interned at runtime (constructed on-the-fly) and where you might see some runtime performance issues for long strings vs. short strings.


It's hard to imagine a scenario where you're creating a string at runtime which is not at least O(n) on the size of the string you're creating. However, Lua's hash function only examines a maximum number of character (*not* the first k characters, but rather k characters spread through the string), so it is not dependent on the length of the string. Arguably, there are more collisions with long strings as a result of the partial hashing, but I haven't actually observed this in practice.


Yes, this is kind of a silly discussion (started by me, of course). I was only trying to point out that there are cases where the hashing of strings happens at runtime and therefore has >0 effect on performance.

This actually bites me, and I assume any user of a C++ interface to Lua. For instance, I have a C++ function that looks like this:

    obj->setProperty("foo", 7.5);

This places a numeric property in obj (which looks like a table to Lua) with the name "foo". But this has to hash "foo" every time you call this function because the string is pushed with:

    lua_pushstring(L, "foo");

I have tried a few ways to try to internalize strings like this so that they don't have to be hashed every time. But everything I have tried has either had enough overhead that it was not worth the obfuscation it would cause on the C++ side.

For instance, I tried making an Atom class which would store strings in the registry and keep the ref in the Atom instance. Then you would do something like this:

    obj->setProperty(fooAtom, 7.5);

this would push the string from the registry without having to hash it again. But this had enough overhead that it was not worth it.

It would be nice if Lua had an efficient way to expose internalized strings to C. Anyone know of any good tricks?

--
chris marrin              ,""$, "As a general rule,don't solve puzzles
chris@marrin.com        b`    $  that open portals to Hell" ,,.
        ,.`           ,b`    ,`                            , 1$'
     ,|`             mP    ,`                              :$$'     ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`