rust-web3 icon indicating copy to clipboard operation
rust-web3 copied to clipboard

Sha3 is significantly slower than a sync alternative solution

Open ppoliani opened this issue 4 years ago • 5 comments
trafficstars

I've been using the sha3 method for quite some time until I realized it's performing quite badly. To make a comparison I replaced all calls to sha3 with another crate that synchronously calculates sha3. More specifically the execution time dropped from around 400ms down to just 200μs.

I wonder why there is such a huge difference. Maybe we should simply replace that with the sync alternative

ppoliani avatar Apr 19 '21 13:04 ppoliani

@ppoliani Because the method you linked is simply calling JSON-RPC and the sha3 is happening on the node. There is a JSON-RPC + transport overhead added to every call. The reason for the API to exist in the first place is only because the clients expose it and web3 tries to be a client for all methods. Feel free to reference any sha3/keccak256 library you want in your project though and calculate it synchronously.

tomusdrw avatar Apr 19 '21 16:04 tomusdrw

@tomusdrw Thanka for the explanation. The rationale of keeping a consistent with the JSON-RPC methods makes sense.

At the same time I believe that keccak256 is such a common task for off-chain services that adding it to a utils module would make sense. What do you think? I'm happy to open a PR if that's ok.

ppoliani avatar Apr 19 '21 16:04 ppoliani

I'm kind of hesitant. We currently only require tiny-keccak for signing, so it's one dependency less if you don't need signing. I'd rather keep the library minimal and avoid unnecessary dependencies. Maybe we could additonal example which shows how to keccak? What is the specific use case you needed it for?

tomusdrw avatar Apr 19 '21 16:04 tomusdrw

I totally understand that.

My use case is pretty much replicate the following

keccak256(abi.encodePacked(data))

The above is a solidity code that off-chain services tend to use quite often. So I have implemented my own encodePacked function and used the sha3 from this crate.

ppoliani avatar Apr 19 '21 16:04 ppoliani

Actually, apologies, it seems that tiny-keccak is included unconditionally and we already have web3::signing::keccak256 exposed.

Happy to accept a PR with encodePacked (since I believe it's not available in ethabi, but should be) and an example how to use both.

tomusdrw avatar Apr 20 '21 13:04 tomusdrw