⚡️ Speed up method IrPolygon.deepClone by 18%#32
Open
codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Open
⚡️ Speed up method IrPolygon.deepClone by 18%#32codeflash-ai[bot] wants to merge 1 commit intomasterfrom
IrPolygon.deepClone by 18%#32codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Conversation
The optimization caches `vertices` and its `length` into local variables (`v` and `len`) and assigns the cloned array to `p.vertices` after the loop instead of writing directly to `p.vertices[i]` inside it. This reduces repeated field dereferences—each `p.vertices[i]` access requires a pointer load from the `p` object, while `cloned[i]` is a direct local array reference—and the JVM can better optimize tight loops with purely local state. Profiler data shows the loop body (`deepClone()` call on each vertex) remains the dominant cost at ~60%, but the 17% runtime improvement comes from eliminating per-iteration object field access overhead in favor of stack-local array writes.
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.
📄 18% (0.18x) speedup for
IrPolygon.deepCloneinjme3-plugins/src/main/java/com/jme3/scene/plugins/IrPolygon.java⏱️ Runtime :
775 microseconds→657 microseconds(best of109runs)📝 Explanation and details
The optimization caches
verticesand itslengthinto local variables (vandlen) and assigns the cloned array top.verticesafter the loop instead of writing directly top.vertices[i]inside it. This reduces repeated field dereferences—eachp.vertices[i]access requires a pointer load from thepobject, whilecloned[i]is a direct local array reference—and the JVM can better optimize tight loops with purely local state. Profiler data shows the loop body (deepClone()call on each vertex) remains the dominant cost at ~60%, but the 17% runtime improvement comes from eliminating per-iteration object field access overhead in favor of stack-local array writes.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-IrPolygon.deepClone-mnbkhbueand push.