Use loop form in orderedMin4 to enable inlining#6
Merged
Conversation
The unrolled-with-early-exits pattern costs 130 in the compiler's inlining model (budget: 80), preventing orderedMin4 from being inlined into orderedBubbledown's hot loop. A loop form costs 63, bringing it under budget. Benchmarked in isolation on Apple M4 Max (n≈1000, 5 runs): OrderedPop 286.0ns → 236.5ns -17.31% (p=0.008) OrderedPopMax 283.9ns → 234.9ns -17.26% (p=0.008) OrderedPushPop 290.4ns → 246.5ns -15.12% (p=0.008) OrderedPush 11.83ns → 11.76ns ~ (p=0.381)
There was a problem hiding this comment.
Pull request overview
This PR optimizes the generic min/max heap’s hot path by rewriting orderedMin4 into a loop form that stays within the Go compiler’s inlining budget, enabling inlining into orderedBubbledown and reducing per-iteration overhead.
Changes:
- Replaces the unrolled/early-exit implementation of
orderedMin4with a bounded loop over up to 4 consecutive elements. - Adds documentation explaining the inlining-cost motivation for the loop form.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
orderedMin4from unrolled-with-early-exits to a loop, reducing the Go compiler's inlining cost from 130 to 63 (budget: 80)orderedMin4to be inlined intoorderedBubbledown's hot loop, eliminating function call overhead on every iterationBenchmarks
Isolated change, Apple M4 Max,
go test -bench BenchmarkOrdered -benchmem -count=5 -benchtime=2s:Two other candidate optimizations (
isMinHeapsimplification, bounds-check-elimination hint inorderedBubbledown) were benchmarked in isolation and showed slight regressions, so they were dropped.Test plan
go test -race -count=1 ./...passesorderedMin4through Pop/PopMax/Remove/Fix paths🤖 Generated with Claude Code