Skip to content
TopicTracker
From HackerNewsView original
TranslationTranslation

A deep dive into SmallVector:push_back

A technical deep dive into the push_back implementation of LLVM's SmallVector, exploring its design trade-offs, memory management strategies, and performance characteristics compared to std::vector.

Background

- SmallVector is a C++ data structure (used in LLVM, Clang, and other large C++ projects) that attempts to combine the benefits of a fixed-size array (no heap allocation for small data) with those of a dynamic vector (can still grow when needed). This is called a "small-size optimization." - This post gives an unusually detailed, assembly-level walkthrough of what the C++ compiler actually generates for `push_back` — the operation that appends an element to the SmallVector — and explores edge cases like reallocation, move semantics, and alignment. - The target audience is experienced C++ developers interested in performance tuning and low-level code generation; the post assumes deep familiarity with C++ templates, move constructors, and x86-64 calling conventions.