lua-users home
lua-l archive

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


On 7 July 2014 at 10:12, Paco Zamora Martínez <pakozm@gmail.com> wrote:
>>
>> > yield( splits[i-1][1]..word[i+1]..word[i]..splits[i+2][2] )
>>
> I tried table.concat and string.format, but both perform worst. This was
> counter-intuitive to me, because Lua string concat generates copies of
> intermediate strings. However, seems that for short strings and small number
> of concatenated strings, string __concat performs better than string.format
> or table.concat. Does anyone know if my observation is true?

The "folk wisdom" about copies of intermediate strings in Lua is often
mis-stated, I think.

("aa"):upper() .. ("bb"):upper() .. ("cc"):upper() .. ("dd"):upper()

It translates to a single concatenation bytecode in both Lua and
LuaJIT, so it produces the following strings:

"aa"
"bb"
"cc"
"AA"
"BB"
"CC"
"DD"
"AABBCCDD"

This does generate intermediate strings:

local s = ""
for _, w in ipairs({"aa", "bb", "cc", "dd"})
   s = s .. w:upper()
end

It produces

""
"aa"
"bb"
"cc"
"AA"
"BB"
"CC"
"AABB"
"AABBCC"
"AABBCCDD"

And this second pattern is the one that people tell to avoid when they
talk about "intermediate strings". For that, one should do instead:


local t = {}
for _, w in ipairs({"aa", "bb", "cc", "dd"})
   table.insert(s, w:upper())
end
local s = table.concat(t)

That will produce:

"aa"
"bb"
"cc"
"AA"
"BB"
"CC"
"AABBCCDD"

plus an extra table. Of course this is an oversimplified example for
illustration purposes, but often the loop is long and the naive
approach above can produce a huge pyramid of intermediate strings.

Over the years, the sensible advice was somehow distorted into some
"all string concatenation is evil" cargo-cult, but that is not true,
especially for short sequences of concatenations in an expression.
Using a..b..c will usually be cheaper and produce less garbage than
either string.format("%s%s%s", a, b, c) or table.concat({a, b, c}).

I guess I should turn this email into a blog post titled "Lua string
concatenation considered not harmful" :)

-- Hisham