Skip to content

zeroize: add optimized zeroize_bytes function - #1604

Open
tarcieri wants to merge 1 commit into
masterfrom
zeroize/zeroize_bytes
Open

tarcieri wants to merge 1 commit into
masterfrom
zeroize/zeroize_bytes

Conversation

@tarcieri

Copy link
Copy Markdown
Member

Adds an optimized byte slice zeroing function which uses a regular fill followed by optimization_barrier on platforms with stable asm! and falls back on the bulk volatile write implementation added in #1602 on other platforms.

This approach should maintain the previous guarantees the purely volatile write-based approach provided on all platforms, while also providing the performance of the optimization_barrier-based approach on platforms with asm!.

This function is public but also able to subsume the previous role of the internal volatile_zero function added in #1603.

@tarcieri
tarcieri requested a review from newpavlov November 15, 2026 17:46
@tarcieri

Copy link
Copy Markdown
Member Author

Benchmarks:

slice/u8/15             time:   [4.6863 ns 4.7155 ns 4.7480 ns]
                        thrpt:  [2.9423 GiB/s 2.9625 GiB/s 2.9810 GiB/s]
                 change:
                        time:   [+3.0800% +3.9977% +4.9139%] (p = 0.00 < 0.05)
                        thrpt:  [−4.6838% −3.8440% −2.9880%]
                        Performance has regressed.
Found 38 outliers among 100 measurements (38.00%)
  22 (22.00%) low severe
  1 (1.00%) low mild
  4 (4.00%) high mild
  11 (11.00%) high severe
slice/u8/zeroize_bytes/15
                        time:   [3.9108 ns 3.9138 ns 3.9173 ns]
                        thrpt:  [3.5662 GiB/s 3.5694 GiB/s 3.5721 GiB/s]
Found 8 outliers among 100 measurements (8.00%)
  3 (3.00%) high mild
  5 (5.00%) high severe
slice/u8/16             time:   [537.73 ps 538.76 ps 540.02 ps]
                        thrpt:  [27.594 GiB/s 27.658 GiB/s 27.711 GiB/s]
                 change:
                        time:   [−33.872% −33.387% −33.011%] (p = 0.00 < 0.05)
                        thrpt:  [+49.278% +50.120% +51.223%]
                        Performance has improved.
Found 11 outliers among 100 measurements (11.00%)
  2 (2.00%) low mild
  6 (6.00%) high mild
  3 (3.00%) high severe
slice/u8/zeroize_bytes/16
                        time:   [2.4793 ns 2.4831 ns 2.4876 ns]
                        thrpt:  [5.9902 GiB/s 6.0010 GiB/s 6.0102 GiB/s]
Found 12 outliers among 100 measurements (12.00%)
  5 (5.00%) high mild
  7 (7.00%) high severe
slice/u8/17             time:   [734.99 ps 735.95 ps 736.94 ps]
                        thrpt:  [21.484 GiB/s 21.513 GiB/s 21.541 GiB/s]
                 change:
                        time:   [−22.035% −21.816% −21.617%] (p = 0.00 < 0.05)
                        thrpt:  [+27.579% +27.903% +28.262%]
                        Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
  1 (1.00%) low severe
  1 (1.00%) low mild
  4 (4.00%) high mild
  3 (3.00%) high severe
slice/u8/zeroize_bytes/17
                        time:   [2.6343 ns 2.6374 ns 2.6408 ns]
                        thrpt:  [5.9954 GiB/s 6.0031 GiB/s 6.0101 GiB/s]
Found 5 outliers among 100 measurements (5.00%)
  3 (3.00%) high mild
  2 (2.00%) high severe
slice/u8/32             time:   [689.56 ps 690.21 ps 690.95 ps]
                        thrpt:  [43.133 GiB/s 43.179 GiB/s 43.219 GiB/s]
                 change:
                        time:   [−27.490% −27.329% −27.182%] (p = 0.00 < 0.05)
                        thrpt:  [+37.328% +37.607% +37.912%]
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  3 (3.00%) high mild
  4 (4.00%) high severe
slice/u8/zeroize_bytes/32
                        time:   [2.9741 ns 2.9775 ns 2.9813 ns]
                        thrpt:  [9.9963 GiB/s 10.009 GiB/s 10.021 GiB/s]
Found 11 outliers among 100 measurements (11.00%)
  6 (6.00%) high mild
  5 (5.00%) high severe
slice/u8/63             time:   [5.3139 ns 5.3453 ns 5.3747 ns]
                        thrpt:  [10.917 GiB/s 10.977 GiB/s 11.042 GiB/s]
                 change:
                        time:   [−4.1458% −3.4178% −2.6103%] (p = 0.00 < 0.05)
                        thrpt:  [+2.6803% +3.5388% +4.3251%]
                        Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
  1 (1.00%) high mild
  2 (2.00%) high severe
slice/u8/zeroize_bytes/63
                        time:   [5.8955 ns 5.9021 ns 5.9102 ns]
                        thrpt:  [9.9275 GiB/s 9.9411 GiB/s 9.9523 GiB/s]
Found 10 outliers among 100 measurements (10.00%)
  3 (3.00%) high mild
  7 (7.00%) high severe
slice/u8/64             time:   [1.5049 ns 1.5080 ns 1.5114 ns]
                        thrpt:  [39.437 GiB/s 39.526 GiB/s 39.606 GiB/s]
                 change:
                        time:   [−14.370% −14.135% −13.932%] (p = 0.00 < 0.05)
                        thrpt:  [+16.188% +16.462% +16.781%]
                        Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
  6 (6.00%) low mild
  3 (3.00%) high mild
  1 (1.00%) high severe
