Skip to content

perf(lua): pre-size tables in MapToTable - #716

Merged
gianlucam76 merged 1 commit into
projectsveltos:mainfrom
gianlucam76:perf/maptotable-presize
Nov 5, 2026
Merged

gianlucam76 merged 1 commit into
projectsveltos:mainfrom
gianlucam76:perf/maptotable-presize

Conversation

@gianlucam76

Copy link
Copy Markdown
Member

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.

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
gianlucam76 force-pushed the perf/maptotable-presize branch from aaa651f to 955cafb Compare November 5, 2026 14:34
@gianlucam76
gianlucam76 merged commit 4a6864a into projectsveltos:main Nov 5, 2026
4 checks passed
@gianlucam76
gianlucam76 deleted the perf/maptotable-presize branch November 5, 2026 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant