Format and clippy

This commit is contained in:
Xymist 2022-08-26 10:25:05 +01:00
parent ac01e9e9a4
commit 12a1a64c54
7 changed files with 47 additions and 39 deletions

View file

@ -78,7 +78,7 @@ to optimize, you will probably need to get a baseline on your hardware.
There are fixtures provided that will create the directory structure of the There are fixtures provided that will create the directory structure of the
[kubernetes](https://github.com/kubernetes/kubernetes) source code, from [kubernetes](https://github.com/kubernetes/kubernetes) source code, from
somewhere arround commit sha 985c9202ccd250a5fe22c01faf0d8f83d804b9f3. This will somewhere around commit sha 985c9202ccd250a5fe22c01faf0d8f83d804b9f3. This will
create a directory tree of 23511 files a relative large source tree to get a create a directory tree of 23511 files a relative large source tree to get a
good idea of performance. To create the source tree under good idea of performance. To create the source tree under
`/tmp/ivy-trees/kubernetes` run the following command. This will need to be run `/tmp/ivy-trees/kubernetes` run the following command. This will need to be run
@ -95,16 +95,16 @@ luajit ./scripts/benchmark.lua
Current benchmark status running on a `e2-standard-2` 2 vCPU + 8 GB memory VM Current benchmark status running on a `e2-standard-2` 2 vCPU + 8 GB memory VM
running on GCP. running on GCP.
Rust Rust
| Name | Total | Adverage | Min | Max | | Name | Total | Average | Min | Max |
|--------------------------------|---------------|---------------|---------------|---------------| |--------------------------------|---------------|---------------|---------------|---------------|
| ivy_match(file.lua) 1000000x | 03.961640 (s) | 00.000004 (s) | 00.000003 (s) | 00.002146 (s) | | ivy_match(file.lua) 1000000x | 03.961640 (s) | 00.000004 (s) | 00.000003 (s) | 00.002146 (s) |
| ivy_files(kubernetes) 100x | 03.895758 (s) | 00.038958 (s) | 00.034903 (s) | 00.043660 (s) | | ivy_files(kubernetes) 100x | 03.895758 (s) | 00.038958 (s) | 00.034903 (s) | 00.043660 (s) |
CPP CPP
| Name | Total | Adverage | Min | Max | | Name | Total | Average | Min | Max |
|--------------------------------|---------------|---------------|---------------|---------------| |--------------------------------|---------------|---------------|---------------|---------------|
| ivy_match(file.lua) 1000000x | 01.855197 (s) | 00.000002 (s) | 00.000001 (s) | 00.000177 (s) | | ivy_match(file.lua) 1000000x | 01.855197 (s) | 00.000002 (s) | 00.000001 (s) | 00.000177 (s) |
| ivy_files(kubernetes) 100x | 14.696396 (s) | 00.146964 (s) | 00.056604 (s) | 00.168478 (s) | | ivy_files(kubernetes) 100x | 14.696396 (s) | 00.146964 (s) | 00.056604 (s) | 00.168478 (s) |

View file

@ -22,5 +22,5 @@ pub fn find_files(options: Options) -> Vec<String> {
files.push(candidate_path.to_str().unwrap().to_string()); files.push(candidate_path.to_str().unwrap().to_string());
} }
return files; files
} }

View file

@ -1,30 +1,35 @@
mod matcher;
mod finder; mod finder;
mod matcher;
mod sorter; mod sorter;
mod thread_pool; mod thread_pool;
use std::sync::Mutex;
use std::collections::HashMap; use std::collections::HashMap;
use std::os::raw::{c_int, c_char};
use std::ffi::CString;
use std::ffi::CStr; use std::ffi::CStr;
use std::ffi::CString;
use std::os::raw::{c_char, c_int};
use std::sync::Mutex;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
lazy_static! { lazy_static! {
static ref GLOBAL_FILE_CACHE: Mutex<HashMap<String, Vec<String>>> = return Mutex::new(HashMap::new()) ; static ref GLOBAL_FILE_CACHE: Mutex<HashMap<String, Vec<String>>> = Mutex::new(HashMap::new());
} }
fn to_string(input: *const c_char) -> String { fn to_string(input: *const c_char) -> String {
return unsafe { CStr::from_ptr(input) }.to_str().unwrap().to_string(); unsafe { CStr::from_ptr(input) }
.to_str()
.unwrap()
.to_string()
} }
fn get_files(directory: &String) -> Vec<String> { fn get_files(directory: &String) -> Vec<String> {
let mut cache = GLOBAL_FILE_CACHE.lock().unwrap(); let mut cache = GLOBAL_FILE_CACHE.lock().unwrap();
if !cache.contains_key(directory) { if !cache.contains_key(directory) {
let finder_options = finder::Options{ directory: directory.clone() }; let finder_options = finder::Options {
cache.insert( directory.clone(), finder::find_files(finder_options)); directory: directory.clone(),
};
cache.insert(directory.clone(), finder::find_files(finder_options));
} }
return cache.get(directory).unwrap().to_vec(); return cache.get(directory).unwrap().to_vec();
@ -41,8 +46,9 @@ pub extern "C" fn ivy_match(c_pattern: *const c_char, c_text: *const c_char) ->
let pattern = to_string(c_pattern); let pattern = to_string(c_pattern);
let text = to_string(c_text); let text = to_string(c_text);
let m = matcher::Matcher::new( pattern ); let m = matcher::Matcher::new(pattern);
return m.score(text) as i32;
m.score(text) as i32
} }
#[no_mangle] #[no_mangle]
@ -52,7 +58,7 @@ pub extern "C" fn ivy_files(c_pattern: *const c_char, c_base_dir: *const c_char)
// Bail out early if the pattern is empty its never going to find anything // Bail out early if the pattern is empty its never going to find anything
if pattern.is_empty() { if pattern.is_empty() {
return CString::new("").unwrap().into_raw() return CString::new("").unwrap().into_raw();
} }
let files = get_files(&directory); let files = get_files(&directory);
@ -66,6 +72,5 @@ pub extern "C" fn ivy_files(c_pattern: *const c_char, c_base_dir: *const c_char)
output.push('\n'); output.push('\n');
} }
return CString::new(output).unwrap().into_raw() CString::new(output).unwrap().into_raw()
} }

