Hacker News new | past | comments | ask | show | jobs | submit login

I have found that a general pattern of setting up a memory arena or anything of that kind does help, especially of course if one anticipates more frequent / heavier usage of heap. Helps with performance (e.g.: faster allocations), memory fragmentation (which itself also helps with performance indirectly of course) and other things. Can be very simple.[1]

Or more complicated (if anticipating a lot of allocation work for very varying buffer sizes) - e.g. slab allocators. memcached is an example where this is used, a couple pictures explain the gist.[2]

[1]: note: can be even simpler of course, but quick example of structs used:

```

  typedef struct
      char *memory;
      size_t size;
      size_t used;
  } memory_arena_t;

  typedef struct {
      memory_arena_t *arenas;
      size_t arena_count;
      size_t max_arenas;
      size_t arena_size;
      size_t total_size;
      size_t total_used;
  } memory_allocator_t;
```

[2]: https://siemens.blog/posts/memcached-memory-model/ - I'm sure that when heap is visualised, it would show how this helps keeping fragmentation at bay as well (this helps wasting fewer memory pages, too).






Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: