[php-src] PR #15152: hash: Add SHA-NI implementation of SHA-256
| From: | TimWolla | Date: | Mon, 29 Jul 2024 11:12:06 +0000 |
| Subject: | [php-src] PR #15152: hash: Add SHA-NI implementation of SHA-256 | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-27557@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/15152
Author: TimWolla
This PR adds a SHA-NI implementation for SHA-256, greatly improving its performance. It also adds a
SSE2 implementation for CPUs that do not support SHA-NI, which improves the performance, but in a
much less competitive fashion.
Both implementations are taken from Tarsnap/libcperciva. Before creating the implementation, I have
reached out to the author Dr. Colin Percival (@cperciva) regarding the inclusion in PHP and how to
give proper attribution. Before merging this PR, I'll reach out once more to get an official
approval that the license comments look correct.
# Open Tasks
- [ ] I'm not at all sure if the way I've integrated this with the build process is any
good. If someone more experienced with that could give me some helpful pointers, I'd appreciate
that. @petk perhaps?
# Benchmarks
Test script:
```
<?php
$algorithm = $_SERVER['argv'][1];
$data = str_repeat('x', $_SERVER['argv'][2]);
for ($i = 0; $i < $_SERVER['argv'][3]; $i++) {
hash($algorithm, $data);
}
```
Running on a Intel(R) Core(TM) i7-1365U PHP configured as
./configure --enable-zend-test
--enable-option-checking=fatal --enable-phpdbg --enable-fpm, using the SHA-NI implementation
with the CPU support check.
### Before
```
$ hyperfine -L algorithm md5,sha1,sha256,sha512 '/tmp/master test.php {algorithm} 16
999999'
hyperfine -L algorithm md5,sha1,sha256,sha512 '/tmp/master test.php {algorithm} 100
499999'
hyperfine -L algorithm md5,sha1,sha256,sha512 '/tmp/master test.php {algorithm} 1024
99999'
hyperfine -L algorithm md5,sha1,sha256,sha512 '/tmp/master test.php {algorithm} 10240
9999'
Benchmark 1: /tmp/master test.php md5 16 999999
Time (mean ± σ): 162.2 ms ± 2.9 ms [User: 159.2 ms, System: 2.9 ms]
Range (min … max): 160.6 ms … 172.8 ms 18 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Benchmark 2: /tmp/master test.php sha1 16 999999
Time (mean ± σ): 187.3 ms ± 5.9 ms [User: 185.4 ms, System: 1.8 ms]
Range (min … max): 183.3 ms … 206.9 ms 16 runs
Benchmark 3: /tmp/master test.php sha256 16 999999
Time (mean ± σ): 338.7 ms ± 2.5 ms [User: 336.3 ms, System: 2.0 ms]
Range (min … max): 335.7 ms … 343.1 ms 10 runs
Benchmark 4: /tmp/master test.php sha512 16 999999
Time (mean ± σ): 434.1 ms ± 4.8 ms [User: 430.9 ms, System: 2.9 ms]
Range (min … max): 429.5 ms … 444.3 ms 10 runs
Summary
/tmp/master test.php md5 16 999999 ran
1.15 ± 0.04 times faster than /tmp/master test.php sha1 16 999999
2.09 ± 0.04 times faster than /tmp/master test.php sha256 16 999999
2.68 ± 0.06 times faster than /tmp/master test.php sha512 16 999999
Benchmark 1: /tmp/master test.php md5 100 499999
Time (mean ± σ): 128.5 ms ± 3.0 ms [User: 126.1 ms, System: 2.1 ms]
Range (min … max): 126.6 ms … 137.9 ms 21 runs
Warning: The first benchmarking run for this command was significantly slower than the rest (137.2
ms). This could be caused by (filesystem) caches that were not filled until after the first run. You
should consider using the '--warmup' option to fill those caches before the actual
benchmark. Alternatively, use the '--prepare' option to clear the caches before each
timing run.
Benchmark 2: /tmp/master test.php sha1 100 499999
Time (mean ± σ): 141.1 ms ± 4.8 ms [User: 138.9 ms, System: 1.9 ms]
Range (min … max): 138.8 ms … 161.0 ms 21 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Benchmark 3: /tmp/master test.php sha256 100 499999
Time (mean ± σ): 284.5 ms ± 5.0 ms [User: 282.1 ms, System: 2.1 ms]
Range (min … max): 282.0 ms … 298.5 ms 10 runs
Warning: The first benchmarking run for this command was significantly slower than the rest (298.5
ms). This could be caused by (filesystem) caches that were not filled until after the first run. You
should consider using the '--warmup' option to fill those caches before the actual
benchmark. Alternatively, use the '--prepare' option to clear the caches before each
timing run.
Benchmark 4: /tmp/master test.php sha512 100 499999
Time (mean ± σ): 220.4 ms ± 4.5 ms [User: 217.6 ms, System: 2.5 ms]
Range (min … max): 217.2 ms … 232.7 ms 13 runs
Summary
/tmp/master test.php md5 100 499999 ran
1.10 ± 0.05 times faster than /tmp/master test.php sha1 100 499999
1.71 ± 0.05 times faster than /tmp/master test.php sha512 100 499999
2.21 ± 0.07 times faster than /tmp/master test.php sha256 100 499999
Benchmark 1: /tmp/master test.php md5 1024 99999
Time (mean ± σ): 159.0 ms ± 3.0 ms [User: 156.6 ms, System: 2.1 ms]
Range (min … max): 157.1 ms … 167.2 ms 18 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Benchmark 2: /tmp/master test.php sha1 1024 99999
Time (mean ± σ): 152.1 ms ± 4.8 ms [User: 149.7 ms, System: 2.2 ms]
Range (min … max): 148.6 ms … 170.2 ms 19 runs
Benchmark 3: /tmp/master test.php sha256 1024 99999
Time (mean ± σ): 388.1 ms ± 5.6 ms [User: 384.7 ms, System: 3.2 ms]
Range (min … max): 384.4 ms … 402.8 ms 10 runs
Benchmark 4: /tmp/master test.php sha512 1024 99999
Time (mean ± σ): 281.2 ms ± 5.3 ms [User: 278.1 ms, System: 2.8 ms]
Range (min … max): 277.7 ms … 294.2 ms 10 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Summary
/tmp/master test.php sha1 1024 99999 ran
1.04 ± 0.04 times faster than /tmp/master test.php md5 1024 99999
1.85 ± 0.07 times faster than /tmp/master test.php sha512 1024 99999
2.55 ± 0.09 times faster than /tmp/master test.php sha256 1024 99999
Benchmark 1: /tmp/master test.php md5 10240 9999
Time (mean ± σ): 142.6 ms ± 1.3 ms [User: 140.0 ms, System: 2.4 ms]
Range (min … max): 141.9 ms … 148.2 ms 21 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Benchmark 2: /tmp/master test.php sha1 10240 9999
Time (mean ± σ): 133.9 ms ± 2.6 ms [User: 131.4 ms, System: 2.2 ms]
Range (min … max): 131.4 ms … 144.4 ms 22 runs
Benchmark 3: /tmp/master test.php sha256 10240 9999
Time (mean ± σ): 354.3 ms ± 1.3 ms [User: 352.5 ms, System: 1.7 ms]
Range (min … max): 352.8 ms … 356.5 ms 10 runs
Benchmark 4: /tmp/master test.php sha512 10240 9999
Time (mean ± σ): 238.0 ms ± 1.2 ms [User: 235.1 ms, System: 2.5 ms]
Range (min … max): 237.3 ms … 241.5 ms 12 runs
Summary
/tmp/master test.php sha1 10240 9999 ran
1.07 ± 0.02 times faster than /tmp/master test.php md5 10240 9999
1.78 ± 0.04 times faster than /tmp/master test.php sha512 10240 9999
2.65 ± 0.05 times faster than /tmp/master test.php sha256 10240 9999
```
### After
```
$ hyperfine -L algorithm md5,sha1,sha256,sha512 'sapi/cli/php test.php {algorithm} 16
999999'
hyperfine -L algorithm md5,sha1,sha256,sha512 'sapi/cli/php test.php {algorithm} 100
499999'
hyperfine -L algorithm md5,sha1,sha256,sha512 'sapi/cli/php test.php {algorithm} 1024
99999'
hyperfine -L algorithm md5,sha1,sha256,sha512 'sapi/cli/php test.php {algorithm} 10240
9999'
Benchmark 1: sapi/cli/php test.php md5 16 999999
Time (mean ± σ): 169.0 ms ± 4.9 ms [User: 167.3 ms, System: 1.5 ms]
Range (min … max): 165.3 ms … 181.8 ms 17 runs
Benchmark 2: sapi/cli/php test.php sha1 16 999999
Time (mean ± σ): 191.1 ms ± 10.4 ms [User: 188.8 ms, System: 1.9 ms]
Range (min … max): 184.0 ms … 221.3 ms 15 runs
Benchmark 3: sapi/cli/php test.php sha256 16 999999
Time (mean ± σ): 146.6 ms ± 2.6 ms [User: 144.4 ms, System: 2.0 ms]
Range (min … max): 143.7 ms … 156.6 ms 20 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Benchmark 4: sapi/cli/php test.php sha512 16 999999
Time (mean ± σ): 434.0 ms ± 2.0 ms [User: 432.6 ms, System: 1.3 ms]
Range (min … max): 431.7 ms … 437.9 ms 10 runs
Summary
sapi/cli/php test.php sha256 16 999999 ran
1.15 ± 0.04 times faster than sapi/cli/php test.php md5 16 999999
1.30 ± 0.07 times faster than sapi/cli/php test.php sha1 16 999999
2.96 ± 0.05 times faster than sapi/cli/php test.php sha512 16 999999
Benchmark 1: sapi/cli/php test.php md5 100 499999
Time (mean ± σ): 130.0 ms ± 3.1 ms [User: 127.7 ms, System: 2.2 ms]
Range (min … max): 127.6 ms … 138.3 ms 23 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Benchmark 2: sapi/cli/php test.php sha1 100 499999
Time (mean ± σ): 142.9 ms ± 11.9 ms [User: 140.8 ms, System: 2.0 ms]
Range (min … max): 138.8 ms … 191.9 ms 21 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Benchmark 3: sapi/cli/php test.php sha256 100 499999
Time (mean ± σ): 98.8 ms ± 1.1 ms [User: 96.3 ms, System: 2.1 ms]
Range (min … max): 97.7 ms … 101.8 ms 30 runs
Benchmark 4: sapi/cli/php test.php sha512 100 499999
Time (mean ± σ): 221.7 ms ± 5.5 ms [User: 219.2 ms, System: 2.2 ms]
Range (min … max): 218.9 ms … 239.6 ms 13 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Summary
sapi/cli/php test.php sha256 100 499999 ran
1.31 ± 0.03 times faster than sapi/cli/php test.php md5 100 499999
1.45 ± 0.12 times faster than sapi/cli/php test.php sha1 100 499999
2.24 ± 0.06 times faster than sapi/cli/php test.php sha512 100 499999
Benchmark 1: sapi/cli/php test.php md5 1024 99999
Time (mean ± σ): 159.3 ms ± 3.6 ms [User: 156.5 ms, System: 2.6 ms]
Range (min … max): 156.9 ms … 169.6 ms 19 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Benchmark 2: sapi/cli/php test.php sha1 1024 99999
Time (mean ± σ): 152.3 ms ± 3.7 ms [User: 149.8 ms, System: 2.0 ms]
Range (min … max): 148.5 ms … 163.4 ms 19 runs
Benchmark 3: sapi/cli/php test.php sha256 1024 99999
Time (mean ± σ): 80.8 ms ± 1.5 ms [User: 78.5 ms, System: 2.1 ms]
Range (min … max): 79.7 ms … 86.8 ms 37 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Benchmark 4: sapi/cli/php test.php sha512 1024 99999
Time (mean ± σ): 281.8 ms ± 5.8 ms [User: 278.2 ms, System: 3.3 ms]
Range (min … max): 278.3 ms … 296.7 ms 10 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Summary
sapi/cli/php test.php sha256 1024 99999 ran
1.88 ± 0.06 times faster than sapi/cli/php test.php sha1 1024 99999
1.97 ± 0.06 times faster than sapi/cli/php test.php md5 1024 99999
3.49 ± 0.10 times faster than sapi/cli/php test.php sha512 1024 99999
Benchmark 1: sapi/cli/php test.php md5 10240 9999
Time (mean ± σ): 142.9 ms ± 1.7 ms [User: 139.1 ms, System: 3.4 ms]
Range (min … max): 141.9 ms … 147.8 ms 20 runs
Warning: The first benchmarking run for this command was significantly slower than the rest (147.6
ms). This could be caused by (filesystem) caches that were not filled until after the first run. You
should consider using the '--warmup' option to fill those caches before the actual
benchmark. Alternatively, use the '--prepare' option to clear the caches before each
timing run.
Benchmark 2: sapi/cli/php test.php sha1 10240 9999
Time (mean ± σ): 133.8 ms ± 1.9 ms [User: 131.0 ms, System: 2.4 ms]
Range (min … max): 130.5 ms … 140.5 ms 22 runs
Benchmark 3: sapi/cli/php test.php sha256 10240 9999
Time (mean ± σ): 67.6 ms ± 1.5 ms [User: 65.0 ms, System: 2.5 ms]
Range (min … max): 66.5 ms … 74.0 ms 44 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Benchmark 4: sapi/cli/php test.php sha512 10240 9999
Time (mean ± σ): 237.7 ms ± 1.3 ms [User: 233.7 ms, System: 3.8 ms]
Range (min … max): 236.3 ms … 241.1 ms 12 runs
Summary
sapi/cli/php test.php sha256 10240 9999 ran
1.98 ± 0.05 times faster than sapi/cli/php test.php sha1 10240 9999
2.11 ± 0.05 times faster than sapi/cli/php test.php md5 10240 9999
3.52 ± 0.08 times faster than sapi/cli/php test.php sha512 10240 9999
```
## Direct Comparison
```
$ hyperfine 'sapi/cli/php test.php sha256 16 999999' '/tmp/master test.php sha256 16
999999'
hyperfine 'sapi/cli/php test.php sha256 100 499999' '/tmp/master test.php sha256
100 499999'
hyperfine 'sapi/cli/php test.php sha256 1024 99999' '/tmp/master test.php sha256
1024 99999'
hyperfine 'sapi/cli/php test.php sha256 10240 9999' '/tmp/master test.php sha256
10240 9999'
Benchmark 1: sapi/cli/php test.php sha256 16 999999
Time (mean ± σ): 147.8 ms ± 1.7 ms [User: 145.5 ms, System: 2.1 ms]
Range (min … max): 145.8 ms … 151.9 ms 19 runs
Benchmark 2: /tmp/master test.php sha256 16 999999
Time (mean ± σ): 342.7 ms ± 6.6 ms [User: 340.4 ms, System: 2.1 ms]
Range (min … max): 335.7 ms … 354.7 ms 10 runs
Summary
sapi/cli/php test.php sha256 16 999999 ran
2.32 ± 0.05 times faster than /tmp/master test.php sha256 16 999999
Benchmark 1: sapi/cli/php test.php sha256 100 499999
Time (mean ± σ): 99.1 ms ± 1.7 ms [User: 96.6 ms, System: 2.3 ms]
Range (min … max): 97.5 ms … 104.2 ms 30 runs
Benchmark 2: /tmp/master test.php sha256 100 499999
Time (mean ± σ): 283.6 ms ± 2.1 ms [User: 281.5 ms, System: 2.0 ms]
Range (min … max): 281.6 ms … 287.7 ms 10 runs
Summary
sapi/cli/php test.php sha256 100 499999 ran
2.86 ± 0.05 times faster than /tmp/master test.php sha256 100 499999
Benchmark 1: sapi/cli/php test.php sha256 1024 99999
Time (mean ± σ): 81.1 ms ± 2.4 ms [User: 78.3 ms, System: 2.6 ms]
Range (min … max): 79.7 ms … 92.5 ms 37 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Benchmark 2: /tmp/master test.php sha256 1024 99999
Time (mean ± σ): 391.6 ms ± 8.0 ms [User: 388.9 ms, System: 2.5 ms]
Range (min … max): 384.8 ms … 408.8 ms 10 runs
Summary
sapi/cli/php test.php sha256 1024 99999 ran
4.83 ± 0.17 times faster than /tmp/master test.php sha256 1024 99999
Benchmark 1: sapi/cli/php test.php sha256 10240 9999
Time (mean ± σ): 68.4 ms ± 2.1 ms [User: 65.5 ms, System: 2.5 ms]
Range (min … max): 67.0 ms … 76.2 ms 44 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system
without any interferences from other programs. It might help to use the '--warmup' or
'--prepare' options.
Benchmark 2: /tmp/master test.php sha256 10240 9999
Time (mean ± σ): 360.7 ms ± 11.9 ms [User: 357.5 ms, System: 2.9 ms]
Range (min … max): 352.9 ms … 386.8 ms 10 runs
Warning: The first benchmarking run for this command was significantly slower than the rest (386.8
ms). This could be caused by (filesystem) caches that were not filled until after the first run. You
should consider using the '--warmup' option to fill those caches before the actual
benchmark. Alternatively, use the '--prepare' option to clear the caches before each
timing run.
Summary
sapi/cli/php test.php sha256 10240 9999 ran
5.27 ± 0.24 times faster than /tmp/master test.php sha256 10240 9999
```