[Pkg-javascript-commits] [node-leveldown] 08/492: better memory management for batch()

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:38 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.

commit d8ac73993999d6d6374aebdb38a3c294468b48fd
Author: Rod Vagg <rod at vagg.org>
Date:   Sun Jul 15 20:29:40 2012 +1000

    better memory management for batch()
---
 lib/levelup.js  | 24 ++++++++++++------------
 src/async.cc    |  1 -
 src/batch.cc    | 10 +++++-----
 src/batch.h     | 21 +++++++++++++++------
 src/database.cc | 13 ++++++-------
 test/common.js  | 10 +++++++---
 6 files changed, 45 insertions(+), 34 deletions(-)

diff --git a/lib/levelup.js b/lib/levelup.js
index 5cb32e1..bfc5777 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -98,7 +98,6 @@ var bridge              = require('../build/Release/levelup')
                   return callback(err)
                 throw err
               }
-               // reference to key to prevent premature GC
               callback && callback(null, toEncoding(value, options.valueEncoding || options.encoding), key)
             })
           } else
@@ -119,7 +118,7 @@ var bridge              = require('../build/Release/levelup')
                   return callback(err)
                 throw err
               }
-              callback && callback(null, key, value)  // reference to key&value to prevent premature GC
+              callback && callback(null, key, value)
             })
           } else
             callback(new errors.ReadError('Database has not been opened'))
@@ -138,7 +137,7 @@ var bridge              = require('../build/Release/levelup')
                   return callback(err)
                 throw err
               }
-              callback && callback(null, key) // reference to key to prevent premature GC
+              callback && callback(null, key)
             })
           } else
             callback(new errors.ReadError('Database has not been opened'))
@@ -146,6 +145,7 @@ var bridge              = require('../build/Release/levelup')
 
       , batch: function (arr, options_, callback_) {
           var callback = getCallback(options_, callback_)
+            , empty    = {}
             , options, keyEncoding, valueEncoding
 
           if (!this.isOpen())
@@ -155,16 +155,16 @@ var bridge              = require('../build/Release/levelup')
           keyEncoding   = options.keyEncoding   || options.encoding
           valueEncoding = options.valueEncoding || options.encoding
           arr           = arr.map(function (e) {
-            var o = {}
-            if (e.type) {
-              o.type = e.type
-              if (e.key) {
-                o.key = toBuffer(e.key, keyEncoding)
-                if (e.value)
-                  o.value = toBuffer(e.value, valueEncoding)
+            if (e.type && e.key) {
+              var o = {
+                  type  : e.type
+                , key   : toBuffer(e.key, keyEncoding)
               }
+              if (e.value)
+                o.value = toBuffer(e.value, valueEncoding)
+              return o
             }
-            return o
+            return empty
           })
           this.db.batch(arr, options, function (err) {
             if (err) {
@@ -173,7 +173,7 @@ var bridge              = require('../build/Release/levelup')
                 return callback(err)
               throw err
             }
-            callback && callback(null)
+            callback && callback()
           })
         }
     }
diff --git a/src/async.cc b/src/async.cc
index 6f36f2d..d7b678b 100644
--- a/src/async.cc
+++ b/src/async.cc
@@ -115,7 +115,6 @@ BatchWorker::~BatchWorker () {
   operations.clear();
 }
 
-
 void BatchWorker::Execute() {
   WriteBatch batch;
   for (unsigned int i = 0; i < operations.size(); i++)
diff --git a/src/batch.cc b/src/batch.cc
index 9a4ccd2..0025685 100644
--- a/src/batch.cc
+++ b/src/batch.cc
@@ -8,18 +8,18 @@
 using namespace std;
 
 BatchDelete::~BatchDelete () {
-  delete key;
+  keyPtr.Dispose();
 }
 
 void BatchDelete::Execute (WriteBatch* batch) {
-  batch->Delete(Slice(*key));
+  batch->Delete(key);
 }
 
 BatchWrite::~BatchWrite () {
-  delete key;
-  delete value;
+  keyPtr.Dispose();
+  valuePtr.Dispose();
 }
 
 void BatchWrite::Execute (WriteBatch* batch) {
-  batch->Put(Slice(*key), Slice(*value));
+  batch->Put(key, value);
 }
\ No newline at end of file
diff --git a/src/batch.h b/src/batch.h
index 3860670..bcb6a18 100644
--- a/src/batch.h
+++ b/src/batch.h
@@ -15,29 +15,38 @@ public:
 class BatchDelete : public BatchOp {
 public:
   BatchDelete (
-      string* key
+      Slice key
+    , Persistent<Object> keyPtr
   ) : key(key)
+    , keyPtr(keyPtr)
   {};
   ~BatchDelete ();
+
   virtual void Execute (WriteBatch* batch);
 
 protected:
-  string* key;
+  Slice key;
+  Persistent<Object> keyPtr;
 };
 
 class BatchWrite : public BatchDelete {
 public:
   BatchWrite (
-      string* key
-    , string* value
-  ) : BatchDelete(key)
+      Slice key
+    , Slice value
+    , Persistent<Object> keyPtr
+    , Persistent<Object> valuePtr
+  ) : BatchDelete(key, keyPtr)
     , value(value)
+    , valuePtr(valuePtr)
   {};
   ~BatchWrite ();
+
   virtual void Execute (WriteBatch* batch);
 
 private:
-  string* value;
+  Slice value;
+  Persistent<Object> valuePtr;
 };
 
 #endif
\ No newline at end of file
diff --git a/src/database.cc b/src/database.cc
index f9cc964..12b9242 100644
--- a/src/database.cc
+++ b/src/database.cc
@@ -216,26 +216,25 @@ Handle<Value> Database::Batch (const Arguments& args) {
     Local<Object> keyBuffer = obj->Get(str_key)->ToObject();
     if (!keyBuffer->IsObject() || !Buffer::HasInstance(keyBuffer))
       continue;
-    string* key = new string(Buffer::Data(keyBuffer), Buffer::Length(keyBuffer));
+    Slice key(Buffer::Data(keyBuffer), Buffer::Length(keyBuffer));
 
     if (obj->Get(str_type)->StrictEquals(str_del)) {
-      operations.push_back(new BatchDelete(key));
+      operations.push_back(new BatchDelete(key, Persistent<Object>::New(keyBuffer)));
     } else if (obj->Get(str_type)->StrictEquals(str_put) && obj->Has(str_value)) {
       Local<Object> valueBuffer = obj->Get(str_value)->ToObject();
       if (!valueBuffer->IsObject() || !Buffer::HasInstance(valueBuffer))
         continue;
-      string* value = new string(Buffer::Data(valueBuffer), Buffer::Length(valueBuffer));     
-      operations.push_back(new BatchWrite(key, value));
+      Slice value(Buffer::Data(valueBuffer), Buffer::Length(valueBuffer));
+      operations.push_back(new BatchWrite(key, value, Persistent<Object>::New(keyBuffer), Persistent<Object>::New(valueBuffer)));
     }
   }
 
-  BatchWorker* worker = new BatchWorker(
+  AsyncQueueWorker(new BatchWorker(
       database
     , callback
     , operations
     , sync
-  );
-  AsyncQueueWorker(worker);
+  ));
 
   return Undefined();
 }
diff --git a/test/common.js b/test/common.js
index f68ec48..204f4d1 100644
--- a/test/common.js
+++ b/test/common.js
@@ -62,11 +62,15 @@ global.loadBinaryTestData = function (callback) {
 global.binaryTestDataMD5Sum = '920725ef1a3b32af40ccd0b78f4a62fd'
 
 global.checkBinaryTestData = function (testData, callback) {
-  fs.writeFile('__tst.dat', testData, function (err) {
-    child_process.exec('md5sum __tst.dat', function (error, stdout, stderr) {
+  var fname = '__tst.dat.' + Math.random()
+  fs.writeFile(fname, testData, function (err) {
+    refute(err)
+    child_process.exec('md5sum ' + fname, function (err, stdout, stderr) {
+      refute(err)
+      refute(stderr)
       var md5Sum = stdout.split(' ')[0]
       assert.equals(md5Sum, global.binaryTestDataMD5Sum)
-      rimraf('__tst.dat', callback)
+      fs.unlink(fname, callback)
     })
   })
 }
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git



More information about the Pkg-javascript-commits mailing list