Skip to content

Conversation

@xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Nov 7, 2025

Description

This PR will slice #7419 into multiple PRS, being the first one.

To start optimising encoding/decoding we will introduce the concept of "trivially encoded/decoded types". A type is trivially encoded or decoded if the encoding memory representation is the same as the runtime memory representation.

PR #7419 introduced an intrinsic that would return a bool if both representations match. For this PR I decided to expose these memory representations directly. For that we have two new intrinsics: __runtime_mem_id and __encoding_mem_id.

I am not 100% sure if they are going to be useful in other scenarios, but given that the optimiser is able to optimise them away, I think exposing them like this is better than what #7419 does.

These ids are opaque numbers that do not convey anything about the type or their representation. They can only be compared for equality, and when a type does not have an encoding representation the intrinsic returns zero.

With this in mind, the encode function now checks if a type is "trivially encoded" or not. If it is, we just allocate and memcopy its bytes. As this function should not return an aliased pointer.

On top of that, we also have an encode_and_return function that avoids creating tuples and other stuff, given that the optimiser still generates some mem_copys in these cases.

A simple example of the benefit is the test main_args_empty, where the binary size went from 96 bytes to 64 bytes.

Before

script {
    pub entry fn __entry() -> __ptr slice, !3 {
        local mut slice __aggr_memcpy_0
        local { u64, u64 } __anon_0
        local slice __ret_value

        entry():
        v0 = get_local __ptr slice, __ret_value
        v1 = call main_0(), !6
        v2 = get_local __ptr { u64, u64 }, __anon_0, !7
        v3 = const u64 0
        v4 = get_elem_ptr v2, __ptr u64, v3, !7
        v5 = const u64 0, !8
        store v5 to v4, !7
        v6 = const u64 1
        v7 = get_elem_ptr v2, __ptr u64, v6, !7
        v8 = const u64 0, !9
        store v8 to v7, !7
        v9 = asm(s: v2) -> __ptr slice s {
        }
        v10 = get_local __ptr slice, __aggr_memcpy_0
        mem_copy_val v10, v9
        mem_copy_val v0, v10
        ret __ptr slice v0
    }

    entry_orig fn main_0() -> (), !13 {
        entry():
        v0 = const unit ()
        ret () v0
    }
}

;; ASM: Final program
;; Program kind: Script
.program:
move $$tmp $pc
jmpf $zero i4
DATA_SECTION_OFFSET[0..32]
DATA_SECTION_OFFSET[32..64]
CONFIGURABLES_OFFSET[0..32]
CONFIGURABLES_OFFSET[32..64]
lw   $$ds $$tmp i1
add  $$ds $$ds $$tmp
cfei i0                       ; allocate stack space for globals
move $$locbase $sp            ; save locals base register for function __entry
cfei i48                      ; allocate 48 bytes for locals and 0 slots for call arguments
addi $r0 $$locbase i32        ; get offset to local __ptr slice
jal  $$reta $pc i9            ; [call]: call main_0
addi $r1 $$locbase i16        ; get offset to local __ptr { u64, u64 }
sw   $$locbase $zero i2       ; store word
sw   $$locbase $zero i3       ; store word
mcpi $$locbase $r1 i16        ; copy memory
mcpi $r0 $$locbase i16        ; copy memory
lw   $r1 $r0 i1               ; load size of returned slice
lw   $r0 $r0 i0               ; load pointer to returned slice
retd  $r0 $r1
pshh i524288                  ; save registers 40..64
move $$locbase $sp            ; save locals base register for function main_0
poph i524288                  ; restore registers 40..64
jal  $zero $$reta i0          ; return from call
.data:

After

script {
    pub entry fn __entry() -> __ptr never, !3 {
        entry():
        v0 = call main_0(), !6
        v1 = const u64 0, !7
        v2 = const u64 0, !8
        retd v1 v2, !9
    }

    entry_orig fn main_0() -> (), !13 {
        entry():
        v0 = const unit ()
        ret () v0
    }
}

;; ASM: Final program
;; Program kind: Script
.program:
move $$tmp $pc
jmpf $zero i4
DATA_SECTION_OFFSET[0..32]
DATA_SECTION_OFFSET[32..64]
CONFIGURABLES_OFFSET[0..32]
CONFIGURABLES_OFFSET[32..64]
lw   $$ds $$tmp i1
add  $$ds $$ds $$tmp
cfei i0                       ; allocate stack space for globals
move $$locbase $sp            ; save locals base register for function __entry
jal  $$reta $pc i2            ; [call]: call main_0
retd  $zero $zero
pshh i524288                  ; save registers 40..64
move $$locbase $sp            ; save locals base register for function main_0
poph i524288                  ; restore registers 40..64
jal  $zero $$reta i0          ; return from call
.data:

Next PR will introduce this concept into the AbiEncode trait, allowing the type itself to control if it wants to be trivially encoded or not.

Missing optimizations

If we look at the test main_args_various_types, after the main is called we see that IR is still not optimal.

v37 = get_local __ptr { u64 }, __ret_val1
v38 = call main_19(v36, v37)

