Skip to content

perf: short-circuit Slice equality on length mismatch#6338

Open
VolodymyrBg wants to merge 1 commit intoOpenZeppelin:masterfrom
VolodymyrBg:perf/memory-slice-equal-short-circuit
Open

perf: short-circuit Slice equality on length mismatch#6338
VolodymyrBg wants to merge 1 commit intoOpenZeppelin:masterfrom
VolodymyrBg:perf/memory-slice-equal-short-circuit

Conversation

@VolodymyrBg
Copy link
Contributor

Memory.equal(Slice, Slice) always computed two keccak256 hashes even when slice lengths differed — making equality impossible by definition. Bytes.equal in the same codebase already short-circuits on a.length == b.length before hashing.

Added an early return false when lenA != lenB, skip both keccak256 and _pointer extraction. Consistent with Bytes.equal pattern.

@VolodymyrBg VolodymyrBg requested a review from a team as a code owner February 10, 2026 11:33
@openzeppelin-security-agent
Copy link

The latest updates on your security scan. Learn more about OpenZeppelin Platform.

Project Scan Issues Details Updated
Openzeppelin Contracts 🟡 Queued View Feb 10, 2026, 11:33 AM (UTC)

@changeset-bot
Copy link

changeset-bot bot commented Feb 10, 2026

⚠️ No Changeset found

Latest commit: cd9e51c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 10, 2026

Walkthrough

The Memory.equal function in contracts/utils/Memory.sol has been refactored to reorder its logic flow. The function now performs an early length comparison between two memory regions before computing their pointers. If the lengths differ, the function immediately returns false. The pointer computations that determine memory positions are now executed only after confirming the lengths are equal. The final comparison mechanism using keccak256 remains unchanged. This represents a logic reordering with no modifications to the function signature or public API.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main optimization: adding an early return when Slice lengths differ, improving performance by short-circuiting equality checks.
Description check ✅ Passed The description clearly explains the performance issue, the fix applied, and references the consistency with existing Bytes.equal pattern, directly relating to the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

No actionable comments were generated in the recent review. 🎉


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant