Skip to content

Optimize Entities::entity_does_not_exist_error_details_message, remove UnsafeWorldCell from error#17115

Merged
alice-i-cecile merged 6 commits intobevyengine:mainfrom
JaySpruce:rework_entity_does_not_exist_details
Jan 5, 2025
Merged

Optimize Entities::entity_does_not_exist_error_details_message, remove UnsafeWorldCell from error#17115
alice-i-cecile merged 6 commits intobevyengine:mainfrom
JaySpruce:rework_entity_does_not_exist_details

Conversation

@JaySpruce
Copy link
Member

@JaySpruce JaySpruce commented Jan 3, 2025

Objective

The error EntityFetchError::NoSuchEntity has an UnsafeWorldCell inside it, which it uses to call Entities::entity_does_not_exist_error_details_message when being printed. That method returns a String that, if the track_location feature is enabled, contains the location of whoever despawned the relevant entity.

I initially had to modify this error while working on #17043. The UnsafeWorldCell was causing borrow problems when being returned from a command, so I tried replacing it with the String that the method returns, since that was the world cell's only purpose.

Unfortunately, Strings are slow, and it significantly impacted performance (on top of that PR's performance hit):

17043 benchmarks

With String

error_handling_insert_slow

No String

error_handling_insert_fixed

For that PR, I just removed the error details entirely, but I figured I'd try to find a way to keep them around.

Solution

  • Replace the String with a helper struct that holds the location, and only turn it into a string when someone actually wants to print it.
  • Replace the UnsafeWorldCell with the aforementioned struct.
  • Do the same for QueryEntityError::NoSuchEntity.

Benchmarking

This had some interesting performance impact:

This PR vs main

dne_rework_1
dne_rework_2
dne_rework_3

Other work

QueryEntityError::QueryDoesNotMatch also has an UnsafeWorldCell inside it. This one would be more complicated to rework while keeping the same functionality.

Migration Guide

The errors EntityFetchError::NoSuchEntity and QueryEntityError::NoSuchEntity now contain an EntityDoesNotExistDetails struct instead of an UnsafeWorldCell. If you were just printing these, they should work identically.

@JaySpruce JaySpruce changed the title Optimize "Entity DNE" error details, remove UnsafeWorldCell from error Optimize Entities::entity_does_not_exist_error_details_message, remove UnsafeWorldCell from error Jan 3, 2025
@alice-i-cecile alice-i-cecile requested a review from hymm January 3, 2025 22:24
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Jan 3, 2025
@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Jan 3, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Jan 3, 2025

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Could you add some context on what users should update when this change get released in a new version of Bevy?
It will be used to help writing the migration guide for the version. Putting it after a ## Migration Guide will help it get automatically picked up by our tooling.

#[derive(Clone, Copy)]
pub enum EntityFetchError<'w> {
#[derive(Error, Debug, Clone, Copy)]
pub enum EntityFetchError {
Copy link
Member

Choose a reason for hiding this comment

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

Yay no lifetime!

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.

Much cleaner. I'd like to merge this first.

Copy link
Contributor

@BenjaminBrienen BenjaminBrienen left a comment

Choose a reason for hiding this comment

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

I suggested a small code style improvement. Otherwise, looks good!

JaySpruce and others added 3 commits January 3, 2025 16:45
Co-authored-by: Benjamin Brienen <benjamin.brienen@outlook.com>
JaySpruce added a commit to JaySpruce/bevy that referenced this pull request Jan 4, 2025
also revert `EntityFetchError` changes for real this time (still could cause problems if left as-is, but bevyengine#17115 will fix it, and this will make the merge easier)
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 5, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 5, 2025
Merged via the queue into bevyengine:main with commit 4fde223 Jan 5, 2025
@JaySpruce JaySpruce deleted the rework_entity_does_not_exist_details branch January 5, 2025 03:25
mrchantey pushed a commit to mrchantey/bevy that referenced this pull request Feb 4, 2025
…ove `UnsafeWorldCell` from error (bevyengine#17115)

## Objective

The error `EntityFetchError::NoSuchEntity` has an `UnsafeWorldCell`
inside it, which it uses to call
`Entities::entity_does_not_exist_error_details_message` when being
printed. That method returns a `String` that, if the `track_location`
feature is enabled, contains the location of whoever despawned the
relevant entity.

I initially had to modify this error while working on bevyengine#17043. The
`UnsafeWorldCell` was causing borrow problems when being returned from a
command, so I tried replacing it with the `String` that the method
returns, since that was the world cell's only purpose.

Unfortunately, `String`s are slow, and it significantly impacted
performance (on top of that PR's performance hit):
<details>
<summary>17043 benchmarks</summary>

### With `String`

![error_handling_insert_slow](https://github.com/user-attachments/assets/5629ba6d-69fc-4c16-84c9-8be7e449232d)

### No `String`

![error_handling_insert_fixed](https://github.com/user-attachments/assets/6393e2d6-e61a-4558-8ff1-471ff8356c1c)

</details>

For that PR, I just removed the error details entirely, but I figured
I'd try to find a way to keep them around.

## Solution

- Replace the `String` with a helper struct that holds the location, and
only turn it into a string when someone actually wants to print it.
- Replace the `UnsafeWorldCell` with the aforementioned struct.
- Do the same for `QueryEntityError::NoSuchEntity`.

## Benchmarking

This had some interesting performance impact:

<details>
<summary>This PR vs main</summary>


![dne_rework_1](https://github.com/user-attachments/assets/05bf91b4-dddc-4d76-b2c4-41c9d25c7a57)

![dne_rework_2](https://github.com/user-attachments/assets/34aa76b2-d8a7-41e0-9670-c213207e457d)

![dne_rework_3](https://github.com/user-attachments/assets/8b9bd4e4-77c8-45a7-b058-dc0dfd3dd323)

</details>

## Other work

`QueryEntityError::QueryDoesNotMatch` also has an `UnsafeWorldCell`
inside it. This one would be more complicated to rework while keeping
the same functionality.

## Migration Guide

The errors `EntityFetchError::NoSuchEntity` and
`QueryEntityError::NoSuchEntity` now contain an
`EntityDoesNotExistDetails` struct instead of an `UnsafeWorldCell`. If
you were just printing these, they should work identically.

---------

Co-authored-by: Benjamin Brienen <benjamin.brienen@outlook.com>
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 D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide 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