lightyear icon indicating copy to clipboard operation
lightyear copied to clipboard

Arena allocator

Open cBournhonesque opened this issue 8 months ago • 1 comments

Context

There are a lot of situations where we suddenly need to allocate a bunch of small objects that we will then discard:

  • when buffering all the updates that will go into the replication messages
  • when we do priority filtering
  • when build the final packets
  • when we read the received packets into data structures

It might be beneficial to use an fast arena allocator that allocated a bunch of memory. We use this allocator in the hot path, and then regularly we just cheaply drop the chunk of memory at once.

Solution

Introduces a ArenaManager that wraps a SyncBlinkAlloc from the blink-alloc crate, which provides a multi-thread safe arena allocator.

Use this arena everytime we send to allocate the pending_actions and pending_updates that we need to build the final ReplicationMessage.

Benchmarks

send_insert_float/1_client

num_entities main branch
1000 1.30ms 1.21ms
10000 13.10ms 13.23ms

Again it doesn't seem to be bringing much improvement, maybe the allocations in replication_send are not the problem, rather it's the ones in the packet building steps? Double-check.

Anyways it's nice to have a working implementation

cBournhonesque avatar Jun 05 '24 16:06 cBournhonesque