bfd: Grow in-memory BFD buffers geometrically
BFD library patch improves performance by using geometric growth for in-memory buffers.
This patch optimizes the BFD library’s handling of in-memory buffers by changing their growth strategy from a fixed 128-byte increment to a geometric progression (growing by one-eighth of capacity, with a minimum of 128 bytes). This significantly reduces the number of reallocations and copies required when building large in-memory BFDs, which was previously a quadratic performance bottleneck, especially when writing PDB streams. The change improves performance for operations like linking large executables with PDB support.
- proposer
Introduces geometric buffer growth for in-memory BFDs to avoid quadratic reallocations and improve performance.
“memory_bwrite and memory_bseek grew the buffer in 128-byte steps, so building a large in-memory BFD out of many small writes reallocated and copied the whole buffer repeatedly. This made writing PDB streams quadratic: linking a 235 MB executable with --pdb spent more than half of its time in realloc.”
In Details
This patch modifies the BFD library's memory buffer management. Previously, memory_bwrite and memory_bseek grew buffers in fixed 128-byte increments, leading to quadratic complexity (O(n^2)) for many small writes, as seen when generating PDB streams. The solution implements a geometric growth strategy (e.g., doubling capacity or growing by 1/8th) to amortize reallocation costs, improving performance for large in-memory buffer operations. It also includes fallbacks for exact allocation if geometric growth fails.
- BFD
- Binary File Descriptor library, a GNU library used for accessing and manipulating various executable binary file formats.
- in-memory BFD
- A Binary File Descriptor object that is created and manipulated entirely in RAM, rather than being backed by a file on disk.
- reallocation
- The process of moving a block of memory to a new, larger location when the current memory block is no longer sufficient to hold the data.
- quadratic complexity
- A type of computational complexity where the runtime or space required grows proportionally to the square of the input size (O(n^2)). This is generally considered inefficient.
- PDB
- Program Database, a file format used by Microsoft Visual C++ for storing debugging and project information.