Skip to content

perf(core,levm): remove some unnecessary clones and make functions const #2438

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

Merged
merged 2 commits into from
Apr 19, 2025

Conversation

edg-l
Copy link
Contributor

@edg-l edg-l commented Apr 10, 2025

Motivation

Increase perfomance, improve code.

Description

Some methods took Vec by value just to take it's length, requiring a costly clone each time.

Some methods could be made const, if the compiler can make use of this it may increase perfomance.

Changed a drain to a into_iter, which is simpler and has better perfomance.

Aided by the following clippy command:

cargo clippy --all-features -- -D clippy::perfomance -D clippy::nursery -A clippy::use_self -A clippy::too_long_first_doc_paragraph -A clippy::derive_partial_eq_without_eq -A clippy::option_if_let_else

@edg-l edg-l requested a review from a team as a code owner April 10, 2025 15:35
Copy link

github-actions bot commented Apr 10, 2025

Lines of code report

Total lines added: 0
Total lines removed: 4
Total lines changed: 4

Detailed view
+-------------------------------------------------+-------+------+
| File                                            | Lines | Diff |
+-------------------------------------------------+-------+------+
| ethrex/crates/common/trie/node/extension.rs     | 421   | -1   |
+-------------------------------------------------+-------+------+
| ethrex/crates/common/trie/trie.rs               | 811   | -1   |
+-------------------------------------------------+-------+------+
| ethrex/crates/vm/levm/src/hooks/default_hook.rs | 290   | -2   |
+-------------------------------------------------+-------+------+

Copy link

Benchmark for 85a0b1f

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 40.6±1.77ms 33.9±2.02ms -16.50%
Trie/cita-trie insert 1k 3.6±0.04ms 3.8±0.15ms +5.56%
Trie/ethrex-trie insert 10k 139.7±2.18ms 140.4±3.97ms +0.50%
Trie/ethrex-trie insert 1k 11.8±0.11ms 11.8±0.15ms 0.00%

Copy link

github-actions bot commented Apr 10, 2025

Benchmark Results Comparison

PR Results

Benchmark Results: Factorial

Command Mean [ms] Min [ms] Max [ms] Relative
revm_Factorial 239.4 ± 0.7 238.2 240.9 1.00
levm_Factorial 807.8 ± 5.0 800.6 817.6 3.37 ± 0.02

Benchmark Results: Factorial - Recursive

Command Mean [s] Min [s] Max [s] Relative
revm_FactorialRecursive 1.436 ± 0.072 1.319 1.509 1.00
levm_FactorialRecursive 13.965 ± 0.043 13.904 14.042 9.72 ± 0.49

Benchmark Results: Fibonacci

Command Mean [ms] Min [ms] Max [ms] Relative
revm_Fibonacci 214.2 ± 1.2 211.0 215.4 1.00
levm_Fibonacci 802.5 ± 9.3 790.9 816.3 3.75 ± 0.05

Benchmark Results: ManyHashes

Command Mean [ms] Min [ms] Max [ms] Relative
revm_ManyHashes 8.6 ± 0.1 8.5 8.8 1.00
levm_ManyHashes 16.4 ± 0.4 16.2 17.6 1.91 ± 0.05

Benchmark Results: BubbleSort

Command Mean [s] Min [s] Max [s] Relative
revm_BubbleSort 3.225 ± 0.024 3.198 3.270 1.00
levm_BubbleSort 5.699 ± 0.064 5.643 5.866 1.77 ± 0.02

Benchmark Results: ERC20 - Transfer

Command Mean [ms] Min [ms] Max [ms] Relative
revm_ERC20Transfer 245.4 ± 3.3 243.1 253.3 1.00
levm_ERC20Transfer 492.9 ± 4.0 484.3 499.5 2.01 ± 0.03

Benchmark Results: ERC20 - Mint

Command Mean [ms] Min [ms] Max [ms] Relative
revm_ERC20Mint 138.6 ± 0.8 137.5 139.9 1.00
levm_ERC20Mint 321.1 ± 2.8 317.5 326.6 2.32 ± 0.02

Benchmark Results: ERC20 - Approval

Command Mean [s] Min [s] Max [s] Relative
revm_ERC20Approval 1.025 ± 0.008 1.015 1.035 1.00
levm_ERC20Approval 1.872 ± 0.026 1.845 1.927 1.83 ± 0.03

Main Results

Benchmark Results: Factorial

Command Mean [ms] Min [ms] Max [ms] Relative
revm_Factorial 239.6 ± 1.3 236.1 240.9 1.00
levm_Factorial 804.1 ± 9.8 796.1 829.5 3.36 ± 0.04

Benchmark Results: Factorial - Recursive

Command Mean [s] Min [s] Max [s] Relative
revm_FactorialRecursive 1.435 ± 0.109 1.295 1.570 1.00
levm_FactorialRecursive 13.973 ± 0.028 13.939 14.021 9.74 ± 0.74

Benchmark Results: Fibonacci

Command Mean [ms] Min [ms] Max [ms] Relative
revm_Fibonacci 216.1 ± 1.1 214.3 217.7 1.00
levm_Fibonacci 778.9 ± 7.3 769.7 792.3 3.60 ± 0.04

Benchmark Results: ManyHashes

