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

faster conversion to slice #39

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 36 additions & 6 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include <map>
#include <vector>
#include <array>
#include <memory>

/**
* Forward declarations.
Expand Down Expand Up @@ -249,6 +251,37 @@ static size_t StringOrBufferLength (napi_env env, napi_value value) {
return size;
}

struct NapiSlice {
NapiSlice(napi_env env, napi_value from) {
if (IsString(env, from)) {
NAPI_STATUS_THROWS_VOID(napi_get_value_string_utf8(env, from, nullptr, 0, &size_));
char* data;
if (size_ + 1 < stack_.size()) {
data = stack_.data();
} else {
heap_.reset(new char[size_ + 1]);
data = heap_.get();
}
data[size_] = 0;
NAPI_STATUS_THROWS_VOID(napi_get_value_string_utf8(env, from, data, size_ + 1, &size_));
data_ = data;
} else if (IsBuffer(env, from)) {
void* data;
NAPI_STATUS_THROWS_VOID(napi_get_buffer_info(env, from, &data, &size_));
data_ = static_cast<char*>(data);
}
}

operator leveldb::Slice() const {
return leveldb::Slice(data_, size_);
}

std::unique_ptr<char[]> heap_;
std::array<char, 1024> stack_;
Copy link
Member

Choose a reason for hiding this comment

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

This looks like an optimization for an overly-specific use case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Most keys are < 1024 in length...

Copy link
Member

Choose a reason for hiding this comment

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

That's probably a fair assumption, but doesn't it then over-allocate memory?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's stack allocated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe 1024 is a bit high considering cache locality...

char* data_;
size_t size_;
};

/**
* Takes a Buffer or string property 'name' from 'opts'.
* Returns null if the property does not exist or is zero-length.
Expand Down Expand Up @@ -1977,11 +2010,9 @@ NAPI_METHOD(batch_put) {
NAPI_ARGV(3);
NAPI_BATCH_CONTEXT();

leveldb::Slice key = ToSlice(env, argv[1]);
leveldb::Slice value = ToSlice(env, argv[2]);
const auto key = NapiSlice(env, argv[1]);
const auto value = NapiSlice(env, argv[2]);
batch->Put(key, value);
DisposeSliceBuffer(key);
DisposeSliceBuffer(value);

NAPI_RETURN_UNDEFINED();
}
Expand All @@ -1993,9 +2024,8 @@ NAPI_METHOD(batch_del) {
NAPI_ARGV(2);
NAPI_BATCH_CONTEXT();

leveldb::Slice key = ToSlice(env, argv[1]);
const auto key = NapiSlice(env, argv[1]);
batch->Del(key);
DisposeSliceBuffer(key);

NAPI_RETURN_UNDEFINED();
}
Expand Down