Home / Forums / Implementing Small Vector Optimization (SVO) in a custom C++ container

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

Implementing Small Vector Optimization (SVO) in a custom C++ container

PointerGuru
Systems & Memory Engineer
MEMBER
Rep: 209
Join Date: Feb 2021
Posts: 6
Thanks: 107
5y ago · Feb 4, 2021 9:07 AM
#1
Small Vector Optimization embeds a static array directly inside the vector struct (e.g. 16 or 32 bytes). For small element counts, zero heap allocations occur. When capacity is exceeded, it seamlessly transitions to dynamic heap allocation.
PointerGuru · Systems & Memory Engineer
Raw pointers, custom allocators, virtual memory mapping, and page...
The following users thanked PointerGuru for this post:
CacheOptimizer
Data-Oriented Architect
VIP
Rep: 111
Join Date: Jul 2020
Posts: 6
Thanks: 34
5y ago · Feb 4, 2021 12:23 PM
#2
SVO vectors are massive performance wins for short-lived collections like function arguments, AST children nodes, and query result lists. It keeps the data in the CPU L1 cache.
CacheOptimizer · Data-Oriented Architect
L1/L2/L3 cache line tuning, Structure of Arrays (SoA), and prefet...
StdRangesFan
Functional C++
MEMBER
Rep: 119
Join Date: Mar 2022
Posts: 6
Thanks: 79
5y ago · Feb 4, 2021 1:23 PM
#3
Make sure your union properly handles object lifetime construction via placement new and explicit destructor calls when transitioning between the inline buffer and heap pointer!
StdRangesFan · Functional C++
C++20 view pipelines, lazy evaluation, and composable range algor...