本機 Lua 中的高效可變字節數組 (Efficient mutable byte array in native Lua)


問題描述

本機 Lua 中的高效可變字節數組 (Efficient mutable byte array in native Lua)

我正在嘗試在本地 Lua 中高效地實現 LZ77 解碼器(即沒有 C 庫,並且不依賴於非核心 Lua 庫) ‑ 請參閱 liblzg.

對於加載和解析二進製文件,Lua 字符串可以完美運行,並且性能良好(例如使用 s:byte(k) 方法) . 但是,對於創建解碼後的輸出數據,字符串並不是非常理想的,因為它們是不可變的,並且當輸出變大時,字符串連接往往會花費很多時間。

解碼器必須能夠:

  • 一次將一個字節附加到輸出(最多數百萬次)
  • 從輸出緩衝區讀取(或多或少隨機訪問)

什麼是最好的選擇?輸出數據的大小是事先知道的,因此可以預先分配。


參考解法

方法 1:

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

方法 2:

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)

(by marcus256lhfdaurnimator)

參考文件

  1. Efficient mutable byte array in native Lua (CC BY‑SA 3.0/4.0)

#compression #lua






相關問題

CSS 壓縮和組合 / js 縮小 - 在運行時或構建時更好? (CSS compression & combining / js minification - Better to do at runtime or at build time?)

在javascript中壓縮包含音頻PCM數據的blob (compress blob containing audio PCM data in javascript)

如何提高@font-face 的加載性能 (How to improve loading performance of @font-face)

ServiceStack 流壓縮 (ServiceStack Stream Compression)

有沒有辦法讀取壓縮存檔的屬性? (Is there any way to read the properties of a Compressed Archive?)

德爾福 2009 中的 Zlib (Zlib in Delphi 2009)

SQL 2008開啟頁面壓縮後如何回收空間? (How to reclaim space after turning on page compression in SQL 2008?)

本機 Lua 中的高效可變字節數組 (Efficient mutable byte array in native Lua)

在 JavaScript 中壓縮純文本? (Compressing plaintext in JavaScript?)

如何遞歸壓縮文件夾? (How to zip a folder recursively?)

使用 GZIPOutputStream 壓縮字符串 (Compressing a string using GZIPOutputStream)

壓縮後 1 KB 可以容納多少文本? (How much text I can fit in 1 kilobyte with compression?)







留言討論