Skip to content

Commit

Permalink
Simplify providing buffer for better performance and determinism
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Aug 21, 2020
1 parent d0844ad commit 926a086
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ exports.Packr = require('./pack').Packr
let unpackModule = require('./unpack')
let extractor = tryRequire('./build/Release/msgpackr.node')
if (extractor)
unpackModule.setExtractor(extractor)
unpackModule.setExtractor(extractor.extractStrings)
exports.Unpackr = unpackModule.Unpackr
exports.PackrStream = require('./stream').PackrStream
exports.UnpackrStream = require('./stream').UnpackrStream
Expand Down
7 changes: 2 additions & 5 deletions src/extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ NAN_METHOD(extractStrings) {
Local<Context> context = Nan::GetCurrentContext();
position = Local<Number>::Cast(info[0])->IntegerValue(context).ToChecked();
int size = Local<Number>::Cast(info[1])->IntegerValue(context).ToChecked();
if (info[2]->IsArrayBufferView())
source = (uint8_t*) node::Buffer::Data(info[2]);
next_token: while (position < size) {
uint8_t token = source[position++];
if (token < 0xa0) {
Expand Down Expand Up @@ -205,13 +207,8 @@ void setupTokenTable() {
});
}

NAN_METHOD(setSource) {
source = (uint8_t*) node::Buffer::Data(info[0]);
}

void initializeModule(v8::Local<v8::Object> exports) {
setupTokenTable();
Nan::SetMethod(exports, "setSource", setSource);
Nan::SetMethod(exports, "extractStrings", extractStrings);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ msgpack_codec = msgpack_codec && msgpack_codec.msgpack;
what_the_pack = what_the_pack && what_the_pack.initialize(2**20);

var pkg = require("../package.json");
var data = require("./example4.json");
var data = require("./samples/outcomes.json");
var packed = msgpack_lite.encode(data);
var expected = JSON.stringify(data);

Expand Down
14 changes: 6 additions & 8 deletions unpack.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict"
let setSource = () => {} // don't need to do anything without the native support here
let decoder
try {
decoder = new TextDecoder()
Expand Down Expand Up @@ -43,10 +42,10 @@ class Unpackr {
srcStringEnd = 0
srcString = null
strings = EMPTY_ARRAY
if (src !== source) {
src = source
setSource(source)
}
// if (src !== source) {
src = source
/// setSource(source)
// }
let value
if (this) {
currentUnpackr = this
Expand Down Expand Up @@ -78,8 +77,7 @@ exports.getPosition = () => {
return position
}
exports.setExtractor = (extractor) => {
setSource = extractor.setSource
extractStrings = extractor.extractStrings
extractStrings = extractor
}

function read() {
Expand Down Expand Up @@ -297,7 +295,7 @@ function readString(headerLength) {
return function readString(length) {
let string = strings[stringPosition++]
if (string == null) {
strings = extractStrings ? extractStrings(position - headerLength, srcEnd) : [decoder.decode(src.slice(position, position + length))]
strings = extractStrings ? extractStrings(position - headerLength, srcEnd, src) : [decoder.decode(src.slice(position, position + length))]
stringPosition = 0
string = strings[stringPosition++]
}
Expand Down

0 comments on commit 926a086

Please sign in to comment.