lua-users home
lua-l archive

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

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"

...? (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)

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!).

┌─── ───── ─────
│ ⍎'⎕',∊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