| 
									
										
										
										
											2020-09-19 12:17:46 -07:00
										 |  |  | #![feature(test)]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | extern crate test;
 | 
					
						
							| 
									
										
										
										
											2021-12-03 09:00:31 -08:00
										 |  |  | use {
 | 
					
						
							|  |  |  |     bincode::{deserialize, serialize},
 | 
					
						
							|  |  |  |     solana_sdk::{
 | 
					
						
							|  |  |  |         instruction::{AccountMeta, Instruction},
 | 
					
						
							|  |  |  |         message::{Message, SanitizedMessage},
 | 
					
						
							|  |  |  |         pubkey::{self, Pubkey},
 | 
					
						
							| 
									
										
										
										
											2022-01-20 17:33:49 +08:00
										 |  |  |         sysvar::instructions::{self, construct_instructions_data},
 | 
					
						
							| 
									
										
										
										
											2021-12-03 09:00:31 -08:00
										 |  |  |     },
 | 
					
						
							|  |  |  |     std::convert::TryFrom,
 | 
					
						
							|  |  |  |     test::Bencher,
 | 
					
						
							|  |  |  | };
 | 
					
						
							| 
									
										
										
										
											2020-09-19 12:17:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | fn make_instructions() -> Vec<Instruction> {
 | 
					
						
							| 
									
										
										
										
											2020-10-19 12:23:14 -07:00
										 |  |  |     let meta = AccountMeta::new(pubkey::new_rand(), false);
 | 
					
						
							| 
									
										
										
										
											2021-03-03 21:46:48 -08:00
										 |  |  |     let inst = Instruction::new_with_bincode(pubkey::new_rand(), &[0; 10], vec![meta; 4]);
 | 
					
						
							| 
									
										
										
										
											2020-09-19 12:17:46 -07:00
										 |  |  |     vec![inst; 4]
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[bench]
 | 
					
						
							|  |  |  | fn bench_bincode_instruction_serialize(b: &mut Bencher) {
 | 
					
						
							|  |  |  |     let instructions = make_instructions();
 | 
					
						
							|  |  |  |     b.iter(|| {
 | 
					
						
							|  |  |  |         test::black_box(serialize(&instructions).unwrap());
 | 
					
						
							|  |  |  |     });
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[bench]
 | 
					
						
							| 
									
										
										
										
											2022-01-20 17:33:49 +08:00
										 |  |  | fn bench_construct_instructions_data(b: &mut Bencher) {
 | 
					
						
							| 
									
										
										
										
											2020-09-19 12:17:46 -07:00
										 |  |  |     let instructions = make_instructions();
 | 
					
						
							| 
									
										
										
										
											2021-08-17 15:17:56 -07:00
										 |  |  |     let message =
 | 
					
						
							|  |  |  |         SanitizedMessage::try_from(Message::new(&instructions, Some(&Pubkey::new_unique())))
 | 
					
						
							|  |  |  |             .unwrap();
 | 
					
						
							| 
									
										
										
										
											2020-09-19 12:17:46 -07:00
										 |  |  |     b.iter(|| {
 | 
					
						
							| 
									
										
										
										
											2022-01-20 17:33:49 +08:00
										 |  |  |         let instructions = message.decompile_instructions();
 | 
					
						
							|  |  |  |         test::black_box(construct_instructions_data(&instructions));
 | 
					
						
							| 
									
										
										
										
											2020-09-19 12:17:46 -07:00
										 |  |  |     });
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[bench]
 | 
					
						
							|  |  |  | fn bench_bincode_instruction_deserialize(b: &mut Bencher) {
 | 
					
						
							|  |  |  |     let instructions = make_instructions();
 | 
					
						
							|  |  |  |     let serialized = serialize(&instructions).unwrap();
 | 
					
						
							|  |  |  |     b.iter(|| {
 | 
					
						
							|  |  |  |         test::black_box(deserialize::<Vec<Instruction>>(&serialized).unwrap());
 | 
					
						
							|  |  |  |     });
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[bench]
 | 
					
						
							|  |  |  | fn bench_manual_instruction_deserialize(b: &mut Bencher) {
 | 
					
						
							|  |  |  |     let instructions = make_instructions();
 | 
					
						
							| 
									
										
										
										
											2021-08-17 15:17:56 -07:00
										 |  |  |     let message =
 | 
					
						
							|  |  |  |         SanitizedMessage::try_from(Message::new(&instructions, Some(&Pubkey::new_unique())))
 | 
					
						
							|  |  |  |             .unwrap();
 | 
					
						
							| 
									
										
										
										
											2022-01-20 17:33:49 +08:00
										 |  |  |     let serialized = construct_instructions_data(&message.decompile_instructions());
 | 
					
						
							| 
									
										
										
										
											2020-09-19 12:17:46 -07:00
										 |  |  |     b.iter(|| {
 | 
					
						
							|  |  |  |         for i in 0..instructions.len() {
 | 
					
						
							| 
									
										
										
										
											2021-10-19 21:01:58 -07:00
										 |  |  |             #[allow(deprecated)]
 | 
					
						
							| 
									
										
										
										
											2020-09-20 10:58:12 -07:00
										 |  |  |             test::black_box(instructions::load_instruction_at(i, &serialized).unwrap());
 | 
					
						
							| 
									
										
										
										
											2020-09-19 12:17:46 -07:00
										 |  |  |         }
 | 
					
						
							|  |  |  |     });
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[bench]
 | 
					
						
							|  |  |  | fn bench_manual_instruction_deserialize_single(b: &mut Bencher) {
 | 
					
						
							|  |  |  |     let instructions = make_instructions();
 | 
					
						
							| 
									
										
										
										
											2021-08-17 15:17:56 -07:00
										 |  |  |     let message =
 | 
					
						
							|  |  |  |         SanitizedMessage::try_from(Message::new(&instructions, Some(&Pubkey::new_unique())))
 | 
					
						
							|  |  |  |             .unwrap();
 | 
					
						
							| 
									
										
										
										
											2022-01-20 17:33:49 +08:00
										 |  |  |     let serialized = construct_instructions_data(&message.decompile_instructions());
 | 
					
						
							| 
									
										
										
										
											2020-09-19 12:17:46 -07:00
										 |  |  |     b.iter(|| {
 | 
					
						
							| 
									
										
										
										
											2021-10-19 21:01:58 -07:00
										 |  |  |         #[allow(deprecated)]
 | 
					
						
							| 
									
										
										
										
											2020-09-20 10:58:12 -07:00
										 |  |  |         test::black_box(instructions::load_instruction_at(3, &serialized).unwrap());
 | 
					
						
							| 
									
										
										
										
											2020-09-19 12:17:46 -07:00
										 |  |  |     });
 | 
					
						
							|  |  |  | }
 |