-
Notifications
You must be signed in to change notification settings - Fork 128
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
Question: is there a ways to determine byte size of the packed message without actually decoding it? #40
Comments
The byte length of a msgpack item is a variable. Detecting number of items or byte lengths would take almost same seconds to decode the msgpack stream. What is an use case which needs to determine boudaries of the msgpack or count sequenced msgpacked items?
I could guess an use case for above. It may allow less memory copy. var msgpack = require("msgpack-lite");
var e = new msgpack.Encoder();
e.on("data", console.log); // debug only
var buf = e.buffer = new Buffer(100);
var start = e.offset = 10;
e.encode("some data");
var length = e.offset - start;
msgpack.decode(buf.slice(10, e.offset)); // debug only It requests enough length of buffer. |
Yes I know that detecting the size would require to recursively iterate over maps and arrays but decoding data means also allocating Objects and later gc'ing them. I need to scan over large stream of msgpacked objects only cherrypicking n'th of them. For now I can just throw away whatever items I don't need but it's an obvious waste. Thanks for the hint regarding 2nd question. This is a little bit verbose, but that will do :). |
Right.
It would need a kind of function getNth(source, n, callback) {
var dry = msgpack.createCodec({dryrun: true}); // proposal
var wet = msgpack.createCodec({dryrun: false}); // normal behavior codec
var codec = n ? dry : wet; // first codec
var decoder = msgpack.createDecodeStream({codec: codec});
var cnt = 0;
source.pipe(decoder).on("data", function (data) {
if (cnt === n) callback(null, data);
decoder.codec = (cnt === n-1) ? wet : dry; // next codec
cnt++;
}).on("error", callback);
} |
Yes exactly. The |
The use case might be to only determine boudaries of the msgpack or count sequenced msgpacked items.
e.g.
Perhaps an interface similar to Decode().
Another question: is there a way to encode message to provided buffer with offset and boundary length?
The text was updated successfully, but these errors were encountered: