dragonfly icon indicating copy to clipboard operation
dragonfly copied to clipboard

Improve performance by pre-allocating vectors

Open abhijat opened this issue 3 months ago • 3 comments

In some cases we create vectors for response eg OpResult<StringVec> dfly::OpInter(const dfly::Transaction* t, dfly::EngineShard* es, bool remove_first) and push elements into it iterating over a set.

These vectors can end up being multiple 100s of MiB in size (250MiB vectors have been observed).

This ends up in a lot of reallocations over time as the vector grows and potentially moves to new memory pages.

It can be more efficient (to be tested if it actually is) to first determine the result size, allocate vector with that capacity, and then add results to it, or even use a chunked vector similar to deque but with configurable chunk size, which chains together contiguous blocks of memory and allocates a new one when the existing one is full.

abhijat avatar Sep 30 '25 08:09 abhijat

We can use a vector pool -- we do something similar on the connection level and specifically for pipeline messages. The more the pool is used the more we allow it to grow (and similarly we shrink it if it remains unused)

kostasrim avatar Oct 13 '25 06:10 kostasrim

@kostasrim connection vector is KB, here @abhijat tell us about 250MB. so I prefer to see deque

BorysTheDev avatar Oct 13 '25 06:10 BorysTheDev

  1. it's a minor issue
  2. It can be more efficient (to be tested if it actually is) is a key question. A reproducible example with proof where we preallocate vectors indeed moves the needle is required before we talk about the implementation.

romange avatar Oct 13 '25 07:10 romange