From b94b8269e16514ffd738fac653c4d6e46a03bdd4 Mon Sep 17 00:00:00 2001 From: Nick Downing Date: Fri, 7 Jan 2022 16:10:43 +1100 Subject: [PATCH] Make writing use blocks >= 16 kbytes where possible --- logjson.mjs | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/logjson.mjs b/logjson.mjs index 70cb975..ccbe174 100644 --- a/logjson.mjs +++ b/logjson.mjs @@ -203,28 +203,45 @@ class Database { async flush() { // hold mutex whilst calling this function - let ptr = this.eof - for (let i = 0; i < this.write_list.length; ++i) { - let buffer = this.write_list[i] - let len = buffer.length - assert( - (await this.log.write(buffer, 0, len, ptr)).bytesWritten === len - ) - - // each block in write list is also pinned in read cache, unpin - // (if not in read cache then it's special root entry with < >) + let ptr = this.eof, len = 0, i = 0, j = 0 + for (; j < this.write_list.length; ++j) { + if (len >= 0x1000) { + let buffer = Buffer.concat(this.write_list.slice(i, j)) + assert(buffer.length === len) + assert( + (await this.log.write(buffer, 0, len, ptr)).bytesWritten === len + ) + + ptr += len + len = 0 + i = j + } + len += this.write_list[j].length + } + let buffer = Buffer.concat(this.write_list.slice(i, j)) + assert(buffer.length === len) + assert( + (await this.log.write(buffer, 0, len, ptr)).bytesWritten === len + ) + ptr += len + assert(ptr === this.eof1) + + // each block in write list is also pinned in read cache, unpin + // note: do this afterwards as needs to be on disk when unpinned + // note: special root entry is not in read cache and is ignored + /*let*/ ptr = this.eof + for (/*let*/ i = 0; i < this.write_list.length; ++i) { if (Object.prototype.hasOwnProperty.call(this.read_cache, ptr)) { let cached_value = this.read_cache[ptr] --cached_value.refs if (cached_value.stale_count === 0) delete this.read_cache[ptr] } - - ptr += len + ptr += this.write_list[i].length } assert(ptr === this.eof1) - this.eof = ptr + this.eof = ptr this.write_list = [] } -- 2.34.1