Skip to content

Conversation

@xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Nov 20, 2025

Description

Continuation of #7488.

This PR introduces the option for each type to control its own is_trivial.

Primitive data types have direct implementation (only decoding bool is false, as we need to guarantee its value is zero or one).

Structs, tuples, arrays etc... are trivial if their runtime representation and "inner types" allow for trivial encoding or decoding.

Enums are more complicated as we need to control that the enum tag is valid; they are not trivial.

String arrays are not trivially encoded/decoded for backwards compatibility with "encoding v0" that needs padding in some cases.

Some tests are showing a small regression (6 gas), which comes from the introduced "if" not being optimised away. I want to solve this in another PR, where the "if" condition will come from a const, and IR generation will guarantee that only one branch will ever be emitted.

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

Note

Add per-type trivial encode/decode flags and use raw_ptr fast paths in ABI (encode/decode) with supporting changes to codec, String/Vec, and tests.

  • std/codec:
    • Introduce AbiEncode::is_encode_trivial and AbiDecode::is_decode_trivial; add helpers is_encode_trivial/is_decode_trivial.
    • Switch encode/abi_decode to branch on triviality; add decode_from_raw_ptr and raw-ptr-based decode_* helpers.
    • Make BufferReader parameter accessors return raw_ptr; minor refactors to use raw ptr copies.
    • Implement triviality for primitives, arrays/tuples (conditional), str[strN] non-trivial, enums non-trivial.
  • std/string & std/vec: implement trivial encode/decode markers (non-trivial) and adapt codec impls.
  • Tests/fixtures:
    • Update snapshots (IR/ASM/bytecode/gas), JSON ABI offsets, contract/predicate IDs, and logging expectations to match new encoding paths.

Written by Cursor Bugbot for commit dddd944. This will update automatically on new commits. Configure here.

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 20, 2025

CodSpeed Performance Report

Merging #7501 will degrade performances by 13.65%

Comparing xunilrj/trivial-fns-codec-traits (d97e289) with master (c73137d)

Summary

❌ 1 regression
✅ 24 untouched

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
compile 5.3 s 6.1 s -13.65%

@xunilrj xunilrj force-pushed the xunilrj/trivial-fns-codec-traits branch from 02e1c5c to 182bf4a Compare November 22, 2025 20:56
@xunilrj xunilrj self-assigned this Nov 23, 2025
@xunilrj xunilrj marked this pull request as ready for review November 23, 2025 18:09
@xunilrj xunilrj requested review from a team as code owners November 23, 2025 18:09
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

This is the final PR Bugbot will review for you during this billing cycle

Your free Bugbot reviews will reset on December 7

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

@JoshuaBatty
Copy link
Member

-13.24% compile time regression is a bit unfortunate. Any way we can get this number down a bit?

@xunilrj
Copy link
Contributor Author

xunilrj commented Dec 4, 2025

Bytecode e2e

Improvements Regressions
Count 106 37
Average 18.99% -23.49%
Median 13.27% -6.36%
Max 47.47% -525.00%
Min 0.61% -0.52%

Gas Usage e2e

Improvements Regressions
Count 40 19
Average 8.10% -18.67%
Median 5.77% -2.89%
Max 36.80% -262.26%
Min 0.04% -0.21%

Gas Usage In Langauge Tests

Improvements Regressions
Count 6 19
Average 1.41% -1.94%
Median 0.65% -0.12%
Max 3.64% -13.59%
Min 0.03% -0.01%

@cursor
Copy link

cursor bot commented Dec 4, 2025

PR Summary

Introduce per-type trivial encode/decode checks (integrated into codegen), implement for std types, and update snapshots, IDs, and script tooling accordingly.

  • ABI/Codec:
    • Add per-type is_encode_trivial/is_decode_trivial hooks and integrate them in codegen to select fast/strict paths; refactor array/string decode to simpler memcpy-style paths (smaller bytecode/lower gas).
  • Std Library:
    • Implement triviality methods for String and Vec<T> (both non-trivial), and mark several custom test types as non-trivial encoders.
  • Tests & Snapshots:
    • Update IR/ASM snapshots, gas/size metrics, JSON ABI configurable offsets, and bytecode hashes.
    • Refresh hardcoded contract/predicate IDs across multiple test scripts.
  • Tooling:
    • Fix update-contract-ids.sh to pass --path when invoking forc contract-id.

Written by Cursor Bugbot for commit d97e289. This will update automatically on new commits. Configure here.

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.

4 participants