-
Notifications
You must be signed in to change notification settings - Fork 278
perf: better string building #997
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
base: main
Are you sure you want to change the base?
Conversation
52925ec to
267b5d9
Compare
Signed-off-by: Henry Schreiner <[email protected]>
267b5d9 to
e584ec3
Compare
Signed-off-by: Henry Schreiner <[email protected]>
|
Almost all the savings here was from avoiding the NamedTuple indirection. This is now only 1% faster total time, probably 5% or something like that faster for the str operation. Saving the intermediate value doesn't have any measurable effect anymore, so I've remove that. Now it's mostly up to if you think this looks better (and it still is a little faster). |
I'm indifferent. |
|
I was also under the impression, apparently incorrectly, that join would be faster. Because naively concatenating strings can be O(n^2) with regards to memory allocation operations, and I thought the |
|
I'm nearly sure it's the fact the strings are generally small. If they were large I'm almost sure it would be the other way around. I played around with several ways to do this - I thought making all four separately then using an f-string to join them would be fastest, but short circuiting if None was too important. Now that the largest cost (accessing the nested field in the NamedTuple) is gone, it's possible that is faster. |
There's an optimization in CPython specifically for |
This makes the
__str__method faster - about 6-7% less time to dostr(Version(v)); probably about 25% faster for just the str operation. This is used quite a bit in SpecifierSet, it makes a small but measurable improvement there. It's larger than the speedup you get if the generator is replaced bymap#996, though you can get close by using the:=trick as well. This is still a bit faster, and I think it reads better.