once_cell has now been merged into rust core. This removes the
lazy_static dependency and migrates over to the built in `OnceLock`.
Its always good to remove dependencies where possible, this also give us
a preference for the built in `OnceLock`
```
Benchmark 1: chore: add benchmark for `set_items`
Time (mean ± σ): 6.327 s ± 0.199 s [User: 15.316 s, System: 1.323 s]
Range (min … max): 6.087 s … 6.712 s 10 runs
Benchmark 2: refactor: remove lazy_static
Time (mean ± σ): 6.171 s ± 0.251 s [User: 15.223 s, System: 1.382 s]
Range (min … max): 5.910 s … 6.776 s 10 runs
Summary
'refactor: remove lazy_static' ran
1.03 ± 0.05 times faster than 'chore: add benchmark for `set_items`'
```
107 lines
2.5 KiB
Rust
107 lines
2.5 KiB
Rust
mod finder;
|
|
mod matcher;
|
|
mod sorter;
|
|
|
|
use std::collections::HashMap;
|
|
use std::ffi::CStr;
|
|
use std::ffi::CString;
|
|
use std::os::raw::{c_char, c_int};
|
|
use std::sync::Mutex;
|
|
use std::sync::OnceLock;
|
|
|
|
struct Ivy {
|
|
pub file_cache: HashMap<String, Vec<String>>,
|
|
}
|
|
|
|
static INSTANCE: OnceLock<Mutex<Ivy>> = OnceLock::new();
|
|
|
|
impl Ivy {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
file_cache: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
pub fn global() -> &'static Mutex<Ivy> {
|
|
INSTANCE.get_or_init(|| Mutex::new(Ivy::new()))
|
|
}
|
|
}
|
|
|
|
fn to_string(input: *const c_char) -> String {
|
|
unsafe { CStr::from_ptr(input) }
|
|
.to_str()
|
|
.unwrap()
|
|
.to_string()
|
|
}
|
|
|
|
fn get_files(directory: &String) -> Vec<String> {
|
|
let mut ivy = Ivy::global().lock().unwrap();
|
|
if !ivy.file_cache.contains_key(directory) {
|
|
let finder_options = finder::Options {
|
|
directory: directory.clone(),
|
|
};
|
|
|
|
ivy.file_cache
|
|
.insert(directory.clone(), finder::find_files(finder_options));
|
|
}
|
|
|
|
return ivy.file_cache.get(directory).unwrap().to_vec();
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn ivy_init(c_base_dir: *const c_char) {
|
|
let directory = to_string(c_base_dir);
|
|
get_files(&directory);
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn ivy_cwd() -> *const c_char {
|
|
return CString::new(std::env::current_dir().unwrap().to_str().unwrap())
|
|
.unwrap()
|
|
.into_raw();
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn ivy_match(c_pattern: *const c_char, c_text: *const c_char) -> c_int {
|
|
let pattern = to_string(c_pattern);
|
|
let text = to_string(c_text);
|
|
|
|
inner_match(pattern, text)
|
|
}
|
|
|
|
pub fn inner_match(pattern: String, text: String) -> i32 {
|
|
let m = matcher::Matcher::new(pattern);
|
|
|
|
m.score(text.as_str()) as i32
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn ivy_files(c_pattern: *const c_char, c_base_dir: *const c_char) -> *const c_char {
|
|
let pattern = to_string(c_pattern);
|
|
let directory = to_string(c_base_dir);
|
|
|
|
let output = inner_files(pattern, directory);
|
|
|
|
CString::new(output).unwrap().into_raw()
|
|
}
|
|
|
|
pub fn inner_files(pattern: String, base_dir: String) -> String {
|
|
let mut output = String::new();
|
|
|
|
// Bail out early if the pattern is empty; it's never going to find anything
|
|
if pattern.is_empty() {
|
|
return output;
|
|
}
|
|
|
|
let files = get_files(&base_dir);
|
|
|
|
let sorter_options = sorter::Options::new(pattern);
|
|
|
|
let files = sorter::sort_strings(sorter_options, files);
|
|
for file in files.iter() {
|
|
output.push_str(&file.content);
|
|
output.push('\n');
|
|
}
|
|
|
|
output
|
|
}
|