The Rust code is AI translated from original article: https://blog.csdn.net/liulilittle/article/details/148572223 (C++ implements the AES-256-CFB algorithm with advanced hardware acceleration via the AES-NI instruction set)
Your custom Rust SIMD implementation for AES-256-CFB is significantly faster than the OpenSSL implementation across all tested data sizes. The performance difference is most dramatic for smaller data sizes and narrows somewhat for larger data, but your custom code maintains a clear lead.
Detailed Analysis by Data Size:
-
64 Bytes:
- Custom SIMD:
- Time: ~17.03 ns
- Throughput: ~3.50 GiB/s
- OpenSSL:
- Time: ~387.77 ns
- Throughput: ~157.40 MiB/s
- Analysis: This is where the difference is most stark. Your custom SIMD is roughly 22.7 times faster in terms of raw time. The throughput difference is massive (GiB/s vs. MiB/s). This strongly suggests that OpenSSL has a higher fixed overhead for initializing its encryption context, handling API calls, etc., which becomes very prominent when processing tiny amounts of data. Your direct intrinsic-based code has minimal setup per call within the benchmark loop.
- Custom SIMD:
-
256 Bytes:
- Custom SIMD:
- Time: ~132.48 ns
- Throughput: ~1.80 GiB/s
- OpenSSL:
- Time: ~585.57 ns
- Throughput: ~416.93 MiB/s
- Analysis: Your custom SIMD is now about 4.4 times faster. OpenSSL's throughput has increased significantly as the fixed overhead is amortized over more data. Your custom SIMD's throughput decreased from the 64-byte case, which is interesting. This could be due to factors like:
- The 64-byte test might be so small that it benefits exceptionally from CPU caches (L1) and minimal loop overhead.
- As data size increases slightly, the loop structure of CFB mode (processing block by block) starts to have a more noticeable impact relative to the raw AES instruction speed.
- Custom SIMD:
-
1024 Bytes (1 KiB):
- Custom SIMD:
- Time: ~642.69 ns
- Throughput: ~1.48 GiB/s
- OpenSSL:
- Time: ~1.3680 µs (1368 ns)
- Throughput: ~713.88 MiB/s
- Analysis: Your custom SIMD is about 2.13 times faster. The gap continues to narrow, but it's still a substantial advantage. Both implementations show improved throughput, with OpenSSL scaling well.
- Custom SIMD:
-
4096 Bytes (4 KiB):
- Custom SIMD:
- Time: ~2.6824 µs
- Throughput: ~1.42 GiB/s
- OpenSSL:
- Time: ~4.4894 µs
- Throughput: ~870.10 MiB/s
- Analysis: Your custom SIMD is about 1.67 times faster. The throughput of your custom implementation seems to be stabilizing around 1.4 GiB/s.
- Custom SIMD:
-
16384 Bytes (16 KiB):
- Custom SIMD:
- Time: ~10.846 µs
- Throughput: ~1.41 GiB/s
- OpenSSL:
- Time: ~17.081 µs
- Throughput: ~914.74 MiB/s
- Analysis: Your custom SIMD is about 1.58 times faster. OpenSSL's throughput is still climbing and getting closer to 1 GiB/s. Your custom code's throughput is very consistent now.
- Custom SIMD:
-
65536 Bytes (64 KiB):
- Custom SIMD:
- Time: ~43.809 µs
- Throughput: ~1.39 GiB/s
- OpenSSL:
- Time: ~67.076 µs
- Throughput: ~931.78 MiB/s
- Analysis: Your custom SIMD is about 1.53 times faster. Both seem to be approaching their peak throughput for this CFB mode implementation on your hardware. Your custom code maintains a throughput of around 1.4 GiB/s, while OpenSSL is just under 1 GiB/s.
- Custom SIMD:
Key Observations and Interpretations:
- Overhead Dominance at Small Sizes: The massive performance difference at 64 bytes clearly shows the impact of library overhead in OpenSSL versus your direct, lean SIMD implementation. For applications encrypting many small, independent messages, your custom code would offer substantial benefits.
- Amortization of Overhead: As data size increases, OpenSSL's fixed overhead per call becomes less significant relative to the actual cryptographic work, allowing its highly optimized core routines to perform better.
- Custom SIMD Throughput Plateau: Your custom SIMD implementation reaches a throughput plateau around 1.4 GiB/s. This is likely the effective maximum speed for AES-256-CFB using your specific SIMD logic on your CPU, considering memory bandwidth, loop overheads in CFB mode, and the AES-NI instruction latency/throughput.
- OpenSSL's Scalability: OpenSSL scales well with increasing data size, indicating its core cryptographic operations are efficient once the initial overhead is overcome. It's important to remember OpenSSL is a general-purpose library designed for robustness and a wide range of algorithms and modes, which can contribute to some overhead.
- Outliers: The presence of outliers (e.g., "Found 10 outliers among 100 measurements") is normal in benchmarking due to system activity, CPU frequency scaling, cache effects, etc. Criterion attempts to mitigate these, but they can still appear. The key is the overall trend and the median/mean performance. The percentages here are not overly alarming.
target-cpu=nativeEffectiveness: These results suggest thatRUSTFLAGS="-C target-cpu=native"(or specific feature flags) allowed the Rust compiler (LLVM) to generate highly efficient code for the AES-NI intrinsics and the surrounding loop structures.
original benchmark output:
AES-256-CFB Encryption/Custom SIMD/64
time: [17.019 ns 17.029 ns 17.042 ns]
thrpt: [3.4976 GiB/s 3.5001 GiB/s 3.5023 GiB/s]
Found 10 outliers among 100 measurements (10.00%)
1 (1.00%) low severe
4 (4.00%) high mild
5 (5.00%) high severe
AES-256-CFB Encryption/OpenSSL/64
time: [387.13 ns 387.77 ns 388.46 ns]
thrpt: [157.12 MiB/s 157.40 MiB/s 157.66 MiB/s]
Found 3 outliers among 100 measurements (3.00%)
2 (2.00%) high mild
1 (1.00%) high severe
AES-256-CFB Encryption/Custom SIMD/256
time: [132.44 ns 132.48 ns 132.53 ns]
thrpt: [1.7990 GiB/s 1.7997 GiB/s 1.8002 GiB/s]
Found 7 outliers among 100 measurements (7.00%)
3 (3.00%) high mild
4 (4.00%) high severe
AES-256-CFB Encryption/OpenSSL/256
time: [584.95 ns 585.57 ns 586.31 ns]
thrpt: [416.40 MiB/s 416.93 MiB/s 417.37 MiB/s]
Found 3 outliers among 100 measurements (3.00%)
2 (2.00%) high mild
1 (1.00%) high severe
AES-256-CFB Encryption/Custom SIMD/1024
time: [642.36 ns 642.69 ns 643.07 ns]
thrpt: [1.4830 GiB/s 1.4839 GiB/s 1.4846 GiB/s]
Found 8 outliers among 100 measurements (8.00%)
4 (4.00%) high mild
4 (4.00%) high severe
AES-256-CFB Encryption/OpenSSL/1024
time: [1.3673 µs 1.3680 µs 1.3686 µs]
thrpt: [713.54 MiB/s 713.88 MiB/s 714.23 MiB/s]
thrpt: [713.54 MiB/s 713.88 MiB/s 714.23 MiB/s]
Found 9 outliers among 100 measurements (9.00%)
1 (1.00%) low mild
6 (6.00%) high mild
2 (2.00%) high severe
AES-256-CFB Encryption/Custom SIMD/4096
time: [2.6811 µs 2.6824 µs 2.6839 µs]
thrpt: [1.4213 GiB/s 1.4221 GiB/s 1.4228 GiB/s]
AES-256-CFB Encryption/OpenSSL/4096
time: [4.4887 µs 4.4894 µs 4.4901 µs]
thrpt: [869.97 MiB/s 870.10 MiB/s 870.23 MiB/s]
Found 6 outliers among 100 measurements (6.00%)
5 (5.00%) high mild
1 (1.00%) high severe
AES-256-CFB Encryption/Custom SIMD/16384
time: [10.837 µs 10.846 µs 10.857 µs]
thrpt: [1.4054 GiB/s 1.4068 GiB/s 1.4081 GiB/s]
Found 17 outliers among 100 measurements (17.00%)
7 (7.00%) high mild
10 (10.00%) high severe
AES-256-CFB Encryption/OpenSSL/16384
time: [17.065 µs 17.081 µs 17.103 µs]
thrpt: [913.60 MiB/s 914.74 MiB/s 915.64 MiB/s]
Found 16 outliers among 100 measurements (16.00%)
5 (5.00%) high mild
11 (11.00%) high severe
AES-256-CFB Encryption/Custom SIMD/65536
time: [43.725 µs 43.809 µs 43.894 µs]
time: [43.725 µs 43.809 µs 43.894 µs]
thrpt: [1.3905 GiB/s 1.3932 GiB/s 1.3959 GiB/s]
Found 8 outliers among 100 measurements (8.00%)
8 (8.00%) high mild
AES-256-CFB Encryption/OpenSSL/65536
time: [67.064 µs 67.076 µs 67.089 µs]
thrpt: [931.59 MiB/s 931.78 MiB/s 931.94 MiB/s]
Found 17 outliers among 100 measurements (17.00%)
7 (7.00%) low mild
2 (2.00%) high mild
8 (8.00%) high severe