Skip to content

fix(apijson): replace O(n^2) array encoding with O(n) bytes.Buffer - #4346

Merged
ssicard merged 1 commit into
nextfrom
fix/array-encoder-quadratic
Jun 18, 2026
Merged

fix(apijson): replace O(n^2) array encoding with O(n) bytes.Buffer#4346
ssicard merged 1 commit into
nextfrom
fix/array-encoder-quadratic

Conversation

@ssicard

@ssicard ssicard commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Summary

newArrayTypeEncoder builds JSON arrays by calling sjson.SetRawBytes(json, "-1", item) in a loop. sjson re-parses and reallocates the entire accumulated buffer on each call, giving O(n^2) time and memory complexity. For a 30,000-element IP list update via Rules.Lists.Items.Update, this allocates ~6.2 GB and OOM-kills the process.

This replaces the sjson loop with a single-pass bytes.Buffer concatenation, restoring O(n). Output is byte-identical to the previous implementation.

Context

Benchmarks (from #4316)

N before B/op after B/op mem reduction before ns/op after ns/op speedup
100 92 KB 8 KB 11x 138 us 24 us 5.9x
1,000 6.9 MB 66 KB 105x 7.1 ms 0.21 ms 33x
10,000 692 MB 845 KB 819x 437 ms 1.18 ms 369x
30,000 6.2 GB 2.0 MB 3,070x 4,091 ms 3.10 ms 1,320x

Tests

All existing internal/apijson tests pass. Added array_bench_test.go with correctness verification against encoding/json and benchmarks at N=100..30,000 as a regression guard.

Fixes #4315

newArrayTypeEncoder built JSON arrays by calling sjson.SetRawBytes in a
loop, which re-parses and reallocates the entire buffer on each iteration.
For a 30,000-element array this allocates ~6.2 GB and causes OOM kills.

Replace with single-pass bytes.Buffer concatenation. Output is
byte-identical to the previous implementation.

Includes benchmark and correctness tests as a regression guard.

Fixes #4315
Ref: https://jira.cfdata.org/browse/APIX-1066
@ssicard ssicard self-assigned this Jun 17, 2026
@ssicard
ssicard requested a review from a team June 17, 2026 18:51
@ssicard
ssicard merged commit 4a8a07b into next Jun 18, 2026
12 of 14 checks passed
@ssicard
ssicard deleted the fix/array-encoder-quadratic branch June 18, 2026 15:31
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.

2 participants