ssbc / ssb-validate2-rsjs

Cryptographic validation of Scuttlebutt messages in the form of bindings (Rust) for Node.js.
3 stars 0 forks source link

Optimise performance #28

Open mycognosist opened 3 years ago

mycognosist commented 3 years ago

Opening this as a thread to share links to Rust optimisation resources and to document experiments. I'll focus more on this once the API and behaviour are stable. First step will be to setup performance benchmarks so that experiment results can be measured.

Resources

mycognosist commented 3 years ago

Running validateBatch on 7000 message values using karma in ssb-validate2-rsjs-wasm:

[ BATCH VALIDATION ( 7000 ) ]
parsing hmac key:                    0.02685546875 ms
validating hmac key:                 0.01513671875 ms
deserializing messages:             46.158935546875 ms
converting messages to bytes:        0.02197265625 ms
verifying messages (one by one):  1860.22705078125 ms
validating messages (parallel):    413.16796875 ms
hashing messages:                   31.681884765625 ms
serializing message keys:            2.117919921875 ms
TOTAL: validateBatch:             2395.7021484375 ms
mycognosist commented 3 years ago

As can be seen in the benchmark numbers above, message signature verification is the most time-consuming part of validateBatch. This can be dramatically reduced by performing batch verification in parallel (like we do in -node) but we are unable to do so because of a thread_rng runtime error. RNG is required by the verify_batch function in ed25519_dalek.

The solution should be:

Add getrandom = { version = "0.2", features = ["js"] } to Cargo.toml for ssb-validate2-rsjs-wasm.

Unfortunately, this does not work in our case. I have also tried adding rand as a top-level dependency with the getrandom feature enabled (versions 0.7 and 0.8 where tried). I have also tried adding these dependencies at the level of ssb-verify-signatures.