⚡️ Speed up method ThermalErodeFilter.filter by 68%#25
Open
codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Open
⚡️ Speed up method ThermalErodeFilter.filter by 68%#25codeflash-ai[bot] wants to merge 1 commit intomasterfrom
ThermalErodeFilter.filter by 68%#25codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Conversation
The optimization hoists the `deltas` array allocation outside the per-cell loop and reuses it across all ~20,000 iterations, eliminating repeated small allocations that the profiler showed consumed ~1% of runtime each cycle. Additionally, caching `this.talus` and `this.c` as local `final` variables removes ~160,000 field dereferences (four neighbor checks plus computation per cell), cutting indirect memory access overhead. The profiler confirms the inner loops now spend less time per hit (e.g., neighbor checking dropped from 153.6 ns to 95.3 ns per operation), and the overall runtime improved 68% with no correctness regressions across all test cases.
Independent JMH Benchmark ReviewWhat this PR doesHoists an allocation ( JMH Verdict: Legitimate optimization — ApproveHoisting allocations out of loops is a real optimization that the JIT cannot do automatically. The JIT performs escape analysis, but it cannot prove that a new array allocated inside a loop iteration is unused across iterations — so it must allocate fresh on each pass. Why this works:
This is the kind of optimization that shows real improvement: reducing GC pressure in a hot loop. RecommendationApprove this PR. Allocation hoisting is a genuine, measurable optimization that JIT cannot perform automatically. |
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.
📄 68% (0.68x) speedup for
ThermalErodeFilter.filterinjme3-terrain/src/main/java/com/jme3/terrain/noise/filter/ThermalErodeFilter.java⏱️ Runtime :
812 microseconds→482 microseconds(best of109runs)📝 Explanation and details
The optimization hoists the
deltasarray allocation outside the per-cell loop and reuses it across all ~20,000 iterations, eliminating repeated small allocations that the profiler showed consumed ~1% of runtime each cycle. Additionally, cachingthis.talusandthis.cas localfinalvariables removes ~160,000 field dereferences (four neighbor checks plus computation per cell), cutting indirect memory access overhead. The profiler confirms the inner loops now spend less time per hit (e.g., neighbor checking dropped from 153.6 ns to 95.3 ns per operation), and the overall runtime improved 68% with no correctness regressions across all test cases.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-ThermalErodeFilter.filter-mnaq7av3and push.