Compare commits

...

2 Commits

Author SHA1 Message Date
Elf M. Sternberg a6d4fda582 Minor re-arrangement for compatibiility. 2022-11-13 12:40:39 -08:00
Elf M. Sternberg 40811151ab 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.
2022-11-13 12:33:34 -08:00
3 changed files with 20 additions and 10 deletions

View File

@ -1,11 +1,13 @@
use squozen::prepare_pattern::prepare_pattern;
use squozen::prepare_pattern::prepare_pattern_raw;
const COUNT: usize = 5 * 1000 * 1000 * 100;
fn main() {
let mut end = COUNT;
let mut dest = Vec::<u8>::with_capacity(100);
while end > 0 {
let _g = prepare_pattern(b"/foo/bar/whatever[0-9]*");
dest.clear();
prepare_pattern_raw(b"/foo/bar/whatever[0-9]*", &mut dest);
end = end - 1;
}
}

View File

@ -1,6 +1,6 @@
#include "patprep.h"
const int count = 5 * 1000 * 1000 * 1000;
const int count = 5 * 1000 * 1000 * 100;
void main() {
for (int i = 0; i <= count; i++) {

View File

@ -28,7 +28,13 @@ where
}
pub fn prepare_pattern(name: &[u8]) -> Vec<u8> {
let mut eol = name.len();
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 eol = name.len();
if eol == 0 {
panic!("Library error - This function should never be called with an empty string.")
}
@ -36,17 +42,19 @@ pub fn prepare_pattern(name: &[u8]) -> Vec<u8> {
// After this point, eol always points to the index from where we want to
// stop, not to the character beyond that.
eol = hunt(name, eol - 1, 0, |&c| c != b'*' && c != b'?');
let mut eol = hunt(name, eol - 1, 0, |&c| c != b'*' && c != b'?');
if name[eol] == b']' {
eol = hunt(&name, eol - 1, 0, |&c| c == b'[');
eol = if eol > 0 { eol - 1 } else { 0 }
}
if eol == 0 {
return if GLOBCHARS.contains(&name[0]) {
vec![b'/']
if GLOBCHARS.contains(&name[0]) {
dest.push(b'/');
return;
} else {
vec![name[0]]
dest.push(name[0]);
return;
};
}
@ -57,9 +65,9 @@ pub fn prepare_pattern(name: &[u8]) -> Vec<u8> {
start
};
if start > eol {
vec![b'/']
dest.push(b'/');
} else {
name[start..eol + 1].to_vec()
dest.extend_from_slice(&name[start..eol + 1]);
}
}