diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2022-01-04 00:50:32 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2022-01-04 00:51:04 +0100 |
commit | 1fc520ed286f815cae1da1e9f8014cb18a256744 (patch) | |
tree | 285e2474f85c03b7d93a4e905276569589cee156 | |
parent | 7abb2bb96e0cd584f44dd8b219ad16d0232a6485 (diff) | |
download | busybox-w32-1fc520ed286f815cae1da1e9f8014cb18a256744.tar.gz busybox-w32-1fc520ed286f815cae1da1e9f8014cb18a256744.tar.bz2 busybox-w32-1fc520ed286f815cae1da1e9f8014cb18a256744.zip |
md5/shaXsum: use FEATURE_COPYBUF_KB to size the buffer instead of fixed 4k
function old new delta
md5_sha1_sum_main 536 565 +29
hash_file 419 401 -18
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 29/-18) Total: 11 bytes
In my test, for unrolled sha1, COPYBUF_KB=64 increases throughput
from 367 MB/s to 457 MB/s.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/md5_sha1_sum.c | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c index 4efa23061..3b389cb6b 100644 --- a/coreutils/md5_sha1_sum.c +++ b/coreutils/md5_sha1_sum.c | |||
@@ -151,10 +151,12 @@ static unsigned char *hash_bin_to_hex(unsigned char *hash_value, | |||
151 | return (unsigned char *)hex_value; | 151 | return (unsigned char *)hex_value; |
152 | } | 152 | } |
153 | 153 | ||
154 | #define BUFSZ (CONFIG_FEATURE_COPYBUF_KB < 4 ? 4096 : CONFIG_FEATURE_COPYBUF_KB * 1024) | ||
155 | |||
154 | #if !ENABLE_SHA3SUM | 156 | #if !ENABLE_SHA3SUM |
155 | # define hash_file(f,w) hash_file(f) | 157 | # define hash_file(b,f,w) hash_file(b,f) |
156 | #endif | 158 | #endif |
157 | static uint8_t *hash_file(const char *filename, unsigned sha3_width) | 159 | static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned sha3_width) |
158 | { | 160 | { |
159 | int src_fd, hash_len, count; | 161 | int src_fd, hash_len, count; |
160 | union _ctx_ { | 162 | union _ctx_ { |
@@ -227,8 +229,7 @@ static uint8_t *hash_file(const char *filename, unsigned sha3_width) | |||
227 | } | 229 | } |
228 | 230 | ||
229 | { | 231 | { |
230 | RESERVE_CONFIG_UBUFFER(in_buf, 4096); | 232 | while ((count = safe_read(src_fd, in_buf, BUFSZ)) > 0) { |
231 | while ((count = safe_read(src_fd, in_buf, 4096)) > 0) { | ||
232 | update(&context, in_buf, count); | 233 | update(&context, in_buf, count); |
233 | } | 234 | } |
234 | hash_value = NULL; | 235 | hash_value = NULL; |
@@ -238,7 +239,6 @@ static uint8_t *hash_file(const char *filename, unsigned sha3_width) | |||
238 | final(&context, in_buf); | 239 | final(&context, in_buf); |
239 | hash_value = hash_bin_to_hex(in_buf, hash_len); | 240 | hash_value = hash_bin_to_hex(in_buf, hash_len); |
240 | } | 241 | } |
241 | RELEASE_CONFIG_BUFFER(in_buf); | ||
242 | } | 242 | } |
243 | 243 | ||
244 | if (src_fd != STDIN_FILENO) { | 244 | if (src_fd != STDIN_FILENO) { |
@@ -251,6 +251,7 @@ static uint8_t *hash_file(const char *filename, unsigned sha3_width) | |||
251 | int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 251 | int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
252 | int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) | 252 | int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) |
253 | { | 253 | { |
254 | unsigned char *in_buf; | ||
254 | int return_value = EXIT_SUCCESS; | 255 | int return_value = EXIT_SUCCESS; |
255 | unsigned flags; | 256 | unsigned flags; |
256 | #if ENABLE_SHA3SUM | 257 | #if ENABLE_SHA3SUM |
@@ -279,6 +280,12 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) | |||
279 | if (!*argv) | 280 | if (!*argv) |
280 | *--argv = (char*)"-"; | 281 | *--argv = (char*)"-"; |
281 | 282 | ||
283 | /* The buffer is not alloc/freed for each input file: | ||
284 | * for big values of COPYBUF_KB, this helps to keep its pages | ||
285 | * pre-faulted and possibly even fully cached on local CPU. | ||
286 | */ | ||
287 | in_buf = xmalloc(BUFSZ); | ||
288 | |||
282 | do { | 289 | do { |
283 | if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) { | 290 | if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) { |
284 | FILE *pre_computed_stream; | 291 | FILE *pre_computed_stream; |
@@ -310,7 +317,7 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) | |||
310 | *filename_ptr = '\0'; | 317 | *filename_ptr = '\0'; |
311 | filename_ptr += 2; | 318 | filename_ptr += 2; |
312 | 319 | ||
313 | hash_value = hash_file(filename_ptr, sha3_width); | 320 | hash_value = hash_file(in_buf, filename_ptr, sha3_width); |
314 | 321 | ||
315 | if (hash_value && (strcmp((char*)hash_value, line) == 0)) { | 322 | if (hash_value && (strcmp((char*)hash_value, line) == 0)) { |
316 | if (!(flags & FLAG_SILENT)) | 323 | if (!(flags & FLAG_SILENT)) |
@@ -339,7 +346,7 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) | |||
339 | } | 346 | } |
340 | fclose_if_not_stdin(pre_computed_stream); | 347 | fclose_if_not_stdin(pre_computed_stream); |
341 | } else { | 348 | } else { |
342 | uint8_t *hash_value = hash_file(*argv, sha3_width); | 349 | uint8_t *hash_value = hash_file(in_buf, *argv, sha3_width); |
343 | if (hash_value == NULL) { | 350 | if (hash_value == NULL) { |
344 | return_value = EXIT_FAILURE; | 351 | return_value = EXIT_FAILURE; |
345 | } else { | 352 | } else { |