View file

@ -1,5 +1,5 @@
use fuzzy_matcher::FuzzyMatcher;
use fuzzy_matcher::skim::SkimMatcherV2; use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher;
pub struct Matcher { pub struct Matcher {
/// The search pattern that we want to match against some text /// The search pattern that we want to match against some text
@ -9,17 +9,16 @@ pub struct Matcher {
impl Matcher { impl Matcher {
pub fn new(pattern: String) -> Self { pub fn new(pattern: String) -> Self {
return Self { Self {
pattern, pattern,
matcher: SkimMatcherV2::default(), matcher: SkimMatcherV2::default(),
} }
} }
pub fn score(self: &Self, text: String) -> i64 { pub fn score(&self, text: String) -> i64 {
if let Some((score, _indices)) = self.matcher.fuzzy_indices(&text, &self.pattern) { self.matcher
return score; .fuzzy_indices(&text, &self.pattern)
} .map(|(score, _indices)| score)
.unwrap_or_default()
return 0;
} }
} }

View file

@ -1,9 +1,8 @@
use super::matcher; use super::matcher;
use super::thread_pool; use super::thread_pool;
use std::sync::Mutex;
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex;
pub struct Match { pub struct Match {
pub score: i64, pub score: i64,
@ -17,7 +16,10 @@ pub struct Options {
impl Options { impl Options {
pub fn new(pattern: String) -> Self { pub fn new(pattern: String) -> Self {
return Self { pattern, minimun_score: 20 }; Self {
pattern,
minimun_score: 20,
}
} }
} }
@ -35,14 +37,16 @@ pub fn sort_strings(options: Options, strings: Vec<String>) -> Arc<Mutex<Vec<Mat
if score > 25 { if score > 25 {
let mut tmp = thread_matches.lock().unwrap(); let mut tmp = thread_matches.lock().unwrap();
let content = string.clone(); let content = string.clone();
tmp.push(Match{ score, content }); tmp.push(Match { score, content });
} }
}) })
} }
drop(pool); drop(pool);
matches.lock().unwrap().sort_by(|a, b| a.score.cmp(&b.score)); matches
return matches; .lock()
.unwrap()
.sort_by(|a, b| a.score.cmp(&b.score));
matches
} }

View file

@ -35,7 +35,7 @@ impl ThreadPool {
threads.push(Worker::new(id, Arc::clone(&receiver))); threads.push(Worker::new(id, Arc::clone(&receiver)));
} }
return ThreadPool { jobs, threads }; ThreadPool { jobs, threads }
} }
pub fn execute<F>(&self, f: F) pub fn execute<F>(&self, f: F)
@ -62,7 +62,7 @@ impl Drop for ThreadPool {
} }
struct Worker { struct Worker {
id: usize, _id: usize,
thread: Option<thread::JoinHandle<()>>, thread: Option<thread::JoinHandle<()>>,
} }
@ -79,9 +79,9 @@ impl Worker {
} }
}); });
return Worker { Worker {
id, _id: id,
thread: Some(thread), thread: Some(thread),
}; }
} }
} }

View file

@ -35,7 +35,7 @@ local benchmark = function(name, n, callback)
) )
end end
print "| Name | Total | Adverage | Min | Max |" print "| Name | Total | Average | Min | Max |"
print "|--------------------------------|---------------|---------------|---------------|---------------|" print "|--------------------------------|---------------|---------------|---------------|---------------|"
benchmark("ivy_match(file.lua) 1000000x", 1000000, function() benchmark("ivy_match(file.lua) 1000000x", 1000000, function()