perf: inline FastMaterializeJsonRenderer hot methods#899
Open
He-Pin wants to merge 1 commit into
Open
Conversation
Override flushBuffer() and flushCharBuilder() in FastMaterializeJsonRenderer with direct @inline implementations. Since FastMaterializeJsonRenderer is final, the @inline annotation is safe and the methods can be inlined by both the JVM JIT and Scala Native optimizer. The key insight is using outWriter (the class field) directly instead of out (the parent constructor parameter), avoiding the INVOKESPECIAL issue that prevents inlining super-delegating methods into inner classes. Benchmark results (manifestJsonEx): - Before: jrsonnet 2.11x faster - After: jrsonnet 1.50x faster (29% gap reduction)
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
The
std.manifestJsonExfunction was 2.11x slower than jrsonnet (Rust implementation) on Scala Native. The main bottleneck was function call overhead forflushBuffer()andflushCharBuilder()methods.Key Design Decision
Override
flushBuffer()andflushCharBuilder()inFastMaterializeJsonRendererwith direct@inlineimplementations. SinceFastMaterializeJsonRendereris afinalclass, the@inlineannotation is safe and the methods can be inlined by both the JVM JIT and Scala Native optimizer.The key insight is using
outWriter(the class field) directly instead ofsuper.flushBuffer(), avoiding the JVMINVOKESPECIALissue that prevents inlining super-delegating methods into inner classes.Modification
Changed
sjsonnet/src/sjsonnet/Renderer.scala:flushBuffer()with direct@inlineimplementationflushCharBuilder()with direct@inlineimplementationoutWriterfield directly instead ofsuperdelegationBenchmark Results
Scala Native vs jrsonnet (hyperfine)
JMH (JVM)
Baseline JMH benchmarks are stable; the change benefits all platforms.
Analysis
The
@inlineannotation onfinalclass methods allows the Scala Native optimizer to inline these hot-path methods at link time. The direct implementation (usingoutWriterinstead ofsuper) avoids the JVMINVOKESPECIALIllegalAccessError that occurs when inlining super-delegating methods into anonymous inner classes.References
@inlinedocumentationINVOKESPECIALspecificationResult
./mill __.test)./mill __.reformat)