-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathbench.rs
More file actions
924 lines (869 loc) · 32.3 KB
/
Copy pathbench.rs
File metadata and controls
924 lines (869 loc) · 32.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
use canbench_rs::{bench, bench_fn, bench_scope, BenchResult};
use candid::{
CandidType, Decode, DecoderConfig, Deserialize, Encode, IDLArgs, IDLValue, Int, Nat, Principal,
};
use std::collections::BTreeMap;
#[allow(clippy::all)]
mod nns;
const N: usize = 2097152;
const COST: usize = 25_000_000;
const SKIP: usize = 10_000;
#[bench(raw)]
fn blob() -> BenchResult {
use serde_bytes::ByteBuf;
let vec: Vec<u8> = vec![0x61; N];
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&ByteBuf::from(vec)).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, ByteBuf).unwrap();
}
})
}
#[bench(raw)]
fn text() -> BenchResult {
let vec: Vec<u8> = vec![0x61; N];
let text = String::from_utf8(vec).unwrap();
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&text).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, String).unwrap();
}
})
}
#[bench(raw)]
fn vec_int16() -> BenchResult {
let vec: Vec<i16> = vec![-1; N];
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&vec).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, Vec<i16>).unwrap();
}
})
}
#[bench(raw)]
fn vec_nat() -> BenchResult {
let vec: Vec<Nat> = (0u64..262144).map(Nat::from).collect();
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&vec).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, Vec<Nat>).unwrap();
}
})
}
#[bench(raw)]
fn vec_nat64() -> BenchResult {
let vec: Vec<u64> = (0u64..N as u64).collect();
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&vec).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, Vec<u64>).unwrap();
}
})
}
#[bench(raw)]
fn vec_nat32() -> BenchResult {
let vec: Vec<u32> = (0u32..N as u32).collect();
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&vec).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, Vec<u32>).unwrap();
}
})
}
#[bench(raw)]
fn btreemap() -> BenchResult {
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
let n = 1048576;
let map: BTreeMap<String, Nat> = (0u32..n as u32)
.map(|i| (i.to_string(), Nat::from(i)))
.collect();
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&map).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, BTreeMap<String, Nat>).unwrap();
}
})
}
#[bench(raw)]
fn option_list() -> BenchResult {
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
let n = 2048;
#[derive(CandidType, Deserialize)]
struct List {
head: Int,
tail: Option<Box<List>>,
}
let list: Option<Box<List>> = (0..n).fold(None, |acc, x| {
Some(Box::new(List {
head: Int::from(x),
tail: acc,
}))
});
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&list).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, Option<Box<List>>).unwrap();
}
})
}
#[bench(raw)]
fn variant_list() -> BenchResult {
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
let n = 2048;
#[derive(CandidType, Deserialize)]
enum VariantList {
Nil,
Cons(Int, Box<VariantList>),
}
let list: VariantList = (0..n).fold(VariantList::Nil, |acc, x| {
VariantList::Cons(Int::from(x), Box::new(acc))
});
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&list).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, VariantList).unwrap();
}
})
}
#[bench(raw)]
fn nns() -> BenchResult {
use candid_parser::utils::CandidSource;
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
let nns_did = CandidSource::Text(include_str!("./nns.did"));
let motion_proposal = r#"
(
record {
id = opt record { id = 32_768 : nat64 };
command = opt variant {
MakeProposal = record {
url = "http://127.0.0.1";
title = opt "make a proposal";
action = opt variant {
CreateServiceNervousSystem = record {
url = opt "http://127.0.0.1";
governance_parameters = opt record {
neuron_maximum_dissolve_delay_bonus = opt record { basis_points = opt (30 : nat64);};
neuron_maximum_age_for_age_bonus = opt record { seconds = opt (1_000 : nat64);};
neuron_maximum_dissolve_delay = opt record { seconds = opt (2_000 : nat64);};
neuron_minimum_dissolve_delay_to_vote = opt record { seconds = opt (1_000 : nat64);};
neuron_maximum_age_bonus = opt record { basis_points = opt (30 : nat64);};
neuron_minimum_stake = opt record { e8s = opt (500 : nat64);};
proposal_wait_for_quiet_deadline_increase = opt record { seconds = opt (30 : nat64);};
proposal_initial_voting_period = opt record { seconds = opt (10 : nat64);};
proposal_rejection_fee = opt record { e8s = opt (1_000 : nat64);};
voting_reward_parameters = opt record { reward_rate_transition_duration = opt record { seconds = opt (42 : nat64);}; initial_reward_rate = opt record { basis_points = opt (200 : nat64);}; final_reward_rate = opt record { basis_points = opt (300 : nat64);};};
};
fallback_controller_principal_ids = vec {
principal "2vxsx-fae";
principal "aaaaa-aa";
principal "a4gq6-oaaaa-aaaab-qaa4q-cai";
};
logo = opt record { base64_encoding = opt "LOGO_BASE64" };
name = opt "field name";
ledger_parameters = opt record {
transaction_fee = opt record { e8s = opt (1_000 : nat64);};
token_symbol = opt "ICP";
token_logo = opt record { base64_encoding = opt "LOGO_BASE64";};
token_name = opt "ICP";
};
description = opt "description";
dapp_canisters = vec {
record { id = opt principal "a4gq6-oaaaa-aaaab-qaa4q-cai" };
};
swap_parameters = opt record {
minimum_participants = opt (10 : nat64);
neurons_fund_participation = opt true;
duration = opt record { seconds = opt (100 : nat64);};
neuron_basket_construction_parameters = opt record { dissolve_delay_interval = opt record { seconds = opt (20 : nat64);}; count = opt (20 : nat64);};
confirmation_text = opt "confirmation";
maximum_participant_icp = opt record { e8s = opt (10_000 : nat64);};
minimum_icp = opt record { e8s = opt (10 : nat64);};
minimum_direct_participation_icp = opt record { e8s = opt (1 : nat64);};
minimum_participant_icp = opt record { e8s = opt (1 : nat64);};
start_time = opt record { seconds_after_utc_midnight = opt (0 : nat64);};
maximum_direct_participation_icp = opt record { e8s = opt (10_000 : nat64);};
maximum_icp = opt record { e8s = opt (10_000 : nat64);};
neurons_fund_investment_icp = opt record { e8s = opt (1_000 : nat64);};
};
initial_token_distribution = opt record {
treasury_distribution = opt record { total = opt record { e8s = opt (1_000 : nat64);};};
developer_distribution = opt record { developer_neurons = vec { record { controller = opt principal "a4gq6-oaaaa-aaaab-qaa4q-cai"; stake = opt record { e8s = opt (100 : nat64);};};};};
swap_distribution = opt record { total = opt record { e8s = opt (1_000 : nat64);};};
};
}
};
summary = "summary";
}
};
neuron_id_or_subaccount = opt variant { Subaccount = blob "\be\ef" };
},
)
"#;
bench_fn(|| {
let _p = bench_scope("0. Parsing");
let (env, serv) = nns_did.load().unwrap();
let args = candid_parser::parse_idl_args(motion_proposal).unwrap();
let serv = serv.unwrap();
let method = &env.get_method(&serv, "manage_neuron").unwrap();
let arg_tys = method
.args
.iter()
.map(|arg| arg.typ.clone())
.collect::<Vec<_>>();
drop(_p);
let bytes = {
let _p = bench_scope("1. Encoding");
args.to_bytes_with_types(&env, &arg_tys).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, nns::ManageNeuron).unwrap();
}
})
}
#[bench(raw)]
fn nns_list_proposal() -> BenchResult {
use crate::nns::{ListProposalInfoResponse, ProposalInfo};
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
let proposal = ProposalInfo {
id: None,
status: 42,
topic: 42,
failure_reason: None,
ballots: vec![],
proposal_timestamp_seconds: 42,
reward_event_round: 42,
deadline_timestamp_seconds: Some(42),
failed_timestamp_seconds: 42,
reject_cost_e8s: 42,
derived_proposal_information: None,
latest_tally: None,
reward_status: 42,
decided_timestamp_seconds: 42,
proposal: None,
proposer: None,
executed_timestamp_seconds: 42,
};
let list_proposals_info_response = ListProposalInfoResponse {
proposal_info: std::iter::repeat(proposal).take(1000).collect(),
};
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&list_proposals_info_response).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, ListProposalInfoResponse).unwrap();
}
})
}
#[bench(raw)]
fn extra_args() -> BenchResult {
let mut config = DecoderConfig::new();
config.set_skipping_quota(SKIP);
let vec_null = hex::decode("4449444c036c01d6fca702016d026c00010080ade204").unwrap();
let vec_opt_record = hex::decode("4449444c176c02017f027f6c02010002006c02000101016c02000201026c02000301036c02000401046c02000501056c02000601066c02000701076c02000801086c02000901096c02000a010a6c02000b010b6c02000c010c6c02000d020d6c02000e010e6c02000f010f6c02001001106c02001101116c02001201126c02001301136e146d150116050101010101").unwrap();
bench_fn(|| {
assert!(Decode!([config]; &vec_null).is_err());
assert!(Decode!([config]; &vec_opt_record).is_err());
})
}
// Vec of fully populated complex structs (21 fields, nested vecs/maps/opts)
// Exercises the dominant real-world payload shape: list endpoints returning large records.
// The existing nns_list_proposal benchmark uses mostly-empty ProposalInfo; this uses
// fully-populated Neurons which are ~5-10x heavier per element.
#[bench(raw)]
fn nns_list_neurons() -> BenchResult {
use crate::nns::*;
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
let make_neuron = |i: u64| Neuron {
id: Some(NeuronId { id: i }),
staked_maturity_e8s_equivalent: Some(1_000_000),
controller: Some(Principal::from_slice(&i.to_be_bytes())),
recent_ballots: (0..100)
.map(|j| BallotInfo {
vote: if j % 2 == 0 { 1 } else { 2 },
proposal_id: Some(NeuronId { id: j }),
})
.collect(),
kyc_verified: true,
neuron_type: Some(1),
not_for_profit: false,
maturity_e8s_equivalent: 500_000,
cached_neuron_stake_e8s: 10_000_000_000,
created_timestamp_seconds: 1_700_000_000 + i,
auto_stake_maturity: Some(true),
aging_since_timestamp_seconds: 1_700_000_000,
hot_keys: (0..5)
.map(|j| Principal::from_slice(&[j as u8; 10]))
.collect(),
account: serde_bytes::ByteBuf::from(vec![i as u8; 32]),
joined_community_fund_timestamp_seconds: Some(1_700_000_000),
dissolve_state: Some(DissolveState::DissolveDelaySeconds(15_778_800)),
followees: (0..10)
.map(|topic| {
(
topic,
Followees {
followees: (0..5).map(|f| NeuronId { id: f as u64 }).collect(),
},
)
})
.collect(),
neuron_fees_e8s: 10_000,
transfer: Some(NeuronStakeTransfer {
to_subaccount: serde_bytes::ByteBuf::from(vec![0u8; 32]),
neuron_stake_e8s: 10_000_000_000,
from: Some(Principal::from_slice(&[1u8; 10])),
memo: 42,
from_subaccount: serde_bytes::ByteBuf::from(vec![0u8; 32]),
transfer_timestamp: 1_700_000_000,
block_height: 1_000_000,
}),
known_neuron_data: Some(KnownNeuronData {
name: format!("neuron-{}", i),
description: Some(format!("A known neuron #{}", i)),
}),
spawn_at_timestamp_seconds: None,
};
let neuron_infos: Vec<(u64, nns::NeuronInfo)> = (0..100)
.map(|i| {
(
i,
nns::NeuronInfo {
dissolve_delay_seconds: 15_778_800,
recent_ballots: (0..100)
.map(|j| BallotInfo {
vote: 1,
proposal_id: Some(NeuronId { id: j }),
})
.collect(),
neuron_type: Some(1),
created_timestamp_seconds: 1_700_000_000,
state: 1,
stake_e8s: 10_000_000_000,
joined_community_fund_timestamp_seconds: Some(1_700_000_000),
retrieved_at_timestamp_seconds: 1_700_000_000,
known_neuron_data: Some(KnownNeuronData {
name: format!("neuron-{}", i),
description: Some(format!("Known neuron #{}", i)),
}),
voting_power: 20_000_000_000,
age_seconds: 31_557_600,
},
)
})
.collect();
let response = nns::ListNeuronsResponse {
neuron_infos,
full_neurons: (0..100).map(make_neuron).collect(),
};
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&response).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, nns::ListNeuronsResponse).unwrap();
}
})
}
// Schema evolution — encode with a newer type (16 fields), decode with an older
// type (4 fields). Forces the decoder to skip 12 unknown fields per record, exercising
// the field-skipping mechanism that has zero coverage in the existing benchmark.
#[bench(raw)]
fn subtype_decode() -> BenchResult {
#[derive(CandidType)]
struct RecordV2 {
id: u64,
name: String,
balance: u64,
active: bool,
score: f64,
owner: Principal,
data: serde_bytes::ByteBuf,
count: u32,
extra_field_1: String,
extra_field_2: u64,
extra_field_3: Option<String>,
extra_field_4: Vec<u8>,
extra_field_5: bool,
extra_field_6: f64,
extra_field_7: Principal,
extra_field_8: u32,
}
#[derive(CandidType, Deserialize)]
struct RecordV1 {
id: u64,
name: String,
balance: u64,
active: bool,
}
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST);
let records: Vec<RecordV2> = (0..1000)
.map(|i| RecordV2 {
id: i as u64,
name: format!("record-{}", i),
balance: i as u64 * 1000,
active: i % 2 == 0,
score: i as f64 * 1.5,
owner: Principal::from_slice(&(i as u32).to_be_bytes()),
data: serde_bytes::ByteBuf::from(vec![i as u8; 64]),
count: i as u32,
extra_field_1: format!("extra-{}", i),
extra_field_2: i as u64 * 42,
extra_field_3: Some(format!("optional-{}", i)),
extra_field_4: vec![i as u8; 16],
extra_field_5: i % 3 == 0,
extra_field_6: i as f64 * 2.7,
extra_field_7: Principal::from_slice(&(i as u32 + 1000).to_be_bytes()),
extra_field_8: i as u32 * 7,
})
.collect();
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&records).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, Vec<RecordV1>).unwrap();
}
})
}
// Vec of service references (subscriber/registry collection pattern).
// This is the ONLY decode path where check_subtype() fires (deserialize_service
// and deserialize_function). The existing benchmark has zero coverage of this
// code path. See https://github.com/dfinity/candid/issues/603
#[bench(raw)]
fn vec_service() -> BenchResult {
use candid::types::{ArgType, Function, TypeEnv, TypeInner};
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST);
let method_type: candid::types::Type = TypeInner::Func(Function {
modes: vec![],
args: vec![ArgType {
name: None,
typ: TypeInner::Text.into(),
}],
rets: vec![ArgType {
name: None,
typ: TypeInner::Nat64.into(),
}],
})
.into();
let methods: Vec<(String, candid::types::Type)> = (0..20)
.map(|i| (format!("method_{:02}", i), method_type.clone()))
.collect();
let service_type: candid::types::Type = TypeInner::Service(methods).into();
let vec_service_type: candid::types::Type = TypeInner::Vec(service_type).into();
let services: Vec<IDLValue> = (0..1000)
.map(|i| {
let bytes = (i as u64).to_be_bytes();
IDLValue::Service(Principal::from_slice(&bytes))
})
.collect();
let args = IDLArgs::new(&[IDLValue::Vec(services)]);
let env = TypeEnv::new();
let types = vec![vec_service_type.clone()];
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
args.to_bytes_with_types(&env, &types).unwrap()
};
{
let _p = bench_scope("2. Decoding");
IDLArgs::from_bytes_with_types_with_config(&bytes, &env, &types, &config).unwrap();
}
})
}
// Ok/Err discriminated union — the canonical ICP canister return type.
// Every canister method returns variant { Ok: Record; Err: ErrorEnum }.
// Tests heterogeneous variant payloads (struct vs enum) which the existing
// variant_list benchmark (homogeneous recursive) does not cover.
#[bench(raw)]
fn result_variant() -> BenchResult {
#[derive(CandidType, Deserialize)]
struct AccountResponse {
account: Principal,
balance_real: i128,
balance_fake: i128,
balance_ledger: i128,
last_update: u64,
overdraft_limit: u128,
name: String,
}
#[derive(CandidType, Deserialize)]
enum AccountError {
NotAuthorized(Principal),
AccountNotFound,
InsufficientBalance { needed: u128, available: i128 },
InternalError(String),
}
#[derive(CandidType, Deserialize)]
enum AccountResult {
Ok(AccountResponse),
Err(AccountError),
}
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
let results: Vec<AccountResult> = (0..1000)
.map(|i| {
if i % 4 == 0 {
AccountResult::Err(AccountError::InsufficientBalance {
needed: i as u128 * 1000,
available: i as i128 * 100,
})
} else if i % 4 == 1 {
AccountResult::Err(AccountError::NotAuthorized(Principal::from_slice(
&(i as u32).to_be_bytes(),
)))
} else {
AccountResult::Ok(AccountResponse {
account: Principal::from_slice(&(i as u32).to_be_bytes()),
balance_real: i as i128 * 1_000_000,
balance_fake: 0,
balance_ledger: i as i128 * 1_000_000,
last_update: 1_700_000_000 + i as u64,
overdraft_limit: 10_000_000_000,
name: format!("account-{}", i),
})
}
})
.collect();
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&results).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, Vec<AccountResult>).unwrap();
}
})
}
// GovernanceCachedMetrics — 34+ fields including ~10 bucket maps of
// vec record { nat64; float64 }. Tests field hash matching overhead at scale
// and exercises float64 encoding (not covered elsewhere).
#[bench(raw)]
fn wide_record() -> BenchResult {
use crate::nns::GovernanceCachedMetrics;
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
let buckets: Vec<(u64, f64)> = (0..20)
.map(|i| (i * 15_778_800, i as f64 * 1_000_000.0))
.collect();
let count_buckets: Vec<(u64, u64)> = (0..20).map(|i| (i * 15_778_800, i * 100)).collect();
let metrics = GovernanceCachedMetrics {
total_maturity_e8s_equivalent: 50_000_000_000_000,
not_dissolving_neurons_e8s_buckets: buckets.clone(),
dissolving_neurons_staked_maturity_e8s_equivalent_sum: 1_000_000_000,
garbage_collectable_neurons_count: 42,
dissolving_neurons_staked_maturity_e8s_equivalent_buckets: buckets.clone(),
neurons_with_invalid_stake_count: 3,
not_dissolving_neurons_count_buckets: count_buckets.clone(),
ect_neuron_count: 100,
total_supply_icp: 500_000_000_000_000_000,
neurons_with_less_than_6_months_dissolve_delay_count: 5_000,
dissolved_neurons_count: 10_000,
community_fund_total_maturity_e8s_equivalent: 1_000_000_000_000,
total_staked_e8s_seed: 2_000_000_000_000,
total_staked_maturity_e8s_equivalent_ect: 500_000_000,
total_staked_e8s: 100_000_000_000_000_000,
not_dissolving_neurons_count: 30_000,
total_locked_e8s: 80_000_000_000_000_000,
neurons_fund_total_active_neurons: 200,
total_staked_maturity_e8s_equivalent: 5_000_000_000_000,
not_dissolving_neurons_e8s_buckets_ect: buckets.clone(),
total_staked_e8s_ect: 3_000_000_000_000,
not_dissolving_neurons_staked_maturity_e8s_equivalent_sum: 2_000_000_000,
dissolved_neurons_e8s: 500_000_000_000,
dissolving_neurons_e8s_buckets_seed: buckets.clone(),
neurons_with_less_than_6_months_dissolve_delay_e8s: 1_000_000_000_000,
not_dissolving_neurons_staked_maturity_e8s_equivalent_buckets: buckets.clone(),
dissolving_neurons_count_buckets: count_buckets.clone(),
dissolving_neurons_e8s_buckets_ect: buckets.clone(),
dissolving_neurons_count: 15_000,
dissolving_neurons_e8s_buckets: buckets.clone(),
total_staked_maturity_e8s_equivalent_seed: 700_000_000,
community_fund_total_staked_e8s: 5_000_000_000_000,
not_dissolving_neurons_e8s_buckets_seed: buckets,
timestamp_seconds: 1_700_000_000,
seed_neuron_count: 500,
};
let metrics_vec: Vec<GovernanceCachedMetrics> = std::iter::repeat(metrics).take(100).collect();
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&metrics_vec).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, Vec<GovernanceCachedMetrics>).unwrap();
}
})
}
// 21-arm enum with mixed payload shapes (unit, tuple, struct variants).
// The existing variant_list only tests a 2-arm recursive variant. This tests
// variant tag matching overhead at scale with heterogeneous payloads.
#[bench(raw)]
fn large_variant() -> BenchResult {
#[derive(CandidType, Deserialize, Clone)]
enum LargeAction {
Transfer {
from: Principal,
to: Principal,
amount: u64,
memo: Option<u64>,
},
Approve {
spender: Principal,
amount: u64,
expires_at: Option<u64>,
},
Burn {
amount: u64,
},
Mint {
to: Principal,
amount: u64,
},
SetFee(u64),
SetAdmin(Principal),
Pause,
Unpause,
Upgrade(serde_bytes::ByteBuf),
AddMinter(Principal),
RemoveMinter(Principal),
SetName(String),
SetSymbol(String),
SetLogo(String),
SetMetadata {
key: String,
value: String,
},
CreateProposal {
title: String,
summary: String,
url: String,
},
Vote {
proposal_id: u64,
vote: bool,
},
Execute(u64),
Reject(u64),
RegisterNeuron {
stake: u64,
dissolve_delay: u64,
},
DisburseNeuron {
neuron_id: u64,
to: Principal,
amount: Option<u64>,
},
}
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
let actions: Vec<LargeAction> = (0..2100)
.map(|i| {
let p = Principal::from_slice(&(i as u32).to_be_bytes());
match i % 21 {
0 => LargeAction::Transfer {
from: p,
to: p,
amount: i as u64 * 100,
memo: Some(42),
},
1 => LargeAction::Approve {
spender: p,
amount: i as u64,
expires_at: Some(1_700_000_000),
},
2 => LargeAction::Burn {
amount: i as u64 * 50,
},
3 => LargeAction::Mint {
to: p,
amount: i as u64 * 1000,
},
4 => LargeAction::SetFee(i as u64),
5 => LargeAction::SetAdmin(p),
6 => LargeAction::Pause,
7 => LargeAction::Unpause,
8 => LargeAction::Upgrade(serde_bytes::ByteBuf::from(vec![i as u8; 32])),
9 => LargeAction::AddMinter(p),
10 => LargeAction::RemoveMinter(p),
11 => LargeAction::SetName(format!("name-{}", i)),
12 => LargeAction::SetSymbol(format!("SYM{}", i)),
13 => LargeAction::SetLogo(format!("https://example.com/logo-{}.png", i)),
14 => LargeAction::SetMetadata {
key: format!("key-{}", i),
value: format!("val-{}", i),
},
15 => LargeAction::CreateProposal {
title: format!("Proposal {}", i),
summary: format!("Summary for {}", i),
url: format!("https://example.com/{}", i),
},
16 => LargeAction::Vote {
proposal_id: i as u64,
vote: i % 2 == 0,
},
17 => LargeAction::Execute(i as u64),
18 => LargeAction::Reject(i as u64),
19 => LargeAction::RegisterNeuron {
stake: i as u64 * 100_000_000,
dissolve_delay: 15_778_800,
},
_ => LargeAction::DisburseNeuron {
neuron_id: i as u64,
to: p,
amount: Some(i as u64 * 100),
},
}
})
.collect();
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&actions).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, Vec<LargeAction>).unwrap();
}
})
}
// Option<Option<T>> — "3-state update" semantics used in real ICP APIs.
// None = don't change, Some(None) = clear, Some(Some(x)) = set.
// Exercises Candid's opt subtyping rules with double-wrapped optionals.
#[bench(raw)]
fn double_option() -> BenchResult {
#[derive(CandidType, Deserialize)]
struct UpdateRequest {
target_balance: Option<Option<i128>>,
expiration: Option<Option<u64>>,
spending_limit: Option<Option<u128>>,
description: Option<Option<String>>,
account: Principal,
}
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
let requests: Vec<UpdateRequest> = (0..1000)
.map(|i| match i % 3 {
0 => UpdateRequest {
target_balance: None,
expiration: None,
spending_limit: None,
description: None,
account: Principal::from_slice(&(i as u32).to_be_bytes()),
},
1 => UpdateRequest {
target_balance: Some(None),
expiration: Some(None),
spending_limit: Some(None),
description: Some(None),
account: Principal::from_slice(&(i as u32).to_be_bytes()),
},
_ => UpdateRequest {
target_balance: Some(Some(i as i128 * 1_000_000)),
expiration: Some(Some(1_700_000_000 + i as u64)),
spending_limit: Some(Some(i as u128 * 500)),
description: Some(Some(format!("update-{}", i))),
account: Principal::from_slice(&(i as u32).to_be_bytes()),
},
})
.collect();
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&requests).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, Vec<UpdateRequest>).unwrap();
}
})
}
// Multi-argument Encode!/Decode! — real canister calls often pass multiple
// arguments (e.g., principal + amount + memo). This exercises a different
// internal code path than single-struct encoding.
#[bench(raw)]
fn multi_arg() -> BenchResult {
let mut config = DecoderConfig::new();
config.set_decoding_quota(COST).set_skipping_quota(SKIP);
let principals: Vec<Principal> = (0..1000)
.map(|i| Principal::from_slice(&(i as u64).to_be_bytes()))
.collect();
let amounts: Vec<u64> = (0..1000).map(|i| i * 1_000_000).collect();
let memos: Vec<String> = (0..1000).map(|i| format!("memo-{}", i)).collect();
bench_fn(|| {
let bytes = {
let _p = bench_scope("1. Encoding");
Encode!(&principals, &amounts, &memos).unwrap()
};
{
let _p = bench_scope("2. Decoding");
Decode!([config]; &bytes, Vec<Principal>, Vec<u64>, Vec<String>).unwrap();
}
})
}
fn main() {}