Repository navigation
Avoid quadratic suffix allocations in runes.Buffer construction - #1602
Merged
dmitriplotnikov merged 1 commit intoNov 4, 2026
Merged
Conversation
- Replace `utf8.DecodeRuneInString(data[idx:])` loops in `countRemainingCodePoints` and `newBufferWithLimit` with `for range` string iteration. Under `-gcflags=all=-d=libfuzzer` (OSS-Fuzz), the inlined empty-string check in `DecodeRuneInString` invokes `runtime.libfuzzerHookStrCmp(data[idx:], "")`, allocating a heap copy of the remaining input suffix on every rune (~5 GB on 100 KB inputs, causing severe GC thrashing and timeouts).
- Bound preallocated slice capacities in `newBufferWithLimit` to the validated code-point count when `limit >= 0 && len(data) > limit`.
- Add unit tests and `BenchmarkNewBuffer`.
Benchmarks (`go test -bench=BenchmarkNewBuffer -benchmem`):
goos: linux
goarch: amd64
pkg: cel.dev/cel-go/common/runes
cpu: Intel(R) Xeon(R) CPU @ 2.20GHz
│ before.txt │ after.txt │
│ sec/op │ sec/op vs base │
NewBuffer/ASCII_100KB-8 890.8µ ± 3% 875.2µ ± 8% ~ (p=0.485 n=6)
NewBuffer/BMP_100KRunes-8 1.549m ± 11% 1.247m ± 21% -19.54% (p=0.004 n=6)
NewBuffer/Supplemental_100KRunes-8 2.216m ± 42% 1.639m ± 25% -26.01% (p=0.015 n=6)
NewBuffer/Exceeded_500KB-8 633.8µ ± 15% 450.6µ ± 7% -28.90% (p=0.002 n=6)
geomean 1.180m 947.5µ -19.70%
│ before.txt │ after.txt │
│ B/op │ B/op vs base │
NewBuffer/ASCII_100KB-8 977.9Ki ± 0% 977.9Ki ± 0% ~ (p=0.665 n=6)
NewBuffer/BMP_100KRunes-8 1.291Mi ± 0% 1.150Mi ± 0% -10.89% (p=0.002 n=6)
NewBuffer/Supplemental_100KRunes-8 2.057Mi ± 0% 1.338Mi ± 0% -34.95% (p=0.002 n=6)
NewBuffer/Exceeded_500KB-8 16.00 ± 0% 16.00 ± 0% ~ (p=1.000 n=6) ¹
geomean 80.76Ki 70.47Ki -12.75%
¹ all samples are equal
│ before.txt │ after.txt │
│ allocs/op │ allocs/op vs base │
NewBuffer/ASCII_100KB-8 25.00 ± 0% 25.00 ± 0% ~ (p=1.000 n=6) ¹
NewBuffer/BMP_100KRunes-8 26.00 ± 0% 26.00 ± 0% ~ (p=1.000 n=6) ¹
NewBuffer/Supplemental_100KRunes-8 26.00 ± 0% 26.00 ± 0% ~ (p=1.000 n=6) ¹
NewBuffer/Exceeded_500KB-8 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=6) ¹
geomean 11.40 11.40 +0.00%
¹ all samples are equal
Under `-d=libfuzzer` (`asan-fuzzer` `FuzzParser` target):
- 102,823 B over-limit input: 2,591 ms (1,285 GCs) -> 9 ms (1 GC)
- 500,000 B over-limit input: >61,000 ms (timeout) -> 42 ms (2 GCs)
TristonianJones
approved these changes
Nov 3, 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.
utf8.DecodeRuneInString(data[idx:])loops incountRemainingCodePointsandnewBufferWithLimitwithfor rangestring iteration. Under-gcflags=all=-d=libfuzzer(OSS-Fuzz), the inlined empty-string check inDecodeRuneInStringinvokesruntime.libfuzzerHookStrCmp(data[idx:], ""), allocating a heap copy of the remaining input suffix on every rune (~5 GB on 100 KB inputs, causing severe GC thrashing and timeouts).newBufferWithLimitto the validated code-point count whenlimit >= 0 && len(data) > limit.BenchmarkNewBuffer.Benchmarks (
go test -bench=BenchmarkNewBuffer -benchmem)goos: linux·goarch: amd64·pkg: cel.dev/cel-go/common/runes·cpu: Intel(R) Xeon(R) CPU @ 2.20GHzbefore.txt(sec/op)after.txt(sec/op)NewBuffer/ASCII_100KB-8NewBuffer/BMP_100KRunes-8NewBuffer/Supplemental_100KRunes-8NewBuffer/Exceeded_500KB-8before.txt(B/op)after.txt(B/op)NewBuffer/ASCII_100KB-8NewBuffer/BMP_100KRunes-8NewBuffer/Supplemental_100KRunes-8NewBuffer/Exceeded_500KB-81 all samples are equal
before.txt(allocs/op)after.txt(allocs/op)NewBuffer/ASCII_100KB-8NewBuffer/BMP_100KRunes-8NewBuffer/Supplemental_100KRunes-8NewBuffer/Exceeded_500KB-81 all samples are equal
Under
-d=libfuzzer(asan-fuzzerFuzzParsertarget):