Skip to content

Skip gap-finder prefix via binary search#25

Merged
jserv merged 1 commit intomainfrom
gap-finder
May 11, 2026
Merged

Skip gap-finder prefix via binary search#25
jserv merged 1 commit intomainfrom
gap-finder

Conversation

@jserv
Copy link
Copy Markdown
Contributor

@jserv jserv commented May 11, 2026

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_inner by skipping the prefix of irrelevant regions using a binary search. This removes repeated O(n) scans on mmap calls and addr-hint probes with no behavior change.

  • Performance
    • Added first_region_end_above() to binary-search the first region with end > gap_start, then start the loop from that index.
    • Kept the end <= gap_start guard for alignment edge cases; no new data structures or semantics.

Written for commit 6473537. Summary will update on new commits.

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.
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

@jserv jserv merged commit de4f126 into main May 11, 2026
5 checks passed
@jserv jserv deleted the gap-finder branch May 11, 2026 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant