String references

In my code, I need to keep track of a specific value (string, always ...) in local. I would like to know if the runtime will re-create or this line after putting it in the local version in the official Lua 5.3 releases. Any ideas? In this lua.org document I have at least heard that the Lua implementation does string internalization (keep one copy of any string).

I am restarting my code, so I have been doing minor things so far. An example of what I can do for each function is:

local src = l[1]

-- `src` would hold a string

      

+3


source to share


1 answer


Whether the strings are interned or not, this is not really a problem. String interpretation is simply a mechanism for speeding up string comparisons and (possibly) saving some memory over the processor needed to create the string.

The important thing is that strings in lua are what they usually call reference types

. This means that runtime values ​​are stored and exchanged with string references, and assigning a string to a runtime value is just copying the pointer and setting the correct tag for that value.

Another thing your code does is avoid doing multiple hash lookups during your function execution. For example,

local a       = tbl['mykey']
-- ...
local other_a = tbl['mykey']

      



will result in two hash lookups, and

local cached_a = tbl['mykey']
-- ...
local a = cached_a
-- ...
local other_a = cached_a

      

will reduce it to one search. But again, this usually doesn't matter much for whole keys. But sometimes even whole keys trigger a hash lookup, even if they are small. Also, it is implementation dependent. Lua is very simple.

+3


source







All Articles