Open
Conversation
vweevers
reviewed
Jun 17, 2022
| } | ||
|
|
||
| std::unique_ptr<char[]> heap_; | ||
| std::array<char, 1024> stack_; |
Member
There was a problem hiding this comment.
This looks like an optimization for an overly-specific use case.
Contributor
Author
There was a problem hiding this comment.
Most keys are < 1024 in length...
Member
There was a problem hiding this comment.
That's probably a fair assumption, but doesn't it then over-allocate memory?
Contributor
Author
There was a problem hiding this comment.
it's stack allocated?
Contributor
Author
There was a problem hiding this comment.
Maybe 1024 is a bit high considering cache locality...
ronag
commented
Jun 17, 2022
Contributor
Author
ronag
left a comment
There was a problem hiding this comment.
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;
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.