Skip to content

Share runtime instances across call sites - #1618

Merged
sbillig merged 2 commits into
masterfrom
dedupe-runtime-instances
Sep 28, 2026
Merged

sbillig merged 2 commits into
masterfrom
dedupe-runtime-instances

Conversation

@sbillig

@sbillig sbillig commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

Problem

Callee semantic instance keys carried details that only identify the caller. As a result, every recv arm and every calling module got its own copy of each helper it reached, even though the bodies were identical. Sonatina's exact function merge folded some copies back together, but not reliably. The Uniswap v3 port's UniswapV3Factory shipped a second 1.8 KB modify_position and went over EIP-170 (24916 bytes).

In the factory module, the colliding keys differed in three places:

  1. Root provider site. ProviderSource::RootProvider { site } named the recv arm, although every arm of a contract shares the contract's root-provider registrations.
  2. Provenance. EffectProviderSubst stored EffectProviderSpecializations, whose provenance names the caller's body and binding. It's only needed while the call site is being resolved; everything it determines already flows into the key through the substitution and the resolved ProviderBinding.
  3. Caller impl env. Inherent methods (MemBuffer::alloc, MemSlice::len, ...) and effectful free functions never took the context-independent path, so they inherited the calling module's scope and assumptions.

Change

  • EffectProviderSubst holds ProviderBindings.
  • ProviderSource::RootProvider carries a RootProviderScope (Func or Contract) instead of the arm's site.
  • Concrete free and inherent functions, including effectful ones, shed caller assumptions once their bounds, signature, and effect keys normalize the same without them.
  • Context-independent callees, trait-selected ones included, normalize at the caller's ingot root. The caller's scope can't be dropped entirely: a core body can need impls that only the caller's ingot sees, e.g. Result::unwrap lowering panic_with_value to std::evm::revert with Encode<Sol> for a user error type. Library paths and impl selection resolve per ingot, so the ingot root is the canonical choice.
  • Stable keys name the ingot of the normalization scope, since every ingot's root module has the same name.

Results

Uniswap v3 port at --optimize s, runtime bytes:

contract before after Solidity
UniswapV3Factory 24916 22272 24535
UniswapV3Pool 21504 20692 22142
TickBitmapTest 11069 7464 7504
  • Duplicate instances in the factory module drop from 177 to 8. The remaining 8 are init vs runtime copies, different parameter classes, or different message types.
  • Total runtime across the port's contracts shrinks by 13.5 KB.
  • createPool goes from 268k gas more than Solidity to 265k less.

A few helpers that used to be inlined per arm are now shared and outlined, which costs some gas in small paths (e.g. NoDelegateCall overhead). fe-lang/sonatina#339 addresses the larger memory-placement cost behind those paths.

Tests

  • New codegen test recv_arms_share_helper_instances: a helper called from two arms, plus the std helpers they reach, is emitted once.
  • New HIR tests: equal provider bindings share one callee instance, and different ones (StorageMap<.., 0> vs <.., 1>) stay distinct. These replace a test that required two identical StorageMap<.., 0> providers to produce distinct instances.
  • 30 Sonatina IR snapshots shrink by about 2,600 lines.
  • cargo nextest r --release, clippy, and fmt all pass.

Callee semantic keys carried caller-only identity, so every recv arm and
every calling module got its own copy of each helper it reached, with
identical bodies. Sonatina's function merge folded some copies back, but
not reliably: the v3 factory shipped a second 1.8 KB `modify_position`.

- `EffectProviderSubst` holds the resolved provider bindings. Call-site
  provenance stays in the typed body; everything it determines already
  flows into the key through the substitution and the binding.
- Root providers name the contract or function whose registrations they
  come from, not the arm that materialized them.
- Concrete free and inherent functions, including effectful ones, shed
  caller assumptions the way concrete trait impls do.
- Context-independent callees normalize at their caller's ingot root
  rather than a module. Library paths and impl selection resolve per
  ingot, and a body can need impls only the caller's ingot sees, such as
  `Encode<Sol>` for a panic payload.
- Stable keys name the ingot of the normalization scope, since every
  ingot's root module has the same name.

In the v3 port, the UniswapV3Factory module drops from 177 duplicate
instances to 8 (init vs runtime copies or different parameter classes),
and the factory runtime shrinks from 24916 to 22272 bytes, back under
EIP-170.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-28T19:38:46.700203Z 608db2a PR opened
🔒 Security Review ✅ Completed 2026-09-28T19:41:16.548039Z 608db2a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cburgdorf

Copy link
Copy Markdown
Collaborator

Amazing 😍

@sbillig
sbillig merged commit c3f73ef into master Sep 28, 2026
9 checks passed
@sbillig
sbillig deleted the dedupe-runtime-instances branch September 28, 2026 22:55
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.

2 participants