-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaccess.rs
More file actions
123 lines (108 loc) · 3.36 KB
/
access.rs
File metadata and controls
123 lines (108 loc) · 3.36 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
use std::ptr::read_unaligned;
use light_program_profiler::profile;
use pinocchio::{program_error::ProgramError, pubkey::Pubkey};
use smallvec::SmallVec;
// Helper function to create a pre-populated SmallVec - NOT profiled
pub fn create_populated_vec(_program_id: &Pubkey) -> SmallVec<[Pubkey; 10]> {
let mut vec: SmallVec<[Pubkey; 10]> = SmallVec::new();
for i in 0..10 {
vec.push(Pubkey::from([(i + 1) as u8; 32]));
}
vec
}
// Helper: Create vec with target at first position (1 iteration to find)
pub fn create_vec_target_first(target: &Pubkey, filler: &Pubkey) -> SmallVec<[Pubkey; 10]> {
let mut vec: SmallVec<[Pubkey; 10]> = SmallVec::new();
vec.push(*target);
for _ in 1..10 {
vec.push(*filler);
}
vec
}
// Helper: Create vec with target at last position (10 iterations to find)
pub fn create_vec_target_last(target: &Pubkey, filler: &Pubkey) -> SmallVec<[Pubkey; 10]> {
let mut vec: SmallVec<[Pubkey; 10]> = SmallVec::new();
for _ in 0..9 {
vec.push(*filler);
}
vec.push(*target);
vec
}
#[profile]
pub fn get_first_pubkey(vec: &SmallVec<[Pubkey; 10]>) -> Result<&Pubkey, ProgramError> {
vec.get(0).ok_or(ProgramError::InvalidAccountData)
}
#[profile]
pub fn get_10th_pubkey(vec: &SmallVec<[Pubkey; 10]>) -> Result<&Pubkey, ProgramError> {
vec.get(9).ok_or(ProgramError::InvalidAccountData)
}
#[profile]
pub fn find_pubkey_1_iters<'a>(
vec: &'a SmallVec<[Pubkey; 10]>,
target: &Pubkey,
) -> Result<&'a Pubkey, ProgramError> {
vec.iter()
.find(|x| pubkey_eq(x, target))
.ok_or(ProgramError::InvalidAccountData)
}
#[profile]
pub fn find_pubkey_10_iters<'a>(
vec: &'a SmallVec<[Pubkey; 10]>,
target: &Pubkey,
) -> Result<&'a Pubkey, ProgramError> {
vec.iter()
.find(|x| pubkey_eq(x, target))
.ok_or(ProgramError::InvalidAccountData)
}
#[profile]
pub fn position_pubkey_1_iters(
vec: &SmallVec<[Pubkey; 10]>,
target: &Pubkey,
) -> Option<usize> {
vec.iter().position(|x| pubkey_eq(x, target))
}
#[profile]
pub fn position_pubkey_10_iters(
vec: &SmallVec<[Pubkey; 10]>,
target: &Pubkey,
) -> Option<usize> {
vec.iter().position(|x| pubkey_eq(x, target))
}
#[profile]
pub fn update_index(vec: &mut SmallVec<[Pubkey; 10]>, index: usize, new_value: &Pubkey) {
vec[index] = *new_value;
}
#[profile]
pub fn update_get_mut(
vec: &mut SmallVec<[Pubkey; 10]>,
index: usize,
new_value: &Pubkey,
) -> Result<(), ProgramError> {
let element = vec.get_mut(index).ok_or(ProgramError::InvalidAccountData)?;
*element = *new_value;
Ok(())
}
#[profile]
pub fn update_iter_mut_find(
vec: &mut SmallVec<[Pubkey; 10]>,
target: &Pubkey,
new_value: &Pubkey,
) -> Result<(), ProgramError> {
let element = vec
.iter_mut()
.find(|x| pubkey_eq(x, target))
.ok_or(ProgramError::InvalidAccountData)?;
*element = *new_value;
Ok(())
}
#[inline(always)]
pub const fn pubkey_eq(p1: &Pubkey, p2: &Pubkey) -> bool {
let p1_ptr = p1.as_ptr() as *const u64;
let p2_ptr = p2.as_ptr() as *const u64;
unsafe {
read_unaligned(p1_ptr) == read_unaligned(p2_ptr)
&& read_unaligned(p1_ptr.add(1)) == read_unaligned(p2_ptr.add(1))
&& read_unaligned(p1_ptr.add(2)) == read_unaligned(p2_ptr.add(2))
&& read_unaligned(p1_ptr.add(3)) == read_unaligned(p2_ptr.add(3))
}
}