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

Optimize Box::default and Arc::default to construct more types in place #131460

Merged
merged 3 commits into from
Oct 16, 2024

Commits on Oct 10, 2024

  1. Configuration menu
    Copy the full SHA
    8a14622 View commit details
    Browse the repository at this point in the history
  2. allocate before calling T::default in <Box<T>>::default()

    The `Box<T: Default>` impl currently calls `T::default()` before allocating
    the `Box`.
    
    Most `Default` impls are trivial, which should in theory allow
    LLVM to construct `T: Default` directly in the `Box` allocation when calling
    `<Box<T>>::default()`.
    
    However, the allocation may fail, which necessitates calling `T's` destructor if it has one.
    If the destructor is non-trivial, then LLVM has a hard time proving that it's
    sound to elide, which makes it construct `T` on the stack first, and then copy it into the allocation.
    
    Create an uninit `Box` first, and then write `T::default` into it, so that LLVM now only needs to prove
    that the `T::default` can't panic, which should be trivial for most `Default` impls.
    jwong101 committed Oct 10, 2024
    Configuration menu
    Copy the full SHA
    dd0620b View commit details
    Browse the repository at this point in the history
  3. allocate before calling T::default in <Arc<T>>::default()

    Same rationale as in the previous commit.
    jwong101 committed Oct 10, 2024
    Configuration menu
    Copy the full SHA
    5e474f7 View commit details
    Browse the repository at this point in the history