Skip to content

Commit

Permalink
Use RwLock for recent files in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed May 25, 2024
1 parent f934849 commit 18d36ea
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
6 changes: 3 additions & 3 deletions crates/maple_core/src/datastore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::recent_files::SortedRecentFiles;
use crate::stdio_server::InputHistory;
use dirs::Dirs;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use parking_lot::{Mutex, RwLock};
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::io::BufReader;
Expand All @@ -32,11 +32,11 @@ static RECENT_FILES_JSON_PATH: Lazy<Option<PathBuf>> =
static INPUT_HISTORY_JSON_PATH: Lazy<Option<PathBuf>> =
Lazy::new(|| generate_data_file_path("input_history.json").ok());

pub static RECENT_FILES_IN_MEMORY: Lazy<Mutex<SortedRecentFiles>> = Lazy::new(|| {
pub static RECENT_FILES_IN_MEMORY: Lazy<RwLock<SortedRecentFiles>> = Lazy::new(|| {
let maybe_persistent = load_json(RECENT_FILES_JSON_PATH.as_deref())
.map(|f: SortedRecentFiles| f.remove_invalid_entries())
.unwrap_or_default();
Mutex::new(maybe_persistent)
RwLock::new(maybe_persistent)
});

pub static INPUT_HISTORY_IN_MEMORY: Lazy<Arc<Mutex<InputHistory>>> = Lazy::new(|| {
Expand Down
2 changes: 1 addition & 1 deletion crates/maple_core/src/stdio_server/plugin/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn note_recent_file(file_path: String) {
return;
}

let mut recent_files = RECENT_FILES_IN_MEMORY.lock();
let mut recent_files = RECENT_FILES_IN_MEMORY.write();
recent_files.upsert(file_path);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/maple_core/src/stdio_server/provider/impls/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl FilesProvider {
let expanded_paths = ctx.expanded_paths(&args.paths).await?;

let recent_files = crate::datastore::RECENT_FILES_IN_MEMORY
.lock()
.read()
.recent_n_files(100);
let recent_files_bonus = Bonus::RecentFiles(recent_files.into());

Expand Down
24 changes: 15 additions & 9 deletions crates/maple_core/src/stdio_server/provider/impls/recent_files.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::datastore::RECENT_FILES_IN_MEMORY;
use crate::stdio_server::provider::hooks::CachedPreviewImpl;
use crate::stdio_server::provider::{BaseArgs, ClapProvider, Context, ProviderResult as Result};
use parking_lot::Mutex;
use parking_lot::RwLock;
use paths::AbsPathBuf;
use printer::Printer;
use serde_json::json;
Expand All @@ -12,7 +12,7 @@ use types::{ClapItem, MatchedItem, RankCalculator, Score};
pub struct RecentFilesProvider {
args: BaseArgs,
printer: Printer,
lines: Arc<Mutex<Vec<MatchedItem>>>,
lines: Arc<RwLock<Vec<MatchedItem>>>,
}

impl RecentFilesProvider {
Expand Down Expand Up @@ -40,8 +40,10 @@ impl RecentFilesProvider {
) -> Result<printer::PickerUpdateInfo> {
let cwd = cwd.to_string();

let mut recent_files = RECENT_FILES_IN_MEMORY.lock();
let recent_files = RECENT_FILES_IN_MEMORY.read();
let ranked = if query.is_empty() {
let mut recent_files = recent_files.clone();

// Sort the initial list according to the cwd.
//
// This changes the order of existing recent file entries.
Expand Down Expand Up @@ -127,7 +129,7 @@ impl RecentFilesProvider {
preview,
};

let mut lines = self.lines.lock();
let mut lines = self.lines.write();
*lines = ranked;

Ok(update_info)
Expand Down Expand Up @@ -159,11 +161,15 @@ impl ClapProvider for RecentFilesProvider {
async fn on_move(&mut self, ctx: &mut Context) -> Result<()> {
let lnum = ctx.vim.display_getcurlnum().await?;

let maybe_curline = self
.lines
.lock()
.get(lnum - 1)
.map(|r| r.item.raw_text().to_string());
let mut maybe_curline = None;

{
let lines = self.lines.read();
if let Some(line) = lines.get(lnum - 1).map(|r| r.item.raw_text().to_string()) {
maybe_curline.replace(line);
}
drop(lines);
}

if let Some(curline) = maybe_curline {
let preview_height = ctx.preview_height().await?;
Expand Down
6 changes: 3 additions & 3 deletions crates/maple_core/src/stdio_server/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ impl ProviderSession {
return;
}
}
}
None => break, // channel has closed.
}
}
None => break, // channel has closed.
}
}
_ = on_typed_timer.as_mut(), if on_typed.is_some() => {
if let Some(_params) = on_typed.take() {
Expand Down

0 comments on commit 18d36ea

Please sign in to comment.