Skip to content

Optimize validate_cast_and_convert_metadata - #3716

Closed
jswrenn wants to merge 1 commit into
mainfrom
G463fe47353c89d061e6458fa9c4bd19328a954a8
Closed

jswrenn wants to merge 1 commit into
mainfrom
G463fe47353c89d061e6458fa9c4bd19328a954a8

Conversation

@jswrenn

@jswrenn jswrenn commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

This routine begins by rounding bytes_len down to the nearest multiple of
align (thus computing the intermediary max_total_bytes), subtracting the
offset of the trailing slice, and calculating how many elements (elems) fit in
that space. Then, in an expensive computation involving elems, the element
size, the trailing slice offset, align, the total self_bytes is computed.

This commit observes that when the element size is no larger than the align,
self_bytes is simply equal to max_total_bytes. For example, if
max_total_bytes is 16 bytes, the header occupies 9 bytes and two 3-byte
elements fit, they would occupy 15 bytes altogether, leaving 1 byte in excess.

This excess must be smaller than one element, otherwise another element would
fit. When elem_size <= align, the gap is smaller than align and trailing
padding fills it exactly.


Latest Update: v4 — Compare vs v3

📚 Full Patch History

Links show the diff between the row version and the column version.

Version v3 v2 v1 Base
v4 vs v3 vs v2 vs v1 vs Base
v3 vs v2 vs v1 vs Base
v2 vs v1 vs Base
v1 vs Base
⬇️ Download this PR

Branch

git fetch origin refs/heads/G463fe47353c89d061e6458fa9c4bd19328a954a8 && git checkout -b pr-G463fe47353c89d061e6458fa9c4bd19328a954a8 FETCH_HEAD

Checkout

git fetch origin refs/heads/G463fe47353c89d061e6458fa9c4bd19328a954a8 && git checkout FETCH_HEAD

Cherry Pick

git fetch origin refs/heads/G463fe47353c89d061e6458fa9c4bd19328a954a8 && git cherry-pick FETCH_HEAD

Pull

git pull origin refs/heads/G463fe47353c89d061e6458fa9c4bd19328a954a8

Stacked PRs enabled by GHerrit.

@codecov-commenter

codecov-commenter commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.94%. Comparing base (3e328bc) to head (6f00605).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3716   +/-   ##
=======================================
  Coverage   92.94%   92.94%           
=======================================
  Files          16       16           
  Lines        2466     2466           
=======================================
  Hits         2292     2292           
  Misses        174      174           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@joshlf joshlf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to use Kani to completely prove the correctness of this fast path for usize? IIRC we were unable to do that for the full version of the algorithm.

Comment thread zerocopy/src/layout.rs Outdated
Base automatically changed from G35c8b8db33f4c984d5716af9a26c0a5ec9bd9c7e to main September 24, 2026 15:06
@jswrenn

jswrenn commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator Author

@joshlf Unfortunately, not really. For this particular case, we can prove panic freedom (i.e., that we can merely call the method) and that split_at <= bytes_len, but seemingly nothing else. Given our hefty test suite for validate_cast_and_convert_metadata and our upcoming changes to this DstLayout and this method, I don't think this very limited proof is worth adding.

@jswrenn
jswrenn requested a review from joshlf September 25, 2026 16:31
Comment thread zerocopy/benches/ref_from_bytes_dynamic_padding.x86-64.mca Outdated
This routine begins by rounding `bytes_len` down to the nearest multiple of
`align` (thus computing the intermediary `max_total_bytes`), subtracting the
offset of the trailing slice, and calculating how many elements (`elems`) fit in
that space. Then, in an expensive computation involving `elems`, the element
size, the trailing slice offset, `align`, the total `self_bytes` is computed.

This commit observes that when the element size is no larger than the `align`,
`self_bytes` is simply equal to `max_total_bytes`. For example, if
`max_total_bytes` is 16 bytes, the header occupies 9 bytes and two 3-byte
elements fit, they would occupy 15 bytes altogether, leaving 1 byte in excess.

This excess must be smaller than one element, otherwise another element would
fit. When `elem_size <= align`, the gap is smaller than `align` and trailing
padding fills it exactly.

gherrit-pr-id: G463fe47353c89d061e6458fa9c4bd19328a954a8
@jswrenn

jswrenn commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

Obsoleted (for now) by #3659.

@jswrenn jswrenn closed this Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants