From 6be43e1ff57326ee9e403c6c306746120fae8651 Mon Sep 17 00:00:00 2001 From: Pascal Zittlau Date: Wed, 3 Dec 2025 15:51:15 +0100 Subject: [PATCH] binary search in findAllocation --- src/AddressAllocator.zig | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/AddressAllocator.zig b/src/AddressAllocator.zig index 0c29570..e879788 100644 --- a/src/AddressAllocator.zig +++ b/src/AddressAllocator.zig @@ -147,11 +147,22 @@ pub fn findAllocation( if (size == 0) return null; const size_i: i64 = @intCast(size); - // OPTIM: Use binary search to find the start of the valid range inside the reserved ranges. - + const start_idx = sort.lowerBound( + Range, + address_allocator.ranges.items, + valid_range, + Range.compare, + ); // `candidate_start` tracks the beginning of the current free region being examined. var candidate_start = valid_range.start; - for (address_allocator.ranges.items) |reserved| { + // If the range before the start index overlaps with our search start, we have to adjust. + if (start_idx > 0) { + const prev = address_allocator.ranges.items[start_idx - 1]; + if (prev.end > candidate_start) { + candidate_start = prev.end; + } + } + for (address_allocator.ranges.items[start_idx..]) |reserved| { if (candidate_start >= valid_range.end) { log.debug("findAllocation: Searched past the valid range.", .{}); break;