Make writing use blocks >= 16 kbytes where possible
authorNick Downing <nick@ndcode.org>
Fri, 7 Jan 2022 05:10:43 +0000 (16:10 +1100)
committerNick Downing <nick@ndcode.org>
Fri, 7 Jan 2022 05:10:43 +0000 (16:10 +1100)
logjson.mjs

index 70cb975..ccbe174 100644 (file)
@@ -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 = []
   }