r/cpp • • Feb 16 '26

Favorite optimizations ??

I'd love to hear stories about people's best feats of optimization, or something small you are able to use often!

136 Upvotes

185 comments sorted by

View all comments

17

u/Ilyushyin Feb 16 '26

Reusing memory! I almost never return a vector or string from functions but have the function modify the vector, so you can use the same allocation(s) throughout your whole data processing pipeline.

Also using arena allocator whenever possible.

7

u/lordnacho666 Feb 16 '26

Yeah, simply using any allocator other than the default one does wonders.

2

u/Both_Helicopter_1834 Feb 16 '26

To me, that doesn't seem obviously true in general. Can you elaborate?

1

u/lordnacho666 Feb 16 '26

Let me see if I can find an old SO question. (Yes I know they're all old lol)

1

u/Fabulous-Meaning-966 Feb 19 '26

Depends on your baseline. This is often true for the glibc allocator in Linux, less so for the default libc allocator in FreeBSD (jemalloc).

1

u/lordnacho666 Feb 19 '26

That's a fair point actually

12

u/thisismyfavoritename Feb 16 '26

technically you can move in and return which should achieve similar performance and has a way clearer API

0

u/vandercryle Feb 16 '26

This is the right way. The other was terrible advice.

→ More replies (10)

0

u/donalmacc Game Developer Feb 16 '26

I agree in theory, but in practice it’s very easy to break that optimisation and cause a copy.

16

u/borzykot Feb 16 '26

I'd argue that's a bad practice in general. Functions with return should be your default. You can add second overload with out parameter if you really need this micro optimization. Everytime I encounter out parameters in, for instance, UE I'm asking myself "is this function just adds new elements?". And you know what, in some cases it also clears the original collection. And you never know that from the callee side.

1

u/conundorum Feb 16 '26

Usually, this pattern returns a reference to the passed object, like so:

template<typename T>
std::vector<T>& add_elem(std::vector<T>& vec, T elem) {
    vec.emplace_back(std::move(elem));
    return vec;
}

const correctness is used to indicate parameter type: const reference is assumed to be an in-parameter, non-const reference is assumed to be an out-parameter, and by-value is usually assumed to be an elision-friendly "constructed to be moved" in-parameter. (Unless you're working with C-style functions, in which case you just pray.)


This does assume that people understand const correctness well enough to instantly grok that the lack of const means the function intends to modify the parameter, so it might be best to document intent with empty IN and OUT macros, WinAPI style.

2

u/borzykot Feb 17 '26

Ok. Now tell me what this function does?

void collect_children(const node& parent, std::vector<node*>& children);

1

u/VerledenVale Feb 16 '26

Can have a parameter you move in to use as return value which defaults to empty container.

-2

u/cdb_11 Feb 16 '26

Everytime I encounter out parameters in, for instance, UE I'm asking myself "is this function just adds new elements?". And you know what, in some cases it also clears the original collection.

template <class T> using out = T&;
template <class T> using inout = T&;

Or a dummy macro

#define OUT
#define INOUT

1

u/Sopel97 Feb 16 '26

this achieves nothing

4

u/cdb_11 Feb 16 '26

It documents the parameters.

If you want to enforce that output parameters are always cleared, I bet you could add template specializations that automatically clear the container in the constructor.

1

u/Both_Helicopter_1834 Feb 16 '26

Taco kid sez, why not both:

void fm(vector<T> &v);

inline vector<T> f(vector<T> const &v) { vector<T> v_{v}; fm(v_); return v_; }

Or, if the function is doing something like a merge sort:.

// out2 may be orig. If out1 is empty, result is in out2.

void f_help(vector<T> const &orig, vector<T> &out1, vector<T> &out2);

inline void fm(vector<T> &v) { vector<T> tmp; f_help(v, tmp, v); if (v.empty()) v = move(tmp); }

inline vector<T> f(vector<T> const &v) { vector<T> tmp[2]; f_help(v, tmp[0], tmp[1]); if (tmp[0].empty()) return tmp[1]; return tmp[0]; }

0

u/CocktailPerson Feb 17 '26
inline vector<T> f(vector<T> const &v) { vector<T> v_{v}; fm(v_); return v_; }

This creates a copy, which we're trying to avoid.

1

u/Both_Helicopter_1834 Feb 17 '26

Sometimes you need both the original and the modified container.

0

u/CocktailPerson Feb 17 '26

But we're talking about reusing memory.

If your data processing pipeline relies on reusing memory for performance, then it's a bad thing to provide a version of a sub-computation that implicitly copies the memory instead.

1

u/Both_Helicopter_1834 Feb 17 '26

I'm confused. Are you saying you can't imagine a scenario where you'd want a modified copy of a large object, but you'd also need to keep the original?

0

u/CocktailPerson Feb 17 '26

No, of course not. I'm saying I don't want your overload as part of the API. If I want a modified copy, I'll make a copy and modify it. Don't make it easier to do the less-performant thing.

1

u/Both_Helicopter_1834 Feb 17 '26

OK your purchase price of $0.00 is cheerfully refunded.

1

u/CocktailPerson Feb 17 '26

Well, if anything, you should get the refund. You asked "why not both?", and I told you why not. Sorry you didn't like the answer.