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

slow #3

Closed
timoxley opened this issue Nov 27, 2013 · 6 comments
Closed

slow #3

timoxley opened this issue Nov 27, 2013 · 6 comments

Comments

@timoxley
Copy link

not sure if there is anything that can be done about this but generating keys is damn slow:

var keypair = require('keypair');
console.time();
keypair();
console.timeEnd();

Output of multiple runs on Macbook, 3GHz Intel Core i7:

22324ms
6746ms
1522ms
10037ms

It seems to be more slow on the first run, I guess that's v8 getting it's optimisation on?

If this isn't isolated to me, it might be worthwhile mentioning performance in the readme, and recommend this be run in a child process or something to keep the blocking out of the main thread.

@juliangruber
Copy link
Owner

True, just tested with time node example.js and it took between 8 and 30 seconds. I didn't notice this while writing the module because I had { bits: 1024 } as default back then and afterwards switched to 2048 as by some recommendations.

With 1024 bits you get key pairs in 500ms-2s. 20-30 seconds def. sucks :D Maybe this can be rewritten more effectively with a buffer bigint abstraction...

I'll add a note to the readme.

juliangruber added a commit that referenced this issue Nov 27, 2013
@timoxley
Copy link
Author

Thanks @juliangruber. I'm curious how an emscriptened version of whatever ssh-keygen uses would fare out of the box compared to this handcrafted stuff.

@juliangruber
Copy link
Owner

The functions keypair uses are just copied over from node-forge. Unfortunately node-forge doesn't yet use typedarrays/buffers (digitalbazaar/forge#60) so that could make it faster. Would an emscriptened code used typed arrays?

@timoxley
Copy link
Author

@juliangruber yeah afaik emscripten emulates heap allocation/access using typed arrays. This may or may not improve performance though as there may be some inefficiency introduced by compiling to js, but my understanding is that emscripten will use lower level constructs by default, which is how it manages to get great perf out of stuff that wasn't even designed for js. asm.js is supposed to make this even more magic by providing more hints to the js engine about what kinds of optimisations it can safely perform.

@juliangruber
Copy link
Owner

if you manage to do this i'd gladly replace this module's index.js with module.exports = require('timoxleys-ssh-keygen') :)

@dlongley
Copy link

Just a note: using TypedArrays/buffers has no real effect on the speed issue here. Generating RSA keys is slow because it involves looking for large prime numbers (the larger the bit size, the larger the numbers). The process involves incrementing a counter and then checking the result to see if it's a probable prime or not via (very slow) modular exponentiation. That's where the real bottleneck is.

There are some new papers on faster prime generation that have come out recently that I've been looking into in order to speed up the implementation in forge. However, there's no ETA on such an upgrade (or how much it would improve things) yet.

If you're concerned about blocking the main thread, forge has two options for avoiding this; you can use the step-by-step key generation mechanism in a setTimeout/setImmediate loop or you can use the built-in Web Workers approach that also includes an automatic # of cores detection run to try to optimize the # of Web Workers used for key generation.

Anyway -- my main note here is that the use of TypedArrays (or not) is unlikely to have that great of a performance impact on large prime number generation in JavaScript.

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