You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
@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.
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.
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.
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.
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.
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.
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.
Activity
tarcieri commentedon Jul 17, 2026
I'm guessing this is related to #1252, which it seems might incur a cost that didn't previously exist.
Notably the
Zeroizeimpl for[T; N]callsiter_mutwhich 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.newpavlov commentedon Jul 17, 2026
No, it's one of issues what #1252 attempted to resolve, but based on your comments I kept
volatile_writeinstead of replacing it with simple writes which would've generated optimized code as I initially intended.tarcieri commentedon Jul 17, 2026
@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.
newpavlov commentedon Jul 17, 2026
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_writewith a simple write would allow the compiler to use more efficient write instructions (likely SIMD-based ones) whileoptimization_barrierwould prevent the compiler from removing them.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.tarcieri commentedon Jul 17, 2026
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.
newpavlov commentedon Jul 17, 2026
You can see the difference between old, current, and my suggested version here: https://rust.godbolt.org/z/4q1dfYT43
It seems that
optimization_barrierhas prevented the compiler from unrolling the loop, which has caused the performance drop, but significantly reduced size of the zeroization code.tarcieri commentedon Jul 17, 2026
I think we should drop the internal use of
optimization_barrierthen. I was fine with it as a belt-and-suspenders defense provided it’s zero-cost, but it seems that’s not the case.newpavlov commentedon Jul 17, 2026
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.
tarcieri commentedon Jul 17, 2026
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 foru8.Furthermore, as we’ve discussed in the past, this approach lacks guarantees which zeroize has advertised since its inception.
newpavlov commentedon Jul 17, 2026
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 liketrait Zeroize { const DEFAULT_VALUE: Self; }instead ofDefaultIsZeros), but I think we can write hacky specializations usingTypeIdfor 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.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.
zeroize: remove internal use of `optimization_barrier`
zeroize: remove internal use of `optimization_barrier`
zeroize: remove internal use of `optimization_barrier`
zeroize: remove internal use of `optimization_barrier`
zeroize: remove internal use of `optimization_barrier` (RustCrypto#1535)