From e5d1e37efffd2244b5cbd0bd0c334bdd6fdd3db6 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 26 Sep 2014 15:29:46 +0200 Subject: [PATCH] Overload of getTiger() that accepts a buffer, so there is no need to reallocate a new buffer at every call. --- src/sums.d | 6 +++++- src/tiger.d | 51 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/sums.d b/src/sums.d index 983b566..788c329 100644 --- a/src/sums.d +++ b/src/sums.d @@ -25,6 +25,8 @@ import std.exception; import std.format; import std.array : appender; +mixin(import("tigersumConfig.d")); + private void writeError (string parFile, string parMessage) { stderr.writefln("tigersum: %s: %s", parFile, parMessage); } @@ -34,6 +36,8 @@ private void writeWarning (string parMessage) { } void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, string parFileMode, bool parBsdStyle) { + ulong[] read_buff = new ulong[READ_BUFF_BLOCKS * 64 / ulong.sizeof]; + foreach (file_name; parFiles) { const auto curr_path = parCWD ~ file_name; File curr_file; @@ -46,7 +50,7 @@ void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, stri } if (!isDir(curr_path)) { - const string hash = tigerToString(getTiger(curr_file, parTiger2)); + const string hash = tigerToString(getTiger(curr_file, parTiger2, cast(char[])read_buff)); if (parBsdStyle) { writefln("tiger (%s) = %s", file_name, hash); } diff --git a/src/tiger.d b/src/tiger.d index 18288d5..7825640 100644 --- a/src/tiger.d +++ b/src/tiger.d @@ -49,6 +49,35 @@ public ulong[3] getTiger (in string parData, bool parV2) { return retval; } +public ulong[3] getTiger (ref File parData, bool parV2, char[] parBuffer) { + assert(parBuffer.length == 64 * READ_BUFF_BLOCKS); + assert((cast(size_t)parBuffer.ptr & cast(size_t)(ulong.alignof - 1)) == 0); + + ulong[3] retval; + auto data_left = parData.size; + const auto buff_length = 64 * READ_BUFF_BLOCKS; + + tiger_init(retval); + while (data_left >= buff_length) { + parData.rawRead(parBuffer); + tiger_chunk(parBuffer.ptr, buff_length, retval); + data_left -= buff_length; + } + + const char padding = (parV2 ? 0x80 : 0x01); + if (data_left) { + parData.rawRead(parBuffer[0 .. data_left]); + const ulong proc_length = data_left & ~0x3fUL; + tiger_chunk(parBuffer.ptr, proc_length, retval); + tiger_last_chunk(parBuffer.ptr + proc_length, data_left - proc_length, parData.size, retval, padding); + } + else { + tiger_last_chunk(null, 0, parData.size, retval, padding); + } + + return retval; +} + public ulong[3] getTiger (ref File parData, bool parV2) { const auto buff_length = 64 * READ_BUFF_BLOCKS; char[] unaligned = new char[buff_length + ulong.alignof - 1]; @@ -59,27 +88,7 @@ public ulong[3] getTiger (ref File parData, bool parV2) { char[] aligned = unaligned[offset .. buff_length]; assert(aligned.length == buff_length); - ulong[3] retval; - auto data_left = parData.size; - tiger_init(retval); - while (data_left >= buff_length) { - parData.rawRead(aligned); - tiger_chunk(aligned.ptr, buff_length, retval); - data_left -= buff_length; - } - - const char padding = (parV2 ? 0x80 : 0x01); - if (data_left) { - parData.rawRead(aligned[0 .. data_left]); - const ulong proc_length = data_left & ~0x3fUL; - tiger_chunk(aligned.ptr, proc_length, retval); - tiger_last_chunk(aligned.ptr + proc_length, data_left - proc_length, parData.size, retval, padding); - } - else { - tiger_last_chunk(null, 0, parData.size, retval, padding); - } - - return retval; + return getTiger(parData, parV2, aligned); } public string tigerToString (in ulong[3] parTiger) {