-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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 / refactor CowData
, combining resize and fork to avoid unnecessary reallocations.
#100619
base: master
Are you sure you want to change the base?
Conversation
749d0a5
to
3a4c1f9
Compare
Hrm. I'm kinda stumped as to what's leaking. I think it's foolproof, in any case foolproof if the |
c45c762
to
d69cb3b
Compare
d69cb3b
to
84b402c
Compare
Oh my god... This leak is costing me and arm and a leg to debug. Here is a failing log for reference: https://github.com/godotengine/godot/actions/runs/12435300159/job/34720869143 I have finally tracked it down to the GDScript tests only. Normal operation does not appear to cause leaks. At this point, I am reasonably sure it's not my implementation that's causing it, but some kind of circular reference. Though it's hard to be completely certain. Running the unit tests locally without |
I have an update. Turns out one of the 'preconditions' I was imagining to be true was false all along - This is true on master as well, as this fails unit tests: master...Ivorforce:godot:cowdata-refcount-0 After meticulously re-creating my PR step by step, I'm pretty sure that's what's causing the issues. I don't know how it's possible that |
fe0c304
to
69c3f06
Compare
I think I finally fixed it. So, Instead, I managed to handle it more graciously through #100694. With the change, |
… unnecessary reallocations.
69c3f06
to
2617af3
Compare
This PR optimizes
CowData
resize
operations. This affects all non-trivial resizes forString
andVector
(e.g.Packed
arrays), including concatenation.This PR includes #100694, because without it, leaks are occurring due to cyclic references that the previous implementation luckily avoided through unnecessary forking.This PR also supersedes #100672.
Reasoning
The current implementation of
resize
first forks if there are multiple owners, and then resizes the freshly forked array.In the case that the needed array is larger than the previous array, time is wasted because a new location may have to be found for the larger buffer on the subsequent
realloc
, and the data has to be copied over again.In the case that the needed array is smaller than the previous array, time is wasted looking for a larger buffer than necessary, and constructing (and subsequently destructing) more elements than necessary.
Benchmark
I've benchmarked plain String concatenation:
This yields:
1349ms
onmaster
(8483d79 / ba2c5c1)825ms
on this PR (77d8900)As such, it can be concluded that String concatenation through
+
can be 1.6x as fast as before. The PR is likely to affect and optimize many more functions involvingString
orVector
.