-
-
Notifications
You must be signed in to change notification settings - Fork 22k
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 String construction from statically known strings by evaluating strlen
at compile-time.
#100132
Optimize String construction from statically known strings by evaluating strlen
at compile-time.
#100132
Conversation
5d48c21
to
a165edf
Compare
a165edf
to
97e8c25
Compare
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
I am deleting the |
97e8c25
to
d3055f8
Compare
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.
Still approved.
d3055f8
to
0d390f0
Compare
template <typename Element> | ||
struct StrRange { | ||
const Element *c_str; | ||
size_t len; |
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.
I would have used a more explicit uint64 type. No need to use uint32 as it will be padded anyway. So it will be clearer that every size is supported.
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.
You mean use uint64
instead of size_t
? That would be fairly unusual when dealing with arrays.
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.
Again, stylistic. But it removes ambiguity.
Unusual where ? CowData use uint64_t for its size, LocalVector uses uint32_t
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.
Interesting. I'm not sure why that is. Possibly because they're designed to be easily exposeable to GDScript?
Still, I think size_t
is cleaner; it's very common to use that for sizes across c++.
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.
I know that it is the default in C++ standard library. It's just, why be ambiguous on the size of a type when you don't have to. Size_t probably always is 64bits nowadays anyway, this is why this is a style choice. Any choice will do, just raising a point :).
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.
I'll keep it as size_t
for now. But let's keep the discussion open for other people to chime in!
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.
Style changes in core are inherently a very low priority, so I'm not too surprised there's mixing and matching of different names/formats. A lot of the discrepancies could come down to how several templates originally used int
/32-bit storage logic instead of 64-bit, but I can't say for certain. In any case, size_t
is perfectly acceptable, and is arguably the best option for logic/metrics having to deal with… Well, size!
176b6d5
to
84c71d0
Compare
Sorry for repeated pushes, I accidentally pushed an old version and had to reset -.- |
…strlen` to be evaluated at compile time, where possible.
84c71d0
to
a3f48f7
Compare
Thanks! |
In this PR,
String
constructors are now available which make use of known string lengths, avoiding astrlen
call. For example, when using string literals likeString s = "Test"
it is knowable at compile-time that"Test"
is a 4-length string. This is used to speed upString
construction.This PR builds on #99816. There, I have tested
String
construction from a long shader string literal, taken from the codebase. I ran the same benchmark to see how much known-length construction speeds up allocation:335us
on master (aa8d9b8)139us
on #99816 (e1c4239)100us
on this branch (10aac990e158592e2ed50b9dbeb513c7505128e2)The speed-up is likely to be slightly better for short strings.