lua-users home
lua-l archive

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


On Thu, Dec 06, 2018 at 03:22:45PM -0800, Sven Olsen wrote:
> >
> > Are you an advocate only for +=. or for similar operators for all
> > binary operators?
> >
> 
> As someone who's implemented and used `..=`, my 2 cents would be that
> adding it to the language is actually unwise.  If you're going to create a
> large string through repeated concatenations, you should almost certainly
> be using some kind of pattern that leverages table.concat().  It's far more
> efficient because it avoids the allocation of intermediate strings.  `..=`
> just creates the temptation to build long strings in a way that wastes a
> lot of memory and processing time. I'm still cleaning up ancient parts of
> my own codebase where I used `..=` when I should have used table.concat().
> 
> Arithmetic compound assignment ops are nice to have, but, the
> generalization to a compound assignment for all binary ops is a bad idea,
> imo.
> 

FWIW, string concatenation operators in Perl, for example, are very
efficient because Perl strings are mutable, and the parser and interpreter
are very clever about optimizing string ops, including implementing CoW.
Code like

  $foo .= $a . $b . $c

is basically a memcpy from $a into $foo, $b into $foo and then $c into $foo.
Much of Perl's crazy syntax, semantics, and insanely ugly internals is a
product of the emphasis on optimizing string operations.

For a language like Lua I think a better solution to performant string
construction would be something like Java's StringBuffer. This is trivial to
do in Lua because the language emphasizes making use of the C API, and
userdata objects are first-class in terms of language treatment. The real
issue is that the Lua ecosystem isn't a batteries included kind of
environment.