Skip to content

Conversation

@snissn
Copy link
Contributor

@snissn snissn commented Dec 11, 2024

Optimize String and Bytes Handling with MCOPY

Summary:
This PR introduces an optimization for the String() and Bytes operations by leveraging the MCOPY opcode, resulting in reduced gas costs.

Details:
I've been working on MCOPY support for the Filecoin EVM as part of the FIL-Builders team. I noticed that the current implementation of String() and Bytes in this repo can benefit from MCOPY to achieve better gas efficiency.

Gas Comparison:
This optimization was validated by running the Forge test suite. Below is a summary of the key results:

With Optimization (This PR):

Ran 8 tests for test/comparison/CborDecode_ReadCbor.t.sol:ComparisonTest
[PASS] test_decodeArrayU8_ReadCbor()             (gas: 2334)
[PASS] test_decodeFalse_ReadCbor()               (gas: 437)
[PASS] test_decodeFixedArray_ReadCbor()          (gas: 4402)
[PASS] test_decodeInteger_ReadCbor()             (gas: 554)
[PASS] test_decodeNull_ReadCbor()                (gas: 240)
[PASS] test_decodeStringWithWeirdChar_ReadCbor() (gas: 1052)
[PASS] test_decodeString_ReadCbor()              (gas: 1125)
[PASS] test_decodeTrue_ReadCbor()                (gas: 495)

Current main Branch:

Ran 8 tests for test/comparison/CborDecode_ReadCbor.t.sol:ComparisonTest
[PASS] test_decodeArrayU8_ReadCbor()             (gas: 2334)
[PASS] test_decodeFalse_ReadCbor()               (gas: 437)
[PASS] test_decodeFixedArray_ReadCbor()          (gas: 4472)
[PASS] test_decodeInteger_ReadCbor()             (gas: 554)
[PASS] test_decodeNull_ReadCbor()                (gas: 240)
[PASS] test_decodeStringWithWeirdChar_ReadCbor() (gas: 1122)
[PASS] test_decodeString_ReadCbor()              (gas: 1195)
[PASS] test_decodeTrue_ReadCbor()                (gas: 495)

Improvements

  • Reduced Gas Costs:
    • test_decodeFixedArray_ReadCbor(): 4472 → 4402
    • test_decodeStringWithWeirdChar_ReadCbor(): 1122 → 1052
    • test_decodeString_ReadCbor(): 1195 → 1125

Notes

This PR improves efficiency without changing the core logic or functionality.


Let me know if there's anything you'd like to add or modify!

@turbocrime
Copy link
Owner

thanks for the pr!

i wasn't aware of mcopy, it's definitely preferable to the loop.

@turbocrime turbocrime merged commit 939b4c6 into turbocrime:main Dec 11, 2024
@snissn
Copy link
Contributor Author

snissn commented Dec 11, 2024

@turbocrime Thanks for merging the PR! I’m glad this was helpful — I appreciate the opportunity to contribute!

@snissn snissn deleted the optimization/mcopy branch December 11, 2024 20:40
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