v39 = get_local __ptr { u64 }, _result, !58        <- not needed
mem_copy_val v39, v37                                    <- not needed
v40 = get_local __ptr { u64 }, _result, !59        <- not needed      
v41 = get_local __ptr { u64 }, item_, !62          <- not needed
mem_copy_val v41, v40                                    <- not needed
v42 = get_local __ptr { u64 }, item_, !64          <- not needed

v43 = const u64 8
retd v42 v43, !66

Ideally, IR would be

v37 = get_local __ptr { u64 }, __ret_val1
v38 = call main_19(v36, v37)
v39 = const u64 8
retd v37 v39, !66

Gas Usage Diff

Test Before After Percentage
should_fail/arith_overflow/u16_add_overflow (test.toml) 44 43 2.27%
should_fail/arith_overflow/u16_mul_overflow (test.toml) 45 44 2.22%
should_fail/arith_overflow/u16_sub_underflow (test.toml) 23 22 4.35%
should_fail/arith_overflow/u32_add_overflow (test.toml) 44 43 2.27%
should_fail/arith_overflow/u32_mul_overflow (test.toml) 45 44 2.22%
should_fail/arith_overflow/u32_sub_underflow (test.toml) 23 22 4.35%
should_fail/arith_overflow/u64_add_overflow (test.toml) 17 15 11.76%
should_fail/arith_overflow/u64_mul_overflow (test.toml) 18 16 11.11%
should_fail/arith_overflow/u64_sub_underflow (test.toml) 16 14 12.50%
should_fail/arith_overflow/u8_add_overflow (test.toml) 38 36 5.26%
should_fail/arith_overflow/u8_mul_overflow (test.toml) 39 37 5.13%
should_fail/arith_overflow/u8_sub_underflow (test.toml) 17 15 11.76%
should_fail/vec_set_index_out_of_bounds (test.toml) 182 179 1.65%
should_fail/vec_swap_param1_out_of_bounds (test.toml) 237 234 1.27%
should_fail/vec_swap_param2_out_of_bounds (test.toml) 241 238 1.24%
should_pass/blanket_impl (test.toml) 142 51 64.08%
should_pass/blanket_impl_u16 (test.toml) 142 51 64.08%
should_pass/break_in_non_statement_positions (test.toml) 142 51 64.08%
should_pass/conditional_compilation/run (test.toml) 142 51 64.08%
should_pass/continue_in_non_statement_positions (test.toml) 1226 1135 7.42%
should_pass/empty_fields_in_storage_struct (test.toml)::test_read_write_bytes 11204 10275 8.29%
should_pass/empty_fields_in_storage_struct (test.toml)::test_read_write_map 7865 7107 9.64%
should_pass/empty_fields_in_storage_struct (test.toml)::test_read_write_vec 11474 10125 11.76%
should_pass/forc/dependency_package_field (test.toml) 137 46 66.42%
should_pass/language/abort_control_flow_good (test.toml) 29 27 6.90%
should_pass/language/addrof_intrinsic (test.toml) 609 595 2.30%
should_pass/language/aliased_imports (test.toml) 144 53 63.19%
should_pass/language/args_on_stack (test.toml) 5037 4946 1.81%
should_pass/language/array/array_basics (test.toml) 478 387 19.04%
should_pass/language/array/array_generics (test.toml) 146 55 62.33%
should_pass/language/asm_expr_basic (test.toml) 163 135 17.18%
should_pass/language/associated_const_abi (test.toml)::test 4546 4096 9.90%
should_pass/language/associated_const_abi_multiple (test.toml)::test 1510 1360 9.93%
should_pass/language/associated_const_impl (test.toml) 143 115 19.58%
should_pass/language/associated_const_impl_local_same_name (test.toml) 143 115 19.58%
should_pass/language/associated_const_impl_self (test.toml) 166 138 16.87%
should_pass/language/associated_const_in_decls_of_other_constants (test.toml)::test 529 452 14.56%
should_pass/language/associated_const_trait_impl_method (test.toml) 143 115 19.58%
should_pass/language/associated_const_trait_method (test.toml) 143 115 19.58%
should_pass/language/associated_type_and_associated_const (test.toml) 143 115 19.58%
should_pass/language/associated_type_ascription (test.toml) 143 115 19.58%
should_pass/language/associated_type_container (test.toml) 602 560 6.98%
should_pass/language/associated_type_container_in_library (test.toml) 602 560 6.98%
should_pass/language/associated_type_fully_qualified (test.toml) 195 152 22.05%
should_pass/language/associated_type_iterator (test.toml) 620 578 6.77%
should_pass/language/associated_type_method (test.toml) 143 115 19.58%
should_pass/language/associated_type_parameter (test.toml) 143 115 19.58%
should_pass/language/b256_bad_jumps (test.toml) 137 46 66.42%
should_pass/language/b256_bitwise_ops (test.toml) 1503 1412 6.05%
should_pass/language/b256_ops (test.toml) 1314 1223 6.93%
should_pass/language/basic_func_decl (test.toml) 136 45 66.91%
should_pass/language/binary_and_hex_literals (test.toml) 136 45 66.91%
should_pass/language/binop_intrinsics (test.toml) 142 51 64.08%
should_pass/language/bitwise_not (test.toml) 136 45 66.91%
should_pass/language/blanket_trait (test.toml) 136 45 66.91%
should_pass/language/bool_and_or (test.toml) 142 51 64.08%
should_pass/language/break_and_continue (test.toml) 1098 1007 8.29%
should_pass/language/builtin_type_method_call (test.toml) 142 51 64.08%
should_pass/language/chained_if_let (test.toml) 166 75 54.82%
should_pass/language/complex_ir_cfg (test.toml) 158 142 10.13%
should_pass/language/configurable_consts (test.toml) 2520 2506 0.56%
should_pass/language/configurable_tests (test.toml)::t 2805 2553 8.98%
should_pass/language/const_decl_and_use_in_library (test.toml) 142 51 64.08%
should_pass/language/const_decl_in_library (test.toml) 148 57 61.49%
should_pass/language/const_generics (test.toml) 318 305 4.09%
should_pass/language/const_inits (test.toml) 481 390 18.92%
should_pass/language/contract_caller_dynamic_address (test.toml) 379 301 20.58%
should_pass/language/contract_ret_intrinsic (test.toml)::test 1464 1314 10.25%
should_pass/language/dereferenced_projection_reassignment (test.toml) 238 124 47.90%
should_pass/language/diverging_exprs (test.toml) 291 200 31.27%
should_pass/language/dummy_method_issue (test.toml) 134 120 10.45%
should_pass/language/empty_method_initializer (test.toml) 242 151 37.60%
should_pass/language/enum_destructuring (test.toml) 156 65 58.33%
should_pass/language/enum_if_let (test.toml) 221 130 41.18%
should_pass/language/enum_if_let_large_type (test.toml) 201 110 45.27%
should_pass/language/enum_in_fn_decl (test.toml) 157 66 57.96%
should_pass/language/enum_init_fn_call (test.toml) 185 94 49.19%
should_pass/language/enum_instantiation (test.toml) 660 569 13.79%
should_pass/language/enum_padding (test.toml) 518 487 5.98%
should_pass/language/enum_type_inference (test.toml) 142 51 64.08%
should_pass/language/enum_variant_imports (test.toml) 161 70 56.52%
should_pass/language/eq_and_neq (test.toml) 675 584 13.48%
should_pass/language/eq_intrinsic (test.toml) 142 51 64.08%
should_pass/language/far_jumps/many_blobs (test.toml) 3670216 3670125 0.00%
should_pass/language/far_jumps/single_blob (test.toml) 142 51 64.08%
should_pass/language/for_loops (test.toml) 7882 7791 1.15%
should_pass/language/funcs_with_generic_types (test.toml) 136 45 66.91%
should_pass/language/function_return_type_unification (test.toml) 148 120 18.92%
should_pass/language/generic_functions (test.toml) 136 45 66.91%
should_pass/language/generic_impl_self (test.toml) 740 712 3.78%
should_pass/language/generic_impl_self_where (test.toml) 790 699 11.52%
should_pass/language/generic_inside_generic (test.toml) 163 72 55.83%
should_pass/language/generic_result_method (test.toml) 330 239 27.58%
should_pass/language/generic_struct (test.toml) 136 45 66.91%
should_pass/language/generic_struct_instantiation (test.toml) 137 46 66.42%
should_pass/language/generic_structs (test.toml) 136 45 66.91%
should_pass/language/generic_trait_constraints (test.toml) 187 62 66.84%
should_pass/language/generic_traits (test.toml) 415 324 21.93%
should_pass/language/generic_transpose (test.toml) 215 124 42.33%
should_pass/language/generic_tuple_trait (test.toml) 174 83 52.30%
should_pass/language/generic_type_inference (test.toml) 1349 1335 1.04%
should_pass/language/generic_where_in_impl_self (test.toml) 201 110 45.27%
should_pass/language/generic_where_in_impl_self2 (test.toml) 201 110 45.27%
should_pass/language/gtf_intrinsic (test.toml) 286 195 31.82%
should_pass/language/if_elseif_enum (test.toml) 388 360 7.22%
should_pass/language/if_implicit_unit (test.toml) 55 41 25.45%
should_pass/language/if_let_no_side_effects (test.toml) 154 63 59.09%
should_pass/language/impl_self_method (test.toml) 148 120 18.92%
should_pass/language/impl_self_method_order (test.toml) 148 120 18.92%
should_pass/language/implicit_casting (test.toml) 142 51 64.08%
should_pass/language/implicit_return (test.toml) 142 51 64.08%
should_pass/language/import_method_from_other_file (test.toml) 155 64 58.71%
should_pass/language/import_star_name_clash (test.toml) 1039 948 8.76%
should_pass/language/import_trailing_comma (test.toml) 143 52 63.64%
should_pass/language/import_with_different_callpaths (test.toml) 793 779 1.77%
should_pass/language/impure_ifs (test.toml) 429 338 21.21%
should_pass/language/inline_if_expr_const (test.toml) 55 41 25.45%
should_pass/language/insert_element_reg_reuse (test.toml) 1794 1703 5.07%
should_pass/language/integer_type_inference (test.toml) 547 533 2.56%
should_pass/language/intrinsics/dbg (test.toml) 1193 1102 7.63%
should_pass/language/intrinsics/dbg_release (test.toml) 9785 9694 0.93%
should_pass/language/intrinsics/transmute (test.toml) 402 388 3.48%
should_pass/language/is_prime (test.toml) 1047 956 8.69%
should_pass/language/is_reference_type (test.toml) 136 45 66.91%
should_pass/language/left_to_right_func_args_evaluation (test.toml) 149 58 61.07%
should_pass/language/local_impl_for_ord (test.toml) 136 45 66.91%
should_pass/language/logging (test.toml) 2126 1790 15.80%
should_pass/language/main_args/main_args_empty (test.encoding_v1.toml) 142 41 71.13%
should_pass/language/main_args/main_args_empty (test.toml) 19 18 5.26%
should_pass/language/main_args/main_args_generics (test.toml) 504 472 6.35%
should_pass/language/main_args/main_args_one_u64 (test.toml) 163 63 61.35%
should_pass/language/main_args/main_args_ref (test.toml) 172 72 58.14%
should_pass/language/main_args/main_args_ref_copy (test.toml) 181 81 55.25%
should_pass/language/main_args/main_args_ref_ref (test.toml) 226 125 44.69%
should_pass/language/main_args/main_args_two_u64 (test.toml) 172 72 58.14%
should_pass/language/main_args/main_args_various_types (test.toml) 1283 1130 11.93%
should_pass/language/main_returns_unit (test.toml) 55 41 25.45%
should_pass/language/many_stack_variables (test.toml) 151 60 60.26%
should_pass/language/match_expressions_all (test.toml) 3699 3685 0.38%
should_pass/language/match_expressions_constants (test.toml) 377 263 30.24%
should_pass/language/match_expressions_empty_enums (test.toml) 142 51 64.08%
should_pass/language/match_expressions_enums (test.toml) 2019 1928 4.51%
should_pass/language/match_expressions_explicit_rets (test.toml) 136 45 66.91%
should_pass/language/match_expressions_inside_generic_functions (test.toml) 170 79 53.53%
should_pass/language/match_expressions_mismatched (test.toml) 154 63 59.09%
should_pass/language/match_expressions_nested (test.toml) 649 621 4.31%
should_pass/language/match_expressions_rest (test.toml) 777 686 11.71%
should_pass/language/match_expressions_simple (test.toml) 156 65 58.33%
should_pass/language/match_expressions_structs (test.toml) 151 60 60.26%
should_pass/language/match_expressions_with_self (test.toml) 163 72 55.83%
should_pass/language/memcpy (test.toml) 266 175 34.21%
should_pass/language/method_indirect_inference (test.toml) 218 127 41.74%
should_pass/language/method_nested_type_args (test.toml) 55 41 25.45%
should_pass/language/method_on_empty_struct (test.toml) 137 46 66.42%
should_pass/language/method_on_primitives (test.toml) 99 85 14.14%
should_pass/language/method_unambiguous (test.toml) 186 95 48.92%
should_pass/language/modulo_uint_test (test.toml) 136 45 66.91%
should_pass/language/multi_impl_self (test.toml) 142 51 64.08%
should_pass/language/multi_item_import (test.toml) 136 45 66.91%
should_pass/language/mutable_and_initd (test.toml) 157 66 57.96%
should_pass/language/mutable_arrays (test.toml) 142 51 64.08%
should_pass/language/mutable_arrays_enum (test.toml) 153 62 59.48%
should_pass/language/mutable_arrays_multiple_nested (test.toml) 137 46 66.42%
should_pass/language/mutable_arrays_nested (test.toml) 137 46 66.42%
should_pass/language/mutable_arrays_struct (test.toml) 142 51 64.08%
should_pass/language/mutable_arrays_swap (test.toml) 142 51 64.08%
should_pass/language/name_resolution_after_monomorphization (test.toml) 148 57 61.49%
should_pass/language/nested_generics (test.toml) 136 45 66.91%
should_pass/language/nested_struct_destructuring (test.toml) 137 46 66.42%
should_pass/language/nested_structs (test.toml) 279 188 32.62%
should_pass/language/nested_while_and_if (test.toml) 194 103 46.91%
should_pass/language/new_allocator_test (test.toml) 204 113 44.61%
should_pass/language/non_literal_const_decl (test.toml) 142 51 64.08%
should_pass/language/numeric_constants (test.toml) 170 45 73.53%
should_pass/language/numeric_type_propagation (test.toml) 170 45 73.53%
should_pass/language/op_precedence (test.toml) 136 45 66.91%
should_pass/language/ops (test.toml) 155 64 58.71%
should_pass/language/out_of_order_decl (test.toml) 136 45 66.91%
should_pass/language/overlapped_trait_impls (test.toml) 276 185 32.97%
should_pass/language/prelude_access (test.toml) 55 41 25.45%
should_pass/language/prelude_access2 (test.toml) 55 41 25.45%
should_pass/language/primitive_type_argument (test.toml) 142 51 64.08%
should_pass/language/pusha_popa_multiple_defreg (test.toml)::incorrect_pusha_popa 451 409 9.31%
should_pass/language/raw_identifiers (test.error_type.toml)::test 903 738 18.27%
should_pass/language/raw_identifiers (test.toml)::test 903 738 18.27%
should_pass/language/raw_ptr/vec_ret (test.toml) 637 605 5.02%
should_pass/language/reassignment_operators (test.toml) 137 46 66.42%
should_pass/language/reassignment_rhs_lhs_evaluation_order (test.toml) 202 89 55.94%
should_pass/language/redundant_return (test.toml) 137 46 66.42%
should_pass/language/reexport/aliases (test.toml) 301 210 30.23%
should_pass/language/reexport/multiple_imports_of_same_reexport (test.toml) 329 238 27.66%
should_pass/language/reexport/reexport_paths (test.toml) 277 165 40.43%
should_pass/language/reexport/shadowing_in_reexporting_module (test.toml) 368 277 24.73%
should_pass/language/reexport/simple_glob_import (test.toml) 216 125 42.13%
should_pass/language/reexport/simple_item_import (test.toml) 216 125 42.13%
should_pass/language/reexport/visibility (test.toml) 216 125 42.13%
should_pass/language/ref_mutable_arrays (test.toml) 142 51 64.08%
should_pass/language/ref_mutable_arrays_inline (test.toml) 142 51 64.08%
should_pass/language/ref_mutable_fn_args_bool (test.toml) 136 45 66.91%
should_pass/language/ref_mutable_fn_args_call (test.toml) 142 51 64.08%
should_pass/language/ref_mutable_fn_args_struct (test.toml) 142 51 64.08%
should_pass/language/ref_mutable_fn_args_struct_assign (test.toml) 142 51 64.08%
should_pass/language/ref_mutable_fn_args_u32 (test.toml) 148 120 18.92%
should_pass/language/references/dereferencing_control_flow_expressions (test.toml) 652 540 17.18%
should_pass/language/references/impl_reference_types (test.toml) 2084 1902 8.73%
should_pass/language/references/mutability_of_references (test.toml) 233 142 39.06%
should_pass/language/references/mutability_of_references_memcpy_bug (test.toml)::test 1023 680 33.53%
should_pass/language/references/reassigning_via_references_in_aggregates (test.toml) 2435 2297 5.67%
should_pass/language/references/references_and_type_aliases (test.toml) 222 131 40.99%
should_pass/language/references/references_in_aggregates (test.toml) 3010 2919 3.02%
should_pass/language/references/references_in_asm_blocks (test.toml) 800 709 11.38%
should_pass/language/references/referencing_control_flow_expressions (test.toml) 841 750 10.82%
should_pass/language/references/referencing_function_parameters (test.toml) 2292 2201 3.97%
should_pass/language/references/referencing_function_parameters_simple (test.toml)::test_arg_addr 1268 464 63.41%
should_pass/language/references/referencing_parts_of_aggregates (test.toml) 2124 2033 4.28%
should_pass/language/references/referencing_references (test.toml) 417 326 21.82%
should_pass/language/references/type_unification_of_references (test.toml) 539 448 16.88%
should_pass/language/ret_small_string (test.toml) 169 138 18.34%
should_pass/language/ret_string_in_struct (test.toml) 185 153 17.30%
should_pass/language/retd_b256 (test.toml) 188 74 60.64%
should_pass/language/retd_small_array (test.toml) 196 164 16.33%
should_pass/language/retd_struct (test.toml) 362 331 8.56%
should_pass/language/retd_zero_len_array (test.toml) 107 75 29.91%
should_pass/language/revert_in_first_if_branch (test.toml) 28 25 10.71%
should_pass/language/same_const_name (test.toml) 55 41 25.45%
should_pass/language/self_impl_reassignment (test.toml) 276 185 32.97%
should_pass/language/shadowing/shadowed_glob_imports (test.toml) 178 87 51.12%
should_pass/language/shadowing/shadowed_prelude_imports (test.toml) 146 55 62.33%
should_pass/language/size_of (test.toml) 137 46 66.42%
should_pass/language/slice/slice_intrinsics (test.toml) 123848 104394 15.71%
should_pass/language/slice/slice_script (test.toml) 231 200 13.42%
should_pass/language/storage_slot_sized (test.toml)::test_store_something 7736 4706 39.17%
should_pass/language/string_slice/string_slice_features (test.toml) 163 72 55.83%
should_pass/language/string_slice/string_slice_script (test.toml) 264 232 12.12%
should_pass/language/struct_destructuring (test.toml) 142 51 64.08%
should_pass/language/struct_field_access (test.toml) 142 51 64.08%
should_pass/language/struct_field_reassignment (test.toml) 137 46 66.42%
should_pass/language/struct_instantiation (test.toml) 524 433 17.37%
should_pass/language/supertraits (test.toml) 1146 1055 7.94%
should_pass/language/supertraits_with_trait_methods (test.toml) 151 60 60.26%
should_pass/language/totalord (test.toml) 783 658 15.96%
should_pass/language/trait_constraint_param_order (test.toml) 136 45 66.91%
should_pass/language/trait_generic_override (test.toml) 136 45 66.91%
should_pass/language/trait_import_with_star (test.toml) 55 41 25.45%
should_pass/language/trait_inference (test.toml) 182 57 68.68%
should_pass/language/trait_method_ascription_disambiguate (test.toml) 136 45 66.91%
should_pass/language/trait_method_generic_qualified (test.toml) 136 45 66.91%
should_pass/language/trait_method_qualified (test.toml) 136 45 66.91%
should_pass/language/trait_nested (test.toml) 189 64 66.14%
should_pass/language/tuple_access (test.toml) 176 85 51.70%
should_pass/language/tuple_desugaring (test.toml) 155 127 18.06%
should_pass/language/tuple_field_reassignment (test.toml) 168 77 54.17%
should_pass/language/tuple_in_struct (test.toml) 199 108 45.73%
should_pass/language/tuple_indexing (test.toml) 143 115 19.58%
should_pass/language/tuple_single_element (test.toml) 149 58 61.07%
should_pass/language/tuple_trait (test.toml) 152 61 59.87%
should_pass/language/tuple_types (test.toml) 148 120 18.92%
should_pass/language/type_alias (test.toml) 1064 1050 1.32%
should_pass/language/type_inference_propagation_of_type_constraints (test.toml) 268 237 11.57%
should_pass/language/typeinfo_custom_callpath (test.toml) 67 53 20.90%
should_pass/language/typeinfo_custom_callpath2 (test.toml) 67 53 20.90%
should_pass/language/typeinfo_custom_callpath_with_import (test.toml) 69 55 20.29%
should_pass/language/u256/u256_abi (test.toml) 392 164 58.16%
should_pass/language/unary_not_basic (test.toml) 136 45 66.91%
should_pass/language/unary_not_basic_2 (test.toml) 136 45 66.91%
should_pass/language/unify_never (test.toml) 142 51 64.08%
should_pass/language/unit_type_variants (test.toml) 224 53 76.34%
should_pass/language/use_absolute_path (test.toml) 137 46 66.42%
should_pass/language/use_full_path_names (test.toml) 143 115 19.58%
should_pass/language/where_clause_enums (test.toml) 280 189 32.50%
should_pass/language/where_clause_functions (test.toml) 453 362 20.09%
should_pass/language/where_clause_generic_traits (test.toml) 55 41 25.45%
should_pass/language/where_clause_generic_tuple (test.toml) 142 51 64.08%
should_pass/language/where_clause_impls (test.toml) 158 67 57.59%
should_pass/language/where_clause_methods (test.toml) 541 450 16.82%
should_pass/language/where_clause_structs (test.toml) 254 163 35.83%
should_pass/language/where_clause_traits (test.toml) 136 45 66.91%
should_pass/language/while_loops (test.toml) 389 298 23.39%
should_pass/language/zero_field_types (test.toml) 142 51 64.08%
should_pass/return_in_non_statement_positions (test.toml) 142 51 64.08%
should_pass/return_into (test.toml) 412 321 22.09%
should_pass/stdlib/address_test (test.toml) 1175 1084 7.74%
should_pass/stdlib/alloc_test (test.toml) 265 174 34.34%
should_pass/stdlib/assert_eq (test.toml) 1108 983 11.28%
should_pass/stdlib/assert_eq_revert (test.toml) 303 152 49.83%
should_pass/stdlib/assert_ne (test.toml) 1045 921 11.87%
should_pass/stdlib/assert_ne_revert (test.toml) 302 151 50.00%
should_pass/stdlib/assert_test (test.toml) 136 45 66.91%
should_pass/stdlib/asset_id_into_bytes (test.toml) 173 82 52.60%
should_pass/stdlib/b512_struct_alignment (test.toml) 202 111 45.05%
should_pass/stdlib/b512_test (test.toml) 996 905 9.14%
should_pass/stdlib/block_height (test.toml) 149 58 61.07%
should_pass/stdlib/chess (test.toml) 231 140 39.39%
should_pass/stdlib/contract_id_test (test.toml) 174 83 52.30%
should_pass/stdlib/contract_id_type (test.toml) 169 78 53.85%
should_pass/stdlib/eq_generic (test.toml) 154 140 9.09%
should_pass/stdlib/ge_test (test.toml) 136 45 66.91%
should_pass/stdlib/generic_empty_struct_with_constraint (test.toml) 55 41 25.45%
should_pass/stdlib/identity_eq (test.toml) 1274 1183 7.14%
should_pass/stdlib/if_type_revert (test.toml) 26 24 7.69%
should_pass/stdlib/intrinsics (test.toml) 136 45 66.91%
should_pass/stdlib/iterator (test.toml) 984 859 12.70%
should_pass/stdlib/option_eq (test.toml) 5291 5200 1.72%
should_pass/stdlib/raw_ptr (test.toml) 4316 4225 2.11%
should_pass/stdlib/raw_slice (test.toml) 424 392 7.55%
should_pass/stdlib/require (test.toml) 182 165 9.34%
should_pass/stdlib/storage_vec_insert (test.toml)::test_test_function 2903 2754 5.13%
should_pass/stdlib/u128_div_test (test.toml) 34133 34042 0.27%
should_pass/stdlib/u128_log_test (test.toml) 11809 11718 0.77%
should_pass/stdlib/u128_mul_test (test.toml) 453 362 20.09%
should_pass/stdlib/u128_root_test (test.toml) 7634 7543 1.19%
should_pass/stdlib/u128_test (test.toml) 2177 2086 4.18%
should_pass/stdlib/vec (test.toml) 65253 65162 0.14%
should_pass/stdlib/vec_swap (test.toml) 14862 14771 0.61%
should_pass/storage_element_key_modification (test.toml)::test_storage_key_address 943 863 8.48%
should_pass/storage_element_key_modification (test.toml)::test_storage_key_modification 465 385 17.20%
should_pass/storage_slot_key_calculation (test.toml)::test 2785 2708 2.76%
should_pass/superabi_contract_calls (test.toml)::tests 1310 1004 23.36%
should_pass/superabi_supertrait_same_methods (test.toml)::tests 634 483 23.82%
should_pass/test_abis/abi_impl_methods_callable (test.toml)::tests 599 449 25.04%
should_pass/test_abis/contract_abi-auto_impl (test.toml)::tests 599 449 25.04%
should_pass/test_contracts/basic_storage (test.toml)::collect_basic_storage_contract_gas_usages 37164 35842 3.56%
should_pass/test_contracts/increment_contract (test.toml)::collect_incrementor_contract_gas_usages 2006 1628 18.84%
should_pass/test_contracts/return_struct (test.toml)::collect_my_contract_gas_usages 1076 1000 7.06%
should_pass/test_contracts/storage_access_contract (test.toml)::collect_storage_access_contract_gas_usages 49894 44857 10.10%
should_pass/test_contracts/storage_enum_contract (test.toml)::collect_storage_enum_contract_gas_usages 22372 22213 0.71%
should_pass/unit_tests/aggr_indexing (test.toml)::test1 4192 2082 50.33%
should_pass/unit_tests/contract-multi-contract-calls (test.toml)::test_contract_2_call 631 480 23.93%
should_pass/unit_tests/contract-multi-contract-calls (test.toml)::test_contract_call 634 483 23.82%
should_pass/unit_tests/contract-multi-contract-calls (test.toml)::test_contract_multi_call 1246 944 24.24%
should_pass/unit_tests/contract_multi_test (test.toml)::test_bar 175 68 61.14%
should_pass/unit_tests/contract_multi_test (test.toml)::test_fail 635 484 23.78%
should_pass/unit_tests/contract_multi_test (test.toml)::test_success 633 482 23.85%
should_pass/unit_tests/contract_with_nested_libs (test.toml)::log_test 180 184 -2.22%
should_pass/unit_tests/contract_with_nested_libs (test.toml)::log_test_inner 180 184 -2.22%
should_pass/unit_tests/contract_with_nested_libs (test.toml)::log_test_inner2 173 67 61.27%
should_pass/unit_tests/lib_log_decode (test.toml)::math_u16_overflow_mul 214 218 -1.87%
should_pass/unit_tests/lib_log_decode (test.toml)::math_u32_overflow_mul 214 218 -1.87%
should_pass/unit_tests/lib_log_decode (test.toml)::test_fn 362 146 59.67%
should_pass/unit_tests/lib_multi_test (test.toml)::test_gt 156 82 47.44%
should_pass/unit_tests/lib_multi_test (test.toml)::test_local 156 82 47.44%
should_pass/unit_tests/nested_libs (test.toml)::log_test 179 183 -2.23%
should_pass/unit_tests/nested_libs (test.toml)::log_test_inner 179 183 -2.23%
should_pass/unit_tests/nested_libs (test.toml)::log_test_inner2 172 66 61.63%
should_pass/unit_tests/regalloc_spill (test.toml) 181 90 50.28%
should_pass/unit_tests/script-contract-calls (test.toml)::test_contract_call 563 448 20.43%
should_pass/unit_tests/script_log_decode (test.toml)::test_fn 294 146 50.34%
should_pass/unit_tests/script_with_nested_libs (test.toml)::log_test 179 183 -2.23%
should_pass/unit_tests/script_with_nested_libs (test.toml)::log_test_inner 179 183 -2.23%
should_pass/unit_tests/script_with_nested_libs (test.toml)::log_test_inner2 172 66 61.63%
should_pass/unit_tests/workspace_test (test.toml)::test_bar 175 68 61.14%
should_pass/unit_tests/workspace_test (test.toml)::test_fail 635 484 23.78%
should_pass/unit_tests/workspace_test (test.toml)::test_gt 156 82 47.44%
should_pass/unit_tests/workspace_test (test.toml)::test_local 156 82 47.44%
should_pass/unit_tests/workspace_test (test.toml)::test_success 634 483 23.82%
Improvements Regressions
Count 346 8
Average 34.98% -2.14%
Median 28.79% -2.23%
Max 76.34% -2.23%
Min 0.14% -1.87%
Test Before After Percentage
assert_inline_tests::assert_assert_eq 1576 1529 2.98%
assert_inline_tests::assert_assert_ne 1630 1585 2.76%
assert_inline_tests::revert_assert_assert_eq 1480 1117 24.53%
assert_inline_tests::revert_assert_assert_ne 1488 1115 25.07%
asset_id_contract_tests::asset_id_default 10994 10696 2.71%
bytes_inline_tests::bytes_append 5789 5787 0.03%
bytes_inline_tests::bytes_split_at_twice 1321 1319 0.15%
codec_implemented_tests::test_logging 51837 23126 55.39%
contract_id_contract_tests::contract_id_this 10787 10488 2.77%
crypto_ed25519_inline_tests::ed25519_codec 6125 640 89.55%
crypto_secp256k1_inline_tests::secp256k1_codec 6125 640 89.55%
crypto_secp256r1_inline_tests::secp256r1_codec 6125 640 89.55%
crypto_signature_inline_tests::signature_as_ed25519 6840 6841 -0.01%
crypto_signature_inline_tests::signature_as_secp256r1 6836 6837 -0.01%
crypto_signature_inline_tests::signature_codec 7748 654 91.56%
crypto_signature_inline_tests::signature_verify 43208 43205 0.01%
hash_inline_tests::hash_address 7223 7211 0.17%
hash_inline_tests::hash_asset_id 7223 7211 0.17%
hash_inline_tests::hash_b256 7171 7159 0.17%
hash_inline_tests::hash_call_params 6930 6924 0.09%
hash_inline_tests::hash_contract_id 7223 7211 0.17%
hash_inline_tests::hash_evm_address 7407 7395 0.16%
hash_inline_tests::hash_identity 17767 17743 0.14%
hash_inline_tests::hash_point2d 9311 9305 0.06%
hash_inline_tests::hash_signature 20238 20220 0.09%
hash_inline_tests::hash_str 10508 10505 0.03%
hash_inline_tests::hash_tuple_4 8499 8497 0.02%
hash_inline_tests::hash_u128 5160 5154 0.12%
hash_inline_tests::hash_user_defined_type 42799 42787 0.03%
storage_vec_iter_tests::empty_vec_next_returns_none 50784 50644 0.28%
storage_vec_iter_tests::storage_vec_field_for_loop_iteration 1574940 1573470 0.09%
storage_vec_iter_tests::storage_vec_field_nested_for_loop_iteration 11570183 11559644 0.09%
storage_vec_iter_tests::vec_with_elements_for_loop_iteration 24015966 24011576 0.02%
storage_vec_iter_tests::vec_with_elements_next_returns_element 24016001 24011606 0.02%
u128_inline_tests::parity_u128_log_with_ruint 1851450 1850539 0.05%
u128_inline_tests::revert_u128_zero_root 232 230 0.86%
u128_inline_tests::revert_u128_zero_root_overflow_disabled 238 236 0.84%
u128_inline_tests::u128_log 24393 24391 0.01%
u128_inline_tests::u128_pow 8902 8900 0.02%
u128_inline_tests::u128_root 13907 13872 0.25%
u128_inline_tests::u128_zero_root_unsafe_math 452 450 0.44%
Improvements Regressions
Count 39 2
Average 12.33% -0.01%
Median 0.17% -0.01%
Max 91.56% -0.01%
Min 0.01% -0.01%

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

