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 #[track_caller] to Query methods #12984

Merged
merged 1 commit into from
Apr 24, 2024
Merged

Conversation

BD103
Copy link
Member

@BD103 BD103 commented Apr 15, 2024

Objective

Solution

  • Find all methods under Query that mention panicking, and add #[track_caller] to them.

Changelog

  • Added #[track_caller] to Query::many, Query::many_mut, Query::transmute_lens, and Query::transmute_lens_filtered.

For reviewers

I'm unfamiliar with the depths of the Query struct. Please check whether it makes since for the updated methods to have #[track_caller], and if I missed any!

@BD103 BD103 added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Apr 15, 2024
@BD103
Copy link
Member Author

BD103 commented Apr 15, 2024

cc @Themayu

@@ -922,6 +922,7 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> {
///
/// - [`get_many`](Self::get_many) for the non-panicking version.
#[inline]
#[track_caller]
Copy link
Member

Choose a reason for hiding this comment

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

Iirc track_caller does introduce some minor runtime overhead. For high frequency usage this might show up on benchmarks. Can we do a sanity check here with before / after benches.

Copy link
Member Author

Choose a reason for hiding this comment

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

I created a benchmark comparing the overhead of #[track_caller] on my M1 MacBook. (See the gist.) There appears to be little to no overhead whatsoever.

tracked false           time:   [1.5369 ns 1.5376 ns 1.5383 ns]
Found 8 outliers among 100 measurements (8.00%)
  5 (5.00%) high mild
  3 (3.00%) high severe

untracked false         time:   [1.5371 ns 1.5376 ns 1.5382 ns]
Found 5 outliers among 100 measurements (5.00%)
  3 (3.00%) high mild
  2 (2.00%) high severe

If you look at the average time, they both take 1.5376 nanoseconds. The #[track_caller] has a greater deviation, with both a lower minimum and greater maximum, but this is sub-nanosecond behavior.

Furthermore, you can compare the generated assembly here.

untracked:
  test edi, edi
  jne .LBB7_2
  ret
.LBB7_2: ; panicking code, no `#[track_caller]`
  push rax
  lea rdi, [rip + .L__unnamed_3] ; returns panic message
  lea rdx, [rip + .L__unnamed_4] ; different
  mov esi, 9
  call _ZN3std9panicking11begin_panic17he275593115e721e6E

tracked:
  test edi, edi
  jne .LBB8_2
  ret
.LBB8_2: ; panicking code, with `#[track_caller]`
  push rax
  mov rdx, rsi ; different
  lea rdi, [rip + .L__unnamed_5] ; returns panic message
  mov esi, 7
  call _ZN3std9panicking11begin_panic17he275593115e721e6E

The only difference in the generated code is a lea instruction being replaced with a mov instruction. This change is only noticeable when the function panics, where time is not an issue.

@alice-i-cecile alice-i-cecile added this to the 0.14 milestone Apr 18, 2024
@james7132 james7132 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 Apr 24, 2024
@mockersf mockersf added this pull request to the merge queue Apr 24, 2024
Merged via the queue into bevyengine:main with commit c8d214d Apr 24, 2024
31 checks passed
@BD103 BD103 deleted the track-caller branch April 24, 2024 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events 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.

Resolve #[track_caller] inconsistency in bevy's Query types.
5 participants