3 ms·
The "idiomatic" version is slowly getting slower because the idioms are getting higher-level and more expressive. If you don't use the idioms, you can have the
by pclmulqdq 4y ago
The "idiomatic" version is slowly getting slower because the idioms are getting higher-level and more expressive. If you don't use the idioms, you can have the same speed. Smart pointers (including unique_ptr<Foo>) have non-zero overhead compared to Foo*. The object oriented parts can introduce significant slowdowns. Template code can have huge code footprints if you are not careful, which slows things down.
If you are looking for cycle-level performance, you likely need to use a more C-like subset of the language, but you can still use many conveniences like RAII and the smart pointers with 0 overhead (when construct/destruct are not in the critical path).
- ncmncm 4y agoThe above is commonly encountered, but not good advice. Most important, defend your hot path against incursions. E.g., don't pass smart pointer arguments around on hot paths. That is what matters. New language features are not slower than old features. But there are slow constructs to use carefully. Which they are will always surprise you, usually pleasantly. For example, almost everything around lambdas is fast, fast, fast, except =capture. Measure often. Look for surprises.
- jcelerier 4y ago> Don't pass smart pointer arguments around on hot paths. oh this so much.. I remember optimizing a particle system which would copy a shared_ptr for each particle on every update operation... IIRC just switching to references ended up being 10+ times faster
- latency-guy2 4y agoIf you can design your system so you rarely if ever use pointers, that would be the single best thing you can do for your C++ codebase I like to enlighten my interns every summer about the reality of pointers, and how much of a noob trap they are, and that yes, their professor lied to them in some ways
- pclmulqdq 4y agoI literally said not to construct or destruct smart pointers on the hot path. Also, my experience with lambdas has been otherwise. There are a lot of cases where lambdas need a heap allocation when you otherwise wouldn't. However, being able to use FP ideas can make up for that.
- ncmncm 4y agoMy objection is to the expression "C-like". That does not correctly distinguish what is fast from what isn't, and instead promotes a harmful myth. A lambda needs a heap allocation only if any of its captures do.
- int_19h 4y agoThe idiomatic version has gotten faster since C++03 overall thanks to move semantics. std::unique_ptr<Foo> doesn't have any overhead over Foo* in code where semantics is the same (i.e. you want to delete when going out of scope) on any sensible ABI - it has the same storage size, and all operations are trivially inlineable to the same exact thing you'd do with a raw pointer. About the only time I can think of where it can be slower is when the ABI insists that a struct cannot be passed or returned in a register.
- kccqzy 4y ago> std::unique_ptr<Foo> doesn't have any overhead over Foo* in code where semantics is the same Not true. And you just refuted yourself by being up the ABI question. You should watch Titus Winters' presentation on this topic where he compiles code using unique_ptr and raw pointers, compares the assembler produced, and explains why unique_ptr has non-zero overhead. The overhead is indeed coming from the ABI. Google has been wanting the next C++ to break ABI but they didn't get enough votes in the standard committee. https://youtu.be/rHIkrotSwcc https://youtu.be/rHIkrotSwcc
- ncmncm 4y agoNot breaking binary compatibility is a pure good. The standard could pull unique_ptr in as a built-in feature with a different name, and retain backward compatibility at cost of an unfortunate redundancy. Code using unique_ptr would gradually transition to the new thing. Of course each would have a move constructor from the other, to ease the transition.