diff --git a/benchmarks/btreemap/canbench_results.yml b/benchmarks/btreemap/canbench_results.yml index 6cd4d8db..9335445d 100644 --- a/benchmarks/btreemap/canbench_results.yml +++ b/benchmarks/btreemap/canbench_results.yml @@ -198,7 +198,7 @@ benches: btreemap_v2_contains_vec_32_128: total: calls: 1 - instructions: 428712867 + instructions: 428712852 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -562,14 +562,14 @@ benches: btreemap_v2_insert_10mib_values: total: calls: 1 - instructions: 5253123874 - heap_increase: 322 + instructions: 4414243466 + heap_increase: 161 stable_memory_increase: 3613 scopes: {} btreemap_v2_insert_blob8_u64: total: calls: 1 - instructions: 445536988 + instructions: 444987033 heap_increase: 0 stable_memory_increase: 4 scopes: {} @@ -590,7 +590,7 @@ benches: btreemap_v2_insert_blob_16_128: total: calls: 1 - instructions: 495692268 + instructions: 495692313 heap_increase: 0 stable_memory_increase: 24 scopes: {} @@ -667,7 +667,7 @@ benches: btreemap_v2_insert_blob_4_128: total: calls: 1 - instructions: 416771299 + instructions: 416771929 heap_increase: 0 stable_memory_increase: 13 scopes: {} @@ -688,154 +688,154 @@ benches: btreemap_v2_insert_blob_8_128: total: calls: 1 - instructions: 468148404 + instructions: 468148494 heap_increase: 0 stable_memory_increase: 20 scopes: {} btreemap_v2_insert_u64_blob8: total: calls: 1 - instructions: 419628571 + instructions: 414538939 heap_increase: 0 stable_memory_increase: 5 scopes: {} btreemap_v2_insert_u64_u64: total: calls: 1 - instructions: 427767878 + instructions: 422120086 heap_increase: 0 stable_memory_increase: 6 scopes: {} btreemap_v2_insert_u64_vec8: total: calls: 1 - instructions: 426996018 + instructions: 417793976 heap_increase: 0 stable_memory_increase: 21 scopes: {} btreemap_v2_insert_vec8_u64: total: calls: 1 - instructions: 600791435 + instructions: 600241480 heap_increase: 0 stable_memory_increase: 16 scopes: {} btreemap_v2_insert_vec_1024_128: total: calls: 1 - instructions: 2734644691 + instructions: 2724541315 heap_increase: 0 stable_memory_increase: 193 scopes: {} btreemap_v2_insert_vec_128_128: total: calls: 1 - instructions: 1013642430 + instructions: 1004230958 heap_increase: 0 stable_memory_increase: 51 scopes: {} btreemap_v2_insert_vec_16_128: total: calls: 1 - instructions: 723246682 + instructions: 712421967 heap_increase: 0 stable_memory_increase: 31 scopes: {} btreemap_v2_insert_vec_256_128: total: calls: 1 - instructions: 1396732537 + instructions: 1386961585 heap_increase: 0 stable_memory_increase: 71 scopes: {} btreemap_v2_insert_vec_32_1024: total: calls: 1 - instructions: 1220926606 + instructions: 1172134598 heap_increase: 0 stable_memory_increase: 171 scopes: {} btreemap_v2_insert_vec_32_128: total: calls: 1 - instructions: 768005749 + instructions: 758019348 heap_increase: 0 stable_memory_increase: 33 scopes: {} btreemap_v2_insert_vec_32_16: total: calls: 1 - instructions: 677660003 + instructions: 672563151 heap_increase: 0 stable_memory_increase: 20 scopes: {} btreemap_v2_insert_vec_32_256: total: calls: 1 - instructions: 883874106 + instructions: 866698572 heap_increase: 0 stable_memory_increase: 54 scopes: {} btreemap_v2_insert_vec_32_32: total: calls: 1 - instructions: 673498276 + instructions: 667983184 heap_increase: 0 stable_memory_increase: 20 scopes: {} btreemap_v2_insert_vec_32_4: total: calls: 1 - instructions: 673498948 + instructions: 666709196 heap_increase: 0 stable_memory_increase: 20 scopes: {} btreemap_v2_insert_vec_32_512: total: calls: 1 - instructions: 997682895 + instructions: 969855594 heap_increase: 0 stable_memory_increase: 91 scopes: {} btreemap_v2_insert_vec_32_64: total: calls: 1 - instructions: 706148903 + instructions: 699344644 heap_increase: 0 stable_memory_increase: 24 scopes: {} btreemap_v2_insert_vec_32_8: total: calls: 1 - instructions: 672527454 + instructions: 666308283 heap_increase: 0 stable_memory_increase: 20 scopes: {} btreemap_v2_insert_vec_4_128: total: calls: 1 - instructions: 619661464 + instructions: 608538730 heap_increase: 0 stable_memory_increase: 16 scopes: {} btreemap_v2_insert_vec_512_128: total: calls: 1 - instructions: 1851040948 + instructions: 1840482053 heap_increase: 0 stable_memory_increase: 112 scopes: {} btreemap_v2_insert_vec_64_128: total: calls: 1 - instructions: 855541288 + instructions: 845897952 heap_increase: 0 stable_memory_increase: 41 scopes: {} btreemap_v2_insert_vec_8_128: total: calls: 1 - instructions: 679949414 + instructions: 669688389 heap_increase: 0 stable_memory_increase: 23 scopes: {} @@ -912,35 +912,35 @@ benches: btreemap_v2_mem_manager_insert_blob512_u64: total: calls: 1 - instructions: 3145341193 + instructions: 3144791193 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_mem_manager_insert_u64_blob512: total: calls: 1 - instructions: 634310958 + instructions: 629203086 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_mem_manager_insert_u64_u64: total: calls: 1 - instructions: 547547996 + instructions: 541900204 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_mem_manager_insert_u64_vec512: total: calls: 1 - instructions: 874783181 + instructions: 843362543 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_mem_manager_insert_vec512_u64: total: calls: 1 - instructions: 1959884611 + instructions: 1959334611 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -954,21 +954,21 @@ benches: btreemap_v2_mem_manager_remove_u64_blob512: total: calls: 1 - instructions: 927125019 + instructions: 918002811 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_mem_manager_remove_u64_u64: total: calls: 1 - instructions: 783398371 + instructions: 774336355 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_mem_manager_remove_u64_vec512: total: calls: 1 - instructions: 1244433576 + instructions: 1235311368 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -1108,21 +1108,21 @@ benches: btreemap_v2_pop_first_u64_blob8: total: calls: 1 - instructions: 699797969 + instructions: 692238497 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_pop_first_u64_u64: total: calls: 1 - instructions: 711251164 + instructions: 703685884 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_pop_first_u64_vec8: total: calls: 1 - instructions: 702348001 + instructions: 694788529 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -1171,7 +1171,7 @@ benches: btreemap_v2_pop_first_vec_32_128: total: calls: 1 - instructions: 1103431087 + instructions: 1103431102 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -1248,7 +1248,7 @@ benches: btreemap_v2_pop_first_vec_8_128: total: calls: 1 - instructions: 862745891 + instructions: 862745876 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -1381,21 +1381,21 @@ benches: btreemap_v2_pop_last_u64_blob8: total: calls: 1 - instructions: 678333944 + instructions: 670770920 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_pop_last_u64_u64: total: calls: 1 - instructions: 689228779 + instructions: 681654379 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_pop_last_u64_vec8: total: calls: 1 - instructions: 680731667 + instructions: 673168643 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -1423,7 +1423,7 @@ benches: btreemap_v2_pop_last_vec_16_128: total: calls: 1 - instructions: 1022083121 + instructions: 1022083136 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -1444,7 +1444,7 @@ benches: btreemap_v2_pop_last_vec_32_128: total: calls: 1 - instructions: 1078246309 + instructions: 1078246324 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -1521,77 +1521,77 @@ benches: btreemap_v2_pop_last_vec_8_128: total: calls: 1 - instructions: 862433243 + instructions: 862433228 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_range_count_1k_0b: total: calls: 1 - instructions: 17322 + instructions: 17220 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_range_count_1k_10kib: total: calls: 1 - instructions: 2651807 + instructions: 2576893 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_range_count_20_10mib: total: calls: 1 - instructions: 20577326 + instructions: 20576208 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_range_key_sum_1k_0b: total: calls: 1 - instructions: 17568 + instructions: 17466 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_range_key_sum_1k_10kib: total: calls: 1 - instructions: 57016599 + instructions: 56941685 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_range_key_sum_20_10mib: total: calls: 1 - instructions: 1105821929 + instructions: 1105820811 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_range_value_sum_1k_0b: total: calls: 1 - instructions: 17582 + instructions: 17480 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_range_value_sum_1k_10kib: total: calls: 1 - instructions: 57028595 + instructions: 56953681 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_range_value_sum_20_10mib: total: calls: 1 - instructions: 1105822165 + instructions: 1105821047 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_remove_10mib_values: total: calls: 1 - instructions: 4738893196 + instructions: 4738884700 heap_increase: 0 stable_memory_increase: 657 scopes: {} @@ -1724,21 +1724,21 @@ benches: btreemap_v2_remove_u64_blob8: total: calls: 1 - instructions: 591285424 + instructions: 582340960 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_remove_u64_u64: total: calls: 1 - instructions: 612332092 + instructions: 603270076 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_remove_u64_vec8: total: calls: 1 - instructions: 596864259 + instructions: 587919795 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -1836,7 +1836,7 @@ benches: btreemap_v2_remove_vec_32_8: total: calls: 1 - instructions: 869697893 + instructions: 869697878 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -1871,126 +1871,126 @@ benches: btreemap_v2_scan_iter_1k_0b: total: calls: 1 - instructions: 1456524 + instructions: 1380958 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_iter_1k_10kib: total: calls: 1 - instructions: 56819347 + instructions: 56743813 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_iter_20_10mib: total: calls: 1 - instructions: 1103714524 + instructions: 1103713183 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_iter_rev_1k_0b: total: calls: 1 - instructions: 1457032 + instructions: 1382580 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_iter_rev_1k_10kib: total: calls: 1 - instructions: 56796287 + instructions: 56722199 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_iter_rev_20_10mib: total: calls: 1 - instructions: 1103714083 + instructions: 1103712762 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_keys_1k_0b: total: calls: 1 - instructions: 1193504 + instructions: 1104356 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_keys_1k_10kib: total: calls: 1 - instructions: 2603078 + instructions: 2514294 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_keys_20_10mib: total: calls: 1 - instructions: 18470078 + instructions: 18468498 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_keys_rev_1k_0b: total: calls: 1 - instructions: 1193326 + instructions: 1105970 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_keys_rev_1k_10kib: total: calls: 1 - instructions: 2581338 + instructions: 2494346 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_keys_rev_20_10mib: total: calls: 1 - instructions: 18470064 + instructions: 18468514 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_values_1k_0b: total: calls: 1 - instructions: 1433862 + instructions: 1358296 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_values_1k_10kib: total: calls: 1 - instructions: 56796685 + instructions: 56721151 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_values_20_10mib: total: calls: 1 - instructions: 1103714074 + instructions: 1103712733 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_values_rev_1k_0b: total: calls: 1 - instructions: 1435034 + instructions: 1360582 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_values_rev_1k_10kib: total: calls: 1 - instructions: 56774289 + instructions: 56700201 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_v2_scan_values_rev_20_10mib: total: calls: 1 - instructions: 1103713645 + instructions: 1103712324 heap_increase: 0 stable_memory_increase: 0 scopes: {} diff --git a/benchmarks/btreeset/canbench_results.yml b/benchmarks/btreeset/canbench_results.yml index 7c2c30a1..1968303e 100644 --- a/benchmarks/btreeset/canbench_results.yml +++ b/benchmarks/btreeset/canbench_results.yml @@ -2,196 +2,196 @@ benches: btreeset_insert_blob_1024: total: calls: 1 - instructions: 7286178998 + instructions: 7286690242 heap_increase: 1 stable_memory_increase: 256 scopes: {} btreeset_insert_blob_128: total: calls: 1 - instructions: 1655004300 + instructions: 1655515544 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_insert_blob_16: total: calls: 1 - instructions: 742489208 + instructions: 742764840 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_insert_blob_256: total: calls: 1 - instructions: 2466538169 + instructions: 2467049413 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_insert_blob_32: total: calls: 1 - instructions: 838226077 + instructions: 838550745 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_insert_blob_512: total: calls: 1 - instructions: 4070669124 + instructions: 4071180368 heap_increase: 0 stable_memory_increase: 128 scopes: {} btreeset_insert_blob_64: total: calls: 1 - instructions: 999868179 + instructions: 1000266143 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_insert_blob_8: total: calls: 1 - instructions: 720687797 + instructions: 720924777 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_insert_u32: total: calls: 1 - instructions: 575071142 + instructions: 569311458 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_insert_u64: total: calls: 1 - instructions: 594277483 + instructions: 588524339 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_intersection_blob_1024: total: calls: 1 - instructions: 108717444 + instructions: 108717480 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_intersection_blob_128: total: calls: 1 - instructions: 18182291 + instructions: 18182327 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_intersection_blob_16: total: calls: 1 - instructions: 3647439 + instructions: 3647475 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_intersection_blob_256: total: calls: 1 - instructions: 31425818 + instructions: 31425854 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_intersection_blob_32: total: calls: 1 - instructions: 4922203 + instructions: 4922239 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_intersection_blob_512: total: calls: 1 - instructions: 57189201 + instructions: 57189237 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_intersection_blob_64: total: calls: 1 - instructions: 10298006 + instructions: 10298042 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_intersection_blob_8: total: calls: 1 - instructions: 3391543 + instructions: 3391579 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_intersection_u32: total: calls: 1 - instructions: 2493469 + instructions: 2493481 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_intersection_u64: total: calls: 1 - instructions: 2511552 + instructions: 2511564 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_disjoint_blob_1024: total: calls: 1 - instructions: 52538676 + instructions: 52538700 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_disjoint_blob_128: total: calls: 1 - instructions: 9488928 + instructions: 9488952 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_disjoint_blob_16: total: calls: 1 - instructions: 2342890 + instructions: 2342914 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_disjoint_blob_256: total: calls: 1 - instructions: 15786633 + instructions: 15786657 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_disjoint_blob_32: total: calls: 1 - instructions: 3010030 + instructions: 3010054 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_disjoint_blob_512: total: calls: 1 - instructions: 28037606 + instructions: 28037630 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_disjoint_blob_64: total: calls: 1 - instructions: 5459364 + instructions: 5459388 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_disjoint_blob_8: total: calls: 1 - instructions: 2251564 + instructions: 2251588 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -282,420 +282,420 @@ benches: btreeset_is_superset_blob_1024: total: calls: 1 - instructions: 91980389 + instructions: 91980425 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_superset_blob_128: total: calls: 1 - instructions: 15754409 + instructions: 15754445 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_superset_blob_16: total: calls: 1 - instructions: 3567062 + instructions: 3567098 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_superset_blob_256: total: calls: 1 - instructions: 26966889 + instructions: 26966925 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_superset_blob_32: total: calls: 1 - instructions: 4791847 + instructions: 4791883 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_superset_blob_512: total: calls: 1 - instructions: 48630699 + instructions: 48630735 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_superset_blob_64: total: calls: 1 - instructions: 8895008 + instructions: 8895044 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_superset_blob_8: total: calls: 1 - instructions: 3336630 + instructions: 3336666 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_superset_u32: total: calls: 1 - instructions: 2492181 + instructions: 2492193 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_is_superset_u64: total: calls: 1 - instructions: 2510627 + instructions: 2510639 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_iter_blob_1024: total: calls: 1 - instructions: 435124557 + instructions: 435128581 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_iter_blob_128: total: calls: 1 - instructions: 77509273 + instructions: 77513297 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_iter_blob_16: total: calls: 1 - instructions: 17503860 + instructions: 17507874 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_iter_blob_256: total: calls: 1 - instructions: 129682710 + instructions: 129686734 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_iter_blob_32: total: calls: 1 - instructions: 23960282 + instructions: 23964306 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_iter_blob_512: total: calls: 1 - instructions: 231602184 + instructions: 231606208 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_iter_blob_64: total: calls: 1 - instructions: 42403250 + instructions: 42407274 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_iter_blob_8: total: calls: 1 - instructions: 16845506 + instructions: 16849520 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_iter_u32: total: calls: 1 - instructions: 13548159 + instructions: 13552153 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_iter_u64: total: calls: 1 - instructions: 13630143 + instructions: 13634137 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_range_blob_1024: total: calls: 1 - instructions: 263014476 + instructions: 263016896 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_range_blob_128: total: calls: 1 - instructions: 47360430 + instructions: 47362850 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_range_blob_16: total: calls: 1 - instructions: 11547595 + instructions: 11550015 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_range_blob_256: total: calls: 1 - instructions: 80329124 + instructions: 80331544 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_range_blob_32: total: calls: 1 - instructions: 15397632 + instructions: 15400052 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_range_blob_512: total: calls: 1 - instructions: 140683370 + instructions: 140685790 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_range_blob_64: total: calls: 1 - instructions: 26557443 + instructions: 26559863 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_range_blob_8: total: calls: 1 - instructions: 11163966 + instructions: 11166386 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_range_u32: total: calls: 1 - instructions: 8527562 + instructions: 8529972 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_range_u64: total: calls: 1 - instructions: 8578350 + instructions: 8580760 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_remove_blob_1024: total: calls: 1 - instructions: 7742324318 + instructions: 7742824218 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_remove_blob_128: total: calls: 1 - instructions: 1686815315 + instructions: 1687315215 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_remove_blob_16: total: calls: 1 - instructions: 734847603 + instructions: 735109295 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_remove_blob_256: total: calls: 1 - instructions: 2554095644 + instructions: 2554595544 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_remove_blob_32: total: calls: 1 - instructions: 828046990 + instructions: 828360358 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_remove_blob_512: total: calls: 1 - instructions: 4281038278 + instructions: 4281538178 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_remove_blob_64: total: calls: 1 - instructions: 1008859628 + instructions: 1009246256 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_remove_blob_8: total: calls: 1 - instructions: 713093332 + instructions: 713356340 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_remove_u32: total: calls: 1 - instructions: 565270487 + instructions: 560012231 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_remove_u64: total: calls: 1 - instructions: 588839168 + instructions: 583587452 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_symmetric_difference_blob_1024: total: calls: 1 - instructions: 108731570 + instructions: 108731606 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_symmetric_difference_blob_128: total: calls: 1 - instructions: 18185665 + instructions: 18185701 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_symmetric_difference_blob_16: total: calls: 1 - instructions: 3649063 + instructions: 3649099 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_symmetric_difference_blob_256: total: calls: 1 - instructions: 31430728 + instructions: 31430764 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_symmetric_difference_blob_32: total: calls: 1 - instructions: 4923871 + instructions: 4923907 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_symmetric_difference_blob_512: total: calls: 1 - instructions: 57197183 + instructions: 57197219 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_symmetric_difference_blob_64: total: calls: 1 - instructions: 10300612 + instructions: 10300648 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_symmetric_difference_blob_8: total: calls: 1 - instructions: 3393146 + instructions: 3393182 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_symmetric_difference_u32: total: calls: 1 - instructions: 2509577 + instructions: 2509589 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_symmetric_difference_u64: total: calls: 1 - instructions: 2517637 + instructions: 2517649 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_union_blob_1024: total: calls: 1 - instructions: 108732078 + instructions: 108732114 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_union_blob_128: total: calls: 1 - instructions: 18186173 + instructions: 18186209 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_union_blob_16: total: calls: 1 - instructions: 3649571 + instructions: 3649607 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_union_blob_256: total: calls: 1 - instructions: 31431236 + instructions: 31431272 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_union_blob_32: total: calls: 1 - instructions: 4924379 + instructions: 4924415 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_union_blob_512: total: calls: 1 - instructions: 57197691 + instructions: 57197727 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_union_blob_64: total: calls: 1 - instructions: 10301120 + instructions: 10301156 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_union_blob_8: total: calls: 1 - instructions: 3393654 + instructions: 3393690 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_union_u32: total: calls: 1 - instructions: 2507084 + instructions: 2507096 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreeset_union_u64: total: calls: 1 - instructions: 2513147 + instructions: 2513159 heap_increase: 0 stable_memory_increase: 0 scopes: {} diff --git a/benchmarks/compare/canbench_results.yml b/benchmarks/compare/canbench_results.yml index f35dfed4..00db9bca 100644 --- a/benchmarks/compare/canbench_results.yml +++ b/benchmarks/compare/canbench_results.yml @@ -2,21 +2,21 @@ benches: read_chunks_btreemap_1: total: calls: 1 - instructions: 1219162510 - heap_increase: 3233 + instructions: 799994339 + heap_increase: 1635 stable_memory_increase: 1665 scopes: {} read_chunks_btreemap_1k: total: calls: 1 - instructions: 5415231039 - heap_increase: 1604 + instructions: 4994472213 + heap_increase: 1602 stable_memory_increase: 1665 scopes: {} read_chunks_btreemap_1m: total: calls: 1 - instructions: 134599901409 + instructions: 133159275732 heap_increase: 1892 stable_memory_increase: 3201 scopes: {} @@ -44,42 +44,42 @@ benches: read_chunks_vec_1: total: calls: 1 - instructions: 1363286421 + instructions: 1363286413 heap_increase: 3202 stable_memory_increase: 1665 scopes: {} read_chunks_vec_1k: total: calls: 1 - instructions: 1378474867 + instructions: 1378477856 heap_increase: 3200 stable_memory_increase: 1665 scopes: {} read_chunks_vec_1m: total: calls: 1 - instructions: 4721968950 + instructions: 4724968939 heap_increase: 3784 stable_memory_increase: 1665 scopes: {} write_chunks_btreemap_1: total: calls: 1 - instructions: 1069802998 - heap_increase: 3233 + instructions: 650634827 + heap_increase: 1635 stable_memory_increase: 1665 scopes: {} write_chunks_btreemap_1k: total: calls: 1 - instructions: 4915151136 - heap_increase: 1604 + instructions: 4494392310 + heap_increase: 1602 stable_memory_increase: 1665 scopes: {} write_chunks_btreemap_1m: total: calls: 1 - instructions: 90653823620 + instructions: 89213197943 heap_increase: 1892 stable_memory_increase: 3201 scopes: {} @@ -107,21 +107,21 @@ benches: write_chunks_vec_1: total: calls: 1 - instructions: 1257790953 + instructions: 1257790959 heap_increase: 3202 stable_memory_increase: 1665 scopes: {} write_chunks_vec_1k: total: calls: 1 - instructions: 1272012661 + instructions: 1272015664 heap_increase: 3200 stable_memory_increase: 1665 scopes: {} write_chunks_vec_1m: total: calls: 1 - instructions: 3712427298 + instructions: 3715427301 heap_increase: 3784 stable_memory_increase: 1665 scopes: {} diff --git a/benchmarks/src/vec.rs b/benchmarks/src/vec.rs index 4f099af4..b964b2eb 100644 --- a/benchmarks/src/vec.rs +++ b/benchmarks/src/vec.rs @@ -33,6 +33,11 @@ impl Storable for UnboundedVecN { Cow::Owned(self.0.clone()) } + #[inline] + fn into_bytes(self) -> Vec { + self.0 + } + #[inline] fn from_bytes(bytes: Cow<[u8]>) -> Self { Self(bytes.into_owned()) @@ -75,6 +80,11 @@ impl Storable for BoundedVecN { Cow::Owned(self.0.clone()) } + #[inline] + fn into_bytes(self) -> Vec { + self.0 + } + #[inline] fn from_bytes(bytes: Cow<[u8]>) -> Self { Self(bytes.into_owned()) diff --git a/benchmarks/vec/canbench_results.yml b/benchmarks/vec/canbench_results.yml index d1245455..bd83ad43 100644 --- a/benchmarks/vec/canbench_results.yml +++ b/benchmarks/vec/canbench_results.yml @@ -107,7 +107,7 @@ benches: vec_insert_u64: total: calls: 1 - instructions: 5869519 + instructions: 5359519 heap_increase: 0 stable_memory_increase: 1 scopes: {} diff --git a/docs/src/schema-upgrades.md b/docs/src/schema-upgrades.md index 12758e50..326a57f0 100644 --- a/docs/src/schema-upgrades.md +++ b/docs/src/schema-upgrades.md @@ -19,6 +19,11 @@ impl Storable for Asset { Cow::Owned(bytes) } + fn into_bytes(self) -> Vec { + let mut bytes = vec![]; + ciborium::ser::into_writer(&self, &mut bytes).unwrap() + } + fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { ciborium::de::from_reader(&*bytes).expect("deserialization must succeed.") } diff --git a/examples/src/custom_types_example/src/lib.rs b/examples/src/custom_types_example/src/lib.rs index aa43ca55..e6e7a70b 100644 --- a/examples/src/custom_types_example/src/lib.rs +++ b/examples/src/custom_types_example/src/lib.rs @@ -1,8 +1,6 @@ use candid::{CandidType, Decode, Deserialize, Encode}; use ic_stable_structures::memory_manager::{MemoryId, MemoryManager, VirtualMemory}; -use ic_stable_structures::{ - storable::Bound, DefaultMemoryImpl, StableBTreeMap, Storable, -}; +use ic_stable_structures::{storable::Bound, DefaultMemoryImpl, StableBTreeMap, Storable}; use std::{borrow::Cow, cell::RefCell}; type Memory = VirtualMemory; @@ -30,6 +28,10 @@ impl Storable for UserProfile { Cow::Owned(Encode!(self).unwrap()) } + fn into_bytes(self) -> Vec { + Encode!(&self).unwrap() + } + fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { Decode!(bytes.as_ref(), Self).unwrap() } diff --git a/examples/src/vecs_and_strings/src/lib.rs b/examples/src/vecs_and_strings/src/lib.rs index 237c741f..1cb85cb5 100644 --- a/examples/src/vecs_and_strings/src/lib.rs +++ b/examples/src/vecs_and_strings/src/lib.rs @@ -22,6 +22,10 @@ impl Storable for UserName { self.0.to_bytes() } + fn into_bytes(self) -> Vec { + self.0.into_bytes() + } + fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { Self(String::from_bytes(bytes)) } @@ -40,6 +44,10 @@ impl Storable for UserData { self.0.to_bytes() } + fn into_bytes(self) -> Vec { + self.0.into_bytes() + } + fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { Self(>::from_bytes(bytes)) } diff --git a/fuzz/fuzz_targets/data.rs b/fuzz/fuzz_targets/data.rs index aa9e5ded..7eac81fe 100644 --- a/fuzz/fuzz_targets/data.rs +++ b/fuzz/fuzz_targets/data.rs @@ -24,6 +24,10 @@ impl Storable for BoundedFuzzStruct { Cow::Owned(serde_cbor::ser::to_vec(self).unwrap()) } + fn into_bytes(self) -> Vec { + serde_cbor::ser::to_vec(&self).unwrap() + } + fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { let value: Self = serde_cbor::de::from_slice(bytes.as_ref()).unwrap(); value @@ -41,6 +45,10 @@ impl Storable for UnboundedFuzzStruct { Cow::Owned(serde_cbor::ser::to_vec(self).unwrap()) } + fn into_bytes(self) -> Vec { + serde_cbor::ser::to_vec(&self).unwrap() + } + fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { let value: Self = serde_cbor::de::from_slice(bytes.as_ref()).unwrap(); value diff --git a/src/btreemap.rs b/src/btreemap.rs index 179ecb4e..02f1f0f8 100644 --- a/src/btreemap.rs +++ b/src/btreemap.rs @@ -178,6 +178,12 @@ const PAGE_SIZE_VALUE_MARKER: u32 = u32::MAX; /// Cow::Owned(bytes) /// } /// +/// fn into_bytes(self) -> Vec { +/// let mut bytes = Vec::new(); +/// // TODO: Convert your struct to bytes... +/// bytes +/// } +/// /// fn from_bytes(bytes: Cow<[u8]>) -> Self { /// // TODO: Convert bytes back to your struct /// let (id, name) = (0, "".to_string()); @@ -492,7 +498,7 @@ where /// key.to_bytes().len() <= max_size(Key) /// value.to_bytes().len() <= max_size(Value) pub fn insert(&mut self, key: K, value: V) -> Option { - let value = value.to_bytes_checked().into_owned(); + let value = value.into_bytes_checked(); let root = if self.root_addr == NULL { // No root present. Allocate one. @@ -1428,7 +1434,7 @@ mod test { /// Encodes an object into a byte vector. fn encode(object: T) -> Vec { - object.to_bytes_checked().into_owned() + object.into_bytes_checked() } /// A helper method to succinctly create a blob. @@ -2980,6 +2986,10 @@ mod test { Cow::Borrowed(&[1, 2, 3, 4]) } + fn into_bytes(self) -> Vec { + self.to_bytes().into_owned() + } + fn from_bytes(_: Cow<[u8]>) -> Self { unimplemented!(); } @@ -3116,7 +3126,11 @@ mod test { struct T; impl Storable for T { fn to_bytes(&self) -> Cow<[u8]> { - Cow::Owned(vec![1, 2, 3]) + Cow::Borrowed(&[1, 2, 3]) + } + + fn into_bytes(self) -> Vec { + self.to_bytes().into_owned() } fn from_bytes(bytes: Cow<[u8]>) -> Self { @@ -3138,6 +3152,10 @@ mod test { Cow::Owned(vec![1, 2, 3]) } + fn into_bytes(self) -> Vec { + self.to_bytes().into_owned() + } + fn from_bytes(bytes: Cow<[u8]>) -> Self { assert_eq!(bytes.to_vec(), vec![1, 2, 3]); T2 diff --git a/src/btreeset.rs b/src/btreeset.rs index ebce52a3..8be5ba99 100644 --- a/src/btreeset.rs +++ b/src/btreeset.rs @@ -112,6 +112,10 @@ where /// Cow::Owned(self.id.to_le_bytes().to_vec()) /// } /// +/// fn into_bytes(self) -> Vec { +/// self.id.to_le_bytes().to_vec() +/// } +/// /// fn from_bytes(bytes: Cow<[u8]>) -> Self { /// let id = u64::from_le_bytes(bytes.as_ref().try_into().unwrap()); /// CustomType { id } diff --git a/src/storable.rs b/src/storable.rs index 535fe801..997a2623 100644 --- a/src/storable.rs +++ b/src/storable.rs @@ -11,44 +11,63 @@ mod tests; /// A trait with convenience methods for storing an element into a stable structure. pub trait Storable { - /// Converts an element into bytes. + /// Converts the element into a possibly borrowed byte slice. /// /// NOTE: `Cow` is used here to avoid unnecessary cloning. fn to_bytes(&self) -> Cow<[u8]>; + /// Converts the element into an owned byte vector. + /// + /// This method consumes `self` and avoids cloning when possible. + fn into_bytes(self) -> Vec; + /// Converts bytes into an element. fn from_bytes(bytes: Cow<[u8]>) -> Self; /// The size bounds of the type. const BOUND: Bound; - /// Like `to_bytes`, but includes additional checks to ensure the element's serialized bytes - /// are within the element's bounds. + /// Like `to_bytes`, but checks that bytes conform to declared bounds. fn to_bytes_checked(&self) -> Cow<[u8]> { let bytes = self.to_bytes(); + Self::check_bounds(&bytes); + bytes + } + + /// Like `into_bytes`, but checks that bytes conform to declared bounds. + fn into_bytes_checked(self) -> Vec + where + Self: Sized, + { + let bytes = self.into_bytes(); + Self::check_bounds(&bytes); + bytes + } + + /// Validates that a byte slice fits within this type's declared bounds. + #[inline] + fn check_bounds(bytes: &[u8]) { if let Bound::Bounded { max_size, is_fixed_size, } = Self::BOUND { + let actual = bytes.len(); if is_fixed_size { assert_eq!( - bytes.len(), - max_size as usize, + actual, max_size as usize, "expected a fixed-size element with length {} bytes, but found {} bytes", - max_size, - bytes.len() + max_size, actual ); } else { assert!( - bytes.len() <= max_size as usize, + actual <= max_size as usize, "expected an element with length <= {} bytes, but found {} bytes", max_size, - bytes.len() + actual ); } } - bytes } } @@ -175,10 +194,16 @@ impl fmt::Debug for Blob { } impl Storable for Blob { + #[inline] fn to_bytes(&self) -> Cow<[u8]> { Cow::Borrowed(self.as_slice()) } + #[inline] + fn into_bytes(self) -> Vec { + self.as_slice().to_vec() + } + #[inline] fn from_bytes(bytes: Cow<[u8]>) -> Self { Self::try_from(bytes.borrow()).unwrap() @@ -222,6 +247,11 @@ impl Storable for UnboundedVecN { Cow::Owned(self.0.clone()) } + #[inline] + fn into_bytes(self) -> Vec { + self.0 + } + #[inline] fn from_bytes(bytes: Cow<[u8]>) -> Self { Self(bytes.into_owned()) @@ -262,6 +292,11 @@ impl Storable for BoundedVecN { Cow::Owned(self.0.clone()) } + #[inline] + fn into_bytes(self) -> Vec { + self.0 + } + #[inline] fn from_bytes(bytes: Cow<[u8]>) -> Self { Self(bytes.into_owned()) @@ -286,10 +321,16 @@ impl Storable for BoundedVecN { // in case of a detected error is preferable and safer. impl Storable for () { + #[inline] fn to_bytes(&self) -> Cow<[u8]> { Cow::Borrowed(&[]) } + #[inline] + fn into_bytes(self) -> Vec { + Vec::new() + } + #[inline] fn from_bytes(bytes: Cow<[u8]>) -> Self { assert!(bytes.is_empty()); @@ -304,10 +345,16 @@ impl Storable for () { } impl Storable for Vec { + #[inline] fn to_bytes(&self) -> Cow<[u8]> { Cow::Borrowed(self) } + #[inline] + fn into_bytes(self) -> Vec { + self + } + #[inline] fn from_bytes(bytes: Cow<[u8]>) -> Self { bytes.into_owned() @@ -317,10 +364,16 @@ impl Storable for Vec { } impl Storable for String { + #[inline] fn to_bytes(&self) -> Cow<[u8]> { Cow::Borrowed(self.as_bytes()) } + #[inline] + fn into_bytes(self) -> Vec { + self.into_bytes() + } + #[inline] fn from_bytes(bytes: Cow<[u8]>) -> Self { String::from_utf8(bytes.into_owned()).unwrap() @@ -330,8 +383,14 @@ impl Storable for String { } impl Storable for u128 { + #[inline] fn to_bytes(&self) -> Cow<[u8]> { - Cow::Owned(self.to_be_bytes().to_vec()) + Cow::Owned(self.into_bytes()) + } + + #[inline] + fn into_bytes(self) -> Vec { + self.to_be_bytes().to_vec() } #[inline] @@ -346,8 +405,14 @@ impl Storable for u128 { } impl Storable for u64 { + #[inline] fn to_bytes(&self) -> Cow<[u8]> { - Cow::Owned(self.to_be_bytes().to_vec()) + Cow::Owned(self.into_bytes()) + } + + #[inline] + fn into_bytes(self) -> Vec { + self.to_be_bytes().to_vec() } #[inline] @@ -362,8 +427,14 @@ impl Storable for u64 { } impl Storable for f64 { + #[inline] fn to_bytes(&self) -> Cow<[u8]> { - Cow::Owned(self.to_be_bytes().to_vec()) + Cow::Owned(self.into_bytes()) + } + + #[inline] + fn into_bytes(self) -> Vec { + self.to_be_bytes().to_vec() } #[inline] @@ -378,8 +449,14 @@ impl Storable for f64 { } impl Storable for u32 { + #[inline] fn to_bytes(&self) -> Cow<[u8]> { - Cow::Owned(self.to_be_bytes().to_vec()) + Cow::Owned(self.into_bytes()) + } + + #[inline] + fn into_bytes(self) -> Vec { + self.to_be_bytes().to_vec() } #[inline] @@ -394,8 +471,14 @@ impl Storable for u32 { } impl Storable for f32 { + #[inline] fn to_bytes(&self) -> Cow<[u8]> { - Cow::Owned(self.to_be_bytes().to_vec()) + Cow::Owned(self.into_bytes()) + } + + #[inline] + fn into_bytes(self) -> Vec { + self.to_be_bytes().to_vec() } #[inline] @@ -410,8 +493,14 @@ impl Storable for f32 { } impl Storable for u16 { + #[inline] fn to_bytes(&self) -> Cow<[u8]> { - Cow::Owned(self.to_be_bytes().to_vec()) + Cow::Owned(self.into_bytes()) + } + + #[inline] + fn into_bytes(self) -> Vec { + self.to_be_bytes().to_vec() } #[inline] @@ -426,8 +515,14 @@ impl Storable for u16 { } impl Storable for u8 { + #[inline] fn to_bytes(&self) -> Cow<[u8]> { - Cow::Owned(self.to_be_bytes().to_vec()) + Cow::Owned(self.into_bytes()) + } + + #[inline] + fn into_bytes(self) -> Vec { + self.to_be_bytes().to_vec() } #[inline] @@ -443,8 +538,11 @@ impl Storable for u8 { impl Storable for bool { fn to_bytes(&self) -> Cow<[u8]> { - let num: u8 = if *self { 1 } else { 0 }; - Cow::Owned(num.to_be_bytes().to_vec()) + Cow::Owned(if *self { 1_u8 } else { 0_u8 }.to_be_bytes().to_vec()) + } + + fn into_bytes(self) -> Vec { + if self { 1_u8 } else { 0_u8 }.to_be_bytes().to_vec() } #[inline] @@ -464,10 +562,16 @@ impl Storable for bool { } impl Storable for [u8; N] { + #[inline] fn to_bytes(&self) -> Cow<[u8]> { Cow::Borrowed(&self[..]) } + #[inline] + fn into_bytes(self) -> Vec { + self.to_vec() + } + #[inline] fn from_bytes(bytes: Cow<[u8]>) -> Self { assert_eq!(bytes.len(), N); @@ -483,10 +587,16 @@ impl Storable for [u8; N] { } impl Storable for Reverse { + #[inline] fn to_bytes(&self) -> Cow<[u8]> { self.0.to_bytes() } + #[inline] + fn into_bytes(self) -> Vec { + self.0.into_bytes() + } + #[inline] fn from_bytes(bytes: Cow<[u8]>) -> Self { Self(T::from_bytes(bytes)) @@ -507,6 +617,17 @@ impl Storable for Option { } } + fn into_bytes(self) -> Vec { + match self { + Some(t) => { + let mut bytes = t.into_bytes(); + bytes.push(1); + bytes + } + None => vec![0], + } + } + #[inline] fn from_bytes(bytes: Cow<[u8]>) -> Self { match bytes.split_last() { @@ -541,6 +662,10 @@ impl Storable for Principal { Cow::Borrowed(self.as_slice()) } + fn into_bytes(self) -> Vec { + self.as_slice().to_vec() + } + #[inline] fn from_bytes(bytes: Cow<[u8]>) -> Self { Self::from_slice(&bytes) diff --git a/src/storable/tests.rs b/src/storable/tests.rs index a735defa..827133a3 100644 --- a/src/storable/tests.rs +++ b/src/storable/tests.rs @@ -139,6 +139,10 @@ fn to_bytes_checked_element_too_long_panics() { Cow::Borrowed(&[1, 2, 3, 4]) } + fn into_bytes(self) -> Vec { + self.to_bytes().into_owned() + } + fn from_bytes(_: Cow<[u8]>) -> Self { unimplemented!(); } @@ -160,6 +164,10 @@ fn to_bytes_checked_unbounded_element_no_panic() { Cow::Borrowed(&[1, 2, 3, 4]) } + fn into_bytes(self) -> Vec { + self.to_bytes().into_owned() + } + fn from_bytes(_: Cow<[u8]>) -> Self { unimplemented!(); } @@ -178,6 +186,10 @@ fn to_bytes_checked_element_correct_size_no_panic() { Cow::Borrowed(&[1, 2, 3, 4]) } + fn into_bytes(self) -> Vec { + self.to_bytes().into_owned() + } + fn from_bytes(_: Cow<[u8]>) -> Self { unimplemented!(); } @@ -200,6 +212,10 @@ fn to_bytes_checked_fixed_element_wrong_size_panics() { Cow::Borrowed(&[1, 2, 3, 4]) } + fn into_bytes(self) -> Vec { + self.to_bytes().into_owned() + } + fn from_bytes(_: Cow<[u8]>) -> Self { unimplemented!(); } @@ -221,6 +237,10 @@ fn to_bytes_checked_fixed_element_correct_size_no_panic() { Cow::Borrowed(&[1, 2, 3, 4, 5]) } + fn into_bytes(self) -> Vec { + self.to_bytes().into_owned() + } + fn from_bytes(_: Cow<[u8]>) -> Self { unimplemented!(); } diff --git a/src/storable/tuples.rs b/src/storable/tuples.rs index e4d8fc8b..7d429da5 100644 --- a/src/storable/tuples.rs +++ b/src/storable/tuples.rs @@ -9,40 +9,11 @@ where B: Storable, { fn to_bytes(&self) -> Cow<[u8]> { - match Self::BOUND { - Bound::Bounded { max_size, .. } => { - let a_bytes = self.0.to_bytes(); - let b_bytes = self.1.to_bytes(); - let a_bounds = bounds::(); - let b_bounds = bounds::(); - let a_max_size = a_bounds.max_size as usize; - let b_max_size = b_bounds.max_size as usize; - let a_size_len = bytes_to_store_size_bounded(&a_bounds) as usize; - let b_size_len = bytes_to_store_size_bounded(&b_bounds) as usize; - let sizes_offset = a_max_size + b_max_size; - - debug_assert!(a_bytes.len() <= a_max_size); - debug_assert!(b_bytes.len() <= b_max_size); - - let mut bytes = vec![0; max_size as usize]; - bytes[..a_bytes.len()].copy_from_slice(a_bytes.as_ref()); - bytes[a_max_size..a_max_size + b_bytes.len()].copy_from_slice(b_bytes.as_ref()); - - encode_size_of_bound( - &mut bytes[sizes_offset..sizes_offset + a_size_len], - a_bytes.len(), - &a_bounds, - ); - encode_size_of_bound( - &mut bytes[sizes_offset + a_size_len..sizes_offset + a_size_len + b_size_len], - b_bytes.len(), - &b_bounds, - ); + Cow::Owned(into_bytes_inner_2(Self::BOUND, &self.0, &self.1)) + } - Cow::Owned(bytes) - } - _ => todo!("Serializing tuples with unbounded types is not yet supported."), - } + fn into_bytes(self) -> Vec { + into_bytes_inner_2(Self::BOUND, &self.0, &self.1) } #[inline] @@ -91,6 +62,48 @@ where }; } +#[inline] +fn into_bytes_inner_2(bound: Bound, a: &A, b: &B) -> Vec +where + A: Storable, + B: Storable, +{ + match bound { + Bound::Bounded { max_size, .. } => { + let a_bytes = a.to_bytes(); + let b_bytes = b.to_bytes(); + let a_bounds = bounds::(); + let b_bounds = bounds::(); + let a_max_size = a_bounds.max_size as usize; + let b_max_size = b_bounds.max_size as usize; + let a_size_len = bytes_to_store_size_bounded(&a_bounds) as usize; + let b_size_len = bytes_to_store_size_bounded(&b_bounds) as usize; + let sizes_offset = a_max_size + b_max_size; + + debug_assert!(a_bytes.len() <= a_max_size); + debug_assert!(b_bytes.len() <= b_max_size); + + let mut bytes = vec![0; max_size as usize]; + bytes[..a_bytes.len()].copy_from_slice(a_bytes.as_ref()); + bytes[a_max_size..a_max_size + b_bytes.len()].copy_from_slice(b_bytes.as_ref()); + + encode_size_of_bound( + &mut bytes[sizes_offset..sizes_offset + a_size_len], + a_bytes.len(), + &a_bounds, + ); + encode_size_of_bound( + &mut bytes[sizes_offset + a_size_len..sizes_offset + a_size_len + b_size_len], + b_bytes.len(), + &b_bounds, + ); + + bytes + } + _ => todo!("Serializing tuples with unbounded types is not yet supported."), + } +} + fn decode_size_of_bound(src: &[u8], bounds: &Bounds) -> usize { if bounds.is_fixed_size { bounds.max_size as usize @@ -243,29 +256,11 @@ where // Otherwise: // fn to_bytes(&self) -> Cow<[u8]> { - let a_bytes = self.0.to_bytes(); - let b_bytes = self.1.to_bytes(); - let c_bytes = self.2.to_bytes(); - let a_size = a_bytes.len(); - let b_size = b_bytes.len(); - let c_size = c_bytes.len(); - - let sizes_overhead = sizes_overhead::(a_size, b_size); - let output_size = a_size + b_size + c_size + sizes_overhead; - let mut bytes = vec![0; output_size]; - let mut offset = 0; - - if sizes_overhead != 0 { - bytes[offset] = encode_size_lengths(&[a_size, b_size]); - offset += 1; - } - - offset += encode_tuple_element::(&mut bytes[offset..], a_bytes.as_ref(), false); - offset += encode_tuple_element::(&mut bytes[offset..], b_bytes.as_ref(), false); - offset += encode_tuple_element::(&mut bytes[offset..], c_bytes.as_ref(), true); + Cow::Owned(into_bytes_inner_3(&self.0, &self.1, &self.2)) + } - debug_assert_eq!(offset, output_size); - Cow::Owned(bytes) + fn into_bytes(self) -> Vec { + into_bytes_inner_3(&self.0, &self.1, &self.2) } #[inline] @@ -316,3 +311,35 @@ where _ => Bound::Unbounded, }; } + +#[inline] +fn into_bytes_inner_3(a: &A, b: &B, c: &C) -> Vec +where + A: Storable, + B: Storable, + C: Storable, +{ + let a_bytes = a.to_bytes(); + let b_bytes = b.to_bytes(); + let c_bytes = c.to_bytes(); + let a_size = a_bytes.len(); + let b_size = b_bytes.len(); + let c_size = c_bytes.len(); + + let sizes_overhead = sizes_overhead::(a_size, b_size); + let output_size = a_size + b_size + c_size + sizes_overhead; + let mut bytes = vec![0; output_size]; + let mut offset = 0; + + if sizes_overhead != 0 { + bytes[offset] = encode_size_lengths(&[a_size, b_size]); + offset += 1; + } + + offset += encode_tuple_element::(&mut bytes[offset..], a_bytes.as_ref(), false); + offset += encode_tuple_element::(&mut bytes[offset..], b_bytes.as_ref(), false); + offset += encode_tuple_element::(&mut bytes[offset..], c_bytes.as_ref(), true); + + debug_assert_eq!(offset, output_size); + bytes +} diff --git a/src/vec/tests.rs b/src/vec/tests.rs index 7895c94d..b4c1bd2d 100644 --- a/src/vec/tests.rs +++ b/src/vec/tests.rs @@ -15,6 +15,10 @@ impl Storable for UnfixedU64 { self.0.to_bytes() } + fn into_bytes(self) -> Vec { + self.0.into_bytes() + } + fn from_bytes(bytes: Cow<[u8]>) -> Self { assert!(bytes.len() == 8); Self(u64::from_bytes(bytes)) @@ -259,6 +263,10 @@ impl crate::Storable for BuggyStruct { Cow::Borrowed(&self.0) } + fn into_bytes(self) -> Vec { + self.to_bytes().into_owned() + } + fn from_bytes(_: Cow<[u8]>) -> Self { unimplemented!(); }