binary search in findAllocation
This commit is contained in:
@@ -147,11 +147,22 @@ pub fn findAllocation(
|
|||||||
if (size == 0) return null;
|
if (size == 0) return null;
|
||||||
const size_i: i64 = @intCast(size);
|
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.
|
// `candidate_start` tracks the beginning of the current free region being examined.
|
||||||
var candidate_start = valid_range.start;
|
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) {
|
if (candidate_start >= valid_range.end) {
|
||||||
log.debug("findAllocation: Searched past the valid range.", .{});
|
log.debug("findAllocation: Searched past the valid range.", .{});
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user