Coder Social home page Coder Social logo

Comments (4)

royaltm avatar royaltm commented on September 4, 2024 2

Yes exactly. The dryrun would do the trick. Is there any chance you would consider this feature?

from msgpack-lite.

kawanet avatar kawanet commented on September 4, 2024

@royaltm

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?


// hypothetical api: encode(data[, buffer[, offset[, maxLength]]])
encode("some data", new Buffer(100), 10, 50);

I could guess an use case for above. It may allow less memory copy.
Here is msgpack-lite's Endoder object which is not well documented.
This allows a similar thing as below:

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.

from msgpack-lite.

royaltm avatar royaltm commented on September 4, 2024

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 :).

from msgpack-lite.

kawanet avatar kawanet commented on September 4, 2024

decoding data means also allocating Objects and later gc'ing them.

Right.

I need to scan over large stream of msgpacked objects only cherrypicking n'th of them.

It would need a kind of dryrun feature which returns the byte length instead of the object decoded.

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);
}

from msgpack-lite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.