r/cpp • u/Saptarshi-max • Mar 09 '26
Why std::pmr Might Be Worth It for Real‑Time Embedded C++
http://www.sapnag.me/blog/cppdev/2025-12-26-containers-std-vs-pmr/If you are an Embedded Software developer using C++ or migrating to C++ from C you must have definitely heard about C++17's "std::pmr" (Polymorphic Memory Resources)
Quick background for anyone unfamiliar: PMR lets you tell your containers where to get memory instead of always hitting the global heap which 'std' does
cpp
char buf[4096];
std::pmr::monotonic_buffer_resource pool{buf, sizeof(buf)};
std::pmr::vector<int> v{&pool}; // zero heap involvement
Ran some benchmarks on real hardware (Snapdragon X Plus, GCC 12.2):
std::vector1000 push_backs: ~17µs, 10% variancepmr::vectorsame test: ~69µs, 5.6% variance
It seems, PMR is about 4x slower. But here's the thing, in hard real-time systems that variance number matters more than the average. A missed deadline doesn't care that you were usually fast.
For strings the gap was smaller (~17% slower) but consistency improved 3x.
Honestly I went in expecting PMR to be a great fool proof approach for Embedded software development in C++, but its not. It's a deliberate tradeoff. If you're on a safety-critical path where WCET**(Worst Case Execution Time)** matters, it's probably worth it. If you just want fast code, stick with std.
Full benchmarks on my GitHub if anyone wants to poke at the numbers: www.github.com/saptarshi-max/pmr-benchmark
And the full report and observations in my Blog Post: www.sapnag.me/blog/cppdev/2025-12-26-containers-std-vs-pmr/
Anyone else actually shipped PMR in production especially for Real-time applications? Curious what buffer sizing strategy people use in practice.
64
u/ald_loop Mar 09 '26
why didn’t you just use google benchmark? why would pmr::vector be 4x slower than a heap allocating vector? that doesn’t seem to make any sense.
52
u/SupermanLeRetour Mar 09 '26 edited Mar 09 '26
Not an expert with this but I believe that's not the goal of PMR. You can avoid standard heap allocation by writing your own allocator and provide it as the second template argument of most std container, no need for PMR allocators.
There's a good answer here on SO on what's the use of polymorphic_allocator.
I'm going to be honest, the blog post looks at least partly written by ChatGPT... The pro/cons, the comments with emojis, the tables, the way it's all formulated. Sorry if that's not the case but it looks very sus.
8
u/m-in Mar 09 '26 edited Mar 09 '26
In an embedded project at work, I have been using a “homebrew” library of containers that all do what pmr does. I have no idea how the std got it somehow slower. In my experience, not only is it faster when using a simple allocator, but the compiler can often inline the allocation/deallocation as they are very simple operations (check & bump, nothing) if it can devirtualize the calls. Which it should.
I don’t doubt that the benchmark shows something, but I don’t think it shows what is claimed. Have you looked at the machine code on godbolt? This just doesn’t compute for me at the moment.
As for the variance: it also depends on what the heap allocator is actually doing.
4
u/kammce WG21 | 🇺🇲 NB | Boost | Exceptions Mar 10 '26
I wanted to dig into why your benchmarks are showing this performance issue with PMR. I re-ran your tests but this time I used
I'm using a 2021 Apple M1 Pro with 32GB of memory with OS 15.7.4.
Using your steps, but with
VERBOSE=1set, I noticed that the optimization flag was not showing up. So you were probably testing against-O0and didn't realize it. TheVERBOSE=1flag will show you the commands being executed so you can be sure that you are testing what you think you are testing.After doing this and growing your buffer sizes by 10x, I was able to get these results for monotonic using
benchmark_vector:VS what I get using your steps and your code:
So monotonic is actually 1.52x faster than using STD in this case. When I changed your monotonic resource to have a
null_memory_resource()as a fallback (with the original buffer size):I get a
std::bad_allocthrown. That means that your previous benchmarks were using new/delete when the buffer is exhausted which is the default fallback formonotonic_buffer_resource. So with that, I think your conclusions about PMR aren't accurate. I'll post the full output I get in a reply here.But I will note that having the additional levels of indirection when your memory resource has to fallback to
new_delete_resource()will hit your performance since you are doing additional work.16
u/hk19921992 Mar 09 '26
Implement your own allocator. No need for that virtual dispatch overhead
42
u/Peddy699 Mar 09 '26
I might be missing something but 17us +10% is 18us worst case. 69us +6% 74us worst case.
Why would want to make your worst (and best) case execution time 4 time slower. Does any system benefit from this?
26
u/jwakely libstdc++ tamer, LWG chair Mar 09 '26
OP, you wrote:
But that's very misleading. C++ always allowed you to separate the container logic from the memory allocation, that's the whole point of the allocator parameter in the containers:
This wasn't introduced by C++17.
Your entire post fails to acknowledge this. You just keep saying you're comparing "std containers" to "pmr containers", but what you're actually doing is comparing std::allocator to std::pmr::polymorphic_allocator.
It's also possible to use std::vector with a custom allocator, and you've been able to do that for years before PMR was added in C++17.
You wrote:
No, containers never use any of those functions at all. Containers use the allocator interface provided by
pmr::polymorphic_allocator, and that uses those three functions.21
u/joz12345 Mar 09 '26
Your buffers are too small. In every case where you found pmr slower, you pass a buffer the exact size of the final data and continuously grow the vector - that is going to fill up your local buffer, guaranteeing that the final realloc will use malloc anyway.
7
u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Mar 09 '26
That sounds terrible. For your particular use case, you'd probably be fine with
boost::static_vector.5
u/jwakely libstdc++ tamer, LWG chair Mar 09 '26
I don't know much about cmake, but your build instructions in the github repo create unoptimized binaries for Linux. I think you need to use
-cmake DCMAKE_BUILD_TYPE=Release ..instead of just adding--config Releaseto the cmake-build step.5
u/Liam_Mercier Mar 09 '26
I don't understand why the pmr version would be anywhere near 4x as slow, why?
3
u/johannes1971 Mar 09 '26
Your allocated space is way too small for this. You can barely hold one copy of the buffer, but you need to be able to hold at least two (and possibly more), because the two copies need to be in memory at the same time when the vector grows its buffer size.
I'm curious what happens when you do this with a properly sized buffer. I suspect it will do much better.
2
u/lxbrtn Mar 09 '26
can you compare with https://github.com/ETLCPP/etl
1
u/IfreetBalkan Mar 09 '26
Hmm. And what about invoking vector.reserve(...) prior to the insertions..? Does that change the results?
1
u/yuri-kilochek Mar 09 '26 edited Mar 10 '26
Reminder that
std::pmr::polymorphic_allocator::polymophic_allocator()reads mutable global state and you should be extremely careful to not call it by accident.1
u/Realistic-Reaction40 Mar 10 '26
The allocator customization point in pmr is genuinely underused most people reach for custom allocators the old-school way and end up with templated everything. The monotonic buffer resource in particular is a nice fit for real time contexts where you just want bump allocation with zero fragmentation risk. Good write up.
1
u/Chaosvex Mar 12 '26
AI article.
1
u/TheRavagerSw Mar 12 '26
Implementing c++ stdlib is very hard on embedded compared to c, where you just define heap limit and maybe printf/fopen
1
u/Inevitable_Set6737 Jul 21 '26
Not sure how you concluded that PMR is slwoer? What were you benchmarking? its not clear. You need to create your pool once and pass it around to all the containers. You shouldne be measuring that in the benchmark..... Usually many containers will share your one global pool
0
u/saf_e Mar 09 '26
Main issue of std is using exception, some systems just do not support it.
-1
u/HobbyQuestionThrow Mar 09 '26
In real life systems, you just won't use an STL container for this.