-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Performance issues on Wasm build #892
Comments
Possibly related: https://twitter.com/alexeiZamyatin/status/1355605818218053637 |
Hey, I believe we should investigate this in the rust bindings first. I'm opening an issue there. |
I believe this is the issue: https://github.com/gregdhill/bench-secp256k1-wasm/blob/dce71c9f3a86cd841178b33e24741316fb8e351b/src/lib.rs#L49 The result on my machine, before:
After re-using the context:
Diffdiff --git a/src/lib.rs b/src/lib.rs
index e36aa5e..98fe0de 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -41,12 +41,10 @@ pub mod parity_libsecp256k1 {
}
pub mod bitcoin_core_libsecp256k1 {
- use bitcoin_core_secp256k1::{ffi::types::AlignedType, Error, PublicKey, Secp256k1};
+ use bitcoin_core_secp256k1::{ffi::types::AlignedType, Error, PublicKey, Secp256k1, AllPreallocated};
use test::Bencher;
- fn ecmul() -> Result<(), Error> {
- let mut buf = vec![AlignedType::zeroed(); Secp256k1::preallocate_size()];
- let secp = Secp256k1::preallocated_new(&mut buf)?;
+ fn ecmul(secp: &Secp256k1<AllPreallocated<'_>>) -> Result<(), Error> {
let secret_key = &[
137, 16, 46, 159, 212, 158, 232, 178, 197, 253, 105, 137, 102, 159, 70, 217, 110, 211,
@@ -72,12 +70,16 @@ pub mod bitcoin_core_libsecp256k1 {
#[test]
fn test_libsecp256k1() {
- ecmul().unwrap();
+ let mut buf = vec![AlignedType::zeroed(); Secp256k1::preallocate_size()];
+ let secp = Secp256k1::preallocated_new(&mut buf).unwrap();
+ ecmul(&secp).unwrap();
}
#[bench]
fn bench_libsecp256k1(b: &mut Bencher) {
- b.iter(|| ecmul().unwrap());
+ let mut buf = vec![AlignedType::zeroed(); Secp256k1::preallocate_size()];
+ let secp = Secp256k1::preallocated_new(&mut buf).unwrap();
+ b.iter(|| ecmul(&secp).unwrap());
}
} |
Arguably a design flaw in the rust interface that such a huge performance footgun is essentially invisible in the source code and that the correct way of using the library is a lot more complicated and less obvious than an incorrect way. --- also another argument for just making the verify tables static. |
@gmaxwell I completely agree with you, that's also the reason we added the So yeah a static context can go a long way here, especially if we hide it from the users (by removing the need for context from the API) (FYI, the "global-context" is basically a static pointer with something similiar to a |
I'm not sure. The line is very visible in the source code. Or are you talking about the names? |
Ah. This link is (now and when I looked at it) to the fixed code. So perhaps you see why I thought it was completely invisible that it was creating a context. :) |
Arghh the problem with linking to master, I fixed the link for future reference. |
@elichai do you have any suggestions for creating a singleton global context in wasm? |
@gregdhill I'll have to see a full project, as there are many flavors of wasm runtime etc, and I'll need to play with it. |
I'm closing this since the main issue has been solved. Feel free to continue discussion about how to best use the current library. I opened #893 for a discussion on static contexts. |
@elichai this is my WIP implementation (performance is still undesirable): https://gitlab.com/interlay/btc-parachain/-/blob/greg/rust-secp256k1/crates/bitcoin/src/address.rs#L154 The project itself is built on Substrate and the runtime is compiled to Wasm but I'm not certain how this is executed exactly. |
Not sure if this is an issue with the rust-bindings, but the performance of bitcoin-core/secp256k1 is distinctly slower than other Rust based libraries when compiled to Wasm. See the example benchmarks here.
The text was updated successfully, but these errors were encountered: