Skip to content

Conversation

@goodartistscopy
Copy link
Contributor

@goodartistscopy goodartistscopy commented Dec 30, 2025

Objective

  • Improve code size slightly

Solution

  • The index-less version of ray_mesh_intersection<T>() does not care about the indices type, so any TryInto<usize> will do.
  • By avoiding to monomorphize another version of ray_mesh_intersection<T> (with T=usize) when any of the existing one will do, we get a small improvement in code size (and presumably compilation time as well).

Testing

  • Built the mesh_ray_cast example, which works as expected

The gain is indeed small (0.8kB in dev, 3kB in release and noise (4B) in size-optimized builds)

Showcase

cargo bloat is used to visualize the differences. Profile dev confirms that one less instance of the function is generated:

> # BEFORE (dev)
> cargo bloat --filter "ray_mesh_intersection$" --example mesh_ray_cast
File .text   Size        Crate Name
0.0%  0.0%   856B bevy_picking bevy_picking::mesh_picking::ray_cast::intersections::ray_mesh_intersection
0.0%  0.0%   856B bevy_picking bevy_picking::mesh_picking::ray_cast::intersections::ray_mesh_intersection
0.0%  0.0%   856B bevy_picking bevy_picking::mesh_picking::ray_cast::intersections::ray_mesh_intersection
0.0%  0.0% 2.5KiB              filtered data size, the file size is 263.3MiB

> # AFTER (dev)
> cargo bloat --filter "ray_mesh_intersection$" --example mesh_ray_cast
File .text Size        Crate Name
0.0%  0.0%   856B bevy_picking bevy_picking::mesh_picking::ray_cast::intersections::ray_mesh_intersection
0.0%  0.0%   856B bevy_picking bevy_picking::mesh_picking::ray_cast::intersections::ray_mesh_intersection
0.0%  0.0% 1.7KiB              filtered data size, the file size is 263.3MiB

In release the function is inlined into ray_intersection_over_mesh() which gains 3kB:

> # BEFORE (release)
> cargo bloat --release --filter "ray_intersection_over_mesh$" --example mesh_ray_cast
File .text   Size        Crate Name
0.0%  0.0% 5.3KiB bevy_picking bevy_picking::mesh_picking::ray_cast::intersections::ray_intersection_over_mesh
0.0%  0.0% 5.3KiB              filtered data size, the file size is 100.7MiB

> # AFTER (release)
> cargo bloat --release --filter "ray_intersection_over_mesh$" --example mesh_ray_cast
File .text   Size        Crate Name
0.0%  0.0% 2.3KiB bevy_picking bevy_picking::mesh_picking::ray_cast::intersections::ray_intersection_over_mesh
0.0%  0.0% 2.3KiB              filtered data size, the file size is 100.7MiB

The index-less version of the function does not care about the indices types, so any TryInto<usize> will do. By reusing one of the already monomorphised versions, we get a small improvement in code size and (presumably) compilation time.
@github-actions
Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

Copy link
Member

@mockersf mockersf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, we already have u16 and u32 versions, no need for a u64 version of it

@james7132 james7132 added this pull request to the merge queue Dec 30, 2025
@james7132 james7132 added C-Performance A change motivated by improving speed, memory usage or compile times A-Picking Pointing at and selecting objects of all sorts labels Dec 30, 2025
Merged via the queue into bevyengine:main with commit 0b75766 Dec 30, 2025
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Picking Pointing at and selecting objects of all sorts C-Performance A change motivated by improving speed, memory usage or compile times

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants