C++23 memory and performance optimization
posted on 28 Jul 2026 under category programming
| Date | Language | Author | Description |
|---|---|---|---|
| 28.07.2026 | English | Claus Prüfer (Chief Prüfer) | C++23 Parser Optimization: string_view And Spanstream Reduce Time And Memory |



This article documents the technical changes and benchmark impact of WEBcodeX1/http-1.2 PR #2031.
The PR modernizes the HTTP parser from a C++11-style, allocation-heavy implementation to a C++23-oriented approach with std::string_view, std::span, and std::ispanstream.
The key result is simple: the parser performs less heap work, copies less data, and therefore runs faster while consuming significantly less memory.
std::string_viewstd::string_view is a non-owning view over existing character buffers.
Instead of creating new std::string objects for every substring operation, parsing logic can reference slices of already available memory.
For parser workloads this is highly relevant because tokenization often creates many short-lived substrings. Eliminating those copies directly reduces allocation pressure.
std::span And std::ispanstream (C++23)The optimized header parser reads request data through:
std::span<const char> as a safe view over contiguous request bytesstd::ispanstream for line-by-line stream parsing without building intermediate copied buffersThis replaces the old split-based approach that built vectors of temporary strings and modified request data in place.
unordered_map HashingThe PR introduces a transparent hash (StringHash with is_transparent) and equal_to<> in map types.
This allows heterogeneous lookups (for example string-literals / views) without constructing temporary std::string keys.
That reduces hidden allocations during map access paths.
A new split overload accepts string_view and returns vector<string_view>.
Compared to destructive split on mutable strings, this avoids repeated erase/copy behavior during tokenization and keeps input buffers unchanged where mutation is unnecessary.
getRequests() now returns const RequestsMap_t& instead of returning by valuereplace(0, n, "") to erase(0, n)string_view where possibleThese changes remove avoidable data movement and avoid full container copies.
The old implementation:
vector<string>This generated many temporary allocations and frequent short-lived objects.
The new implementation:
std::ispanstreamstring_view slices for key/value extractionstd::string mainly at final map insertion boundariesResult: less heap churn and lower parser overhead.
substrvector<string>string_viewvector<string_view>Result: fewer transient strings and lower allocator activity.
The benchmark1 generated 500 random valid HTTP requests with varying header counts, GET parameter counts, payload sizes, and mixed GET/POST patterns. Each request was repeatedly measured and results exported to CSV.
Execution time improves mainly because:
Header parsing benefits most because the old flow had the highest temporary-object density.
The memory benchmark tracked allocation calls and total allocated bytes using global operator new wrappers, across 500 requests with median aggregation over repeated measurements.
Memory consumption is lower because string_view and streaming parsing replace copy-heavy token pipelines.
The old code allocated aggressively for intermediate split containers and substrings.
The new code mostly allocates where ownership is genuinely required (final map storage), not during each parse step.
PR #2031 is a textbook example of modern C++ parser optimization: migrate from copy-centric string handling to view-centric parsing with controlled ownership boundaries.
The benchmark data confirms that this is not merely stylistic modernization:
For protocol-heavy systems, these improvements scale directly with request volume and are therefore highly relevant for real production workloads.
WEBcodeX1/http-1.2 Pull Request #203 — perf: optimize httpparser with C++23 string_view, ispanstream, and transparent maps: https://github.com/WEBcodeX1/http-1.2/pull/203 ↩ ↩2 ↩3