lua-users home
lua-l archive

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


The C "buffer" interface in Lua is designed specifically for this: you can append cheaply in it, and the price to pay is, you lose equality-checks-as-cheap-as-a-pointer-comparison. In practice, if you're not performance-bound, using a list-of-strings as your buffer (instead of a single string) gives good enough results without having to use C. table.concat(), which converts the latter into the former, is very effective (although you often never have to convert you list of strings into a single one)


On Thu, Jan 2, 2014 at 10:28 AM, Enrico Colombini <erix@erix.it> wrote:
On 02/01/2014 9.57, Thijs Schreijer wrote:
local first  = [[ some 100mb stuff here ]]
> local second = first
> local third  = first .. "x"

You have two strings here: 'first' and 'second' are the same string, 'third' is a different string.

Assignment does not duplicate strings. Moreover, because strings are interned, there is a single copy of strings with equal contents; if you produce another identical string by different means (e.g. by concatenation), after the interning operation a single copy of the string will exist (please correct me if I am wrong here).

".." is a dangerous operator when doing many concatenations or when operating on large strings.
table.concat and/or the C-level luaL_Buffer can be useful to reduce memory usage in these cases.

--
  Enrico




--
Fabien Fleutot
+---
| 33 chemin de Mange-Pommes
| 31520 Ramonville Saint-Agne -- France
| mobile: +33 6 28 06 09 97
| office: +33 5 61 00 06 49
| home: +33 5 61 75 05 67