-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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 StringBuilder by using LocalVector
instead of Vector
.
#99775
Optimize StringBuilder by using LocalVector
instead of Vector
.
#99775
Conversation
5f2f32a
to
4c5bfd0
Compare
After moving to LocalVectors, almost all of the cost of StringBuilder is from the call to Given that, I suspect that this code will still be in the 80-90ms range, although testing is necessary to confirm of course. What error checks do you think are necessary? |
I had a pretty in-depth look at
Number 2) is wholly unnecessary, as indicated by my test: onlinegdb test code. Casting rules of the C++ spec ensure this should not be different under any compiler, so we should be able to refactor the ternary with a simple This leaves number 1) and number 3). It is impossible to trigger the Number 3) check, because the 1) check will terminate the string early anyway. Though the reliance on To answer your question directly: After going in-depth, I don't think any of these are necessary, but whether or not they are should be discussion of other PRs. |
76e8089
to
77b1a06
Compare
I removed the static-string length parts. They weren't working anyway yet, and I've been moving to a more well-rounded approach to avoiding |
LocalVector
and improving the as_string
assignment loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, straightforward improvement! Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure the changes in the cpp file has any impact on performance. But changing to local Vector is definitely a good thing !
77b1a06
to
ebf4b31
Compare
You're right. I just did some more tests. I think something about using buffer pointers directly confuses the compiler. In some instances i even got worse results by removing the running index. In any case, I've just removed the |
LocalVector
and improving the as_string
assignment loop.LocalVector
instead of Vector
.
ebf4b31
to
8df2dbe
Compare
Let's go ahead and merge this for now as it makes a huge difference and is totally safe. However, I do think we can go ahead with the changes from #97777 I don't think the extra validation from calling To be clear. I think we should merge this and then evaluate the other improvements in follow up PRs (including potentially just replacing the current implementation with what we currently have in StringBuffer) |
Thanks! |
Simple, unopinionated optimization of
StringBuilder
that's invisible to its callers.This PR optimizes:
builder.append("test")
, which no longer callstrlen()
because the size is known statically.as_string
is now better optimized, regarding the iteration.LocalVector
instead ofVector
foregoes COW overhead (lifted from OptimizeSceneTreeEditor::_update_node_tooltip()
#97777).This should slightly improve performance already, and should be easier to merge than #77158 because it does not modify any other parts of the code, and #97777 because it does not change assumptions about the strings.
I especially opted not to remove the explicit call to
String(buffer, string_length)
in the end because it does some error checking that should arguably stay - or at least, it should be the task of another PR to remove it.I do think we should try optimizing strings further - especially with regards to large strings being checked for
strlen
quite repeatedly because of NULL termination - but this will be done in follow-up PRs. Especially, follow-up PRs will be able to callappend(ptr, len)
due to this PR if they already know the size.