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

perf(merkle): optimize hashing by reusing sha256 in a pool #44

Conversation

odeke-em
Copy link
Contributor

This change optimizes SHA256 hashing for leafHash & innerHash by pooling and reusing hashes, which massively reduces on RAM consumption and even CPU time (benchmarking on my noisy machine is skewed but the results can be verified individually) producing results below:

$ benchstat before.txt after.txt
name                          old time/op    new time/op    delta
HashAlternatives/recursive-8    77.6µs ± 2%    77.0µs ± 2%     ~     (p=0.165 n=10+10)
HashAlternatives/iterative-8    76.3µs ± 1%    76.2µs ± 3%     ~     (p=0.720 n=9+10)

name                          old alloc/op   new alloc/op   delta
HashAlternatives/recursive-8    25.4kB ± 0%     6.4kB ± 0%  -74.94%  (p=0.000 n=10+10)
HashAlternatives/iterative-8    28.1kB ± 0%     9.1kB ± 0%  -67.78%  (p=0.000 n=10+10)

name                          old allocs/op  new allocs/op  delta
HashAlternatives/recursive-8       497 ± 0%       199 ± 0%  -59.96%  (p=0.000 n=10+10)
HashAlternatives/iterative-8       498 ± 0%       200 ± 0%  -59.84%  (p=0.000 n=10+10)

Fixes #43

Overview

Checklist

  • New and updated code has appropriate documentation
  • New and updated code has new and/or updated testing
  • Required CI checks are passing
  • Visual proof for any user facing features like CLI or documentation updates
  • Linked issues closed with keywords

This change optimizes SHA256 hashing for leafHash & innerHash
by pooling and reusing hashes, which massively reduces on RAM
consumption and even CPU time (benchmarking on my noisy machine
is skewed but the results can be verified individually) producing
the results below:

```shell
$ benchstat before.txt after.txt
name                          old time/op    new time/op    delta
HashAlternatives/recursive-8    77.6µs ± 2%    77.0µs ± 2%     ~     (p=0.165 n=10+10)
HashAlternatives/iterative-8    76.3µs ± 1%    76.2µs ± 3%     ~     (p=0.720 n=9+10)

name                          old alloc/op   new alloc/op   delta
HashAlternatives/recursive-8    25.4kB ± 0%     6.4kB ± 0%  -74.94%  (p=0.000 n=10+10)
HashAlternatives/iterative-8    28.1kB ± 0%     9.1kB ± 0%  -67.78%  (p=0.000 n=10+10)

name                          old allocs/op  new allocs/op  delta
HashAlternatives/recursive-8       497 ± 0%       199 ± 0%  -59.96%  (p=0.000 n=10+10)
HashAlternatives/iterative-8       498 ± 0%       200 ± 0%  -59.84%  (p=0.000 n=10+10)
```

Fixes celestiaorg#43
@odeke-em odeke-em force-pushed the merkle-optimize-sha256-hashing-by-pooling branch from 19dd495 to af9405a Compare February 29, 2024 17:07
@odeke-em odeke-em changed the title merkle: optimize hashing by reusing sha256 in a pool perf(merkle): optimize hashing by reusing sha256 in a pool Feb 29, 2024
Copy link
Collaborator

@rootulp rootulp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes seem fine to me but it's difficult to approve because the modified functions don't have unit tests. They should've had unit tests prior to this PR but I expect they don't because the merkle package is copy + pasted from the CometBFT repo.

IMO we need to decide if it's okay for the merkle package in this repo to diverge from the upstream CometBFT implementation. cc: @cmwaters

@odeke-em
Copy link
Contributor Author

odeke-em commented Mar 2, 2024

@rootulp how come the code was copied and pasted in here without the appropriate attributions/licenses or without just plain imports or submodules?

@rootulp
Copy link
Collaborator

rootulp commented Mar 4, 2024

I think this is relevant:

This is a forked copy of github.com/cometbft/cometbft/crypto/merkle

I suppose we could also copy + paste this license into that directory.

@celestia-bot celestia-bot requested a review from a team March 6, 2024 14:12
@cmwaters
Copy link
Collaborator

Closing this because for now we've decided to keep the merkle library in our cometbft fork. Thanks for opening the PR in any case. Sorry that we didn't merge it. We may look to apply this to our cometbft fork in the future

@cmwaters cmwaters closed this Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants