Repository navigation
perf(lua): pre-size tables in MapToTable - #716
Merged
gianlucam76 merged 1 commit intoNov 5, 2026
Merged
Conversation
MapToTable built every table with &lua.LTable{} and grew it key by key,
so the hash part and the array part were reallocated several times for
each map and slice. Pre-size them from the length of the input.
gopher-lua only exposes its table constructor through
(*LState).CreateTable, which does not use its receiver, so a nil state
is used because MapToTable has no LState. The tests exercise this path.
Behaviour is unchanged. A test compares the result with a copy of the
previous implementation on a real pod and on inputs covering every
supported type and the types that are silently skipped (nil, int,
nested slices). A benchmark on a 16.5 KB pod (arm64, Apple M3):
before ~300 us/op 721,788 B/op 3,995 allocs/op
after ~165 us/op 224,193 B/op 3,539 allocs/op
On a kind cluster with 3,000 pods, sveltos-agent allocated 15 GB in
5 minutes instead of 40 GB, and its peak RSS fell from 928 to 758 MiB.
gianlucam76
force-pushed
the
perf/maptotable-presize
branch
from
November 5, 2026 14:34
aaa651f to
955cafb
Compare
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.
MapToTable built every table with &lua.LTable{} and grew it key by key, so the hash part and the array part were reallocated several times for each map and slice. Pre-size them from the length of the input.
gopher-lua only exposes its table constructor through (*LState).CreateTable, which does not use its receiver, so a nil state is used because MapToTable has no LState. The tests exercise this path.
Behaviour is unchanged. A test compares the result with a copy of the previous implementation on a real pod and on inputs covering every supported type and the types that are silently skipped (nil, int, nested slices). A benchmark on a 16.5 KB pod (arm64, Apple M3):
before ~300 us/op 721,788 B/op 3,995 allocs/op
after ~165 us/op 224,193 B/op 3,539 allocs/op
On a kind cluster with 3,000 pods, sveltos-agent allocated 15 GB in 5 minutes instead of 40 GB, and its peak RSS fell from 928 to 758 MiB.