The C and Rust versions are now comparable.
The C and Rust versions are now comparable, with a memory-reuse and a memory-safe version for Rust. The memory-safe version is five times faster than the C version; the memory-reuse version (technically safe, but can panic under some very rare circumstances) is ten times faster. I suspect the reasons for he speedup are strictly in the `for()` loop in the C version for copying the string, where the Rust version probably uses memcpy() under the covers to transfer the short string into the destination.
This commit is contained in:
parent
2eab17934c
commit
40811151ab
|
@ -1,6 +1,6 @@
|
||||||
#include "patprep.h"
|
#include "patprep.h"
|
||||||
|
|
||||||
const int count = 5 * 1000 * 1000 * 1000;
|
const int count = 5 * 1000 * 1000 * 100;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
for (int i = 0; i <= count; i++) {
|
for (int i = 0; i <= count; i++) {
|
||||||
|
|
|
@ -28,6 +28,12 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prepare_pattern(name: &[u8]) -> Vec<u8> {
|
pub fn prepare_pattern(name: &[u8]) -> Vec<u8> {
|
||||||
|
let mut dest = Vec::with_capacity(116);
|
||||||
|
prepare_pattern_raw(name, &mut dest);
|
||||||
|
dest
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn prepare_pattern_raw(name: &[u8], dest: &mut Vec<u8>) {
|
||||||
let mut eol = name.len();
|
let mut eol = name.len();
|
||||||
if eol == 0 {
|
if eol == 0 {
|
||||||
panic!("Library error - This function should never be called with an empty string.")
|
panic!("Library error - This function should never be called with an empty string.")
|
||||||
|
@ -43,10 +49,12 @@ pub fn prepare_pattern(name: &[u8]) -> Vec<u8> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if eol == 0 {
|
if eol == 0 {
|
||||||
return if GLOBCHARS.contains(&name[0]) {
|
if GLOBCHARS.contains(&name[0]) {
|
||||||
vec![b'/']
|
dest.push(b'/');
|
||||||
|
return;
|
||||||
} else {
|
} else {
|
||||||
vec![name[0]]
|
dest.push(name[0]);
|
||||||
|
return;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,9 +65,9 @@ pub fn prepare_pattern(name: &[u8]) -> Vec<u8> {
|
||||||
start
|
start
|
||||||
};
|
};
|
||||||
if start > eol {
|
if start > eol {
|
||||||
vec![b'/']
|
dest.push(b'/');
|
||||||
} else {
|
} else {
|
||||||
name[start..eol + 1].to_vec()
|
dest.extend_from_slice(&name[start..eol + 1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue