Reinstate primitive object optimization
authorNick Downing <nick@ndcode.org>
Wed, 5 Jan 2022 01:43:40 +0000 (12:43 +1100)
committerNick Downing <nick@ndcode.org>
Wed, 5 Jan 2022 01:43:40 +0000 (12:43 +1100)
logjson.mjs

index 460f224..62e7349 100644 (file)
@@ -141,7 +141,7 @@ class File {
     }
     let value = this.value
 
-    //if (value instanceof Array) {
+    if (value instanceof Array) {
       let [ptr, len, new_value] = value
       if (new_value === undefined) {
         let buffer = Buffer.alloc(len)
@@ -157,7 +157,7 @@ class File {
         value[2] = new_value
       }
       value = new_value
-    //}
+    }
     return value
   }
 
@@ -168,7 +168,7 @@ class File {
       value instanceof Lazy
     )
     this.dirty = true
-    //if (typeof value === 'object' && value !== null)
+    if (typeof value === 'object' && value !== null)
       value = [-1, 0, value]
     this.value = value
   }
@@ -181,7 +181,7 @@ class File {
       return
     }
 
-    //if (this.value instanceof Array) {
+    if (this.value instanceof Array) {
       let [ptr, len, value] = this.value
       if (
         (value instanceof Lazy && await value.flush(this)) ||
@@ -205,20 +205,20 @@ class File {
         this.value[1] = len - 3
         //this.dirty = true
       }
-    //}
-    //else if (this.dirty) {
-    //  let buffer = Buffer.from(
-    //    `<${JSON.stringify(this.value, null, 2)}>\n`,
-    //    'utf-8'
-    //  )
-
-    //  ptr = this.eof
-    //  len = buffer.length
-    //  assert(
-    //    (await this.log.write(buffer, 0, len, ptr)).bytesWritten === len
-    //  )
-    //  this.eof += len
-    //}
+    }
+    else if (this.dirty) {
+      let buffer = Buffer.from(
+        `<${JSON.stringify(this.value, null, 2)}>\n`,
+        'utf-8'
+      )
+
+      ptr = this.eof
+      len = buffer.length
+      assert(
+        (await this.log.write(buffer, 0, len, ptr)).bytesWritten === len
+      )
+      this.eof += len
+    }
 
     this.dirty = false
     this.mutex.release()
@@ -278,7 +278,7 @@ class LazyArray extends Lazy {
     }
     let value = this.array[key]
 
-    //if (value instanceof Array) {
+    if (value instanceof Array) {
       let [ptr, len, new_value] = value
       if (new_value === undefined) {
         let buffer = Buffer.alloc(len)
@@ -294,7 +294,7 @@ class LazyArray extends Lazy {
         value[2] = new_value
       }
       value = new_value
-    //}
+    }
     return value
   }
 
@@ -306,7 +306,7 @@ class LazyArray extends Lazy {
       value instanceof Lazy
     )
     this.dirty = true
-    //if (typeof value === 'object' && value !== null)
+    if (typeof value === 'object' && value !== null)
       value = [-1, 0, value]
     this.array[key] = value
     this.length = this.array.length
@@ -338,7 +338,7 @@ class LazyArray extends Lazy {
 
     for (let i = 0; i < this.length; ++i) {
       let item = this.array[i]
-      //if (item instanceof Array) {
+      if (item instanceof Array) {
         let [ptr, len, value] = item
         if (
           (value instanceof Lazy && await value.flush(file)) ||
@@ -363,7 +363,7 @@ class LazyArray extends Lazy {
           item[1] = len - 1
           this.dirty = true
         }
-      //}
+      }
     }
     return this.dirty
   }
@@ -374,7 +374,7 @@ class LazyArray extends Lazy {
     let new_array = []
     for (let i = 0; i < this.length; ++i) {
       let item = this.array[i]
-      //if (item instanceof Array)
+      if (item instanceof Array)
         item = item.slice(0, 2)
       new_array.push(item)
     }
@@ -402,7 +402,7 @@ class LazyObject extends Lazy {
     }
     let value = this.object[key]
 
-    //if (value instanceof Array) {
+    if (value instanceof Array) {
       let [ptr, len, new_value] = value
       if (new_value === undefined) {
         let buffer = Buffer.alloc(len)
@@ -418,7 +418,7 @@ class LazyObject extends Lazy {
         value[2] = new_value
       }
       value = new_value
-    //}
+    }
     return value
   }
 
@@ -430,7 +430,7 @@ class LazyObject extends Lazy {
       value instanceof Lazy
     )
     this.dirty = true
-    //if (typeof value === 'object' && value !== null)
+    if (typeof value === 'object' && value !== null)
       value = [-1, 0, value]
     this.object[key] = value
   }
@@ -455,7 +455,7 @@ class LazyObject extends Lazy {
 
     for (let i in this.object) {
       let item = this.object[i]
-      //if (item instanceof Array) {
+      if (item instanceof Array) {
         let [ptr, len, value] = item
         if (
           (value instanceof Lazy && await value.flush(file)) ||
@@ -480,7 +480,7 @@ class LazyObject extends Lazy {
           item[1] = len
           this.dirty = true
         }
-      //}
+      }
     }
     return this.dirty
   }
@@ -491,7 +491,7 @@ class LazyObject extends Lazy {
     let new_object = {}
     for (let i in this.object) {
       let item = this.object[i]
-      //if (item instanceof Array)
+      if (item instanceof Array)
         item = item.slice(0, 2)
       new_object[i] = item
     }