Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Avoid WriteBatch allocation #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 20 additions & 30 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1828,28 +1828,20 @@ struct BatchWorker final : public PriorityWorker {
BatchWorker (napi_env env,
Database* database,
napi_value callback,
leveldb::WriteBatch* batch,
const bool sync,
const bool hasData)
: PriorityWorker(env, database, callback, "classic_level.batch.do"),
batch_(batch), hasData_(hasData) {
const bool sync)
: PriorityWorker(env, database, callback, "classic_level.batch.do") {
options_.sync = sync;
}

~BatchWorker () {
delete batch_;
}

void DoExecute () override {
if (hasData_) {
SetStatus(database_->WriteBatch(options_, batch_));
SetStatus(database_->WriteBatch(options_, &batch_));
}
}

private:
leveldb::WriteOptions options_;
leveldb::WriteBatch* batch_;
const bool hasData_;
leveldb::WriteBatch batch_;
bool hasData_;
};

/**
Expand All @@ -1866,8 +1858,7 @@ NAPI_METHOD(batch_do) {
uint32_t length;
napi_get_array_length(env, array, &length);

leveldb::WriteBatch* batch = new leveldb::WriteBatch();
bool hasData = false;
BatchWorker* worker = new BatchWorker(env, database, callback, sync);

for (uint32_t i = 0; i < length; i++) {
napi_value element;
Expand All @@ -1881,8 +1872,8 @@ NAPI_METHOD(batch_do) {
if (!HasProperty(env, element, "key")) continue;
leveldb::Slice key = ToSlice(env, GetProperty(env, element, "key"));

batch->Delete(key);
if (!hasData) hasData = true;
worker->batch_.Delete(key);
if (!worker->hasData_) worker->hasData_ = true;

DisposeSliceBuffer(key);
} else if (type == "put") {
Expand All @@ -1892,15 +1883,14 @@ NAPI_METHOD(batch_do) {
leveldb::Slice key = ToSlice(env, GetProperty(env, element, "key"));
leveldb::Slice value = ToSlice(env, GetProperty(env, element, "value"));

batch->Put(key, value);
if (!hasData) hasData = true;
worker->batch_.Put(key, value);
if (!worker->hasData_) worker->hasData_ = true;

DisposeSliceBuffer(key);
DisposeSliceBuffer(value);
}
}

BatchWorker* worker = new BatchWorker(env, database, callback, batch, sync, hasData);
worker->Queue(env);

NAPI_RETURN_UNDEFINED();
Expand All @@ -1910,38 +1900,38 @@ NAPI_METHOD(batch_do) {
* Owns a WriteBatch.
*/
struct Batch {
Batch(const Batch&) = delete;
Batch(Batch&&) = delete;
Batch& operator=(const Batch&) = delete;
Batch& operator=(Batch&&) = delete;
Comment on lines +1903 to +1906
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't.


Batch (Database* database)
: database_(database),
batch_(new leveldb::WriteBatch()),
hasData_(false) {}

~Batch () {
delete batch_;
}

void Put (leveldb::Slice key, leveldb::Slice value) {
batch_->Put(key, value);
batch_.Put(key, value);
hasData_ = true;
}

void Del (leveldb::Slice key) {
batch_->Delete(key);
batch_.Delete(key);
hasData_ = true;
}

void Clear () {
batch_->Clear();
batch_.Clear();
hasData_ = false;
}

leveldb::Status Write (bool sync) {
leveldb::WriteOptions options;
options.sync = sync;
return database_->WriteBatch(options, batch_);
return database_->WriteBatch(options, &batch_);
}

Database* database_;
leveldb::WriteBatch* batch_;
leveldb::WriteBatch batch_;
bool hasData_;
};

Expand Down