Skip to content

Commit 1d6dd0d

Browse files
committed
feat(heap,pool): optimizations
1 parent c1219b2 commit 1d6dd0d

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

core/heap.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
#define MAX_BLOCK_ORDER 25 // 2^25 = 32MB
3232
#define MAX_POOL_ORDER 38 // 2^38 = 256GB
3333

34-
// Small object cache (slab) for sizes 32, 64, 128, 256 bytes
35-
#define SLAB_CACHE_SIZE 32
36-
#define SLAB_ORDERS 4 // orders 5, 6, 7, 8
34+
// Small object cache (slab) for sizes 32, 64, 128, 256, 512 bytes
35+
#define SLAB_CACHE_SIZE 64
36+
#define SLAB_ORDERS 5 // orders 5, 6, 7, 8, 9
3737

3838
// Memory modes
3939
#define MMOD_INTERNAL 0xff

core/pool.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,20 @@ raw_p executor_run(raw_p arg) {
180180
__atomic_store_n(&executor->vm, vm, __ATOMIC_RELAXED);
181181

182182
for (;;) {
183+
// Adaptive spin-wait: spin briefly before sleeping (reduces wake latency)
184+
i64_t spin_rounds = 0;
185+
while (spin_rounds < 1000) {
186+
if (__atomic_load_n(&executor->pool->tasks_count, __ATOMIC_ACQUIRE) > 0)
187+
break;
188+
if (__atomic_load_n(&executor->pool->state, __ATOMIC_ACQUIRE) == RUN_STATE_STOPPED)
189+
break;
190+
backoff_spin(&spin_rounds);
191+
}
192+
183193
mutex_lock(&executor->pool->mutex);
184-
cond_wait(&executor->pool->run, &executor->pool->mutex);
194+
// Re-check after acquiring lock
195+
if (executor->pool->tasks_count == 0 && executor->pool->state == RUN_STATE_RUNNING)
196+
cond_wait(&executor->pool->run, &executor->pool->mutex);
185197

186198
if (executor->pool->state == RUN_STATE_STOPPED) {
187199
mutex_unlock(&executor->pool->mutex);

0 commit comments

Comments
 (0)