bfd: Grow in-memory BFD buffers geometrically for efficiency
Improves performance of in-memory BFD buffer growth by using a geometric allocation strategy, reducing quadratic reallocations.
This commit refactors how in-memory BFD buffers (memory_bwrite and memory_bseek) grow. Previously, they increased in small, fixed 128-byte increments, leading to repeated memory reallocations and copies for large buffers. This resulted in quadratic performance, particularly affecting operations like writing PDB streams during linking. The change introduces a geometric growth strategy, allocating an additional 1/8th of the current capacity, which significantly reduces the overhead and improves the performance of building large in-memory BFDs.
In Details
The memory_bwrite and memory_bseek functions in bfdio.c managed the capacity of in-memory BFD buffers. Their growth strategy was linear, incrementing capacity by a fixed amount (128 bytes). This led to O(N^2) complexity for writing large amounts of data, as the entire buffer content had to be reallocated and copied upon each growth. This patch introduces a memory_grow helper that implements a geometric growth strategy (doubling capacity or increasing by 1/8th, with a minimum increase), significantly improving performance for operations involving large in-memory BFDs, such as PDB strea…
- BFD
- Binary File Descriptor library, an abstraction layer within GNU Binutils for handling various executable and object file formats.
- In-memory BFD
- A BFD structure that represents a binary file entirely in memory, often used for temporary buffers or during linking operations.
- PDB stream
- Program Database stream, a data structure used by Microsoft's debugging tools to store symbol and type information, often generated during the linking process for PE executables.
- Reallocation
- The process of moving a block of memory to a new, larger location when the current memory allocation is insufficient.