Skip to content

Commit

Permalink
if cache pack is too big, we should batch writing
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Feb 11, 2022
1 parent 89c92bf commit e7580d8
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions lib/serialization/FileMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Section -> Buffer

// "wpc" + 1 in little-endian
const VERSION = 0x01637077;
const _1_5_GB = 1.5 * 1000 * 1000 * 1000;
const _1GiB = 1 * 1024 * 1024 * 1024;

/**
* @param {Buffer[]} buffers buffers
Expand Down Expand Up @@ -87,7 +89,7 @@ const readUInt64LE = Buffer.prototype.readBigUInt64LE
* @param {FileMiddleware} middleware this
* @param {BufferSerializableType[] | Promise<BufferSerializableType[]>} data data to be serialized
* @param {string | boolean} name file base name
* @param {function(string | false, Buffer[]): Promise<void>} writeFile writes a file
* @param {function(string | false, Buffer[], number): Promise<void>} writeFile writes a file
* @param {string | Hash} hashFunction hash function to use
* @returns {Promise<SerializeResult>} resulting file pointer and promise
*/
Expand Down Expand Up @@ -212,9 +214,9 @@ const serialize = async (
if (name === true) {
name = hashForName(buf, hashFunction);
}
backgroundJobs.push(writeFile(name, buf));
let size = 0;
for (const b of buf) size += b.length;
backgroundJobs.push(writeFile(name, buf, size));
return {
size,
name,
Expand Down Expand Up @@ -422,7 +424,7 @@ class FileMiddleware extends SerializerMiddleware {
// It's important that we don't touch existing files during serialization
// because serialize may read existing files (when deserializing)
const allWrittenFiles = new Set();
const writeFile = async (name, content) => {
const writeFile = async (name, content, size) => {
const file = name
? join(this.fs, filename, `../${name}${extension}`)
: filename;
Expand All @@ -441,10 +443,7 @@ class FileMiddleware extends SerializerMiddleware {
[zConstants.BROTLI_PARAM_MODE]: zConstants.BROTLI_MODE_TEXT,
[zConstants.BROTLI_PARAM_QUALITY]: 2,
[zConstants.BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING]: true,
[zConstants.BROTLI_PARAM_SIZE_HINT]: content.reduce(
(size, b) => size + b.length,
0
)
[zConstants.BROTLI_PARAM_SIZE_HINT]: size
}
});
}
Expand All @@ -456,8 +455,31 @@ class FileMiddleware extends SerializerMiddleware {
stream.on("error", err => reject(err));
stream.on("finish", () => resolve());
}
for (const b of content) stream.write(b);
stream.end();
// use unsafe
if (size <= _1GiB) {
for (const b of content) stream.write(b);
return stream.end();
}

const len = content.length;
let i = 0;
const batchWrite = err => {
if (i === len) {
stream.end();
return;
}
// will be handle in "on" handler
if (err) return;
const start = i;
let batchSize = 0;
while (i < len && batchSize < _1_5_GB)
batchSize += content[i++].length;
return stream.write(
Buffer.concat(content.slice(start, i), batchSize),
batchWrite
);
};
batchWrite();
});
if (name) allWrittenFiles.add(file);
};
Expand Down

0 comments on commit e7580d8

Please sign in to comment.