Eliminate redundant overflow checks and duplicate size calculations in `TrailingSliceLayout::size_for_elems` and `_with_elems` casts
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 55/100
- Tipo di issue
- Refactoring
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- rust
- Ambito
- performance
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- Rust
- Stelle
- 2.6k
- Fork
- 194
- Merge medio
- 18h 15m
- PR unite (30g)
- 14
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di google/zerocopy
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
I maintainer di solito rispondono entro 1 giorno
-
Features to support optional defmt for the byteorder typesForse già presa Una pull request collegata a questa issue è aperta o già unita. Apertacustomer-request
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
google/zerocopy#3914 · 1 commento · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
Misaligned pointer dereference in `Ref` due to incomplete `ByteSlice` move-stability contractAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
google/zerocopy#3766 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di google/zerocopy
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
swc-project/swc-next-issues#533 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 5 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
eandualem/agent-interface#290 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno