Skip to content

perf(StdAssertions): avoid vm call for trivial conditions#693

Merged
DaniPopes merged 1 commit into
foundry-rs:masterfrom
DaniPopes:assertions-perf
Jul 31, 2025
Merged

perf(StdAssertions): avoid vm call for trivial conditions#693
DaniPopes merged 1 commit into
foundry-rs:masterfrom
DaniPopes:assertions-perf

Conversation

@DaniPopes

@DaniPopes DaniPopes commented Jul 30, 2025

Copy link
Copy Markdown
Member

Avoid calling vm when the condition is trivial to check inline. This is a minor performance improvement as a couple of opcodes execute a lot faster than a full cheatcode pipeline (external call, EVM call bookkeeping, abi encoding/decoding, ...) that eventually does nothing.

For example, Uniswap's v4-core spends ~28% of the entire forge test CPU time in a couple of trivial assert functions (uint256, bytes32, true, false), ~13% for forge coverage. This is without accounting for the actual CALL/abi coding etc.

These numbers are skewed due to profiling overhead, however making this change does have ~5% overall test performance improvement, for no compilation time change.

Avoid calling vm when the condition is trivial to check inline.
This is a minor performance improvement as a couple of opcodes execute
a lot faster than a full cheatcode pipeline (external call, EVM call
bookkeeping, abi encoding/decoding, ...) that eventually does nothing.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes assertion functions by adding inline condition checks to avoid unnecessary VM calls when assertions would pass. The optimization adds conditional guards that only invoke the VM when an assertion is about to fail, improving performance for successful test cases.

  • Adds inline condition checks to all basic assertion functions before calling VM methods
  • Modifies assertEq32 and assertNotEq32 functions to use direct VM calls instead of delegating to other assertion functions
  • Applies the optimization pattern consistently across boolean, numeric, and address comparison assertions

Comment thread src/StdAssertions.sol
Comment thread src/StdAssertions.sol
Comment thread src/StdAssertions.sol
@grandizzy grandizzy self-requested a review July 31, 2025 11:13

@grandizzy grandizzy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nice finding, lgtm!

@zerosnacks

Copy link
Copy Markdown
Contributor

This makes sense, a further improvement could be exposing an error invoker directly and avoiding the duplicate assertion check

@yash-atreya yash-atreya left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm!

@DaniPopes

Copy link
Copy Markdown
Member Author

This makes sense, a further improvement could be exposing an error invoker directly and avoiding the duplicate assertion check

It would make sense in theory but in practice the check is free and it would just be adding a lot more cheatcodes for no gain

@DaniPopes DaniPopes merged commit 276ccaa into foundry-rs:master Jul 31, 2025
3 checks passed
@DaniPopes DaniPopes deleted the assertions-perf branch July 31, 2025 11:31
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.

5 participants