Skip to content

Commit

Permalink
Add debug adapter caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony-Eid committed Oct 23, 2024
1 parent f122914 commit 5fced04
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
32 changes: 27 additions & 5 deletions crates/dap/src/adapters.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::transport::Transport;
use ::fs::Fs;
use anyhow::{anyhow, bail, Context, Result};
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use http_client::{github::latest_github_release, HttpClient};
use node_runtime::NodeRuntime;
use serde_json::Value;
use smol::{self, fs::File, process};
use smol::{self, fs::File, lock::Mutex, process};
use std::{
collections::{HashMap, HashSet},
ffi::OsString,
Expand All @@ -20,8 +20,10 @@ pub trait DapDelegate {
fn http_client(&self) -> Option<Arc<dyn HttpClient>>;
fn node_runtime(&self) -> Option<NodeRuntime>;
fn fs(&self) -> Arc<dyn Fs>;
fn cached_binaries(&self) -> Arc<Mutex<HashMap<DebugAdapterName, DebugAdapterBinary>>>;
}

#[derive(PartialEq, Eq, Hash, Debug)]
pub struct DebugAdapterName(pub Arc<str>);

impl Deref for DebugAdapterName {
Expand Down Expand Up @@ -185,7 +187,11 @@ pub trait DebugAdapter: 'static + Send + Sync {
delegate: &dyn DapDelegate,
config: &DebugAdapterConfig,
) -> Result<DebugAdapterBinary> {
// TODO Debugger: Check if a user already has one installed
if let Some(binary) = delegate.cached_binaries().lock().await.get(&self.name()) {
log::info!("Using cached debug adapter binary {}", self.name());
return Ok(binary.clone());
}

log::info!("Getting latest version of debug adapter {}", self.name());
let version = self.fetch_latest_adapter_version(delegate).await.ok();

Expand All @@ -196,14 +202,30 @@ pub trait DebugAdapter: 'static + Send + Sync {
.as_ref()
.is_ok_and(|binary| binary.version == version.tag_name)
{
return binary;
let binary = binary?;

delegate
.cached_binaries()
.lock_arc()
.await
.insert(self.name(), binary.clone());

return Ok(binary);
}

self.install_binary(version, delegate).await?;
binary = self.get_installed_binary(delegate, config).await;
}

binary
let binary = binary?;

delegate
.cached_binaries()
.lock_arc()
.await
.insert(self.name(), binary.clone());

Ok(binary)
}

fn transport(&self) -> Box<dyn Transport>;
Expand Down
16 changes: 14 additions & 2 deletions crates/project/src/dap_store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::ProjectPath;
use anyhow::{anyhow, Context as _, Result};
use collections::{HashMap, HashSet};
use collections::HashSet;
use dap::adapters::{DebugAdapterBinary, DebugAdapterName};
use dap::client::{DebugAdapterClient, DebugAdapterClientId};
use dap::messages::{Message, Response};
use dap::requests::{
Expand Down Expand Up @@ -28,8 +29,9 @@ use language::{Buffer, BufferSnapshot};
use node_runtime::NodeRuntime;
use serde_json::{json, Value};
use settings::WorktreeId;
use smol::lock::Mutex;
use std::{
collections::BTreeMap,
collections::{BTreeMap, HashMap},
hash::{Hash, Hasher},
path::{Path, PathBuf},
sync::{
Expand Down Expand Up @@ -64,6 +66,7 @@ pub struct DebugPosition {
pub struct DapStore {
next_client_id: AtomicUsize,
clients: HashMap<DebugAdapterClientId, DebugAdapterClientState>,
cached_binaries: Arc<Mutex<HashMap<DebugAdapterName, DebugAdapterBinary>>>,
breakpoints: BTreeMap<ProjectPath, HashSet<Breakpoint>>,
capabilities: HashMap<DebugAdapterClientId, Capabilities>,
active_debug_line: Option<(ProjectPath, DebugPosition)>,
Expand All @@ -86,6 +89,7 @@ impl DapStore {
Self {
active_debug_line: None,
clients: Default::default(),
cached_binaries: Default::default(),
breakpoints: Default::default(),
capabilities: HashMap::default(),
next_client_id: Default::default(),
Expand Down Expand Up @@ -242,6 +246,7 @@ impl DapStore {
self.http_client.clone(),
self.node_runtime.clone(),
self.fs.clone(),
self.cached_binaries.clone(),
);
let start_client_task = cx.spawn(|this, mut cx| async move {
let dap_store = this.clone();
Expand Down Expand Up @@ -1227,18 +1232,21 @@ pub struct DapAdapterDelegate {
fs: Arc<dyn Fs>,
http_client: Option<Arc<dyn HttpClient>>,
node_runtime: Option<NodeRuntime>,
cached_binaries: Arc<Mutex<HashMap<DebugAdapterName, DebugAdapterBinary>>>,
}

impl DapAdapterDelegate {
pub fn new(
http_client: Option<Arc<dyn HttpClient>>,
node_runtime: Option<NodeRuntime>,
fs: Arc<dyn Fs>,
cached_binaries: Arc<Mutex<HashMap<DebugAdapterName, DebugAdapterBinary>>>,
) -> Self {
Self {
fs,
http_client,
node_runtime,
cached_binaries,
}
}
}
Expand All @@ -1255,4 +1263,8 @@ impl dap::adapters::DapDelegate for DapAdapterDelegate {
fn fs(&self) -> Arc<dyn Fs> {
self.fs.clone()
}

fn cached_binaries(&self) -> Arc<Mutex<HashMap<DebugAdapterName, DebugAdapterBinary>>> {
self.cached_binaries.clone()
}
}

0 comments on commit 5fced04

Please sign in to comment.