From 6f8da35fc8f1f8046d25b88b7708178cb4126abe Mon Sep 17 00:00:00 2001 From: Liam Dyer Date: Sun, 29 Dec 2024 13:40:03 -0500 Subject: [PATCH] feat: support unsafe no lock for fuzzy matcher Closes #817 --- docs/configuration/reference.md | 5 ++++- lua/blink/cmp/config/fuzzy.lua | 3 +++ lua/blink/cmp/fuzzy/frecency.rs | 10 +++++++--- lua/blink/cmp/fuzzy/init.lua | 2 +- lua/blink/cmp/fuzzy/lib.rs | 4 ++-- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/docs/configuration/reference.md b/docs/configuration/reference.md index bf7d10b8..90cd2af7 100644 --- a/docs/configuration/reference.md +++ b/docs/configuration/reference.md @@ -339,7 +339,8 @@ fuzzy = { use_frecency = true, -- Proximity bonus boosts the score of items matching nearby words use_proximity = true, - max_items = 200, + -- UNSAFE!! When enabled, disables the lock and fsync when writing to the frecency database. This should only be used on unsupported platforms (i.e. alpine termux) + use_unsafe_no_lock = false, -- Controls which sorts to use and in which order, falling back to the next sort if the first one returns nil -- You may pass a function instead of a string to customize the sorting sorts = { 'score', 'sort_text' }, @@ -348,6 +349,8 @@ fuzzy = { -- Whether or not to automatically download a prebuilt binary from github. If this is set to `false` -- you will need to manually build the fuzzy binary dependencies by running `cargo build --release` download = true, + -- Ignores mismatched version between the built binary and the current git sha, when building locally + ignore_version_mismatch = false, -- When downloading a prebuilt binary, force the downloader to resolve this version. If this is unset -- then the downloader will attempt to infer the version from the checked out git tag (if any). -- diff --git a/lua/blink/cmp/config/fuzzy.lua b/lua/blink/cmp/config/fuzzy.lua index 2bf287c6..aa50e0ba 100644 --- a/lua/blink/cmp/config/fuzzy.lua +++ b/lua/blink/cmp/config/fuzzy.lua @@ -2,6 +2,7 @@ --- @field use_typo_resistance boolean When enabled, allows for a number of typos relative to the length of the query. Disabling this matches the behavior of fzf --- @field use_frecency boolean Tracks the most recently/frequently used items and boosts the score of the item --- @field use_proximity boolean Boosts the score of items matching nearby words +--- @field use_unsafe_no_lock boolean UNSAFE!! When enabled, disables the lock and fsync when writing to the frecency database. This should only be used on unsupported platforms (i.e. alpine termux) --- @field sorts ("label" | "sort_text" | "kind" | "score" | blink.cmp.SortFunction)[] Controls which sorts to use and in which order, these three are currently the only allowed options --- @field prebuilt_binaries blink.cmp.PrebuiltBinariesConfig @@ -21,6 +22,7 @@ local fuzzy = { use_typo_resistance = true, use_frecency = true, use_proximity = true, + use_unsafe_no_lock = false, sorts = { 'score', 'sort_text' }, prebuilt_binaries = { download = true, @@ -37,6 +39,7 @@ function fuzzy.validate(config) use_typo_resistance = { config.use_typo_resistance, 'boolean' }, use_frecency = { config.use_frecency, 'boolean' }, use_proximity = { config.use_proximity, 'boolean' }, + use_unsafe_no_lock = { config.use_unsafe_no_lock, 'boolean' }, sorts = { config.sorts, function(sorts) diff --git a/lua/blink/cmp/fuzzy/frecency.rs b/lua/blink/cmp/fuzzy/frecency.rs index 3da1af05..a6725983 100644 --- a/lua/blink/cmp/fuzzy/frecency.rs +++ b/lua/blink/cmp/fuzzy/frecency.rs @@ -1,5 +1,5 @@ use crate::lsp_item::LspItem; -use heed::types::*; +use heed::{types::*, EnvFlags}; use heed::{Database, Env, EnvOpenOptions}; use mlua::Result as LuaResult; use serde::{Deserialize, Serialize}; @@ -31,14 +31,18 @@ pub struct FrecencyTracker { } impl FrecencyTracker { - pub fn new(db_path: &str) -> LuaResult { + pub fn new(db_path: &str, use_unsafe_no_lock: bool) -> LuaResult { fs::create_dir_all(db_path).map_err(|err| { mlua::Error::RuntimeError( "Failed to create frecency database directory: ".to_string() + &err.to_string(), ) })?; let env = unsafe { - EnvOpenOptions::new().open(db_path).map_err(|err| { + let mut opts = EnvOpenOptions::new(); + if use_unsafe_no_lock { + opts.flags(EnvFlags::NO_LOCK | EnvFlags::NO_SYNC | EnvFlags::NO_META_SYNC); + } + opts.open(db_path).map_err(|err| { mlua::Error::RuntimeError( "Failed to open frecency database: ".to_string() + &err.to_string(), ) diff --git a/lua/blink/cmp/fuzzy/init.lua b/lua/blink/cmp/fuzzy/init.lua index b366dbb8..85c33646 100644 --- a/lua/blink/cmp/fuzzy/init.lua +++ b/lua/blink/cmp/fuzzy/init.lua @@ -9,7 +9,7 @@ local fuzzy = { function fuzzy.init_db() if fuzzy.has_init_db then return end - fuzzy.rust.init_db(vim.fn.stdpath('data') .. '/blink/cmp/fuzzy.db') + fuzzy.rust.init_db(vim.fn.stdpath('data') .. '/blink/cmp/fuzzy.db', config.use_unsafe_no_lock) vim.api.nvim_create_autocmd('VimLeavePre', { callback = fuzzy.rust.destroy_db, diff --git a/lua/blink/cmp/fuzzy/lib.rs b/lua/blink/cmp/fuzzy/lib.rs index 70ff8e2d..97e361a1 100644 --- a/lua/blink/cmp/fuzzy/lib.rs +++ b/lua/blink/cmp/fuzzy/lib.rs @@ -18,14 +18,14 @@ lazy_static! { RwLock::new(HashMap::new()); } -pub fn init_db(_: &Lua, db_path: String) -> LuaResult { +pub fn init_db(_: &Lua, (db_path, use_unsafe_no_lock): (String, bool)) -> LuaResult { let mut frecency = FRECENCY.write().map_err(|_| { mlua::Error::RuntimeError("Failed to acquire lock for frecency".to_string()) })?; if frecency.is_some() { return Ok(false); } - *frecency = Some(FrecencyTracker::new(&db_path)?); + *frecency = Some(FrecencyTracker::new(&db_path, use_unsafe_no_lock)?); Ok(true) }