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

Optimize LocalVector::push_back for non-trivial objects #104693

Merged
merged 1 commit into from
Mar 28, 2025

Conversation

mihe
Copy link
Contributor

@mihe mihe commented Mar 27, 2025

I happened to stumble upon this discrepancy between how trivially constructible objects and non-trivially constructible objects were being constructed in LocalVector::push_back, where trivially constructible objects seemed to be using std::move (thus avoiding the redundant copy) and non-trivially constructible objects were not.

It seemed to be something that was missed as part of #100477, as mentioned by @Ivorforce here.

Some quick for-loop profiling using this code with MSVC...

constexpr int N = 100000;
constexpr int M = 8;
LocalVector<Variant> vector;
vector.reserve(N * M);

Variant variants[M] = {
	true,
	42,
	42.0f,
	String("Lorem ipsum dolor sit amet, consectetur adipiscing elit."),
	Vector3(),
	Transform2D(),
	Transform3D(),
	Projection()
};

auto t0 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; ++i) {
	for (int j = 0; j < M; ++j) {
		vector.push_back(variants[j]);
	}
}
auto t1 = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count() << "ms\n";

... yielded 15.48 ms without the change and 12.89 ms with the change, so roughly a 16% reduction for Variant specifically, but obviously depends greatly on what the object does in its copy constructor.

(This also seems to be the only thing standing in the way of LocalVector being able to contain move-only types.)

@mihe mihe added enhancement topic:core cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release labels Mar 27, 2025
@mihe mihe added this to the 4.x milestone Mar 27, 2025
@mihe mihe requested a review from a team as a code owner March 27, 2025 13:25
Copy link
Member

@Ivorforce Ivorforce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct. I merely forgot to add it. Thanks for stepping in!

@mihe
Copy link
Contributor Author

mihe commented Mar 27, 2025

I added the 4.4 cherrypick label to it, since I figured it's a small change and fairly low risk, but I'm honestly not sure what the general stance is on cherrypicking performance improvements like this.

@lawnjelly
Copy link
Member

lawnjelly commented Mar 27, 2025

Just checked and I had already added this in 3.x with #100995 , and it was backporting @Ivorforce PRs, so maybe it was there somewhere originally for 4.x (but got missed somehow).

UPDATE: Ah no, Lukas noticed it as an addition, but it never got added to 4.x.

@clayjohn
Copy link
Member

I added the 4.4 cherrypick label to it, since I figured it's a small change and fairly low risk, but I'm honestly not sure what the general stance is on cherrypicking performance improvements like this.

Usually we don't, but since this change is really to complete a PR from 4.4, I think it makes sense

@akien-mga akien-mga merged commit 4a31936 into godotengine:master Mar 28, 2025
20 checks passed
@akien-mga
Copy link
Member

Thanks!

@mihe mihe deleted the local-vector-move branch March 28, 2025 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release enhancement topic:core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants