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

parser: improve overall parser performance #21

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
23 changes: 18 additions & 5 deletions src/node_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
using v8::Uint8Array;

namespace mdsf {

Expand All @@ -30,15 +31,27 @@ void Parse(const FunctionCallbackInfo<Value>& args) {
THROW_EXCEPTION(TypeError, "Wrong number of arguments");
return;
}
if (!args[0]->IsString() && !args[0]->IsUint8Array()) {

HandleScope scope(isolate);

Local<Value> result;
std::size_t length;

if (args[0]->IsString()) {
String::Utf8Value str(args[0]);
length = str.length();
result = mdsf::parser::Parse(isolate, *str, length);
} else if (args[0]->IsUint8Array()) {
Local<Uint8Array> buf = args[0].As<Uint8Array>();
length = buf->ByteLength();
void* data = buf->Buffer()->GetContents().Data();
const char* str = static_cast<const char*>(data) + buf->ByteOffset();
result = mdsf::parser::Parse(isolate, str, length);
} else {
THROW_EXCEPTION(TypeError, "Wrong argument type");
return;
}

HandleScope scope(isolate);

String::Utf8Value str(args[0]->ToString());
auto result = mdsf::parser::Parse(isolate, str);
args.GetReturnValue().Set(result);
}

Expand Down
4 changes: 1 addition & 3 deletions src/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ static constexpr MaybeLocal<Value> (*kParseFunctions[])(Isolate*,
&internal::ParseObject
};

Local<Value> Parse(Isolate* isolate, const String::Utf8Value& in) {
const char* str = *in;
const size_t length = in.length();
Local<Value> Parse(Isolate* isolate, const char* str, size_t length) {
const char* end = str + length;

Type type;
Expand Down
3 changes: 2 additions & 1 deletion src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace parser {
// Deserializes a UTF-8 encoded string into a JavaScript value
// and returns a handle to it.
v8::Local<v8::Value> Parse(v8::Isolate* isolate,
const v8::String::Utf8Value& in);
const char* str,
std::size_t length);

namespace internal {

Expand Down