-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
base: main
Are you sure you want to change the base?
Conversation
} | ||
|
||
std::unique_ptr<char[]> heap_; | ||
std::array<char, 1024> stack_; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's stack allocated?
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with this in the end:
struct NapiSlice : public rocksdb::Slice {
std::unique_ptr<char[]> heap_;
std::array<char, 128> stack_;
};
napi_status ToNapiSlice(napi_env env, napi_value from, NapiSlice& slice) {
if (IsString(env, from)) {
NAPI_STATUS_RETURN(napi_get_value_string_utf8(env, from, nullptr, 0, &slice.size_));
char* data;
if (slice.size_ + 1 < slice.stack_.size()) {
data = slice.stack_.data();
} else {
slice.heap_.reset(new char[slice.size_ + 1]);
data = slice.heap_.get();
}
data[slice.size_] = 0;
NAPI_STATUS_RETURN(napi_get_value_string_utf8(env, from, data, slice.size_ + 1, &slice.size_));
slice.data_ = data;
} else if (IsBuffer(env, from)) {
void* data;
NAPI_STATUS_RETURN(napi_get_buffer_info(env, from, &data, &slice.size_));
slice.data_ = static_cast<char*>(data);
}
return napi_ok;
}
No description provided.