diff --git a/core/Cargo.toml b/core/Cargo.toml index bee57747..cad6b24c 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -13,6 +13,7 @@ crate-type = ["cdylib"] [dependencies] emacs = "0.17" flx-rs = "0.1.4" +once_cell = "1.7.2" [profile.release] opt-level = 3 diff --git a/core/src/dynmod.rs b/core/src/dynmod.rs index 71da421f..517c2039 100644 --- a/core/src/dynmod.rs +++ b/core/src/dynmod.rs @@ -6,9 +6,34 @@ * $Notice: See LICENSE.txt for modification and distribution information * Copyright © 2021 by Shen, Jen-Chieh $ */ -use emacs::{defun, Env, Result, Value, IntoLisp, Vector}; +use std::collections::{HashMap, VecDeque}; +use std::sync::{Mutex}; -fn flx_rs_score(source: &str, pattern: &str) -> Option> { +use emacs::{defun, Env, Result, Vector}; + +use once_cell::sync::Lazy; + +#[derive(Clone)] +pub struct StrInfo { + // Generated through get_hash_for_string + hash_for_string: HashMap, VecDeque>>, + + // Something that get_heatmap_str would return. + heatmap: Vec, +} + +impl StrInfo { + fn new() -> StrInfo { + StrInfo { + hash_for_string: HashMap::new(), + heatmap: Vec::new(), + } + } +} + +static CACHE: Lazy>>> = Lazy::new(|| Mutex::new(HashMap::new())); + +fn flx_rs_score(source: &str, pattern: &str, cache: &mut Option>) -> Option> { let result: Option = flx_rs::score(source, pattern); if result.is_none() { return None; @@ -29,8 +54,15 @@ fn flx_rs_score(source: &str, pattern: &str) -> Option> { /// /// (fn STR QUERY) #[defun] -fn score(env: &Env, str: String, query: String) -> Result> { - let _vec: Option> = flx_rs_score(&str, &query); +fn score(env: &Env, str: String, query: String, cache_id: Option) -> Result> { + let cache: Option> = None; + + if !cache_id.is_none() { + cache = Some(HashMap::new()); + CAHCE.try_lock().expect("Failed to access cache registry").insert(cache_id.unwrap(), cache.unwrap()); + } + + let _vec: Option> = flx_rs_score(&str, &query, &mut cache); if _vec == None { return Ok(None); }