Skip to content

Commit c1219b2

Browse files
committed
feat(heap): improve borrow/merge pools for slave executors
1 parent 9ac8b40 commit c1219b2

1 file changed

Lines changed: 28 additions & 15 deletions

File tree

core/heap.c

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,21 @@ nil_t heap_borrow(heap_p heap) {
498498
}
499499
}
500500

501+
// Borrow medium blocks (orders 20-24: 1MB-16MB) for common allocations
502+
for (i = 20; i < MAX_BLOCK_ORDER; i++) {
503+
// Only borrow if source has 2+ blocks at this order
504+
if (h->freelist[i] == NULL || h->freelist[i]->next == NULL)
505+
continue;
506+
507+
heap->freelist[i] = h->freelist[i];
508+
h->freelist[i] = h->freelist[i]->next;
509+
h->freelist[i]->prev = NULL;
510+
511+
heap->freelist[i]->next = NULL;
512+
heap->freelist[i]->prev = NULL;
513+
heap->avail |= BSIZEOF(i);
514+
}
515+
501516
// Borrow large pool blocks (>=32MB) for big allocations
502517
for (i = MAX_BLOCK_ORDER; i <= MAX_POOL_ORDER; i++) {
503518
// Only borrow if source has freelist[i] with >1 node and it's a full pool
@@ -542,25 +557,23 @@ nil_t heap_merge(heap_p heap) {
542557
}
543558
heap->foreign_blocks = NULL;
544559

545-
// Merge freelists: find tail, link to main head
560+
// Merge freelists: O(1) prepend by finding tail, linking to main head
546561
for (i = MIN_BLOCK_ORDER; i <= MAX_POOL_ORDER; i++) {
547-
block = heap->freelist[i];
548-
last = NULL;
549-
550-
while (block != NULL) {
551-
last = block;
552-
block = block->next;
553-
}
562+
if (heap->freelist[i] == NULL)
563+
continue;
554564

555-
if (last != NULL) {
556-
last->next = h->freelist[i];
565+
// Find tail of worker's freelist
566+
last = heap->freelist[i];
567+
while (last->next != NULL)
568+
last = last->next;
557569

558-
if (h->freelist[i] != NULL)
559-
h->freelist[i]->prev = last;
570+
// Link: worker_tail -> main_head, main_head = worker_head
571+
last->next = h->freelist[i];
572+
if (h->freelist[i] != NULL)
573+
h->freelist[i]->prev = last;
560574

561-
h->freelist[i] = heap->freelist[i];
562-
heap->freelist[i] = NULL;
563-
}
575+
h->freelist[i] = heap->freelist[i];
576+
heap->freelist[i] = NULL;
564577
}
565578

566579
h->avail |= heap->avail;

0 commit comments

Comments
 (0)