Skip to content

Implement cache using once_cell #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 36 additions & 4 deletions core/src/dynmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<i32>> {
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<Option<u32>, VecDeque<Option<u32>>>,

// Something that get_heatmap_str would return.
heatmap: Vec<i32>,
}

impl StrInfo {
fn new() -> StrInfo {
StrInfo {
hash_for_string: HashMap::new(),
heatmap: Vec::new(),
}
}
}

static CACHE: Lazy<Mutex<HashMap<String, HashMap<String, StrInfo>>>> = Lazy::new(|| Mutex::new(HashMap::new()));

fn flx_rs_score(source: &str, pattern: &str, cache: &mut Option<HashMap<String, StrInfo>>) -> Option<Vec<i32>> {
let result: Option<flx_rs::Score> = flx_rs::score(source, pattern);
if result.is_none() {
return None;
Expand All @@ -29,8 +54,15 @@ fn flx_rs_score(source: &str, pattern: &str) -> Option<Vec<i32>> {
///
/// (fn STR QUERY)
#[defun]
fn score(env: &Env, str: String, query: String) -> Result<Option<Vector>> {
let _vec: Option<Vec<i32>> = flx_rs_score(&str, &query);
fn score(env: &Env, str: String, query: String, cache_id: Option<String>) -> Result<Option<Vector>> {
let cache: Option<HashMap<String, StrInfo>> = 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<Vec<i32>> = flx_rs_score(&str, &query, &mut cache);
if _vec == None {
return Ok(None);
}
Expand Down