lua-users home
lua-l archive

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


Am 25.08.2015 um 17:41 schröbte Ulrich Schmidt:


Am 25.08.2015 um 16:29 schrieb Patrick Donnelly:
On Tue, Aug 25, 2015 at 7:14 AM, Rena <hyperhacker@gmail.com> wrote:
Lua strings are immutable (they can't be changed), so there should
never be
multiple copies of a string in one Lua state[1].
[...]
[1] for very short strings it might keep multiple copies when this
would be
more efficient than keeping references, but this is an implementation
detail
that we don't need to know about. :-)
You have it reversed. Lua may have multiple copies of large strings
but not short strings (len(s) <= 40):

http://www.lua.org/source/5.3/lstring.c.html#luaS_newlstr

Ok now i am completely confused.
After reading your linked source i am back at my starting point, asuming
the only value assigned by reference is a table/list. All other are
assinged by value. So a
_stringvar1= stringvar2_ creates a copy of the source string.
(still assuming we are talking about strings of size 1kB+ as in my
program.)

No. Assignment only manipulates references. You can create new strings via

*   `lua_pushstring`/`lua_pushresult` (from C)
*   concatenation (`x .. y`)
*   a string literal in the cose (e.g. "hello world")
*   file input (e.g. `io.read`)
*   string manipulation (e.g. `s:gsub( "(.)", "%1"` ))
*   ... (basically all function calls that use `lua_pushstring` internally.)

In all theses cases new strings are *not* created if they are short (< 40 characters) and there already is a string object with the same contents.


Ulrich.


Philipp