Adds __runtime_mem_id/__encoding_mem_id intrinsics and uses them to trivially encode and return values, switching entrypoints to direct returns to reduce bytecode and gas; updates parser/docs and aligns tests/snapshots.

  • Compiler/Core:
    • Add intrinsics __runtime_mem_id and __encoding_mem_id with AST, type-checking, const-eval, and IR codegen support.
    • Implement memory-representation modeling and hashing (get_memory_representation, get_memory_id, get_encoding_representation, get_encoding_id).
  • ABI/Codegen:
    • Change generated script entry to return never and use __contract_ret for returns.
    • Update auto-impl to use encode_and_return instead of building slices.
  • Std Library:
    • Optimize encode<T> with trivial-encoding fast path (allocate+memcpy) and add encode_and_return<T>.
  • IR/Parser:
    • Add support for never type.
  • Docs:
    • Document new intrinsics and their constraints in the Sway book.
  • Tests/Snapshots:
    • Update expected contract IDs, raw log fields, bytecode/gas snapshots; prefer --release in several tests for consistency/perf.

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

@xunilrj xunilrj self-assigned this Nov 7, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 7, 2025

CodSpeed Performance Report

Merging #7488 will not alter performance

Comparing xunilrj/trivial-encode-1 (cfe3214) with master (fb10b2f)

Summary

✅ 25 untouched

@xunilrj xunilrj changed the title Implement the concept of trivially encoded types Trivially encoded types Nov 7, 2025
@xunilrj xunilrj force-pushed the xunilrj/trivial-encode-1 branch from d8b0301 to b5bed35 Compare November 10, 2025 13:10
@xunilrj xunilrj marked this pull request as ready for review November 10, 2025 23:44
@xunilrj xunilrj requested review from a team as code owners November 10, 2025 23:44
@xunilrj xunilrj requested a review from a team as a code owner November 13, 2025 11:29
@xunilrj xunilrj force-pushed the xunilrj/trivial-encode-1 branch from 9c8145a to 7e43c6a Compare November 13, 2025 16:35
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