Eliminate redundant overflow checks and duplicate size calculations in `TrailingSliceLayout::size_for_elems` and `_with_elems` casts
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 55/100
- Issue type
- Refactor
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- rust
- Domain
- performance
Research direction
Start with zerocopy/src/layout.rs, especially TrailingSliceLayout::size_for_elems and DstLayout::validate_cast_and_convert_metadata, then trace the cast path in zerocopy/src/pointer/inner.rs and the reference constructors in zerocopy/src/ref.rs. Use the existing layout and reference tests plus the named benchmarks in zerocopy/benches/ to compare behavior and generated assembly. Done means the overflow, padding, and duplicate metadata checks are removed without changing validity or size results.
Written by the indexing model from the issue text.
Description
Summary
While #3659 optimized TrailingSliceLayout::padding_for_elems (by masking self.elem_size & size_mask before multiplying by elems) and eliminated the elems * elem_size round-trip in DstLayout::validate_cast_and_convert_metadata when elem_size <= size_align, several closely related layout arithmetic and overflow-checking redundancies remain on main (70fa3872c8):
TrailingSliceLayout::size_for_elemsperforms redundant overflow checks and usesusize::MAXinstead of sharingmax_slice_len's bound, andRef::from_*_with_elemscallsT::size_for_metadata(count)twice.DstLayout::validate_cast_and_convert_metadataemits a redundantround_down(unused_bytes, size_align)mask whenelem_size > size_alignandelem_size % size_align == 0(and see also closed PR#3716regardingelem_size > size_align).
1. Redundant overflow checks in TrailingSliceLayout::size_for_elems, PtrInner::try_cast_into, and Ref::from_*_with_elems
What happens on main
In zerocopy/src/layout.rs:427-491, TrailingSliceLayout::size_for_elems computes:
pub(crate) const fn size_for_elems(&self, elems: usize) -> Option<usize> {
let max_trailing_bytes = match self.max_trailing_bytes(usize::MAX) {
Some(max) => max,
None => return None,
};
let trailing_size = match self.elem_size.checked_mul(elems) {
Some(size) => size,
None => return None,
};
if trailing_size > max_trailing_bytes {
return None;
}
#[allow(clippy::arithmetic_side_effects)]
let size = (self.offset + trailing_size) + self.padding_for_elems(elems);
Some(size)
}
There are three compounding redundancies here:
- Single pre-multiplication bound suffices: When
elem_size > 0,elems * elem_size <= max_trailing_bytesholds if and only ifelems <= max_trailing_bytes / elem_size(which is a compile-time constant for a given typeT!). Checkingelems > max_trailing_bytes / elem_sizebefore multiplying byelem_sizemakes bothchecked_mulandtrailing_size > max_trailing_bytesredundant—replacing a runtime overflowing multiplication + two branches with a single comparison against a compile-time constant. - Mismatch between
isize::MAX(max_slice_len) andusize::MAX(size_for_elems):
InPtrInner::try_cast_into(zerocopy/src/pointer/inner.rs:584-592), when explicit metadataSome(elems)is passed:- First,
U::is_valid_metadata(elems)checkselems <= trailing.max_slice_len::<{ DstLayout::MAX_SIZE }>()(bounded byisize::MAX as usize). - Immediately afterward,
U::size_for_metadata(elems)callstrailing.size_for_elems(elems), which checksself.elem_size.checked_mul(elems)andtrailing_size <= self.max_trailing_bytes(usize::MAX)(bounded byusize::MAX).
- First,
- Duplicate
T::size_for_metadata(count)call inRef::from_*_with_elems:
Inzerocopy/src/ref.rs:738-749:let expected_len = match T::size_for_metadata(count) { Some(len) => len, None => return Err(SizeError::new(bytes).into()), }; if bytes.len() < expected_len { return Err(SizeError::new(bytes).into()); } let (bytes, remainder) = bytes.split_at(expected_len); Ptr::from_ref(bytes) .try_cast_into_no_leftover::<T, BecauseImmutable>(Some(count))Ref::from_bytes_with_elems,Ref::from_prefix_with_elems, andRef::from_suffix_with_elemscallT::size_for_metadata(count)up front, and then delegate totry_cast_into_no_leftover(Some(count)), which callsT::is_valid_metadata(count)andT::size_for_metadata(count)a second time.
Observed assembly in zerocopy/benches/
In zerocopy/benches/ref_type_from_bytes_with_elems_dynamic_size.x86-64 (offset = 4, elem_size = 2, align = 2), LLVM emits three separate bounds/overflow checks on count (rcx):
add rsi, -4
lea rdi, [rcx + rcx]
test rcx, rcx
setns r8b # Check 1: checked_mul(count, 2) overflow
cmp rdi, -5
setb dil # Check 2: trailing_size <= max_trailing_bytes(usize::MAX)
and dil, r8b
movabs r9, 4611686018427387902
cmp rcx, r9
setb r9b # Check 3: count <= max_slice_len(isize::MAX)
And in zerocopy/benches/ref_type_from_bytes_with_elems_dynamic_padding.x86-64 (offset = 9, elem_size = 3, align = 4), LLVM loads two different 64-bit magic constants (6148914691236517201 $= \lfloor(\text{usize::MAX} - 9)/3\rfloor$ and 3074457345618258599 $= \lfloor(\text{isize::MAX} - 9)/3\rfloor$) and checks cmp r8, -12 twice:
movabs r8, 6148914691236517201
cmp rcx, r8
ja .LBB0_4
lea r8, [rcx + 2*rcx]
cmp r8, -12
ja .LBB0_4
...
movabs r10, 3074457345618258599
cmp rcx, r10
setbe cl
cmp r8, -12
setb r8b
Similarly, in ref_from_bytes_with_elems_dynamic_padding.x86-64, ref_from_prefix_with_elems_dynamic_padding.x86-64, and ref_from_suffix_with_elems_dynamic_padding.x86-64, LLVM checks both count <= 3074457345618258599 (isize::MAX bound) and count <= 6148914691236517201 (usize::MAX bound) + cmp rcx, -12.
Suggested fix
- In
TrailingSliceLayout::size_for_elems(elems)(or asize_for_elems_bounded<const MAX_SIZE: usize>), checkif elems > self.max_slice_len::<MAX_SIZE>() { return None; }first (wheremax_slice_lenis a compile-time constant for a givenTrailingSliceLayout), after whichlet trailing_size = self.elem_size * elems;and(self.offset + trailing_size) + self.padding_for_elems(elems)are guaranteed not to overflow without any furtherchecked_mulortrailing_size > max_trailing_byteschecks. - In
PtrInner::try_cast_into, avoid checkingU::is_valid_metadata(elems)separately fromU::size_for_metadata(elems)when both are bounded byDstLayout::MAX_SIZE(or havesize_for_metadataenforceDstLayout::MAX_SIZE). - In
Ref::from_bytes_with_elems,Ref::from_prefix_with_elems, andRef::from_suffix_with_elems, delegate directly toPtr::from_ref(bytes).try_cast_into(...)/try_cast_into_no_leftover(Some(count))instead of pre-computingT::size_for_metadata(count)and splitting the slice beforehand.
2. DstLayout::validate_cast_and_convert_metadata when elem_size > size_align
In zerocopy/src/layout.rs:1766-1779:
let elems = max_slice_bytes / trailing.elem_size.get();
let unused_bytes = max_slice_bytes % trailing.elem_size.get();
let unused_aligned_bytes = round_down(unused_bytes, size_align);
let self_bytes = max_total_bytes - unused_aligned_bytes;
- When
elem_size % size_align == 0(even ifelem_size > size_align, e.g.#[repr(C)] struct S([u8; 4], [[u8; 6]])withsize_align = 2, elem_size = 6), bothmax_slice_bytes(= round_down(bytes_len - size_base, size_align)) andelem_sizeare multiples ofsize_align. Thusunused_bytes = max_slice_bytes % elem_sizeis already a multiple ofsize_align, soround_down(unused_bytes, size_align)emits an unnecessary& !(size_align - 1)mask whenelem_size > size_align. - Additionally, as explored in closed PR
#3716("Optimizevalidate_cast_and_convert_metadata"), forelem_size > size_alignwhereelem_sizeis not a power of two, computingself_bytesfromunused_bytescan cause LLVM to duplicate division/remainder work compared to reconstructingself_bytesfromelems * elem_size.
Authored by an AI agent acting on Josh Liebow-Feeser's behalf.
- Dominant language
- Rust
- Stars
- 2.6k
- Forks
- 194
- Avg merge
- 18h 15m
- Merged PRs (30d)
- 14
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from google/zerocopy
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
Features to support optional defmt for the byteorder typesPossibly taken A pull request linked to this issue is open or already merged. Opencustomer-request
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
google/zerocopy#3914 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
google/zerocopy#3766 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
swc-project/swc-next-issues#533 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 5 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
eandualem/agent-interface#290 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day