Eliminate redundant overflow checks and duplicate size calculations in `TrailingSliceLayout::size_for_elems` and `_with_elems` casts
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 55/100
- Issue 类型
- 重构
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- rust
- 领域
- performance
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- Rust
- 星标
- 2.6k
- 派生
- 194
- 平均合并
- 18 小时 15 分钟
- 30 天内合并 PR
- 14
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
google/zerocopy 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
Features to support optional defmt for the byteorder types可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭customer-request
难度 2/5 1-3 小时 新手友好度 72/100
google/zerocopy#3914 · 1 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
google/zerocopy#3766 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 64/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
swc-project/swc-next-issues#533 ·
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 5 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
eandualem/agent-interface#290 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复