whyrusleeping / cbor-gen

Codegen for cbor codecs on your types
MIT License
35 stars 25 forks source link

fix: reduce memory held by deferred objects #96

Closed Stebalien closed 7 months ago

Stebalien commented 7 months ago

bytes.Buffer will always overallocate (for performance) and we don't have a great way of predicting the correct size. So, instead, copy once when we're done.

We we're seeing 10x the memory usage expected in a network migration in Filecoin.

Stebalien commented 7 months ago

Question:

We give Deferred to the user so the user would need to put it back in the pool (tends to be sketchy). By pooling bytes.Buffer, we can control the full lifecycle and don't need to ask the user to explicitly free it.

We could put a bytes.Buffer inside Deferred, but:

  1. I wouldn't pool it (we give it to the user, they may hang on to a reference to the underlying byte slice).
  2. That'll make it difficult to shrink the allocation.