Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient mutable byte array in native Lua

I am trying to make an efficient implementation of an LZ77 decoder in native Lua (i.e. no C library, and no dependencies on non-core Lua libraries) - see liblzg.

For loading and parsing the binary file, a Lua string works perfectly, and with good performance (e.g. using the s:byte(k) method). However, for creating the decoded output data, strings are not very optimal, since they are immutable, and string concatenation tends to take lots and lots of time when the output becomes large.

The decoder must be able to:

  • Append one byte to the output at a time (up to millions of times)
  • Read (more or less random access) from the output buffer

What are the best options? The size of the output data is known before hand, so it can be pre-allocated.

like image 272
marcus256 Avatar asked Nov 24 '10 10:11

marcus256


2 Answers

Avoid string concatenation: save output strings to a table and write all strings in it at the end. If you're concerned about the table getting too big, flush it periodically. See http://www.lua.org/pil/11.6.html

like image 64
lhf Avatar answered Oct 06 '22 15:10

lhf


Sounds like a perfect job for table.concat your output is just a table of bytes.

When you need to copy, you do it as you normally would for a table. eg:

for i=#output-5,9 do output[#output+1]=output[i] end

When you finally are done with the output stream, convert it to a string with str=table.concat(output)

like image 41
daurnimator Avatar answered Oct 06 '22 15:10

daurnimator