perf: reduce TOML and repeated join allocations#828
Open
He-Pin wants to merge 2 commits intodatabricks:masterfrom
Open
perf: reduce TOML and repeated join allocations#828He-Pin wants to merge 2 commits intodatabricks:masterfrom
He-Pin wants to merge 2 commits intodatabricks:masterfrom
Conversation
Motivation: std.manifestTomlEx spends avoidable time and allocation in TOML string/key rendering and table traversal. The go_suite manifestTomlEx regression benchmark is small but sensitive to regex matches, intermediate escaped strings, Seq path copies, repeated sorting, and per-value renderer allocation. Modification: Write TOML strings and keys directly to the StringWriter, replace bare-key regex matching with a small ASCII scan, avoid array-render string concatenations and StringWriter.flush(), use sorted key cache plus section flags instead of partition + sorting, reuse one TomlRenderer per table level, and keep table paths in a mutable buffer while preserving visible-key section classification order. Result: bench/resources/go_suite/manifestTomlEx.jsonnet: upstream/master 0.069 ms/op, this change 0.052 ms/op (-24.6%). Verification: ./mill -i __.checkFormat git diff --check ./mill -i 'sjsonnet.jvm[3.3.7].test' ./mill -i 'sjsonnet.js[3.3.7].test' JDK_JAVA_OPTIONS="--enable-native-access=ALL-UNNAMED -Xmx8G -XX:+UseG1GC" ./mill -i 'sjsonnet.native[3.3.7].test' ./mill -i bench.runRegressions bench/resources/go_suite/manifestTomlEx.jsonnet
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.
Motivation
std.manifestTomlExspends avoidable time and allocation in TOML string/key rendering and table traversal. Thego_suite/manifestTomlExbenchmark is sensitive to regex matches, intermediate escaped strings,Seqpath copies, repeated sorting, and per-value renderer allocation.cpp_suite/large_string_joinbuildsstd.makeArray(76846, function(_) 'x')and then joins it. That used to allocate a large backing array filled with the same constant value, then walk it again instd.join.Modification
StringWriter.StringWriter.flush().partitionplus two sorts.TomlRendererper table level and keep table paths in a mutable buffer.std.makeArrayresults as a constant array view.std.jointo consume constant/repeated string arrays with exact-sizeStringBuilderoutput and ASCII-safe result tagging.Results
JMH regression cases,
upstream/masterat8b67cb1e:go_suite/manifestTomlEx.jsonnetcpp_suite/large_string_join.jsonnetNative CLI hyperfine against latest source-built jrsonnet master
5b43fa8(jrsonnet 0.5.0-pre98):cpp_suite/large_string_join.jsonnetVerification
git diff --check./mill -i __.checkFormat./mill -i 'sjsonnet.jvm[3.3.7].test'./mill -i 'sjsonnet.js[3.3.7].test'./mill -i 'sjsonnet.native[3.3.7].test'./mill -i 'sjsonnet.jvm[2.12.21].compile'./mill -i 'sjsonnet.jvm[2.13.18].compile'./mill -i 'sjsonnet.js[2.13.18].compile'./mill -i 'sjsonnet.native[2.13.18].compile'./mill -i 'sjsonnet.native[3.3.7].nativeLink'./mill -i bench.runRegressions bench/resources/go_suite/foldl.jsonnet bench/resources/cpp_suite/bench.04.jsonnet bench/resources/cpp_suite/large_string_join.jsonnet bench/resources/go_suite/manifestTomlEx.jsonnethyperfine --warmup 10 --min-runs 50Boundary Checks
visibleKeyNamesorder to preserve the previous evaluation/trace order.TomlRenderer.escapeKeyAPI is preserved.Evalarray when an existing caller asks forasLazyArray.std.joinfalls back to the existing element-by-element path for non-string constants, mixed arrays, and lazy values.References
a2a96ed47bf87ea33fab7cbc6a47642c358b3e755b43fa88b8c43856dd5a2daa9c5c251153c5e14d