Overload of getTiger() that accepts a buffer, so

there is no need to reallocate a new buffer at every call.
King_DuckZ 2014-09-26 15:29:46 +02:00
parent 9b437e52d3
commit e5d1e37eff
2 changed files with 35 additions and 22 deletions

@ -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);
}

@ -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) {