Skip to content

Commit 7e9119b

Browse files
committed
[SOL] Optimize memory operations (anza-xyz#31)
1 parent 89e0af7 commit 7e9119b

File tree

1 file changed

+7
-117
lines changed
  • compiler-builtins/src/mem

1 file changed

+7
-117
lines changed

compiler-builtins/src/mem/mod.rs

Lines changed: 7 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ intrinsics! {
6767
// number of store operations is greater than 8 the memory operation
6868
// is performed in the run-time system instead, by calling the
6969
// corresponding "C" function.
70-
7170
#[cfg(all(target_os = "solana", not(target_feature = "static-syscalls")))]
7271
mod syscalls {
7372
extern "C" {
@@ -108,36 +107,14 @@ mod syscalls {
108107
#[cfg(target_os = "solana")]
109108
use self::syscalls::*;
110109

111-
#[cfg(target_os = "solana")]
112-
const NSTORE_THRESHOLD: usize = 15;
113-
114110
#[cfg(target_os = "solana")]
115111
#[cfg_attr(
116112
all(feature = "mem-unaligned", not(feature = "mangled-names")),
117113
no_mangle
118114
)]
119115
#[inline]
120116
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
121-
let chunks = (n / 8) as isize;
122-
let nstore = n - (7 * chunks) as usize;
123-
if nstore > NSTORE_THRESHOLD {
124-
sol_memcpy_(dest, src, n as u64);
125-
return dest;
126-
}
127-
let mut i: isize = 0;
128-
if chunks != 0 {
129-
let dest_64 = dest as *mut _ as *mut u64;
130-
let src_64 = src as *const _ as *const u64;
131-
while i < chunks {
132-
*dest_64.offset(i) = *src_64.offset(i);
133-
i += 1;
134-
}
135-
i *= 8;
136-
}
137-
while i < n as isize {
138-
*dest.offset(i) = *src.offset(i);
139-
i += 1;
140-
}
117+
sol_memcpy_(dest, src, n as u64);
141118
dest
142119
}
143120

@@ -148,45 +125,7 @@ pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut
148125
)]
149126
#[inline]
150127
pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
151-
let chunks = (n / 8) as isize;
152-
let nstore = n - (7 * chunks) as usize;
153-
if nstore > NSTORE_THRESHOLD {
154-
sol_memmove_(dest, src, n as u64);
155-
return dest;
156-
}
157-
if src < dest as *const u8 {
158-
// copy from end
159-
let mut i = n as isize;
160-
while i > chunks * 8 {
161-
i -= 1;
162-
*dest.offset(i) = *src.offset(i);
163-
}
164-
i = chunks;
165-
if i > 0 {
166-
let dest_64 = dest as *mut _ as *mut u64;
167-
let src_64 = src as *const _ as *const u64;
168-
while i > 0 {
169-
i -= 1;
170-
*dest_64.offset(i) = *src_64.offset(i);
171-
}
172-
}
173-
} else {
174-
// copy from beginning
175-
let mut i: isize = 0;
176-
if chunks != 0 {
177-
let dest_64 = dest as *mut _ as *mut u64;
178-
let src_64 = src as *const _ as *const u64;
179-
while i < chunks {
180-
*dest_64.offset(i) = *src_64.offset(i);
181-
i += 1;
182-
}
183-
i *= 8;
184-
}
185-
while i < n as isize {
186-
*dest.offset(i) = *src.offset(i);
187-
i += 1;
188-
}
189-
}
128+
sol_memmove_(dest, src, n as u64);
190129
dest
191130
}
192131

@@ -197,29 +136,7 @@ pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mu
197136
)]
198137
#[inline]
199138
pub unsafe extern "C" fn memset(s: *mut u8, c: c_int, n: usize) -> *mut u8 {
200-
let chunks = (n / 8) as isize;
201-
let nstore = n - (7 * chunks) as usize;
202-
if nstore > NSTORE_THRESHOLD {
203-
sol_memset_(s, c as u8, n as u64);
204-
return s;
205-
}
206-
let mut i: isize = 0;
207-
if chunks != 0 {
208-
let mut c_64 = c as u64 & 0xFF as u64;
209-
c_64 |= c_64 << 8;
210-
c_64 |= c_64 << 16;
211-
c_64 |= c_64 << 32;
212-
let s_64 = s as *mut _ as *mut u64;
213-
while i < chunks {
214-
*s_64.offset(i) = c_64;
215-
i += 1;
216-
}
217-
i *= 8;
218-
}
219-
while i < n as isize {
220-
*s.offset(i) = c as u8;
221-
i += 1;
222-
}
139+
sol_memset_(s, c as u8, n as u64);
223140
s
224141
}
225142

@@ -230,34 +147,7 @@ pub unsafe extern "C" fn memset(s: *mut u8, c: c_int, n: usize) -> *mut u8 {
230147
)]
231148
#[inline]
232149
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
233-
let chunks = (n / 8) as isize;
234-
let nstore = n - (7 * chunks) as usize;
235-
if nstore > NSTORE_THRESHOLD {
236-
let mut result = 0;
237-
sol_memcmp_(s1, s2, n as u64, &mut result as *mut i32);
238-
return result;
239-
}
240-
let mut i: isize = 0;
241-
if chunks != 0 {
242-
let s1_64 = s1 as *const _ as *const u64;
243-
let s2_64 = s2 as *const _ as *const u64;
244-
while i < chunks {
245-
let a = *s1_64.offset(i);
246-
let b = *s2_64.offset(i);
247-
if a != b {
248-
break;
249-
}
250-
i += 1;
251-
}
252-
i *= 8;
253-
}
254-
while i < n as isize {
255-
let a = *s1.offset(i);
256-
let b = *s2.offset(i);
257-
if a != b {
258-
return a as i32 - b as i32;
259-
}
260-
i += 1;
261-
}
262-
0
263-
}
150+
let mut result = 0;
151+
sol_memcmp_(s1, s2, n as u64, &mut result as *mut i32);
152+
result
153+
}

0 commit comments

Comments
 (0)