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

Remove a ptr-to-int cast in CommandQueue::apply #10475

Merged
merged 1 commit into from
Nov 9, 2023

Conversation

SkiFire13
Copy link
Contributor

Objective

  • CommandQueue::apply calculates the address of the end of the internal buffer as a usize rather than as a pointer, requiring two casts of cursor to usize. Casting pointers to integers is generally discouraged and may also prevent optimizations. It's also unnecessary here.

Solution

  • Calculate the end address as a pointer rather than a usize.

Small note:

A trivial translation of the old code to use pointers would have computed end_addr as cursor.add(self.bytes.len()), which is not wrong but is an additional unsafe operation that also needs to be properly documented and proven correct. However this operation is already implemented in the form of the safe as_mut_ptr_range, so I just used that.

@alice-i-cecile alice-i-cecile added C-Code-Quality A section of code that is hard to understand or change A-Pointers Relating to Bevy pointer abstractions labels Nov 9, 2023
@alice-i-cecile alice-i-cecile 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 Nov 9, 2023
@james7132 james7132 added this pull request to the merge queue Nov 9, 2023
Merged via the queue into bevyengine:main with commit e75c2f8 Nov 9, 2023
25 checks passed
@SkiFire13 SkiFire13 deleted the remove-ptr-to-int-cast branch November 9, 2023 20:19
@cart cart added this to the 0.12.1 milestone Nov 30, 2023
@cart
Copy link
Member

cart commented Nov 30, 2023

Adding to the milestone because #10746 builds on these changes

cart pushed a commit that referenced this pull request Nov 30, 2023
# Objective

- `CommandQueue::apply` calculates the address of the end of the
internal buffer as a `usize` rather than as a pointer, requiring two
casts of `cursor` to `usize`. Casting pointers to integers is generally
discouraged and may also prevent optimizations. It's also unnecessary
here.

## Solution

- Calculate the end address as a pointer rather than a `usize`.

Small note:

A trivial translation of the old code to use pointers would have
computed `end_addr` as `cursor.add(self.bytes.len())`, which is not
wrong but is an additional `unsafe` operation that also needs to be
properly documented and proven correct. However this operation is
already implemented in the form of the safe `as_mut_ptr_range`, so I
just used that.
rdrpenguin04 pushed a commit to rdrpenguin04/bevy that referenced this pull request Jan 9, 2024
# Objective

- `CommandQueue::apply` calculates the address of the end of the
internal buffer as a `usize` rather than as a pointer, requiring two
casts of `cursor` to `usize`. Casting pointers to integers is generally
discouraged and may also prevent optimizations. It's also unnecessary
here.

## Solution

- Calculate the end address as a pointer rather than a `usize`.

Small note:

A trivial translation of the old code to use pointers would have
computed `end_addr` as `cursor.add(self.bytes.len())`, which is not
wrong but is an additional `unsafe` operation that also needs to be
properly documented and proven correct. However this operation is
already implemented in the form of the safe `as_mut_ptr_range`, so I
just used that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Pointers Relating to Bevy pointer abstractions C-Code-Quality A section of code that is hard to understand or change 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.

None yet

6 participants