Command Mean [ms] Min [ms] Max [ms] Relative
revm_ManyHashes 8.8 ± 0.1 8.7 8.9 1.00
levm_ManyHashes 16.6 ± 0.1 16.4 16.8 1.89 ± 0.03

Benchmark Results: BubbleSort

Command Mean [s] Min [s] Max [s] Relative
revm_BubbleSort 3.201 ± 0.014 3.188 3.229 1.00
levm_BubbleSort 5.647 ± 0.074 5.558 5.838 1.76 ± 0.02

Benchmark Results: ERC20 - Transfer

Command Mean [ms] Min [ms] Max [ms] Relative
revm_ERC20Transfer 244.5 ± 0.9 243.2 246.1 1.00
levm_ERC20Transfer 486.1 ± 7.4 475.8 495.8 1.99 ± 0.03

Benchmark Results: ERC20 - Mint

Command Mean [ms] Min [ms] Max [ms] Relative
revm_ERC20Mint 139.6 ± 1.1 138.2 141.4 1.00
levm_ERC20Mint 315.6 ± 1.0 313.8 317.1 2.26 ± 0.02

Benchmark Results: ERC20 - Approval

Command Mean [s] Min [s] Max [s] Relative
revm_ERC20Approval 1.025 ± 0.008 1.018 1.044 1.00
levm_ERC20Approval 1.852 ± 0.013 1.835 1.869 1.81 ± 0.02

@edg-l edg-l changed the title Remove some unnecessary clones and make functions const chore(common,levm): Remove some unnecessary clones and make functions const Apr 11, 2025
@edg-l edg-l changed the title chore(common,levm): Remove some unnecessary clones and make functions const chore: Remove some unnecessary clones and make functions const Apr 11, 2025
@edg-l edg-l changed the title chore: Remove some unnecessary clones and make functions const chore(perf): Remove some unnecessary clones and make functions const Apr 11, 2025
@edg-l edg-l changed the title chore(perf): Remove some unnecessary clones and make functions const perf(core): Remove some unnecessary clones and make functions const Apr 11, 2025
@edg-l edg-l changed the title perf(core): Remove some unnecessary clones and make functions const perf(core): remove some unnecessary clones and make functions const Apr 11, 2025
@edg-l edg-l changed the title perf(core): remove some unnecessary clones and make functions const perf(core,levm): remove some unnecessary clones and make functions const Apr 11, 2025
Copy link
Collaborator

@Arkenan Arkenan left a comment

Choose a reason for hiding this comment

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

LGTM

@edg-l edg-l enabled auto-merge April 11, 2025 15:50
Copy link

Benchmark for a5e3fec

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 35.1±0.48ms 35.9±0.53ms +2.28%
Trie/cita-trie insert 1k 3.6±0.04ms 3.6±0.04ms 0.00%
Trie/ethrex-trie insert 10k 134.6±2.40ms 132.4±1.42ms -1.63%
Trie/ethrex-trie insert 1k 11.8±0.19ms 11.6±0.08ms -1.69%

Copy link

Benchmark for 7334b82

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 36.5±3.31ms 38.2±3.49ms +4.66%
Trie/cita-trie insert 1k 3.6±0.03ms 3.7±0.04ms +2.78%
Trie/ethrex-trie insert 10k 137.1±3.14ms 136.0±3.58ms -0.80%
Trie/ethrex-trie insert 1k 11.8±0.06ms 11.7±0.09ms -0.85%

@mpaulucci
Copy link
Collaborator

would it make sense to add the clippy configurations to our default lint?

Copy link

Benchmark for 9c806fa

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 35.9±1.33ms 35.7±0.75ms -0.56%
Trie/cita-trie insert 1k 3.6±0.03ms 3.7±0.04ms +2.78%
Trie/ethrex-trie insert 10k 136.0±2.63ms 135.0±2.19ms -0.74%
Trie/ethrex-trie insert 1k 11.8±0.15ms 11.7±0.14ms -0.85%

@edg-l edg-l added this pull request to the merge queue Apr 19, 2025
Merged via the queue into main with commit b5e339c Apr 19, 2025
49 of 51 checks passed
@edg-l edg-l deleted the edgar_clones branch April 19, 2025 09:28
Copy link

Benchmark Block Execution Results Comparison Against Main

Command Mean [s] Min [s] Max [s] Relative
base 180.577 ± 1.148 177.957 181.700 1.00 ± 0.01
head 180.025 ± 0.719 178.928 181.250 1.00

pedrobergamini pushed a commit to pedrobergamini/ethrex that referenced this pull request Aug 24, 2025
…nst (lambdaclass#2438)

**Motivation**

Increase perfomance, improve code.
<!-- Why does this pull request exist? What are its goals? -->

**Description**

Some methods took Vec by value just to take it's length, requiring a
costly clone each time.

Some methods could be made const, if the compiler can make use of this
it may increase perfomance.

Changed a drain to a into_iter, which is simpler and has better
perfomance.

Aided by the following clippy command:
```
cargo clippy --all-features -- -D clippy::perfomance -D clippy::nursery -A clippy::use_self -A clippy::too_long_first_doc_paragraph -A clippy::derive_partial_eq_without_eq -A clippy::option_if_let_else
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants