-
Notifications
You must be signed in to change notification settings - Fork 53
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
Forcing SmolStr
to allocate on stack when the size_hint
of an iterator is bad
#64
Comments
How much performance does this cost if the size_hint was correct after all? The size hint is supposed to be correct. |
It wouldn't cost anything If the size hint is correct. (tbh, I don't think it would make any significant improvement either.) It only affects when size_hint tells it's more than 24 when it's 24 or less, i.e. SmolStr gets allocated on heap when it doesn't have to. There was a test case for incorrect size hint, so I just assumed that it's an assumption that you have to make. |
I do think we should have the invariant that when |
Implementation
What we should do
SmolStr allocates data on heap when
size_hint
of a iterator is above24
.Even when the size hint is incorrect, it keeps it's data on heap, wrapped in
Arc
.I want
SmolStr
to allocate it's contents on stack when the resulting data is turns out to be smaller.Why do we need this
It allows us to introduce new optimization, reduce interaction with
Arc
... etc.Proof
Here is an analysis of assembly code.
(I'm pretty sure that there is a better way to do this)
You can see that my optimization reduces number of instructions and improves uOps per cycle/IPC.
Current version of the code.
https://rust.godbolt.org/z/Mh9GhYbM9
This one with my optimization.
https://rust.godbolt.org/z/3Gh8jacYK
The text was updated successfully, but these errors were encountered: