Hacker News new | ask | show | jobs
by newaccount74 1567 days ago
> > some clever base between 1 and 2

> I wouldn't have thought of it, nor known how to. Would it be somehow costly ? Shifting is basically free.

Multiplying by 1.5 is a shift and an add, so just as cheap.

Multiplying by 1.5 has the advantage that 1 + 1.5 > 1.5 * 1.5, so the second time you realloc it fits into the old memory (which can help reduce memory fragmentation if the allocator takes advantage of it).

2 comments

> Multiplying by 1.5 has the advantage that 1 + 1.5 > 1.5 * 1.5, so the second time you realloc it fits into the old memory (which can help reduce memory fragmentation if the allocator takes advantage of it).

I’m really struggling to think of any mainstream allocator that can take advantage of this.

The clever base between 1 and 2 you are looking for is the golden ratio. I would use it only for bigger numbers though. under 5k 2 is good enough.