34. SBO tradeoffs
In a feed-handler hot path, hidden allocations and pointer chasing cause latency spikes. Small Buffer Optimization (SBO) can keep tiny payloads inline, but crossing the threshold or copying lvalues can reintroduce heap traffic.
#include <string>
void hot(std::string s) noexcept { (void)s.size(); }
int main() {
std::string a(7, 'x');
std::string b(64, 'x');
hot(a);
hot(std::move(b));
}
Part 1.
Which operations in this snippet likely perform heap allocations, and which do not? Explain how SBO and value vs. move semantics affect latency here.
Part 2.
(1) When is pass-by-value for std::string competitive with const std::string&?
(2) How does move-if-noexcept influence std::string moves and SBO behavior?
(3) How do custom allocators interact with SBO capacity and allocation paths?
(4) Compare cache locality of SBO strings versus heap-backed ones during copies.
(5) When prefer std::string_view here, and what lifetime risks arise?
Answer
Answer (Part 1)
a likely fits SBO, so constructing a is allocation-free; hot(a) copies an SBO buffer inline, still no heap. b(64, 'x') likely allocates once in main; hot(std::move(b)) moves the heap pointer without allocating, but the initial b construction already incurred a heap hit. Value-taking in hot is cheap for rvalues (move) and SBO-sized inputs, but costly for large lvalues; prefer string_view/const std::string& or overloads to avoid extra copies on lvalues.
Answer (Part 2)
(1) When callers mostly pass rvalues and sizes exceed SBO so moves steal pointers. For lvalues, pass-by-value copies; const& avoids copies.
(2) If move isn’t noexcept, move_if_noexcept may copy instead, increasing cost. noexcept moves enable pointer-steal moves, stabilizing latency.
(3) SBO ignores the allocator until capacity is exceeded. Crossing the SBO threshold triggers allocator paths; custom allocators can bound latency.
(4) SBO keeps data inline, improving locality and reducing indirection. Heap-backed copies touch multiple cache lines and add misses.
(5) Use std::string_view for read-only processing to avoid allocations/copies. Ensure the referenced string outlives the view to prevent dangling.