Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use reserve for std::vector #346

Merged
merged 1 commit into from
Apr 6, 2022
Merged

Conversation

schiller-manuel
Copy link
Contributor

No description provided.

@wcandillon
Copy link
Contributor

nice 🙌🏼 silly question, could this be done in the vector constructor?

@schiller-manuel
Copy link
Contributor Author

Yes, the constructor can be called with a size. However, this would default initialize each element, and those default initialized elements would then have to be overwritten:

Example:

std::vector<SkPoint> points;
points.reserve(pointsSize);
for (int i = 0; i < pointsSize; i++) {
   points.push_back(...);
}

Instead:

std::vector<SkPoint> points(pointsSize);
for (int i = 0; i < pointsSize; i++) {
   points[i] = ...
}

To avoid the unecessary default constructor calls, I'd go with reserve.

@chrfalch chrfalch merged commit 6edfbae into Shopify:main Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants