Skip to content

Commit 35c2cc1

Browse files
committed
perf(lua): O(1) decode glyph lookup instead of brute-forcing 4 lengths
The decoder tried substring lengths 4,3,2,1 at every position — up to 4 string allocations + 4 table lookups per byte. A glyph's byte length is fixed by its UTF-8 leading byte (self-synchronizing; lengths never overlap), so utf8_char_length gives it in one step. One substring + one lookup per position now. Measured (random data, this machine, before -> after): decode 10MB: 7.77 -> 14.16 MB/s (1.8x) decode 5MB: 5.49 -> 19.87 MB/s (3.6x) decode 1MB: 9.62 -> 22.47 MB/s (2.3x) Lua test/test still passes (correctness preserved).
1 parent f09f945 commit 35c2cc1

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

bin/printable-binary

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -473,17 +473,17 @@ function PrintableBinary.decode(printable_string, options)
473473
matched_char = " "
474474
end
475475

476-
-- Try to match longest first (4-byte, then 3-byte, then 2-byte, then 1-byte)
476+
-- A glyph's byte length is fixed by its UTF-8 leading byte (self-synchronizing),
477+
-- so resolve it with ONE substring + lookup instead of brute-forcing all four
478+
-- lengths (the map has no 4-byte glyphs, and lengths never overlap in UTF-8).
477479
if not matched_char then
478-
for len = 4, 1, -1 do
479-
if i + len - 1 <= s_len then
480-
local sub = string.sub(cleaned_string, i, i + len - 1)
481-
if decode_map[sub] then
482-
result_bytes_as_chars[#result_bytes_as_chars + 1] = string.char(decode_map[sub])
483-
i = i + len
484-
matched_char = sub
485-
break
486-
end
480+
local len = utf8_char_length(string.byte(cleaned_string, i))
481+
if i + len - 1 <= s_len then
482+
local sub = string.sub(cleaned_string, i, i + len - 1)
483+
if decode_map[sub] then
484+
result_bytes_as_chars[#result_bytes_as_chars + 1] = string.char(decode_map[sub])
485+
i = i + len
486+
matched_char = sub
487487
end
488488
end
489489
end

0 commit comments

Comments
 (0)