-
Notifications
You must be signed in to change notification settings - Fork 318
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
Avoid unnecessary Vec resize. #3027
Conversation
I like avoiding the |
that's not entirely true and instead of arguing I'll just drop the benchmark results below:
sorry, I'm not sure I understand. Are you suggesting something like
? |
Hm, interesting. I'm a bit worried that the allocations are optimized away entirely, can you
Yes. |
If `size > 0` current implementation will first create an empty vec and then push an element into it, which will cause a resize that can be easily avoided. It's obviously not a big deal, but this also gets rid of `mut` local variable.
criterion does it automatically but it's even easier to understand the perf impact by looking at assembly for both versions side by side - https://godbolt.org/z/GoT4Kes5b
I don't have a preference, so I've updated this PR with your suggestion :) |
Thanks! I highly doubt this will even be measurable in any Miri benchmark, but I like getting rid of the @bors r+ |
You are very welcome! I'm sure this change won't affect Miri's perf on the global scale, but since most of the code in the world is written by copy-pasting it's best to have more "better" examples :D |
☀️ Test successful - checks-actions |
If
size > 0
current implementation will first create an empty vec and then push an element into it, which will cause a resize that can be easily avoided.It's obviously not a big deal, but this also gets rid of
mut
local variable.