perf: replace unnecessary usize::try_from with as casts#1473
Open
Nicolas0315 wants to merge 1 commit intomemorysafety:mainfrom
Open
perf: replace unnecessary usize::try_from with as casts#1473Nicolas0315 wants to merge 1 commit intomemorysafety:mainfrom
usize::try_from with as casts#1473Nicolas0315 wants to merge 1 commit intomemorysafety:mainfrom
Conversation
Replace `usize::try_from(n).unwrap()` with `n as usize` in three locations: - `fn read_pal_indices` in decode.rs: block dimensions (w4, h4, bw4, bh4) are always non-negative per the AV1 spec. - Segmentation map update in decode.rs: the comment about needing checked casts for `from_raw_parts_mut` is stale — the code now uses bounds-checked `DisjointMut` indexing, so overflow in the cast would cause a bounds-check panic rather than UB. Remove the outdated comment. - `fn cfl_ac_rust` in ipred.rs: w_pad and h_pad are non-negative and immediately asserted to be less than width/height. This removes the overhead of the checked conversion (comparison + unwrap/panic path) on every call. Addresses memorysafety#1344.
kkysen
requested changes
Mar 2, 2026
Collaborator
kkysen
left a comment
There was a problem hiding this comment.
Could you leave inline comments saying that they're non-negative like you have in the PR description?
Also, we should probably update these variables/arguments/fields to be actually unsigned (e.g., u32), but not sure if we want to do that right here (feel free to do that if you'd like, though).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace
usize::try_from(n).unwrap()withn as usizein three locations where the checked conversion is unnecessary:fn read_pal_indices(decode.rs:643): Block dimensions (w4, h4, bw4, bh4) are always non-negative per the AV1 spec.Segmentation map update (decode.rs:3062-3063): The original comment about needing checked casts for
from_raw_parts_mutis stale — the code now uses bounds-checkedDisjointMutindexing, so overflow in the cast would cause a bounds-check panic rather than UB. The outdated comment is removed.fn cfl_ac_rust(ipred.rs:1310): w_pad and h_pad are non-negative and immediately asserted to be less than width/height.This removes the overhead of the checked conversion (comparison + unwrap/panic path) on every call.
usize::try_froms withascasts #1344.