lua-users home
lua-l archive

Re: string immutability

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


On Thursday, January 02, 2014 10:28:43 AM Enrico Colombini wrote:
> ".." 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.
The VM concat instruction is very efficient so a chained .. is no worse than 
table.concat. Where you get in trouble is building a string piecemeal in a 
loop.
Using a variation of your example...
 function GCAndShow(prompt)
 local before = collectgarbage "count"
 collectgarbage"collect"
 local after = collectgarbage "count"
 io.write(string.format('%s: %.1fKiB (%.1fKiB garbage)\n', 
 prompt, after, before-after))
 end
 GCAndShow "initial state"
 local s1 = string.rep('x', 99999)
 GCAndShow "allocated a large string"
 local s2 = s1..s1..s1..s1..s1..s1..s1..s1..s1..s1
 GCAndShow "concatenated string with `..'"
 local s3 = table.concat{s1,s1,s1,s1,s1,s1,s1,s1,s1,s1}
 assert(s2==s3)
 GCAndShow "concatenated string with table.concat"
 local s4 = ""
 for i=1,10 do s4 = s4..s1 end
 assert(s2==s4)
 GCAndShow "concatenated string with loop"
 local s5 = string.format("%s%s%s%s%s%s%s%s%s%s",
 s1,s1,s1,s1,s1,s1,s1,s1,s1,s1)
 assert(s2==s5)
 GCAndShow "concatenated string with string.format"
 s1,s2,s3,s4,s5 = nil
 GCAndShow "deleted all strings"
With Lua 5.1.5 (and nearly the same with LuaJIT 2.0.2)
initial state: 29.1KiB (3.6KiB garbage)
allocated a large string: 151.2KiB (163.0KiB garbage)
concatenated string with `..': 1347.5KiB (732.5KiB garbage)
concatenated string with table.concat: 1347.5KiB (732.7KiB garbage)
concatenated string with loop: 1347.5KiB (5029.5KiB garbage)
concatenated string with string.format: 1348.3KiB (732.5KiB garbage)
deleted all strings: 90.9KiB (1257.5KiB garbage)
With Lua 5.2.2
initial state: 24.9KiB (3.3KiB garbage)
allocated a large string: 122.6KiB (97.8KiB garbage)
concatenated string with `..': 1099.2KiB (0.0KiB garbage)
concatenated string with table.concat: 2075.8KiB (2344.0KiB garbage)
concatenated string with loop: 3052.3KiB (2441.4KiB garbage)
concatenated string with string.format: 4029.1KiB (3028.1KiB garbage)
deleted all strings: 25.1KiB (4004.1KiB garbage)
-- 
tom <telliamed@whoopdedo.org>

AltStyle によって変換されたページ (->オリジナル) /