Skip to content

Conversation

@DaniPopes
Copy link
Member

@DaniPopes DaniPopes commented Jul 30, 2025

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.
Copy link

Copilot AI left a comment

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

@grandizzy grandizzy self-requested a review July 31, 2025 11:13
Copy link
Contributor

@grandizzy grandizzy left a comment

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
Member

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

Copy link
Contributor

@yash-atreya yash-atreya left a comment

Choose a reason for hiding this comment

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

lgtm!

@DaniPopes
Copy link
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.

4 participants