sway/sway-lib-std
Igor Rončević bd3813606a
Optimize vec and bytes modules for bytecode size and gas usage (#7162)
## Description

This PR mostly optimizes implementations of several traits, like
`AbiEncode`, `Iterator`, `PartialEq`, and methods for `Vec` and `Bytes`.

One typical common optimization was replacing the internal calls to the
public `get` method with a direct element read. Each call to the public
`get` in internal implementations of those traits had an overhead of
double-checking the boundary and unnecessary wrapping and unwrapping the
fetched value.

Also, whenever possible `mcp` instruction was used for converting
between `Bytes` and `Vec<u8>`, instead of costly iteration and copying
of each element individually.

The PR also removes calls to getters in internal implementations to
mitigate the issue of inlining of getters explained in #7152.

The bytecode size of the sample script that uses optimized code was
**reduced for 37.58%, from 2384 bytes to 1488**:

```
script;

use std::bytes::Bytes;

fn main() {
    let vec: Vec<u8> = Vec::new();
    let bytes: Bytes = vec.into();
    let vec: Vec<u8> = bytes.into();

    let _ = vec == vec;
    for _ in vec.iter() { }

    let _ = bytes == bytes;
    for _ in bytes.iter() { }
}

```

Out of 822 `in_language_tests`, **303 tests got reduced gas costs, up to
57.75% and in average 18.97%**.

<details>
<summary>Click to see improvements in gas costs</summary>

| Test | Before | After | Percentage |
|------|--------|-------|------------|
| address_inline_tests::address_hash | 1648 | 1468 | 10.92% |
| address_inline_tests::address_try_from_bytes | 1968 | 1714 | 12.91% |
| address_inline_tests::address_try_into_bytes | 11555 | 9566 | 17.21% |
| asset_id_inline_tests::asset_id_hasher | 1240 | 1105 | 10.89% |
| asset_id_inline_tests::asset_id_try_from_bytes | 1968 | 1714 | 12.91%
|
| asset_id_inline_tests::asset_id_try_into_bytes | 11555 | 9566 | 17.21%
|
| b512_inline_tests::b512_into_bytes | 22732 | 18823 | 17.20% |
| b512_inline_tests::b512_try_from_bytes | 2372 | 2127 | 10.33% |
| bytes_conversions_b256_inline_tests::b256_from_be_bytes | 4401 | 3069
| 30.27% |
| bytes_conversions_b256_inline_tests::b256_from_le_bytes | 5681 | 3810
| 32.93% |
| bytes_conversions_b256_inline_tests::b256_to_be_bytes | 5337 | 4697 |
11.99% |
| bytes_conversions_b256_inline_tests::b256_to_le_bytes | 6951 | 5879 |
15.42% |
| bytes_conversions_u16_inline_tests::u16_from_be_bytes | 285 | 218 |
23.51% |
| bytes_conversions_u16_inline_tests::u16_from_le_bytes | 285 | 218 |
23.51% |
| bytes_conversions_u16_inline_tests::u16_to_be_bytes | 365 | 325 |
10.96% |
| bytes_conversions_u16_inline_tests::u16_to_le_bytes | 366 | 326 |
10.93% |
| bytes_conversions_u256_inline_tests::u256_from_be_bytes | 4413 | 3081
| 30.18% |
| bytes_conversions_u256_inline_tests::u256_from_le_bytes | 5681 | 3810
| 32.93% |
| bytes_conversions_u256_inline_tests::u256_to_be_bytes | 5376 | 4736 |
11.90% |
| bytes_conversions_u256_inline_tests::u256_to_le_bytes | 6951 | 5879 |
15.42% |
| bytes_conversions_u32_inline_tests::u32_from_be_bytes | 435 | 325 |
25.29% |
| bytes_conversions_u32_inline_tests::u32_from_le_bytes | 435 | 325 |
25.29% |
| bytes_conversions_u32_inline_tests::u32_to_be_bytes | 594 | 514 |
13.47% |
| bytes_conversions_u32_inline_tests::u32_to_le_bytes | 594 | 514 |
13.47% |
| bytes_conversions_u64_inline_tests::u64_from_be_bytes | 713 | 518 |
27.35% |
| bytes_conversions_u64_inline_tests::u64_from_le_bytes | 713 | 518 |
27.35% |
| bytes_conversions_u64_inline_tests::u64_to_be_bytes | 1052 | 892 |
15.21% |
| bytes_conversions_u64_inline_tests::u64_to_le_bytes | 1052 | 892 |
15.21% |
| bytes_inline_tests::bytes_append | 3619 | 2900 | 19.87% |
| bytes_inline_tests::bytes_append_empty | 1243 | 990 | 20.35% |
| bytes_inline_tests::bytes_append_empty_self | 151 | 130 | 13.91% |
| bytes_inline_tests::bytes_append_self | 2117 | 1667 | 21.26% |
| bytes_inline_tests::bytes_append_to_empty | 1662 | 1349 | 18.83% |
| bytes_inline_tests::bytes_as_raw_slice | 716 | 572 | 20.11% |
| bytes_inline_tests::bytes_b256_into | 2880 | 1536 | 46.67% |
| bytes_inline_tests::bytes_b256_try_from | 6684 | 3812 | 42.97% |
| bytes_inline_tests::bytes_bigger_than_3064 | 249483 | 120753 | 51.60%
|
| bytes_inline_tests::bytes_buffer_ownership | 1335 | 1321 | 1.05% |
| bytes_inline_tests::bytes_capacity | 1010 | 662 | 34.46% |
| bytes_inline_tests::bytes_clear | 691 | 542 | 21.56% |
| bytes_inline_tests::bytes_clear_empty_bytes | 273 | 227 | 16.85% |
| bytes_inline_tests::bytes_clear_twice | 926 | 712 | 23.11% |
| bytes_inline_tests::bytes_clone | 1654 | 1237 | 25.21% |
| bytes_inline_tests::bytes_eq | 1261 | 925 | 26.65% |
| bytes_inline_tests::bytes_for_loop | 2110 | 1393 | 33.98% |
| bytes_inline_tests::bytes_from_b256 | 2875 | 1531 | 46.75% |
| bytes_inline_tests::bytes_from_raw_slice | 332 | 309 | 6.93% |
| bytes_inline_tests::bytes_from_vec_u8 | 2135 | 1092 | 48.85% |
| bytes_inline_tests::bytes_get | 1476 | 1221 | 17.28% |
| bytes_inline_tests::bytes_insert | 1332 | 1037 | 22.15% |
| bytes_inline_tests::bytes_insert_back | 1341 | 1046 | 22.00% |
| bytes_inline_tests::bytes_insert_before_back | 1348 | 1053 | 21.88% |
| bytes_inline_tests::bytes_insert_front | 1339 | 1044 | 22.03% |
| bytes_inline_tests::bytes_insert_twice | 2242 | 1781 | 20.56% |
| bytes_inline_tests::bytes_into_raw_slice | 716 | 572 | 20.11% |
| bytes_inline_tests::bytes_into_vec_u8 | 1687 | 1020 | 39.54% |
| bytes_inline_tests::bytes_is_empty | 722 | 596 | 17.45% |
| bytes_inline_tests::bytes_iter | 2194 | 1461 | 33.41% |
| bytes_inline_tests::bytes_len | 1047 | 753 | 28.08% |
| bytes_inline_tests::bytes_ne | 1233 | 914 | 25.87% |
| bytes_inline_tests::bytes_new | 145 | 121 | 16.55% |
| bytes_inline_tests::bytes_pop | 2856 | 2029 | 28.96% |
| bytes_inline_tests::bytes_ptr | 815 | 620 | 23.93% |
| bytes_inline_tests::bytes_push | 1841 | 1233 | 33.03% |
| bytes_inline_tests::bytes_raw_slice_from | 716 | 572 | 20.11% |
| bytes_inline_tests::bytes_raw_slice_into | 332 | 309 | 6.93% |
| bytes_inline_tests::bytes_remove | 1335 | 1038 | 22.25% |
| bytes_inline_tests::bytes_remove_all | 932 | 714 | 23.39% |
| bytes_inline_tests::bytes_remove_end | 1057 | 845 | 20.06% |
| bytes_inline_tests::bytes_remove_front | 1052 | 840 | 20.15% |
| bytes_inline_tests::bytes_resize | 4687 | 4081 | 12.93% |
| bytes_inline_tests::bytes_set | 1133 | 877 | 22.59% |
| bytes_inline_tests::bytes_set_back | 1156 | 900 | 22.15% |
| bytes_inline_tests::bytes_set_front | 1133 | 877 | 22.59% |
| bytes_inline_tests::bytes_set_twice | 1647 | 1284 | 22.04% |
| bytes_inline_tests::bytes_splice | 1883 | 1431 | 24.00% |
| bytes_inline_tests::bytes_splice_empty_range | 1365 | 1077 | 21.10% |
| bytes_inline_tests::bytes_splice_end | 1385 | 1097 | 20.79% |
| bytes_inline_tests::bytes_splice_entire_range | 1368 | 1121 | 18.06% |
| bytes_inline_tests::bytes_splice_front | 1363 | 1076 | 21.06% |
| bytes_inline_tests::bytes_splice_replace_empty_bytes | 1363 | 1076 |
21.06% |
| bytes_inline_tests::bytes_splice_replace_larger | 2264 | 1709 | 24.51%
|
| bytes_inline_tests::bytes_splice_replace_overlap | 1950 | 1497 |
23.23% |
| bytes_inline_tests::bytes_splice_replace_same_length | 1948 | 1495 |
23.25% |
| bytes_inline_tests::bytes_splice_replace_smaller | 2198 | 1643 |
25.25% |
| bytes_inline_tests::bytes_split_at | 1620 | 1174 | 27.53% |
| bytes_inline_tests::bytes_split_at_end | 1423 | 1110 | 22.00% |
| bytes_inline_tests::bytes_split_at_front | 1404 | 1088 | 22.51% |
| bytes_inline_tests::bytes_split_at_twice | 1619 | 1087 | 32.86% |
| bytes_inline_tests::bytes_swap | 1352 | 1034 | 23.52% |
| bytes_inline_tests::bytes_swap_end | 1069 | 836 | 21.80% |
| bytes_inline_tests::bytes_swap_front | 1068 | 835 | 21.82% |
| bytes_inline_tests::bytes_swap_front_with_end | 1069 | 836 | 21.80% |
| bytes_inline_tests::bytes_swap_twice | 2003 | 1558 | 22.22% |
| bytes_inline_tests::bytes_test_packing | 1325 | 840 | 36.60% |
| bytes_inline_tests::bytes_test_u8_limits | 1639 | 1244 | 24.10% |
| bytes_inline_tests::bytes_try_into_b256 | 6595 | 3803 | 42.34% |
| bytes_inline_tests::bytes_vec_u8_from | 1661 | 995 | 40.10% |
| bytes_inline_tests::bytes_vec_u8_into | 2135 | 1092 | 48.85% |
| bytes_inline_tests::bytes_with_capacity | 856 | 741 | 13.43% |
| bytes_inline_tests::revert_bytes_insert_out_of_bounds | 612 | 484 |
20.92% |
| bytes_inline_tests::revert_bytes_remove_out_of_bounds | 585 | 456 |
22.05% |
| bytes_inline_tests::revert_bytes_set_out_of_bounds | 587 | 457 |
22.15% |
| bytes_inline_tests::revert_bytes_splice_end_out_of_bounds | 696 | 570
| 18.10% |
| bytes_inline_tests::revert_bytes_splice_start_greater_than_end | 649 |
523 | 19.41% |
| bytes_inline_tests::revert_bytes_split_at_out_of_bounds | 606 | 483 |
20.30% |
| bytes_inline_tests::revert_bytes_swap_element_1_out_of_bounds | 587 |
457 | 22.15% |
| bytes_inline_tests::revert_bytes_swap_element_2_out_of_bounds | 591 |
461 | 22.00% |
| bytes_inline_tests::test_encode_decode | 588 | 619 | -5.27% |
| codec_implemented_tests::test_logging | 27189 | 27185 | 0.01% |
| contract_id_inline_tests::contract_id_hash | 1648 | 1468 | 10.92% |
| contract_id_inline_tests::contract_id_try_from_bytes | 1968 | 1714 |
12.91% |
| contract_id_inline_tests::contract_id_try_into_bytes | 11555 | 9566 |
17.21% |
| crypto_ed25519_inline_tests::ed25519_hash | 1926 | 1882 | 2.28% |
| crypto_ed25519_inline_tests::ed25519_into_bytes | 26703 | 22863 |
14.38% |
| crypto_ed25519_inline_tests::ed25519_try_from_bytes | 87153 | 86958 |
0.22% |
| crypto_ed25519_inline_tests::ed25519__verify | 13047 | 12823 | 1.72% |
| crypto_message_inline_tests::message_bytes | 996 | 787 | 20.98% |
| crypto_message_inline_tests::message_eq | 38026 | 32008 | 15.83% |
| crypto_message_inline_tests::message_from_b256 | 19321 | 17401 | 9.94%
|
| crypto_message_inline_tests::message_from_bytes | 3134 | 2516 | 19.72%
|
| crypto_message_inline_tests::message_hash | 606 | 518 | 14.52% |
| crypto_message_inline_tests::message_new | 209 | 186 | 11.00% |
| crypto_message_inline_tests::message_try_into_b256 | 1053 | 942 |
10.54% |
| crypto_point2d_inline_tests::point2d_b256_array_try_from | 1498 | 1360
| 9.21% |
| crypto_point2d_inline_tests::point2d_b256_tuple_try_from | 1457 | 1319
| 9.47% |
| crypto_point2d_inline_tests::point2d_from_b256_array | 2591 | 2213 |
14.59% |
| crypto_point2d_inline_tests::point2d_from_b256_tuple | 2561 | 2183 |
14.76% |
| crypto_point2d_inline_tests::point2d_from_u256_array | 2642 | 2264 |
14.31% |
| crypto_point2d_inline_tests::point2d_from_u256_tuple | 2612 | 2234 |
14.47% |
| crypto_point2d_inline_tests::point2d_from_u8_array | 19636 | 11386 |
42.01% |
| crypto_point2d_inline_tests::point2d_is_zero | 44315 | 36635 | 17.33%
|
| crypto_point2d_inline_tests::point2d_min | 683 | 603 | 11.71% |
| crypto_point2d_inline_tests::point2d_u256_array_try_from | 1549 | 1411
| 8.91% |
| crypto_point2d_inline_tests::point2d_u256_tuple_try_from | 1508 | 1370
| 9.15% |
| crypto_point2d_inline_tests::point2d_x | 887 | 818 | 7.78% |
| crypto_point2d_inline_tests::point2d_y | 890 | 821 | 7.75% |
| crypto_point2d_inline_tests::point2d_zero | 683 | 603 | 11.71% |
| crypto_public_key_inline_tests::public_key_bytes | 638 | 555 | 13.01%
|
| crypto_public_key_inline_tests::public_key_eq | 96007 | 80487 | 16.17%
|
| crypto_public_key_inline_tests::public_key_from_b256 | 19281 | 17361 |
9.96% |
| crypto_public_key_inline_tests::public_key_from_b256_tuple | 38468 |
34628 | 9.98% |
| crypto_public_key_inline_tests::public_key_from_b512 | 38704 | 34864 |
9.92% |
| crypto_public_key_inline_tests::public_key_hash | 606 | 518 | 14.52% |
| crypto_public_key_inline_tests::public_key_is_zero | 13201 | 11281 |
14.54% |
| crypto_public_key_inline_tests::public_key_new | 209 | 186 | 11.00% |
| crypto_public_key_inline_tests::public_key_try_from_bytes | 59073 |
53187 | 9.96% |
| crypto_public_key_inline_tests::public_key_try_into_b256 | 1139 | 1070
| 6.06% |
| crypto_public_key_inline_tests::public_key_try_into_b256_tuple | 1606
| 1468 | 8.59% |
| crypto_public_key_inline_tests::public_key_try_into_b512 | 2043 | 1905
| 6.75% |
| crypto_scalar_inline_tests::scalar_b256_try_from | 878 | 809 | 7.86% |
| crypto_scalar_inline_tests::scalar_bytes | 633 | 564 | 10.90% |
| crypto_scalar_inline_tests::scalar_from_b256 | 1282 | 1093 | 14.74% |
| crypto_scalar_inline_tests::scalar_from_u256 | 1306 | 1117 | 14.47% |
| crypto_scalar_inline_tests::scalar_from_u8_array | 6978 | 2949 |
57.74% |
| crypto_scalar_inline_tests::scalar_is_zero | 22466 | 18626 | 17.09% |
| crypto_scalar_inline_tests::scalar_min | 355 | 315 | 11.27% |
| crypto_scalar_inline_tests::scalar_new | 181 | 158 | 12.71% |
| crypto_scalar_inline_tests::scalar_u256_try_from | 878 | 809 | 7.86% |
| crypto_scalar_inline_tests::scalar_zero | 355 | 315 | 11.27% |
| crypto_secp256k1_inline_tests::secp256k1_address | 6123 | 6033 | 1.47%
|
| crypto_secp256k1_inline_tests::secp256k1_evm_address | 6111 | 6021 |
1.47% |
| crypto_secp256k1_inline_tests::secp256k1_hash | 1926 | 1882 | 2.28% |
| crypto_secp256k1_inline_tests::secp256k1_into_bytes | 26703 | 22863 |
14.38% |
| crypto_secp256k1_inline_tests::secp256k1_recover | 21047 | 18441 |
12.38% |
| crypto_secp256k1_inline_tests::secp256k1_try_from_bytes | 87153 |
86958 | 0.22% |
| crypto_secp256k1_inline_tests::secp256k1_verify | 23267 | 20661 |
11.20% |
| crypto_secp256k1_inline_tests::secp256k1_verify_address | 6663 | 6529
| 2.01% |
| crypto_secp256k1_inline_tests::secp256k1_verify_evm_address | 6632 |
6498 | 2.02% |
| crypto_secp256r1_inline_tests::secp256r1_address | 10221 | 10131 |
0.88% |
| crypto_secp256r1_inline_tests::secp256r1_evm_address | 10209 | 10119 |
0.88% |
| crypto_secp256r1_inline_tests::secp256r1_hash | 1926 | 1882 | 2.28% |
| crypto_secp256r1_inline_tests::secp256r1_into_bytes | 26703 | 22863 |
14.38% |
| crypto_secp256r1_inline_tests::secp256r1_recover | 25145 | 22539 |
10.36% |
| crypto_secp256r1_inline_tests::secp256r1_try_from_bytes | 87153 |
86958 | 0.22% |
| crypto_secp256r1_inline_tests::secp256r1_verify | 27941 | 25295 |
9.47% |
| crypto_secp256r1_inline_tests::secp256r1_verify_address | 10761 |
10627 | 1.25% |
| crypto_secp256r1_inline_tests::secp256r1_verify_evm_address | 11051 |
10917 | 1.21% |
| crypto_signature_inline_tests::signature_address | 11056 | 10878 |
1.61% |
| crypto_signature_inline_tests::signature_evm_address | 11032 | 10854 |
1.61% |
| crypto_signature_inline_tests::signature_recover | 41145 | 35935 |
12.66% |
| crypto_signature_inline_tests::signature_verify | 46227 | 40971 |
11.37% |
| crypto_signature_inline_tests::signature_verify_address | 11106 |
10928 | 1.60% |
| crypto_signature_inline_tests::signature_verify_evm_address | 11105 |
10927 | 1.60% |
| crypto_zk_inline_tests::revert_zk_alt_bn128_add_fail | 4499 | 4407 |
2.04% |
| crypto_zk_inline_tests::revert_zk_alt_bn128_mul_fail | 4264 | 4195 |
1.62% |
| crypto_zk_inline_tests::revert_zk_alt_bn128_pairing_check | 123487 |
123269 | 0.18% |
| crypto_zk_inline_tests::zk_alt_bn128_add | 105641 | 92381 | 12.55% |
| crypto_zk_inline_tests::zk_alt_bn128_mul | 104468 | 91323 | 12.58% |
| crypto_zk_inline_tests::zk_alt_bn128_pairing_check | 1146355 | 1142872
| 0.30% |
| ecr_inline_tests::ecr_ec_recover_address | 3721 | 3533 | 5.05% |
| ecr_inline_tests::ecr_ec_recover_address_r1 | 7819 | 7631 | 2.40% |
| ecr_inline_tests::ecr_ed_verify | 20067 | 15455 | 22.98% |
| hash_inline_tests::hash_hasher_keccak256_b256 | 970 | 882 | 9.07% |
| hash_inline_tests::hash_hasher_keccak256_bool | 797 | 627 | 21.33% |
| hash_inline_tests::hash_hasher_keccak256_bytes | 749 | 579 | 22.70% |
| hash_inline_tests::hash_hasher_keccak256_str | 973 | 885 | 9.04% |
| hash_inline_tests::hash_hasher_keccak256_str_array | 936 | 848 | 9.40%
|
| hash_inline_tests::hash_hasher_keccak256_u16 | 938 | 850 | 9.38% |
| hash_inline_tests::hash_hasher_keccak256_u256 | 970 | 882 | 9.07% |
| hash_inline_tests::hash_hasher_keccak256_u32 | 938 | 850 | 9.38% |
| hash_inline_tests::hash_hasher_keccak256_u64 | 936 | 848 | 9.40% |
| hash_inline_tests::hash_hasher_keccak256_u8 | 794 | 624 | 21.41% |
| hash_inline_tests::hash_hasher_sha256_10_array | 11064 | 8384 | 24.22%
|
| hash_inline_tests::hash_hasher_sha256_1_array | 972 | 884 | 9.05% |
| hash_inline_tests::hash_hasher_sha256_2_array | 2092 | 1716 | 17.97% |
| hash_inline_tests::hash_hasher_sha256_3_tuple | 3172 | 2508 | 20.93% |
| hash_inline_tests::hash_hasher_sha256_4_array | 4300 | 3348 | 22.14% |
| hash_inline_tests::hash_hasher_sha256_4_tuple | 4276 | 3324 | 22.26% |
| hash_inline_tests::hash_hasher_sha256_5_array | 5404 | 4164 | 22.95% |
| hash_inline_tests::hash_hasher_sha256_5_tuple | 5380 | 4140 | 23.05% |
| hash_inline_tests::hash_hasher_sha256_6_array | 6591 | 5063 | 23.18% |
| hash_inline_tests::hash_hasher_sha256_8_array | 8827 | 6723 | 23.84% |
| hash_inline_tests::hash_hasher_sha256_9_array | 9945 | 7553 | 24.05% |
| hash_inline_tests::hash_hasher_sha256_b256 | 952 | 864 | 9.24% |
| hash_inline_tests::hash_hasher_sha256_bool | 779 | 609 | 21.82% |
| hash_inline_tests::hash_hasher_sha256_bytes | 809 | 637 | 21.26% |
| hash_inline_tests::hash_hasher_sha256_u16 | 920 | 832 | 9.57% |
| hash_inline_tests::hash_hasher_sha256_u256 | 952 | 864 | 9.24% |
| hash_inline_tests::hash_hasher_sha256_u32 | 920 | 832 | 9.57% |
| hash_inline_tests::hash_hasher_sha256_u64 | 918 | 830 | 9.59% |
| hash_inline_tests::hash_hasher_sha256_u8 | 776 | 606 | 21.91% |
| hash_inline_tests::hash_hasher_write_str | 955 | 867 | 9.21% |
| hash_inline_tests::hash_hasher_write_str_array | 918 | 830 | 9.59% |
| hash_inline_tests::hash_keccak256 | 997 | 909 | 8.83% |
| hash_inline_tests::hash_sha256 | 979 | 891 | 8.99% |
| hash_inline_tests::hash_sha256_str_array | 950 | 862 | 9.26% |
| identity_inline_tests::identity_hash | 5227 | 3613 | 30.88% |
| primitive_conversions_b256_inline_tests::b256_from_u128 | 677 | 572 |
15.51% |
| primitive_conversions_b256_inline_tests::b256_into_u128 | 677 | 572 |
15.51% |
| primitive_conversions_b256_inline_tests::b256_try_from_bytes | 8510 |
4472 | 47.45% |
| string_inline_tests::string_as_bytes | 1050 | 745 | 29.05% |
| string_inline_tests::string_as_raw_slice | 1020 | 733 | 28.14% |
| string_inline_tests::string_bytes_from | 1484 | 1013 | 31.74% |
| string_inline_tests::string_bytes_into | 1484 | 1013 | 31.74% |
| string_inline_tests::string_capacity | 602 | 436 | 27.57% |
| string_inline_tests::string_clear | 485 | 365 | 24.74% |
| string_inline_tests::string_clone | 2747 | 1879 | 31.60% |
| string_inline_tests::string_from_ascii | 1733 | 1245 | 28.16% |
| string_inline_tests::string_from_ascii_str | 1246 | 1025 | 17.74% |
| string_inline_tests::string_from_bytes | 1484 | 1013 | 31.74% |
| string_inline_tests::string_from_raw_slice | 1391 | 1044 | 24.95% |
| string_inline_tests::string_into_bytes | 2080 | 1449 | 30.34% |
| string_inline_tests::string_into_raw_slice | 1509 | 1227 | 18.69% |
| string_inline_tests::string_is_empty | 1100 | 818 | 25.64% |
| string_inline_tests::string_new | 196 | 173 | 11.73% |
| string_inline_tests::string_ptr | 1169 | 790 | 32.42% |
| string_inline_tests::string_raw_slice_from | 1020 | 733 | 28.14% |
| string_inline_tests::string_raw_slice_into | 1020 | 733 | 28.14% |
| string_inline_tests::string_test_abi_encoding | 939 | 861 | 8.31% |
| string_inline_tests::string_test_equal | 1227 | 1071 | 12.71% |
| string_inline_tests::string_test_hash | 983 | 775 | 21.16% |
| string_inline_tests::string_with_capacity | 2484 | 2047 | 17.59% |
| u128_inline_tests::u128_as_u256 | 10083 | 8974 | 11.00% |
| vec_inline_tests::revert_vec_insert_out_of_bounds | 655 | 527 | 19.54%
|
| vec_inline_tests::revert_vec_remove_out_of_bounds | 629 | 501 | 20.35%
|
| vec_inline_tests::revert_vec_set_out_of_bounds | 631 | 501 | 20.60% |
| vec_inline_tests::revert_vec_swap_element_1_out_of_bounds | 632 | 501
| 20.73% |
| vec_inline_tests::revert_vec_swap_element_2_out_of_bounds | 636 | 505
| 20.60% |
| vec_inline_tests::vec_as_raw_slice | 758 | 587 | 22.56% |
| vec_inline_tests::vec_buffer_ownership | 2325 | 1607 | 30.88% |
| vec_inline_tests::vec_capacity | 1103 | 755 | 31.55% |
| vec_inline_tests::vec_clear | 631 | 505 | 19.97% |
| vec_inline_tests::vec_clear_empty_vec | 189 | 143 | 24.34% |
| vec_inline_tests::vec_clear_twice | 735 | 567 | 22.86% |
| vec_inline_tests::vec_clone | 1798 | 1376 | 23.47% |
| vec_inline_tests::vec_encode_and_decode | 2527 | 1893 | 25.09% |
| vec_inline_tests::vec_from_raw_slice | 310 | 287 | 7.42% |
| vec_inline_tests::vec_get | 1613 | 1367 | 15.25% |
| vec_inline_tests::vec_insert | 1466 | 1171 | 20.12% |
| vec_inline_tests::vec_insert_back | 1467 | 1172 | 20.11% |
| vec_inline_tests::vec_insert_before_back | 1478 | 1183 | 19.96% |
| vec_inline_tests::vec_insert_front | 1477 | 1182 | 19.97% |
| vec_inline_tests::vec_insert_twice | 2493 | 2032 | 18.49% |
| vec_inline_tests::vec_is_empty | 742 | 616 | 16.98% |
| vec_inline_tests::vec_iter | 2303 | 1656 | 28.09% |
| vec_inline_tests::vec_last | 1623 | 1313 | 19.10% |
| vec_inline_tests::vec_len | 1179 | 885 | 24.94% |
| vec_inline_tests::vec_new | 119 | 96 | 19.33% |
| vec_inline_tests::vec_pop | 3292 | 2456 | 25.39% |
| vec_inline_tests::vec_ptr | 834 | 639 | 23.38% |
| vec_inline_tests::vec_push | 2022 | 1414 | 30.07% |
| vec_inline_tests::vec_raw_slice_into | 310 | 287 | 7.42% |
| vec_inline_tests::vec_remove | 1475 | 1178 | 20.14% |
| vec_inline_tests::vec_remove_all | 1048 | 830 | 20.80% |
| vec_inline_tests::vec_remove_end | 1152 | 940 | 18.40% |
| vec_inline_tests::vec_remove_front | 1155 | 943 | 18.35% |
| vec_inline_tests::vec_resize | 5107 | 4437 | 13.12% |
| vec_inline_tests::vec_set | 1244 | 988 | 20.58% |
| vec_inline_tests::vec_set_back | 1267 | 1011 | 20.21% |
| vec_inline_tests::vec_set_front | 1244 | 988 | 20.58% |
| vec_inline_tests::vec_set_twice | 1827 | 1464 | 19.87% |
| vec_inline_tests::vec_swap | 1541 | 1202 | 22.00% |
| vec_inline_tests::vec_swap_end | 1221 | 967 | 20.80% |
| vec_inline_tests::vec_swap_front | 1220 | 966 | 20.82% |
| vec_inline_tests::vec_swap_front_with_end | 1221 | 967 | 20.80% |
| vec_inline_tests::vec_swap_twice | 2319 | 1832 | 21.00% |
| vec_inline_tests::vec_with_capacity | 3078 | 2963 | 3.74% |
| vm_evm_ecr_inline_tests::ecr_ec_recover | 3416 | 3185 | 6.76% |
| vm_evm_evm_address_inline_tests::evm_address_hash | 1904 | 1708 |
10.29% |
| vm_evm_evm_address_inline_tests::evm_address_into_bytes | 7827 | 6558
| 16.21% |
| vm_evm_evm_address_inline_tests::evm_address_try_from_bytes | 3111 |
2340 | 24.78% |

</details>

## Checklist

- [x] I have linked to any relevant issues.
- [x] 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).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] 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.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-05-08 07:25:02 +00:00
..
src Optimize vec and bytes modules for bytecode size and gas usage (#7162) 2025-05-08 07:25:02 +00:00
Forc.toml ci: Ensure that std-lib version matches forc version (#7154) 2025-05-08 08:12:35 +04:00
generate.sh Debug trait and its auto implementation (#7015) 2025-04-22 07:49:31 -03:00
README.md Issue#3044 Update minor mistakes in README.md and src/reentrancy.sw (#3045) 2022-10-15 08:46:23 +00:00

Sway Standard Library

The Sway Standard Library is the foundation of portable Sway software, a set of minimal shared abstractions for the broader Sway ecosystem. It offers core types, like Result<T, E> and Option<T>, library-defined operations on language primitives, native asset management, blockchain contextual operations, access control, storage management, and support for types from other VMs, among many other things.

Usage

The standard library is made implicitly available to all Forc projects created using forc new. In other words, it is not required to manually specify std as an explicit dependency. Forc will automatically use the version of std that matches its version.

Importing items from the standard library can be done using the use keyword, just as importing items from any other Sway library. For example:

use std::storage::StorageMap;

This imports the StorageMap type into the current namespace.

The standard library comes with a "prelude" which is a list of things that Sway automatically imports into every Sway program. It's kept as small as possible, and is focused on things which are used in almost every single Sway program.