Skip to content

zeroize 1.8.2 -> 1.9.0 performance regression #1504

Closed

Description

@ctz

1.8.2:

105,300 ( 4.85%)  ???:<rustls::crypto::hmac::Tag as core::ops::drop::Drop>::drop [/tmp/.tmpcy4z8i/target/release/rustls-ci-bench]

1.9.0:

523,260 (19.26%)  ???:<rustls::crypto::hmac::Tag as core::ops::drop::Drop>::drop [/tmp/.tmppiwQse/target/release/rustls-ci-bench]

This function calls [u8; 64].zeroize() in both cases.

Activity

  1. tarcieri commented on Jul 17, 2026

    @tarcieri
    Member

    I'm guessing this is related to #1252, which it seems might incur a cost that didn't previously exist.

    Notably the Zeroize impl for [T; N] calls iter_mut which is zeroizing a byte-at-a-time, and the change from #1252 is likely applying to each byte, and adds an additional instruction.

    We have a "specialized" implementation for slices (the lack of actual specialization here unfortunately means we have tough tradeoffs between generality and performance) which should avoid this pitfall, if you can call as_mut_slice().zeroize() on the array.

  2. newpavlov commented on Jul 17, 2026

    @newpavlov
    Member

    No, it's one of issues what #1252 attempted to resolve, but based on your comments I kept volatile_write instead of replacing it with simple writes which would've generated optimized code as I initially intended.

  3. tarcieri commented on Jul 17, 2026

    @tarcieri
    Member

    @newpavlov I don’t understand what you’re saying. It seems you have a completely different alternative hypothesis, in which case I don’t understand what you’re suggesting it is.

    Both versions use volatile writes. One is slower than the other. The optimization barrier adds a CPU instruction per byte. I don’t see how eliminating the volatile writes would change anything here. In fact if anything the volatile writes alone suffice and the optimization barrier is just adding needless overhead.

  4. newpavlov commented on Jul 17, 2026

    @newpavlov
    Member

    I am not sure what is the difference with the old version, but the problem with v1.9 is what the resulting binary zeroizes data byte-by-byte. Replacing volitile_write with a simple write would allow the compiler to use more efficient write instructions (likely SIMD-based ones) while optimization_barrier would prevent the compiler from removing them.

    The optimization barrier adds a CPU instruction.

    No, it does not (on arches with stable asm!). It's a no-op (i.e. it compiles to 0 instructions) which acts as a black box for the compiler.

  5. tarcieri commented on Jul 17, 2026

    @tarcieri
    Member

    I would suggest we both look at the generated assembly rather than speculating, as we seem to have two completely different hypotheses about what’s happening. Although @newpavlov hasn’t actually suggested where the performance regression is coming from.

    I am not at a computer at the moment but will try to take a look at the generated assembly when I am.

  6. newpavlov commented on Jul 17, 2026

    @newpavlov
    Member

    You can see the difference between old, current, and my suggested version here: https://rust.godbolt.org/z/4q1dfYT43

    It seems that optimization_barrier has prevented the compiler from unrolling the loop, which has caused the performance drop, but significantly reduced size of the zeroization code.

  7. tarcieri commented on Jul 17, 2026

    @tarcieri
    Member

    I think we should drop the internal use of optimization_barrier then. I was fine with it as a belt-and-suspenders defense provided it’s zero-cost, but it seems that’s not the case.

  8. newpavlov commented on Jul 17, 2026

    @newpavlov
    Member

    IMO the unrolled loop is a pretty horrible codegen for this task, so I prefer the suggested version, but I do not care enough to rehash the discussion again, so feel fee to do whatever you want.

  9. tarcieri commented on Jul 17, 2026

    @tarcieri
    Member

    Your “suggested zeroize” can’t actually be implemented given the constraints of problem at hand.

    It needs to work with the generic impl on [T; N]. Your version is specialized for u8.

    Furthermore, as we’ve discussed in the past, this approach lacks guarantees which zeroize has advertised since its inception.

  10. newpavlov commented on Jul 17, 2026

    @newpavlov
    Member

    Yes, unfortunately without proper specialization it would not work for the generic T: Zeroize (it would've been a different story if we had something like trait Zeroize { const DEFAULT_VALUE: Self; } instead of DefaultIsZeros), but I think we can write hacky specializations using TypeId for the most important types like [u8; N]. Or we could wait for specialization to land on stable and do it then.

    This issue also affects implementation of zeroize_flat_type, which currently zeroizes data byte-by-byte. Considering that it can be used on somewhat large types, this is far from ideal, to put it mildly.

    this approach lacks guarantees which zeroize has advertised since its inception.

    And as I argued in the past, in my opinion those guarantees are misguided, most users do not care about them as long as zeroization works properly in practice, and use of volitile ops is an abuse of the operations intended for use with hardware registers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions