Skip to content

Commit 2558a23

Browse files
committed
Use fewer threads to spare memory (#720)
* Use fewer threads in help proxy server * Use fewer threads in the LSP
1 parent 7c2b7f6 commit 2558a23

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

crates/ark/src/help_proxy.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,30 @@ struct PreviewRdParams {
4444
pub fn start(target_port: u16) -> anyhow::Result<u16> {
4545
let source_port = HelpProxy::get_os_assigned_port()?;
4646

47-
spawn!("ark-help-proxy", move || {
48-
match task(source_port, target_port) {
49-
Ok(value) => log::info!("Help proxy server exited with value: {:?}", value),
50-
Err(error) => log::error!("Help proxy server exited unexpectedly: {}", error),
51-
}
47+
spawn!("ark-help-proxy", move || -> anyhow::Result<()> {
48+
// Create a single-threaded Tokio runtime to spare stack memory. The
49+
// help proxy server does not need to be high performance.
50+
// Note that `new_current_thread()` seems to consume much more memory.
51+
let rt = tokio::runtime::Builder::new_multi_thread()
52+
.enable_all()
53+
.worker_threads(1)
54+
.build()?;
55+
56+
// Execute the task within the runtime.
57+
rt.block_on(async {
58+
match task(source_port, target_port).await {
59+
Ok(value) => log::info!("Help proxy server exited with value: {:?}", value),
60+
Err(error) => log::error!("Help proxy server exited unexpectedly: {}", error),
61+
}
62+
});
63+
64+
Ok(())
5265
});
5366

5467
Ok(source_port)
5568
}
5669

5770
// The help proxy main entry point.
58-
#[tokio::main]
5971
async fn task(source_port: u16, target_port: u16) -> anyhow::Result<()> {
6072
// Create the help proxy.
6173
let help_proxy = HelpProxy::new(source_port, target_port)?;
@@ -101,7 +113,8 @@ impl HelpProxy {
101113
.service(preview_img)
102114
.default_service(web::to(proxy_request))
103115
})
104-
.bind(("127.0.0.1", self.source_port))?;
116+
.bind(("127.0.0.1", self.source_port))?
117+
.workers(1);
105118

106119
// Run the server.
107120
Ok(server.run().await?)

crates/ark/src/lsp/handler.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use amalthea::language::server_handler::ServerHandler;
1212
use bus::BusReader;
1313
use crossbeam::channel::Sender;
1414
use stdext::spawn;
15+
use tokio::runtime::Builder;
1516
use tokio::runtime::Runtime;
1617

1718
use super::backend;
@@ -25,8 +26,17 @@ pub struct Lsp {
2526

2627
impl Lsp {
2728
pub fn new(kernel_init_rx: BusReader<KernelInfo>) -> Self {
29+
let rt = Builder::new_multi_thread()
30+
.enable_all()
31+
// One for the main loop and one spare
32+
.worker_threads(2)
33+
// Used for diagnostics
34+
.max_blocking_threads(2)
35+
.build()
36+
.unwrap();
37+
2838
Self {
29-
runtime: Arc::new(tokio::runtime::Runtime::new().unwrap()),
39+
runtime: Arc::new(rt),
3040
kernel_init_rx,
3141
kernel_initialized: false,
3242
}

0 commit comments

Comments
 (0)