Skip to content
This repository was archived by the owner on Oct 12, 2025. It is now read-only.

Commit 9ae41b1

Browse files
authored
Tidy up async behaviour (#93)
* Disable multi-thread RT We're not doing anything CPU intensive not lots of filesystem I/O. We don't need multiple threads. * Remove pointless blocking task This task spawns a new thread which takes a blocking lock on a mutex. Just take a non-blocking lock on the current thread. * Avoid blocking locks * Remove unnecessary async/await * Disable unused tokio features
1 parent 37c21fa commit 9ae41b1

7 files changed

Lines changed: 18 additions & 75 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pin-project-lite = "0.2.14"
1515
serde = { version = "1.0.186", features = ["derive"] }
1616
serde_json = "1.0.78"
1717
time = "0.3.30"
18-
tokio = { version = "1.37.0", features = ["fs", "io-std", "io-util", "macros", "net", "parking_lot", "process", "rt-multi-thread", "sync", "time"] }
18+
tokio = { version = "1.37.0", features = ["io-std", "io-util", "macros", "net", "process", "rt", "sync", "time"] }
1919
toml = "0.5.8"
2020
tracing = "0.1.39"
2121
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }

src/client.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ async fn status(
110110
instance_map: Arc<Mutex<InstanceMap>>,
111111
mut writer: LspWriter<OwnedWriteHalf>,
112112
) -> Result<()> {
113-
let status = task::spawn_blocking(move || instance_map.blocking_lock().get_status())
114-
.await
115-
.unwrap();
113+
let status = instance_map.lock().await.get_status().await;
116114
writer
117115
.write_message(&Message::ResponseSuccess(ResponseSuccess {
118116
jsonrpc: Version,

src/ext.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ where
6868
}
6969
}
7070

71-
pub async fn config(config: &Config) -> Result<()> {
72-
println!("{:#?}", config);
71+
pub fn config(config: &Config) -> Result<()> {
72+
println!("{config:#?}");
7373
Ok(())
7474
}
7575

src/instance.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,19 @@ impl Instance {
293293
Ok(())
294294
}
295295

296-
pub fn get_status(&self) -> ext::Instance {
296+
async fn get_status(&self) -> ext::Instance {
297297
let clients = self
298298
.clients
299-
.blocking_lock()
299+
.lock()
300+
.await
300301
.values()
301302
.map(|client| client.get_status())
302303
.collect();
303304

304305
let registered_dyn_capabilities = self
305306
.dynamic_capabilities
306-
.blocking_lock()
307+
.lock()
308+
.await
307309
.values()
308310
.map(|reg| reg.method.clone())
309311
.collect();
@@ -324,7 +326,7 @@ impl Instance {
324326
pub struct InstanceMap(HashMap<InstanceKey, Arc<Instance>>);
325327

326328
impl InstanceMap {
327-
pub async fn new(config: &Config) -> Arc<Mutex<Self>> {
329+
pub fn new(config: &Config) -> Arc<Mutex<Self>> {
328330
let instance_map = Arc::new(Mutex::new(InstanceMap(HashMap::new())));
329331
task::spawn(gc_task(
330332
instance_map.clone(),
@@ -344,14 +346,12 @@ impl InstanceMap {
344346
.map(|(_, inst)| inst.deref())
345347
}
346348

347-
pub fn get_status(&self) -> ext::StatusResponse {
348-
ext::StatusResponse {
349-
instances: self
350-
.0
351-
.values()
352-
.map(|instance| instance.get_status())
353-
.collect(),
349+
pub async fn get_status(&self) -> ext::StatusResponse {
350+
let mut instances = Vec::with_capacity(self.0.len());
351+
for instance in self.0.values() {
352+
instances.push(instance.get_status().await);
354353
}
354+
ext::StatusResponse { instances }
355355
}
356356
}
357357

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ enum Cmd {
5353
Reload {},
5454
}
5555

56-
#[tokio::main]
56+
#[tokio::main(flavor = "current_thread")]
5757
async fn main() -> Result<()> {
5858
let cli = Cli::parse();
5959

@@ -75,7 +75,7 @@ async fn main() -> Result<()> {
7575
Some(Cmd::Server {}) => server::run(&config).await,
7676
Some(Cmd::Client { server, args }) => proxy::run(&config, server, args).await,
7777
Some(Cmd::Status { json }) => ext::status(&config, json).await,
78-
Some(Cmd::Config {}) => ext::config(&config).await,
78+
Some(Cmd::Config {}) => ext::config(&config),
7979
Some(Cmd::Reload {}) => ext::reload(&config).await,
8080
None => {
8181
let server_path = env::var("RA_MUX_SERVER").unwrap_or_else(|_| "rust-analyzer".into());

src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::instance::InstanceMap;
1010
use crate::socketwrapper::Listener;
1111

1212
pub async fn run(config: &Config) -> Result<()> {
13-
let instance_map = InstanceMap::new(config).await;
13+
let instance_map = InstanceMap::new(config);
1414
let next_client_id = AtomicUsize::new(0);
1515
let next_client_id = || next_client_id.fetch_add(1, Ordering::Relaxed);
1616

0 commit comments

Comments
 (0)