Skip to content

Dispatch: replace runtime selector computation with compile-time#319

Merged
mbenke merged 21 commits into
mainfrom
mbenke/pe-dispatch
Feb 19, 2026
Merged

Dispatch: replace runtime selector computation with compile-time#319
mbenke merged 21 commits into
mainfrom
mbenke/pe-dispatch

Conversation

@mbenke
Copy link
Copy Markdown
Collaborator

@mbenke mbenke commented Feb 13, 2026

Method selectors (4-byte function identifiers used for EVM dispatch) were computed at runtime: ContractDispatch.hs generated ABIString instances that emitted Yul assembly to build ABI signature strings in memory byte-by-byte, then hashed them with keccak256. This was complex and wasteful since signature strings are fully known at compile time.

This PR replaces the runtime approach with compile-time computation using the SigString typeclass. ContractDispatch.hs now generates SigString instances that return the method name as a string literal. dispatch.solc builds the full signature via string concatenation (concatLit) and hashes it with keccakLit - the partial evaluator folds the entire chain to an integer constant, so no string operations or hashing remain at runtime.

Changes

  • ContractDispatch.hs: mkNameInst generates SigString instances (single sigStr method returning a StrLit) instead of ABIString instances (Yul byte-manipulation assembly). Removed generateStoreBytes, storeChunk, chunk32, bsToInteger and their imports.
  • std/dispatch.solc: Replaced with ABIString the SigString-based dispatch
  • std/rtdispatch.solc: Old runtime ABIString dispatch preserved for reference.
  • std/std.solc: Added string:Add instance delegating to concatLit.

Testing

./run_contests.sh runs tests from test/examples/dispatch ensuring dispatch works correctly after changes (including miniERC20.solc, which is the main litmus test).

Potential TODO

Replace chained ifs by a big match - this would require allowing comptime expressions in patterns - TBD

@mbenke mbenke force-pushed the mbenke/pe-dispatch branch from 66fe5bc to 6ccbea6 Compare February 13, 2026 10:06
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is there a reason for this file be in this commit or is it a temporary generated during tests?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this file necessary in this commit or is it a temporary?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

test/examples/dispatch/*.json files are used by run_contests.sh which is part of #314 - I forgot this has not been merged yet

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No worries... after checking the other PR I noticed that. Sorry for the noise.

-- Dead code elimination: remove functions unreachable from 'start'/'main'
let optimized = eliminateDeadCode evaluated

liftIO $ when (optDumpSpec opts) $ do
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this option enabled by default? I'd argue that it would be useful to reduce the noise of the compiler output when using the standard library.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

No, it is enabled by the --dump-spec flag (so this is not printed even with -v)

Copy link
Copy Markdown
Collaborator

@rodrigogribeiro rodrigogribeiro left a comment

Choose a reason for hiding this comment

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

Great work! LGTM.

@rodrigogribeiro rodrigogribeiro self-requested a review February 13, 2026 11:47
Copy link
Copy Markdown
Member

@Y-Nak Y-Nak left a comment

Choose a reason for hiding this comment

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

LGTM!

@mbenke mbenke force-pushed the mbenke/pe-dispatch branch from 6ccbea6 to 6c6d6b3 Compare February 16, 2026 19:18
@mbenke mbenke merged commit bdc855f into main Feb 19, 2026
3 checks passed
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.

3 participants