signed-integrity cli: Improve file reading

This code uses the Deno reader API to collect a list of chunks of the
file, then allocates an array for the entire file at once, and copies
the chunks into the final array, instead of continuously reallocating
new arrays that are larger and larger.

This is significantly faster than the previous code in the case of large
files.
merge-requests/23/merge
Wesley Aptekar-Cassels 2024-01-09 04:33:28 -05:00
rodzic d96022f870
commit 9203834f78
1 zmienionych plików z 21 dodań i 25 usunięć

Wyświetl plik

@ -117,32 +117,28 @@ let getFileIntegrity = async (path, algos) => {
// are we working with a file?
if (fileInfo.isFile) {
// initialize
content = new Uint8Array()
var buf = new Uint8Array(1000);
// read the first batch
var numread = file.readSync(buf);
// read the rest, if there is anything to read
while (numread !== null) {
//console.log(` +-- read: ${numread}`)
//console.log(` +-- length: ${content.length}`)
// there has to be a better way...
var new_content = new Uint8Array(content.length + numread);
//console.log(` +-- new length: ${new_content.length}`)
new_content.set(content)
if (buf.length === numread) {
new_content.set(buf, content.length)
} else {
new_content.set(buf.slice(0, numread), content.length)
const reader = file.readable.getReader();
const chunks = [];
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
content = new_content
// read some more
numread = file.readSync(buf);
chunks.push(value);
}
let length = 0;
for (const chunk of chunks) {
length += chunk.length;
}
content = new Uint8Array(length);
let index = 0;
for (const chunk of chunks) {
content.set(chunk, index);
index += chunk.length;
}
}