Repository navigation
Replace quadratic string concatenation in qualified struct type names - #1607
Open
dmitriplotnikov wants to merge 5 commits into
Open
dmitriplotnikov wants to merge 5 commits into
dmitriplotnikov wants to merge 5 commits into
Conversation
- Check `sourceInfo.ComputeOffset(1, 0) != 0` once in `newParserHelper` to detect offset-shifted `RelativeSource` inputs with zero extra allocations, making `computeOffset` an `O(1)` no-op on standard sources while preserving embedded policy source offset translation.
- Add `newOffsetRangeID(start, stop int32)` and update `exprHelper.nextMacroID` and `exprHelper.Copy` to record `OffsetRange` directly without round-tripping through `common.Location` or boxing `ast.OffsetRange` into `any`.
- Add `TestParseMultilineMacrosAndMaps` and `BenchmarkParseLargePathological`.
Benchmarks (`go test -bench='^Benchmark(ByCategory|ParseLargePathological)$' -benchmem`):
goos: linux
goarch: amd64
pkg: cel.dev/cel-go/parser
cpu: Intel(R) Xeon(R) CPU @ 2.20GHz
│ before.txt │ after.txt │
│ sec/op │ sec/op vs base │
ByCategory/antlr/Simple-8 571.0µ ± 17% 359.0µ ± 14% -37.13% (p=0.002 n=6)
ByCategory/antlr/Complex-8 2.522m ± 5% 1.802m ± 4% -28.56% (p=0.002 n=6)
ByCategory/antlr/Macros-8 576.2µ ± 16% 442.8µ ± 4% -23.15% (p=0.002 n=6)
ByCategory/antlr/Errors-8 1.922m ± 11% 1.843m ± 6% ~ (p=0.093 n=6)
ByCategory/pratt/Simple-8 36.08µ ± 65% 29.33µ ± 5% -18.69% (p=0.002 n=6)
ByCategory/pratt/Complex-8 190.9µ ± 9% 137.7µ ± 7% -27.84% (p=0.002 n=6)
ByCategory/pratt/Macros-8 76.53µ ± 9% 53.65µ ± 4% -29.90% (p=0.002 n=6)
ByCategory/pratt/Errors-8 108.23µ ± 5% 93.53µ ± 3% -13.58% (p=0.002 n=6)
ParseLargePathological/antlr/multiline_macros-8 117.7m ± 33% 112.1m ± 3% -4.73% (p=0.002 n=6)
ParseLargePathological/antlr/multiline_map-8 255.4m ± 9% 244.4m ± 15% ~ (p=0.699 n=6)
ParseLargePathological/pratt/multiline_macros-8 27.64m ± 27% 19.00m ± 6% -31.26% (p=0.002 n=6)
ParseLargePathological/pratt/multiline_map-8 24.57m ± 5% 18.05m ± 5% -26.54% (p=0.002 n=6)
geomean 1.872m 1.469m -21.57%
│ before.txt │ after.txt │
│ B/op │ B/op vs base │
ByCategory/antlr/Simple-8 106.6Ki ± 4% 106.8Ki ± 4% ~ (p=0.058 n=6)
ByCategory/antlr/Complex-8 506.7Ki ± 0% 506.8Ki ± 0% ~ (p=0.065 n=6)
ByCategory/antlr/Macros-8 134.0Ki ± 0% 133.2Ki ± 0% -0.64% (p=0.002 n=6)
ByCategory/antlr/Errors-8 581.3Ki ± 0% 581.4Ki ± 0% +0.03% (p=0.041 n=6)
ByCategory/pratt/Simple-8 12.77Ki ± 0% 11.47Ki ± 0% -10.16% (p=0.002 n=6)
ByCategory/pratt/Complex-8 58.16Ki ± 0% 48.29Ki ± 0% -16.98% (p=0.002 n=6)
ByCategory/pratt/Macros-8 23.03Ki ± 0% 20.50Ki ± 0% -11.01% (p=0.002 n=6)
ByCategory/pratt/Errors-8 36.94Ki ± 0% 31.31Ki ± 0% -15.24% (p=0.002 n=6)
ParseLargePathological/antlr/multiline_macros-8 30.64Mi ± 0% 30.17Mi ± 0% -1.53% (p=0.002 n=6)
ParseLargePathological/antlr/multiline_map-8 75.02Mi ± 0% 75.02Mi ± 0% ~ (p=0.818 n=6)
ParseLargePathological/pratt/multiline_macros-8 6.970Mi ± 0% 6.164Mi ± 0% -11.56% (p=0.002 n=6)
ParseLargePathological/pratt/multiline_map-8 7.211Mi ± 0% 6.066Mi ± 0% -15.87% (p=0.002 n=6)
geomean 513.7Ki 477.0Ki -7.16%
│ before.txt │ after.txt │
│ allocs/op │ allocs/op vs base │
ByCategory/antlr/Simple-8 1.631k ± 4% 1.631k ± 3% ~ (p=1.000 n=6)
ByCategory/antlr/Complex-8 6.744k ± 0% 6.744k ± 0% ~ (p=1.000 n=6)
ByCategory/antlr/Macros-8 2.049k ± 0% 1.986k ± 0% -3.07% (p=0.002 n=6)
ByCategory/antlr/Errors-8 7.935k ± 0% 7.935k ± 0% ~ (p=1.000 n=6)
ByCategory/pratt/Simple-8 389.0 ± 0% 297.0 ± 0% -23.65% (p=0.002 n=6)
ByCategory/pratt/Complex-8 1612.0 ± 0% 972.0 ± 0% -39.70% (p=0.002 n=6)
ByCategory/pratt/Macros-8 641.0 ± 0% 470.0 ± 0% -26.68% (p=0.002 n=6)
ByCategory/pratt/Errors-8 1163.0 ± 0% 793.0 ± 0% -31.81% (p=0.002 n=6)
ParseLargePathological/antlr/multiline_macros-8 394.8k ± 0% 364.3k ± 0% -7.74% (p=0.002 n=6)
ParseLargePathological/antlr/multiline_map-8 750.5k ± 0% 750.5k ± 0% ~ (p=0.504 n=6)
ParseLargePathological/pratt/multiline_macros-8 161.4k ± 0% 108.6k ± 0% -32.70% (p=0.002 n=6)
ParseLargePathological/pratt/multiline_map-8 175.2k ± 0% 100.2k ± 0% -42.82% (p=0.002 n=6)
geomean 9.707k 7.858k -19.05%
- Build qualified message type names with a pre-sized `strings.Builder` in `antlrVisitor.VisitCreateMessage` instead of repeated `+=` string concatenation.
- Assemble qualified struct type names in a single `strings.Builder` pass in `prattParserWorker.extractStructName` instead of `name = "." + sel.FieldName() + name`, and delete intermediate `Select`/`Ident` IDs outermost-to-innermost only after confirming an `ast.IdentKind` root.
- Add `TestParseDeepQualifiedStructName` and extend `BenchmarkParseLargePathological`.
Benchmarks (`go test -bench='^BenchmarkParseLargePathological/.*/deep_struct_name$' -benchmem`):
goos: linux
goarch: amd64
pkg: cel.dev/cel-go/parser
cpu: Intel(R) Xeon(R) CPU @ 2.20GHz
│ before.txt │ after.txt │
│ sec/op │ sec/op vs base │
ParseLargePathological/antlr/deep_struct_name-8 1508.25m ± 4% 79.91m ± 4% -94.70% (p=0.002 n=6)
ParseLargePathological/pratt/deep_struct_name-8 654.89m ± 111% 39.74m ± 24% -93.93% (p=0.002 n=6)
geomean 993.9m 56.35m -94.33%
│ before.txt │ after.txt │
│ B/op │ B/op vs base │
ParseLargePathological/antlr/deep_struct_name-8 5100.91Mi ± 0% 37.72Mi ± 0% -99.26% (p=0.002 n=6)
ParseLargePathological/pratt/deep_struct_name-8 2537.86Mi ± 0% 12.28Mi ± 0% -99.52% (p=0.002 n=6)
geomean 3.514Gi 21.52Mi -99.40%
│ before.txt │ after.txt │
│ allocs/op │ allocs/op vs base │
ParseLargePathological/antlr/deep_struct_name-8 300.2k ± 2% 200.2k ± 0% -33.32% (p=0.002 n=6)
ParseLargePathological/pratt/deep_struct_name-8 200.0k ± 0% 150.1k ± 0% -24.97% (p=0.002 n=6)
geomean 245.1k 173.3k -29.27%
Collect qualified struct name segments in a pre-allocated slice while deleting intermediate `Select`/`Ident` IDs inline, then reverse and join with `strings.Join` instead of tracking a separate `ids` slice and pre-computing length for `strings.Builder`.
TristonianJones
previously approved these changes
Nov 3, 2026
| sourceInfo: ast.NewSourceInfo(source), | ||
| sourceInfo: sourceInfo, | ||
| nextID: 1, | ||
| isRelative: sourceInfo.ComputeOffset(1, 0) != 0, |
Collaborator
There was a problem hiding this comment.
I'm still a bit wary of this particular line and handling relative source differently. Any reason why we need it in this change or the other one?
Collaborator
Author
There was a problem hiding this comment.
Removing this part of the change from this PR. It belongs in PR #1605 anyway. Will address your concer in that PR.
Remove the isRelative field and ComputeOffset(1, 0) != 0 check from newParserHelper and restore the unconditional OffsetLocation translation in computeOffset.
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.
Builds on #1605.
strings.BuilderinantlrVisitor.VisitCreateMessageinstead of repeated+=string concatenation.strings.Builderpass inprattParserWorker.extractStructNameinstead ofname = "." + sel.FieldName() + name, and delete intermediateSelect/IdentIDs inline.TestParseDeepQualifiedStructNameand extendBenchmarkParseLargePathological.Benchmarks (
go test -bench='^BenchmarkParseLargePathological/.*/deep_struct_name$' -benchmem)goos: linux·goarch: amd64·pkg: cel.dev/cel-go/parser·cpu: AMD EPYC 7B13before.txt(sec/op)after.txt(sec/op)ParseLargePathological/antlr/deep_struct_name-64ParseLargePathological/pratt/deep_struct_name-64before.txt(B/op)after.txt(B/op)ParseLargePathological/antlr/deep_struct_name-64ParseLargePathological/pratt/deep_struct_name-64before.txt(allocs/op)after.txt(allocs/op)ParseLargePathological/antlr/deep_struct_name-64ParseLargePathological/pratt/deep_struct_name-64