Dispatch: replace runtime selector computation with compile-time#319
Conversation
66fe5bc to
6ccbea6
Compare
There was a problem hiding this comment.
Is there a reason for this file be in this commit or is it a temporary generated during tests?
There was a problem hiding this comment.
Is this file necessary in this commit or is it a temporary?
There was a problem hiding this comment.
test/examples/dispatch/*.json files are used by run_contests.sh which is part of #314 - I forgot this has not been merged yet
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
No, it is enabled by the --dump-spec flag (so this is not printed even with -v)
rodrigogribeiro
left a comment
There was a problem hiding this comment.
Great work! LGTM.
6ccbea6 to
6c6d6b3
Compare
Method selectors (4-byte function identifiers used for EVM dispatch) were computed at runtime:
ContractDispatch.hsgeneratedABIStringinstances that emitted Yul assembly to build ABI signature strings in memory byte-by-byte, then hashed them withkeccak256. 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
SigStringtypeclass.ContractDispatch.hsnow generatesSigStringinstances that return the method name as a string literal.dispatch.solcbuilds the full signature via string concatenation (concatLit) and hashes it withkeccakLit- the partial evaluator folds the entire chain to an integer constant, so no string operations or hashing remain at runtime.Changes
ContractDispatch.hs:mkNameInstgeneratesSigStringinstances (singlesigStrmethod returning aStrLit) instead ofABIStringinstances (Yul byte-manipulation assembly). RemovedgenerateStoreBytes,storeChunk,chunk32,bsToIntegerand their imports.std/dispatch.solc: Replaced withABIStringtheSigString-based dispatchstd/rtdispatch.solc: Old runtimeABIStringdispatch preserved for reference.std/std.solc: Addedstring:Addinstance delegating toconcatLit.Testing
./run_contests.shruns tests fromtest/examples/dispatchensuring dispatch works correctly after changes (includingminiERC20.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