slice/u8/zeroize_bytes/64
                        time:   [1.5510 ns 1.5536 ns 1.5564 ns]
                        thrpt:  [38.295 GiB/s 38.366 GiB/s 38.430 GiB/s]
Found 9 outliers among 100 measurements (9.00%)
  3 (3.00%) low mild
  5 (5.00%) high mild
  1 (1.00%) high severe
slice/u8/65             time:   [1.5696 ns 1.5715 ns 1.5732 ns]
                        thrpt:  [38.480 GiB/s 38.521 GiB/s 38.567 GiB/s]
                 change:
                        time:   [−13.816% −13.620% −13.423%] (p = 0.00 < 0.05)
                        thrpt:  [+15.505% +15.768% +16.031%]
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  3 (3.00%) low severe
  1 (1.00%) low mild
  3 (3.00%) high mild
slice/u8/zeroize_bytes/65
                        time:   [1.7672 ns 1.7697 ns 1.7731 ns]
                        thrpt:  [34.142 GiB/s 34.206 GiB/s 34.255 GiB/s]
Found 9 outliers among 100 measurements (9.00%)
  1 (1.00%) low severe
  1 (1.00%) low mild
  2 (2.00%) high mild
  5 (5.00%) high severe
slice/u8/128            time:   [2.4791 ns 2.4813 ns 2.4839 ns]
                        thrpt:  [47.993 GiB/s 48.044 GiB/s 48.086 GiB/s]
                 change:
                        time:   [−10.080% −9.6758% −9.3311%] (p = 0.00 < 0.05)
                        thrpt:  [+10.291% +10.712% +11.210%]
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  2 (2.00%) high mild
  3 (3.00%) high severe
slice/u8/zeroize_bytes/128
                        time:   [1.5584 ns 1.5620 ns 1.5656 ns]
                        thrpt:  [76.141 GiB/s 76.320 GiB/s 76.493 GiB/s]
Found 22 outliers among 100 measurements (22.00%)
  3 (3.00%) low severe
  12 (12.00%) low mild
  2 (2.00%) high mild
  5 (5.00%) high severe
slice/u8/4096           time:   [73.431 ns 73.486 ns 73.550 ns]
                        thrpt:  [51.866 GiB/s 51.910 GiB/s 51.949 GiB/s]
                 change:
                        time:   [−0.3655% −0.1077% +0.1296%] (p = 0.42 > 0.05)
                        thrpt:  [−0.1294% +0.1079% +0.3668%]
                        No change in performance detected.
Found 10 outliers among 100 measurements (10.00%)
  1 (1.00%) low mild
  6 (6.00%) high mild
  3 (3.00%) high severe
slice/u8/zeroize_bytes/4096
                        time:   [34.089 ns 34.242 ns 34.402 ns]
                        thrpt:  [110.89 GiB/s 111.40 GiB/s 111.90 GiB/s]
Found 9 outliers among 100 measurements (9.00%)
  6 (6.00%) high mild
  3 (3.00%) high severe
slice/u8/65536          time:   [1.4060 µs 1.4328 µs 1.4588 µs]
                        thrpt:  [41.840 GiB/s 42.598 GiB/s 43.411 GiB/s]
                 change:
                        time:   [−0.6837% +1.2538% +3.0719%] (p = 0.20 > 0.05)
                        thrpt:  [−2.9804% −1.2382% +0.6884%]
                        No change in performance detected.
Found 9 outliers among 100 measurements (9.00%)
  1 (1.00%) low severe
  7 (7.00%) low mild
  1 (1.00%) high mild
slice/u8/zeroize_bytes/65536
                        time:   [298.43 ns 305.66 ns 312.44 ns]
                        thrpt:  [195.35 GiB/s 199.68 GiB/s 204.52 GiB/s]
slice/u8/fixed16        time:   [475.85 ps 478.08 ps 480.52 ps]
                        thrpt:  [31.010 GiB/s 31.169 GiB/s 31.315 GiB/s]
                 change:
                        time:   [−0.0354% +0.3553% +0.7621%] (p = 0.08 > 0.05)
                        thrpt:  [−0.7564% −0.3540% +0.0354%]
                        No change in performance detected.
Found 11 outliers among 100 measurements (11.00%)
  2 (2.00%) low mild
  3 (3.00%) high mild
  6 (6.00%) high severe
slice/u8/zeroize_bytes/fixed16
                        time:   [266.87 ps 269.01 ps 271.33 ps]
                        thrpt:  [54.918 GiB/s 55.392 GiB/s 55.838 GiB/s]
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

@tarcieri
tarcieri force-pushed the zeroize/zeroize_bytes branch from 7b16c95 to aa132b1 Compare November 15, 2026 17:49
Adds an optimized byte slice zeroing function which uses a regular
`fill` followed by `optimization_barrier` on platforms with stable
`asm!` and falls back on the bulk volatile write implementation added
in #1602 on other platforms.

This approach should maintain the previous guarantees the purely
volatile write-based approach provided on all platforms, while also
providing the performance of the `optimization_barrier`-based approach
on platforms with `asm!`.

This function is public but also able to subsume the previous role of
the internal `volatile_zero` function added in #1603.
@tarcieri
tarcieri force-pushed the zeroize/zeroize_bytes branch from aa132b1 to d180fd9 Compare November 15, 2026 17:53

This branch has not been deployed

No deployments
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.

1 participant