Skip to content

Commit

Permalink
feat: support unsafe no lock for fuzzy matcher
Browse files Browse the repository at this point in the history
Closes #817
  • Loading branch information
Saghen committed Dec 29, 2024
1 parent 21da714 commit 6f8da35
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion docs/configuration/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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).
--
Expand Down
3 changes: 3 additions & 0 deletions lua/blink/cmp/config/fuzzy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions lua/blink/cmp/fuzzy/frecency.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -31,14 +31,18 @@ pub struct FrecencyTracker {
}

impl FrecencyTracker {
pub fn new(db_path: &str) -> LuaResult<Self> {
pub fn new(db_path: &str, use_unsafe_no_lock: bool) -> LuaResult<Self> {
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(),
)
Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/fuzzy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lua/blink/cmp/fuzzy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ lazy_static! {
RwLock::new(HashMap::new());
}

pub fn init_db(_: &Lua, db_path: String) -> LuaResult<bool> {
pub fn init_db(_: &Lua, (db_path, use_unsafe_no_lock): (String, bool)) -> LuaResult<bool> {
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)
}

Expand Down

0 comments on commit 6f8da35

Please sign in to comment.