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

Prefix Buffers #5

Open
skeggse opened this issue Mar 28, 2014 · 2 comments
Open

Prefix Buffers #5

skeggse opened this issue Mar 28, 2014 · 2 comments

Comments

@skeggse
Copy link
Contributor

skeggse commented Mar 28, 2014

Buffers aren't prefixed, leading to behavior like this:

Which causes problems if you want to support arbitrary buffer data.

var msg = new Message;

msg.push(new Buffer('s:go'));

new Message(msg.toBuffer()).args
// => ['go']

At full speed parsing, this can happen in a matter of milliseconds.

var Message = require('..');

function unbuffer() {

  var start = process.hrtime();

  for (var i = 0;; i++) {
    var msg = new Message;
    var buf = new Buffer(4);

    buf[0] = (Math.random() * 256) | 0;
    buf[1] = (Math.random() * 256) | 0;
    buf[2] = 123;
    buf[3] = 125;

    msg.push(buf);

    var args = new Message(msg.toBuffer()).args;

    if (!Buffer.isBuffer(args[0])) {
      console.log('not a buffer', args[0]);
      return [start, process.hrtime()];
    }
  }

}

function ms(times) {
  return (times[1][0] * 1000 + times[1][1] / 1000000) -
    (times[0][0] * 1000 + times[0][1] / 1000000);
}

console.log(ms(unbuffer()) + ' ms');
not a buffer {}
228.04162900149822 ms
not a buffer {}
62.09908200055361 ms
not a buffer {}
100.19535400345922 ms
not a buffer {}
18.36940200254321 ms
not a buffer {}
68.66924300044775 ms
not a buffer {}
11.645527001470327 ms
not a buffer {}
190.55536199733615 ms
not a buffer {}
125.49566200003028 ms
not a buffer {}
111.19815300032496 ms
not a buffer {}
9.694663997739553 ms
@skeggse
Copy link
Contributor Author

skeggse commented Mar 28, 2014

This would be fixed if I could get my optimized version to actually be faster 😒

@skeggse
Copy link
Contributor Author

skeggse commented Mar 28, 2014

The problem, of course, with adding a prefix is that it's not backwards compatible...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant