perf(table): sort from_data fold to avoid O(n^2) table builds#389
Merged
Conversation
Table.from_data/1 folds a plain map into split array/hash storage via put/3. It folded in map-iteration order, which for any map over 32 entries is by key hash rather than ascending. Dense positive-integer keys then arrived out of order: each missed put_array's contiguous fast path, parked in the hash, and was later drained by absorb_from_hash, where every drop_hash_key rejects over an O(n) order list. The result was an O(n^2) build for list- and integer-keyed encodes. Folding the pairs in sorted order feeds integer keys first and ascending (term order places numbers before other keys), so each append hits the O(1) array path and nothing parks. The sort is O(n log n). encode of a 4096-element int_list drops from ~22800 ns/elem to ~600 ns/elem (~38x) and the per-element curve is now flat. String-keyed maps (already linear) and decode are unchanged. Adds benchmarks/encode_decode.exs, the harness that isolated the regression by direction, container shape, and size. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
davydog187
approved these changes
Jul 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Lua.encode!/2(and any stdlib table built from a literal map) was O(n²) for lists and integer-keyed maps. This makes the build O(n log n), a ~38× speedup on the worst shape, with no behaviour change.Root cause
Table.from_data/1builds split array/hash storage by folding a plain map throughput/3. It folded in map-iteration order — and any Erlang map over 32 entries iterates by key hash, not ascending:So for N > 32 the dense integer keys arrived out of order. Each one missed
put_array's contiguous fast path (key == arr_n + 1), parked in the hash, and was later drained byabsorb_from_hash— where everydrop_hash_keydoes anEnum.rejectover an O(n)orderlist. N drains × O(n) reject = O(n²). String-keyed maps never hit this path (theyinsert_hashwith an O(1) prepend), which is why only integer-keyed shapes regressed.Fix
Fold the pairs in sorted order. Elixir term order places numbers before other keys and sorts integers ascending, so integer keys are inserted
1, 2, 3, …and every append takes the O(1) array path — nothing parks, nothing drains.Measurements
mix run benchmarks/encode_decode.exs,encodeofint_list,per_elem_ns:Before: per-element cost grows with N (quadratic). After: roughly flat (linear).
int_mapandrecord_listcollapse the same way;string_mapand alldecoderows are unchanged.Tests
mix test test/lua/vm/table_test.exs test/lua_test.exs— 129 passed.benchmarks/encode_decode.exs, the harness that attributed the regression by direction / container shape / size.