optimising the factors such as resizing and copying#21
Open
Butcher3Years wants to merge 1 commit intoDynamic-arraysfrom
Open
optimising the factors such as resizing and copying#21Butcher3Years wants to merge 1 commit intoDynamic-arraysfrom
Butcher3Years wants to merge 1 commit intoDynamic-arraysfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Optimizing std::vector – reserve, emplace_back, resizing & copying pain 🪓
Branch:
vector-optimizationsFrom The Cherno's C++ series — the episodes where he screams about people killing performance with naive
push_backloops.Core Problem: Reallocation & Copying
std::vectorgrows dynamically. Whensize()reachescapacity(), it:This is expensive — especially with large or complex types (deep copies, many reallocations).
Cherno:
Key Optimizations
reserve(n)– Pre-allocate capacityreserve only allocates memory — does not change size()
emplace_back-Construct in-place (no copy/move)
std::vectorstd::string names;
// BAD – creates temporary string → copies/moves into vector
names.push_back("Cherno");
// GOOD – constructs string directly inside vector's memory
names.emplace_back("Cherno");
// Even better – forward arguments
names.emplace_back(10, 'a'); // "aaaaaaaaaa" – no temporary
push_back → creates temporary → copy/move into vector
emplace_back → perfect forwarding → constructs directly → no extra objects
Avoid unnecessary copies when copying vectors
std::vector src(1000);
// BAD – deep copies 1000 objects
std::vector dst = src;
// BETTER – pre-reserve + bulk insert
std::vector dst;
dst.reserve(src.size());
dst.insert(dst.end(), src.begin(), src.end());
// BEST – if you can steal: use std::move
std::vector dst = std::move(src); // src now empty
RuleWhy It Saves Your AssAlways reserve(n) before known-size loopAvoids reallocations & copiesPrefer emplace_back over push_backNo temporaries, no copy/moveUse reserve + insert for bulk copyMuch faster than copy ctor loopstd::move when you can steal resourcesAvoid deep copiesKnow capacity() vs size()Reallocation = performance killerUse shrink_to_fit() after massive eraseRelease unused capacity (optional)