The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Vincent Hui (6) [Avatar] Offline
#1
We know that dynamic memory allocation is time consuming. Assume we know how much memory we need for an output of a function, how to avoid multiple dynamic memory allocations in functional style?
I have an example http://quick-bench.com/QhDHBqlVkk_1FmXP-4wWIF9pHCw. We can see that function1 is much slower than function2.

How to achieve high performance Functional Programming in C++?
Ivan Cukic (104) [Avatar] Offline
#2
To be honest, I haven't spent much time with FP-specific optimizations. My rule-of-thumb would be to develop what you can in the FP-style, and use the usual C++ optimizations if you need them (when you detect a bottleneck) even if they don't fit FP best practices.

It is the same with OO practices. Just look at the std::getline - it is not a nice OO design, but its API is designed with efficiency in mind.

In your case, the same trick as std::getline would be applicable - to have the same vector instance reused over and over again, where it would need to be reallocated only of the size of the new vector is larger than the capacity of the old one.
Vincent Hui (6) [Avatar] Offline
#3
So far, I found the most simple way to avoid dynamic allocation is to use std::move.

My another question is
How to use std::move with monad to achieve high performance Functional Programming in C++?

or We can have other simple ways to achieve high performance Functional Programming in C++ without using std::move?


Thank a lot.


Reference:
Efficient Pure Functional Programming in C++ Using Move Semantics