-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[Merged by Bors] - Use collect to build mesh attributes #5255
Conversation
Takes advantage of TrustedLen
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'd prefer if the positions and normals used a complete variable name. This code is not obvious for beginners and using single variable names is not helping.
Also, I'm not convinced that it actually matters that much since this shouldn't be in a hot path anyway, but less code is always nice so 🤷♂️
@kornelski Note that there is also no reallocation in the I personally do not agree with IceSentry. Using short variable names inside the let positions: Vec<_> = vertices.iter().map(|(p, _, _)| *p).collect();
let normals: Vec<_> = vertices.iter().map(|(_, n, _)| *n).collect();
let uvs: Vec<_> = vertices.iter().map(|(_, _, uv)| *uv).collect();
// vs
let positions: Vec<_> = vertices
.iter()
.map(|(position, _, _)| *position)
.collect();
let normals: Vec<_> = vertices
.iter()
.map(|(_, normal, _)| *normal)
.collect();
let uvs: Vec<_> = vertices.iter().map(|(_, _, uv)| *uv).collect(); An alternative would be let positions: Vec<_> = vertices.iter().map(|v| v.0).collect();
let normals: Vec<_> = vertices.iter().map(|v| v.1).collect();
let uvs: Vec<_> = vertices.iter().map(|v| v.2).collect(); But personal preferences 🤷 |
Sorry if I don't get it, but If the current code also doesn't reallocate, like stated by nicopap, what is the point of the PR? If it is readability, the current one if pretty clear, but if we wanna a short version, I think the alternative suggested also by nicopap is better: let positions: Vec<_> = vertices.iter().map(|v| v.0).collect();
let normals: Vec<_> = vertices.iter().map(|v| v.1).collect();
let uvs: Vec<_> = vertices.iter().map(|v| v.2).collect(); |
@afonsolage two of the three bits of code updated in this PR actually weren't pre-allocating, so this fixes that. On top of that, the snippet linked by kornelski shows a significant drop in machine instructions with the PR change (which means faster code, and probably better inlining and constant propagation). Even then, it's not that big of a deal, we can imagine a situation where the mesh generation code is very hot, but that's not likely in most games. |
Even when the vector has sufficient capacity, every call to OTOH iteration + collect has special-case optimization via private TrustedLen, which allows Yeah, |
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. The linked godbolt snippet demonstrates the compiler optimizes away the for loop with this change(!!!) this is definitively a perf gain, even if it's pretty niche, it's nice to have.
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.
Looks good to me. Thanks for the godbolt analysis!
bors r+ |
Small optimization. `.collect()` from arrays generates very nice code without reallocations: https://rust.godbolt.org/z/6E6c595bq Co-authored-by: Kornel <[email protected]>
Small optimization. `.collect()` from arrays generates very nice code without reallocations: https://rust.godbolt.org/z/6E6c595bq Co-authored-by: Kornel <[email protected]>
Small optimization. `.collect()` from arrays generates very nice code without reallocations: https://rust.godbolt.org/z/6E6c595bq Co-authored-by: Kornel <[email protected]>
Small optimization. `.collect()` from arrays generates very nice code without reallocations: https://rust.godbolt.org/z/6E6c595bq Co-authored-by: Kornel <[email protected]>
Small optimization.
.collect()
from arrays generates very nice code without reallocations: https://rust.godbolt.org/z/6E6c595bq