Skip to content
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

Add CubicCurve::segment_count + iter_samples adjustment #8711

Merged
merged 3 commits into from
May 31, 2023

Conversation

nicopap
Copy link
Contributor

@nicopap nicopap commented May 29, 2023

Objective

  • Provide a way to use CubicCurve non-iter methods
  • Accept a FnMut over a fn pointer on iter_samples
  • Improve build_*_cubic_100_points benchmark by -45% (this means they are twice as fast)

Solution

Previously, the only way to iterate over an evenly spaced set of points on a CubicCurve was to use one of the iter_* methods.

The return value of those methods were bound by &self lifetime, making them unusable in certain contexts.

Furthermore, other CubicCurve methods (position, velocity, acceleration) required normalizing t over the CubicCurve's internal segment count.

There were no way to access this segment count, making those methods pretty much unusable.

The newly added segment_count allows accessing the segment count.

iter_samples used to accept a fn, a function pointer. This is surprising and contrary to the rust stdlib APIs, which accept Fn traits for Iterator combinators.

iter_samples now accepts a FnMut.

I don't trust a bit the bevy benchmark suit, but according to it, this doubles (-45%) the performance on the build_pos_cubic_100_points and build_accel_cubic_100_points benchmarks.


Changelog

  • Added the CubicCurve::segments method to access the underlying segments of a cubic curve
  • Allow closures as CubicCurve::iter_samples sample_function argument.

Previously, the only way to iterate over an evenly spaced set of points
on a CubicCurve was to use one of the iter_* methods.

The return value of those methods were bound by &self lifetime, making
them unusable in certain contexts.

Furthermore, other CubicCurve methods (position, velocity, acceleration)
required normalizing `t` over the CubicCurve's internal segment count.

There were no way to access this segment count, making those methods
pretty much unusable.

The newly added `segment_count` allows accessing the segment count.

`iter_samples` used to accept a `fn`, a function pointer. This is
surprising and contrary to the rust stdlib APIs, which accept `Fn` traits
for `Iterator` combinators.

`iter_samples` now accepts a `FnMut`.
@nicopap nicopap added C-Performance A change motivated by improving speed, memory usage or compile times C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Animation Make things move and change over time A-Math Fundamental domain-agnostic mathematical operations labels May 29, 2023
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Docs are great, API seems useful and much more idiomatic, lifetimes make sense to me.

Copy link
Member

@aevyrie aevyrie left a comment

Choose a reason for hiding this comment

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

Looks good. Some minor bikeshed/nit feedback. Nice speedup. 🙂

crates/bevy_math/src/cubic_splines.rs Outdated Show resolved Hide resolved
crates/bevy_math/src/cubic_splines.rs Outdated Show resolved Hide resolved
crates/bevy_math/src/cubic_splines.rs Outdated Show resolved Hide resolved
crates/bevy_math/src/cubic_splines.rs Outdated Show resolved Hide resolved
@nicopap nicopap added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label May 31, 2023
@alice-i-cecile alice-i-cecile added this pull request to the merge queue May 31, 2023
Merged via the queue into bevyengine:main with commit c8167c1 May 31, 2023
@nicopap nicopap deleted the cubic-owned branch August 30, 2023 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Animation Make things move and change over time A-Math Fundamental domain-agnostic mathematical operations C-Performance A change motivated by improving speed, memory usage or compile times C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants