Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive trivial is_bit_valid when possible #1303

Merged
merged 1 commit into from
Oct 2, 2024
Merged

Conversation

joshlf
Copy link
Member

@joshlf joshlf commented May 19, 2024

When deriving FromBytes on a type with no generic parameters, the implied TryFromBytes derive's is_bit_valid impl is generated as always returning true. This is faster to codegen, is faster to compile, and is friendlier on the optimizer.

Makes progress on #5

@joshlf joshlf mentioned this pull request May 19, 2024
39 tasks
@joshlf joshlf force-pushed the try-from-bytes-from-bytes branch from c4af213 to 5e1948a Compare May 19, 2024 02:39
jswrenn
jswrenn previously approved these changes May 19, 2024
@joshlf joshlf force-pushed the try-from-bytes-from-bytes branch from 5e1948a to e80cb64 Compare May 19, 2024 03:11
@joshlf joshlf marked this pull request as ready for review May 19, 2024 03:11
@joshlf joshlf enabled auto-merge May 19, 2024 03:11
@joshlf joshlf requested a review from jswrenn May 19, 2024 03:11
@joshlf joshlf disabled auto-merge May 19, 2024 03:19
zerocopy-derive/src/lib.rs Outdated Show resolved Hide resolved
@joshlf joshlf force-pushed the try-from-bytes-from-bytes branch from e80cb64 to c0d052c Compare May 19, 2024 05:05
@joshlf joshlf enabled auto-merge May 19, 2024 05:06
@joshlf joshlf dismissed jswrenn’s stale review May 19, 2024 05:06

Discussed offline: previous version was unsound; requesting a re-review for the fix

@joshlf joshlf force-pushed the try-from-bytes-from-bytes branch 2 times, most recently from d14d132 to b8eeb75 Compare May 19, 2024 05:23
@joshlf joshlf disabled auto-merge May 19, 2024 05:23
@joshlf
Copy link
Member Author

joshlf commented May 19, 2024

@jswrenn Take a look at the .stderr files. The extra Self: FromBytes check added in is_bit_valid results in significantly more verbose error messages. It's still obvious what's happening, so it's not a huge deal. I could also see replacing this with one of the following:

  • Convince ourselves that there's absolutely no way for compilation to succeed if FromBytes isn't satisfied (risky - what about future language changes a la trivial bounds?)
  • In the #[derive(FromBytes)] case, do impl TryFromBytes for Type where Type: FromBytes; maybe this will generate better error messages? Significantly harder to wire through our existing codebase, though
  • In all cases of #[derive(Subtrait)], do impl Trait for Type where Type: Subtrait (so that we don't have to special-case this); now all code can more soundly rely on the top-level trait actually being satisfied. Probably still more verbose errors than currently, but maybe not as bad as with the current solution in this PR.

@codecov-commenter
Copy link

codecov-commenter commented May 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.46%. Comparing base (ea28664) to head (9c82f0c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1303   +/-   ##
=======================================
  Coverage   87.46%   87.46%           
=======================================
  Files          16       16           
  Lines        6021     6021           
=======================================
  Hits         5266     5266           
  Misses        755      755           

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

@joshlf joshlf changed the title Derive trivial is_bit_valid when possible Derive trivial is_bit_valid when possible May 19, 2024
@jswrenn
Copy link
Collaborator

jswrenn commented May 21, 2024

I expected the expansion for this:

#[derive(FromBytes)]
struct Foo {
    a: u8,
    b: u16,
}

...to look like this:

unsafe impl ::zerocopy::TryFromBytes for Foo
where
    u8: ::zerocopy::FromBytes,
    u16: ::zerocopy::FromBytes,
{
    fn only_derive_is_allowed_to_implement_this_trait() {}
    fn is_bit_valid<A>(candidate: ::zerocopy::Maybe<Self, A>) -> bool
    where
        A: ::zerocopy::pointer::invariant::Aliasing
            + ::zerocopy::pointer::invariant::AtLeast<
                ::zerocopy::pointer::invariant::Shared,
            >,
    {
        true
    }
}

Why doesn't it? Am I missing some subtlety here?

@joshlf
Copy link
Member Author

joshlf commented May 21, 2024

It's unnecessary. If the top-level trait is FromBytes, then we already know that a trivial is_bit_valid impl is sound because if it wasn't, the FromBytes derive itself would fail. The bound added inside the body of is_bit_valid is just a hedge in case that reasoning has a hole.

I could also see replacing it with impl TryFromBytes for Foo where Foo: FromBytes. However, that has the downside of being non-local in terms of the control flow inside the derive code. The current approach has the advantage that you can reason about the soundness of try_gen_trivial_is_bit_valid entirely by looking at its body.

@joshlf
Copy link
Member Author

joshlf commented Oct 1, 2024

Okay, rebasing this on our recent work makes the error messages much less verbose. See the .stderr files in b8eeb75 (old) vs 9c82f0c (new) (unfortunately, diffing these directly is not helpful because of how many changes I rebased on). It's still not perfect, but it's a lot better.

In #1752, we bubble errors up as errors rather than as TokenStreams, and so we can bail earlier and not generate multiple errors in a single derive invocation. I suspect that what happened is that that change is responsible for the errors here becoming much less verbose.

@joshlf joshlf added this pull request to the merge queue Oct 2, 2024
github-merge-queue bot pushed a commit that referenced this pull request Oct 2, 2024
When deriving `FromBytes` on a type with no generic parameters, the
implied `TryFromBytes` derive's `is_bit_valid` impl is generated as
always returning `true`. This is faster to codegen, is faster to
compile, and is friendlier on the optimizer.

Makes progress on #5
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Oct 2, 2024
Copy link
Member Author

Choose a reason for hiding this comment

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

TODO: Add more output tests for this optimization.

zerocopy-derive/src/lib.rs Outdated Show resolved Hide resolved
When deriving `FromBytes` on a type with no generic parameters, the
implied `TryFromBytes` derive's `is_bit_valid` impl is generated as
always returning `true`. This is faster to codegen, is faster to
compile, and is friendlier on the optimizer.

Makes progress on #5
@joshlf joshlf enabled auto-merge October 2, 2024 21:18
@joshlf joshlf added this pull request to the merge queue Oct 2, 2024
Merged via the queue into main with commit a9f09d7 Oct 2, 2024
86 checks passed
@joshlf joshlf deleted the try-from-bytes-from-bytes branch October 2, 2024 21:46
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