Conversation
find_free_gap_inner walked guest_t.regions[0..nregions) linearly and skipped any region with end <= gap_start using a cheap continue. With the cached post-allocation gap hint usually pointing high in the array, every mmap call still re-scanned the same skippable prefix on the way to the first interesting region, and the addr-hint direct caller atsys_mmap bypasses the hint cache entirely so it always paid the full prefix cost. Hoist the prefix walk into a static lower_bound helper first_region_end_above and start the existing loop at its result. Correctness rests on invariants already maintained elsewhere: guest_region_add keeps regions[] sorted ascending by start, and sys_mmap MAP_FIXED removes overlapping ranges before insertion (with adjacent anonymous regions coalesced via regions_mergeable), so ends are monotonically non-decreasing and binary-searchable. The defensive end <= gap_start continue stays in the body because ALIGN_UP(regions[i].end, hps) past one region can still skip over a smaller adjacent region whose end is below the new gap_start. No new data structures, no memory growth, no behavior change for callers; helps both the cached-hint path and the addr-hint direct call site.
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.
find_free_gap_inner walked guest_t.regions[0..nregions) linearly and skipped any region with end <= gap_start using a cheap continue. With the cached post-allocation gap hint usually pointing high in the array, every mmap call still re-scanned the same skippable prefix on the way to the first interesting region, and the addr-hint direct caller atsys_mmap bypasses the hint cache entirely so it always paid the full prefix cost.
Hoist the prefix walk into a static lower_bound helper first_region_end_above and start the existing loop at its result. Correctness rests on invariants already maintained elsewhere: guest_region_add keeps regions[] sorted ascending by start, and sys_mmap MAP_FIXED removes overlapping ranges before insertion (with adjacent anonymous regions coalesced via regions_mergeable), so ends are monotonically non-decreasing and binary-searchable. The defensive end <= gap_start continue stays in the body because ALIGN_UP(regions[i].end, hps) past one region can still skip over a smaller adjacent region whose end is below the new gap_start.
No new data structures, no memory growth, no behavior change for callers; helps both the cached-hint path and the addr-hint direct call site.
Summary by cubic
Speed up
find_free_gap_innerby skipping the prefix of irrelevant regions using a binary search. This removes repeated O(n) scans onmmapcalls and addr-hint probes with no behavior change.first_region_end_above()to binary-search the first region withend > gap_start, then start the loop from that index.end <= gap_startguard for alignment edge cases; no new data structures or semantics.Written for commit 6473537. Summary will update on new commits.