Hasty Briefsbeta

Bilingual

A deep dive into SmallVector:push_back

3 days ago
  • #push_back optimization
  • #C++ containers
  • #SmallVector
  • SmallVector::push_back was optimized for trivially copyable element types by tail-calling an out-of-line slow path, eliminating stack frames and callee-saved register spills on the fast path.
  • The optimization reduces instruction count by 0.41–0.51% and shrinks .text by 40,512 bytes, though binary size increases slightly by 0.13% and some outliers grow due to inlining changes.
  • Comparison with std::vector, boost::small_vector, and absl::InlinedVector shows they incur stack frames on the fast path, while SmallVector's design trades metadata size for better hot-path performance.
  • The dual trade-off: while single push_back benefits from no frame, loops suffer as metadata stays in memory due to address escaping in the out-of-line call.
  • SmallVector uses a five-class hierarchy to separate concerns, enabling efficient code generation with minimal per-type overhead and a header smaller than std::vector for many types.