News for hackers. Stuff that matters.
Re:Tny: A simple data serializer in C by Demiurge on Friday September 20, 2013 @11:50PM (#6421926)
What do you mean by 'own memory arena' and what is the performance (or other) difference between that and global heap?
Re:Tny: A simple data serializer in C by vidarh on Saturday September 21, 2013 @06:20AM (#6422548)
Consider that you know that certain data is always de-allocated together. An example is when a document is opened and then closed. In that case you can make a dramatic amount of difference with arenas:

Often you can avoid almost any memory overhead. E.g. any structures that are allocated on opening a document, and deallocated on closure, can be allocated from larger buffers without keeping any information about the individual allocations. That can save anything up to 16-20 bytes per allocation with many malloc() implementations, and reduces typical allocation cost to incrementing a pointer and checking whether or not you need to allocate a new buffer (and the allocation cost for new buffers might be amortised over anything up to thousands of small allocations).

For a practical example, there's a font library that calls malloc() to allocate structures for every single glyph when opening a font. Most of the (thousands) of allocations are 4-8 bytes. Changing that to using an arena for an application I did, cut memory usage per font to about 25%-30% and cut load time for fonts to <10% by avoiding the malloc() calls.

You can achieve the same by throwing abstractions out the window and putting stuff in arrays etc. But arenas is often a very effective way of keeping the abstractions while effectively getting almost the same performance and memory usage.

Re:Tny: A simple data serializer in C by Demiurge on Saturday September 21, 2013 @08:10PM (#6425175)
Ok, thanks for the example! I'm surprised it makes such a difference.
Re:Tny: A simple data serializer in C by alexchamberlain on Saturday September 21, 2013 @03:16AM (#6422231)
Malloc often involves a context switch to kernel mode to grab more memory. This has improved, but still doesn't address the fragmentation issue.