Skip to content

Feat/Delete EVM instance in AnteHandler #352

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

heijiLee
Copy link

@heijiLee heijiLee commented Jul 24, 2025

Description

Closes: #351


Summary

Remove the creation of a Geth EVM instance during the AnteHandler balance check.

Notes

Background

Cosmos EVM currently instantiates a full Geth EVM engine twice per transaction—once for pre‑validation (CanTransfer) and once for execution—leading to significant CPU, memory, and GC overhead. By eliminating the unnecessary VM spin‑up in the AnteHandler, we can streamline the pre‑validation path without affecting state integrity.

Changes

Before: In ante/evm/07_can_transfer.go, call evmKeeper.NewEVM() on every transaction to create a “first” EVM instance and then invoke vm.CanTransfer().

After: Refactored CanTransfer() to remove the NewEVM() call and perform a direct balance lookup via StateDB.GetBalance(). Same results, but Compact.
(Reference: Geth’s pre-validation path also does not spin up an EVM instance.)

All existing tests pass without modification.

Benchmark

Benchstat
go version go1.24.4
cpu : M1 pro, memory : 16 GB

$ benchstat old.txt new.txt
goos: darwin
goarch: arm64
pkg: github.com/cosmos/evm/evmd/ante
cpu: Apple M1 Pro
                                        │   old.txt   │              new.txt               │
                                        │   sec/op    │   sec/op     vs base               │
AnteHandler/tx_type_evm_transfer_sim-10   129.4µ ± 1%   123.6µ ± 1%  -4.54% (p=0.000 n=10)
AnteHandler/tx_type_evm_transfer-10       132.8µ ± 5%   126.8µ ± 5%  -4.54% (p=0.009 n=10)
geomean                                   131.1µ        125.2µ       -4.54%

                                        │   old.txt    │               new.txt                │
                                        │     B/op     │     B/op      vs base                │
AnteHandler/tx_type_evm_transfer_sim-10   50.47Ki ± 0%   44.54Ki ± 0%  -11.76% (p=0.000 n=10)
AnteHandler/tx_type_evm_transfer-10       50.47Ki ± 0%   44.53Ki ± 0%  -11.76% (p=0.000 n=10)
geomean                                   50.47Ki        44.53Ki       -11.76%

                                        │  old.txt   │              new.txt               │
                                        │ allocs/op  │ allocs/op   vs base                │
AnteHandler/tx_type_evm_transfer_sim-10   900.0 ± 0%   794.0 ± 0%  -11.78% (p=0.000 n=10)
AnteHandler/tx_type_evm_transfer-10       900.0 ± 0%   794.0 ± 0%  -11.78% (p=0.000 n=10)
geomean                                   900.0        794.0       -11.78%

old benchmark

Date 0724 02:10 AM PST
Branch : [old/delete-evm-instance-ante]

goos: darwin
goarch: arm64
pkg: github.com/cosmos/evm/evmd/ante
cpu: Apple M1 Pro
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	    8306	    128058 ns/op	   51726 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	    9901	    127294 ns/op	   51690 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	    9702	    128835 ns/op	   51686 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	    9578	    128643 ns/op	   51685 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	    9638	    130023 ns/op	   51685 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	    9658	    129800 ns/op	   51684 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	    9577	    130390 ns/op	   51685 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	    9588	    129076 ns/op	   51684 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	    9739	    130358 ns/op	   51684 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	    9690	    130868 ns/op	   51683 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9256	    131803 ns/op	   51693 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9646	    133016 ns/op	   51683 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9620	    132590 ns/op	   51682 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9604	    131509 ns/op	   51682 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9499	    130281 ns/op	   51681 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9492	    131915 ns/op	   51682 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9543	    133841 ns/op	   51682 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9540	    135006 ns/op	   51681 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9457	    139216 ns/op	   51682 B/op	     900 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    8890	    142789 ns/op	   51681 B/op	     900 allocs/op
PASS
ok  	github.com/cosmos/evm/evmd/ante	427.433s

latest benchmark

Date 0724 02:40 AM PST
Branch : [feat/delete-evm-instance-ante]

goos: darwin
goarch: arm64
pkg: github.com/cosmos/evm/evmd/ante
cpu: Apple M1 Pro
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	    8527	    122564 ns/op	   45644 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	   10000	    121498 ns/op	   45611 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	   10000	    122381 ns/op	   45608 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	   10000	    123051 ns/op	   45605 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	   10000	    123295 ns/op	   45605 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	   10000	    123838 ns/op	   45604 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	   10000	    123878 ns/op	   45605 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	   10000	    125155 ns/op	   45605 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	   10000	    124703 ns/op	   45603 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer_sim-10         	   10000	    124603 ns/op	   45603 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9410	    124709 ns/op	   45612 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	   10000	    126091 ns/op	   45602 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	   10000	    125456 ns/op	   45603 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	   10000	    125998 ns/op	   45602 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9884	    126833 ns/op	   45601 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9985	    126722 ns/op	   45602 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9800	    129210 ns/op	   45602 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9946	    129648 ns/op	   45602 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    9735	    133570 ns/op	   45601 B/op	     794 allocs/op
BenchmarkAnteHandler/tx_type_evm_transfer-10             	    8881	    150091 ns/op	   45602 B/op	     794 allocs/op
PASS
ok  	github.com/cosmos/evm/evmd/ante	443.807s

Average time per operation (ns/op): ↓ 4.54%
Heap allocation bytes (B/op): ↓ 11.76%
Heap allocation calls (allocs/op): ↓ 11.78%

Interpretation:

  • Processing time (–4.5%): Eliminates heavy struct initialization, function calls, and field copying previously done in newEVM().
  • Memory allocations (–11.8%): Removes VM and context structures from the heap.

Expect shorter and less frequent GC pauses in production, resulting in lower processing latency, although this performance gain isn’t reflected in the current benchmark.

Reproduce this benchmark

cd evmd/ante
go test -benchmem -run=^$ -bench ^BenchmarkAnteHandler$ github.com/cosmos/evm/evmd/ante
# for benchstat
go test -benchmem -run=^$ -bench ^BenchmarkAnteHandler$ github.com/cosmos/evm/evmd/ante -count=10 > old.txt
go test -benchmem -run=^$ -bench ^BenchmarkAnteHandler$ github.com/cosmos/evm/evmd/ante -count=10 > new.txt
benchstat old.txt new.txt

Since there was no performance improvement, I mocked tx types bank-msg-send


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • tackled an existing issue or discussed with a team member
  • left instructions on how to review the changes
  • targeted the main branch

Reviewers Checklist

All items are required.
Please add a note if the item is not applicable
and please add your handle next to the items reviewed
if you only reviewed selected items.

I have...

  • added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • confirmed all author checklist items have been addressed
  • confirmed that this PR does not change production code
  • reviewed content
  • tested instructions (if applicable)
  • confirmed all CI checks have passed

@heijiLee heijiLee changed the title Feat/delete evm instance ante Feat/Delete EVM instance in AnteHandler Jul 24, 2025
@heijiLee heijiLee marked this pull request as ready for review July 24, 2025 13:39
@heijiLee heijiLee requested review from a team as code owners July 24, 2025 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: Remove EVM instance creation in AnteHandler Balance-Check
2 participants