apache / apache/arrow

[C++] Add MemoryPool API to resize without copying the data

Open
#40,686 7 comments 1 reaction 1 assignee Claimed by @pitrou View on GitHub
Component: C++ Type: enhancement
Dominant language
C++
Stars
17.1k
Forks
4.3k
Avg merge
3d 13h
Merged PRs (30d)
88

Description

### Describe the enhancement requested

A common memory (re)allocation pattern is to persist a dynamically-sized scratch buffer (for example when decompressing or decoding data). Currently, the two options to achieve that are suboptimal:
1. either resize the buffer when needed, but if the buffer's base pointer changes, the existing data will be copied even though it is ephemeral
2. or allocate a new buffer when needing to increase the scratch size, but it is wasteful if the buffer could otherwise have been resized in place

Both jemalloc and mimalloc expose APIs that would allow us to implement a more optimal behavior of resizing without copying existing data: [mi_expand](https://microsoft.github.io/mimalloc/group__malloc.html#gaaee66a1d483c3e28f585525fb96707e4) for mimalloc, [xallocx](https://jemalloc.net/jemalloc.3.html) for jemalloc.

One possible API would be:
```c++
// MemoryPool methods
Status ReallocateNoCopy(int64_t old_size, int64_t new_size, int64_t alignment, uint8_t** ptr);
Status ReallocateNoCopy(int64_t old_size, int64_t new_size, uint8_t** ptr);

// ResizableBuffer methods
Status ResizeNoCopy(const int64_t new_size, bool shrink_to_fit);
Status ResizeNoCopy(const int64_t new_size);
```

### Component(s)

C++

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.