[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Implemented fast string append and a new call a = string.append(a, b)
- From: David Given <dg@...>
- Date: Sat, 29 Nov 2008 21:49:05 +0000
Paul Sheer wrote:
[...]
> I have implemented a new API call string.append() to facilitate
> fast string concatination.
[...]
> When string.append() is called, it returns a special "flex" string...
[...]
> Which appends to the string in-place, finding the pointer in the
> hash table, and reallocating it. I was just a bit lame to understand
> the GC/hashtable implications.
This sounds very much as if your flexstring objects always return the
same hash value, even if the value changes. This is unlikely to work,
I'm afraid. Lots of things in Lua, like table lookups, rely on strings
being immutable. My immediate reaction is to wonder what happens if I do
this when using flexstrings:
t = {}
s = "foo"
t[s] = "9"
s.append("bar")
print(t["foo"])
...? (Although bear in mind that I'm *not* an expert when it comes to
Lua internals --- there could well be an easy way round this.)
In general, Lua programs never *do* intensive string concatenation like
this. If performance is important, they always use table.concat() instead...
s = {}
for i = 1, 10 do
s[#s+1] = tostring(i)
end
print(table.concat(s))
Also note that the addString() implementation in the PIL is written to
demonstrate an algorithm, and not to be fast --- you probably don't want
to be benchmarking against it (and certainly not as it stands. It's full
of table lookups!).
--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│
│ ⍎'⎕',∊N⍴⊂S←'←⎕←(3=T)⋎M⋏2=T←⊃+/(V⌽"⊂M),(V⊝"M),(V,⌽V)⌽"(V,V←1⎺1)⊝"⊂M)'
│ --- Conway's Game Of Life, in one line of APL
Attachment:
signature.asc
Description: OpenPGP digital signature