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

Not fast enough #3

Open
Sequoia opened this issue Jun 13, 2017 · 4 comments
Open

Not fast enough #3

Sequoia opened this issue Jun 13, 2017 · 4 comments

Comments

@Sequoia
Copy link
Owner

Sequoia commented Jun 13, 2017

root@58ad89a57202:/usr/src/app# yes | pv > /dev/null
11.5MiB 0:00:09 [1.02MiB/s] [                             <=>]

root@58ad89a57202:/usr/src/app# /usr/bin/yes | pv > /dev/null
1.07GiB 0:00:09 [ 142MiB/s] [                             <=>]

Expected

Pure JavaScript yes is the finest yes in all the land.

Actual

Gnu coreutils yes is approximately 142 times faster than Pure JavaScript yes 😭

@laduke
Copy link

laduke commented Jun 13, 2017

For some reason, the node version speeds up if you provide a long string

yocontra pushed a commit to yocontra/yes that referenced this issue Jun 13, 2017
adds buffering and removes cruft to increase perf
@laduke
Copy link

laduke commented Jun 13, 2017

#!/usr/bin/env node
var msg = process.argv.length > 2 ? process.argv[2] : 'y';
var Readable = require('stream').Readable

var rs = new Readable;
var c = 0;
var size = Buffer.byteLength(msg, "utf8")

msg = msg + '\r\n'

rs._read = function () {
    c++;
    rs.push(msg);
};

rs.pipe(process.stdout)


process.on('exit', function () {
  console.error('\n_read() called ' + c + ' times');
});
process.stdout.on('error', process.exit);

@akx
Copy link

akx commented Jun 14, 2017

#!/usr/bin/env node
var msg = (process.argv.length > 2 ? process.argv[2] : 'y') + '\r\n';
var repeatedMsg = msg.repeat(Math.max(1, Math.floor(8192 / Buffer.byteLength(msg, 'utf8'))));
var buf = Buffer.from(repeatedMsg, 'utf8');
var pump = function() {
	process.stdout.write(buf, null, pump);
};	
pump();

seems to reach ~1.85 GiB/s with a 9000 byte payload, where master reaches ~900 KiB/s. That's a 2000x speedup.

@laduke
Copy link

laduke commented Jun 14, 2017

process.stdout._handle.setBlocking( true )

const yes = Buffer.alloc( 8192, 'y\n' ) // set to 5000000 below
const write = process.stdout.write.bind( process.stdout )

const f = () => {
    write( yes )
    setImmediate( f )
}




setImmediate( f )

https://www.reddit.com/r/programming/comments/6gxf02/how_is_gnus_yes_so_fast_xpost_runix/diu5fo7/?utm_content=permalink&utm_medium=front&utm_source=reddit&utm_name=programming

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

3 participants