Skip to content

Commit a2a5bce

Browse files
authored
Merge pull request #109 from posit-dev/feature/single-thread
Run R tasks on the R thread
2 parents 942a03d + 7d0ebeb commit a2a5bce

21 files changed

+440
-434
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
[workspace]
22

3+
# Was necessary after switching to dev tree-sitter to fix this warning:
4+
# > some crates are on edition 2021 which defaults to `resolver = "2"`, but
5+
# > virtual workspaces default to `resolver = "1"`
6+
resolver = "2"
7+
38
members = [
49
"crates/amalthea",
510
"crates/ark",

crates/ark/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ nix = { version = "0.26.2", features = ["signal"] }
3333
notify = "6.0.0"
3434
once_cell = "1.17.1"
3535
parking_lot = "0.12.1"
36-
regex = "1.7.1"
36+
regex = "1.10.0"
3737
reqwest = { version = "0.11.20", features = ["json"] }
3838
ropey = "1.6.0"
3939
rust-embed = "8.0.0"
@@ -43,8 +43,8 @@ serde_json = "1.0.94"
4343
stdext = { path = "../stdext" }
4444
tokio = { version = "1.26.0", features = ["full"] }
4545
tower-lsp = "0.19.0"
46-
tree-sitter = "0.20.9"
47-
tree-sitter-r = { git = "https://github.com/r-lib/tree-sitter-r", branch = "next" }
46+
tree-sitter = { git = "https://github.com/tree-sitter/tree-sitter" }
47+
tree-sitter-r = { git = "https://github.com/r-lib/tree-sitter-r", branch = "bugfix/update" }
4848
uuid = "1.3.0"
4949
url = "2.4.1"
5050
walkdir = "2"

crates/ark/src/data_viewer/r_data_viewer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crossbeam::channel::Sender;
1414
use harp::exec::RFunction;
1515
use harp::exec::RFunctionExt;
1616
use harp::object::RObject;
17-
use harp::r_lock;
1817
use harp::utils::r_assert_length;
1918
use harp::utils::r_is_data_frame;
2019
use harp::utils::r_is_matrix;
@@ -44,6 +43,7 @@ use crate::data_viewer::message::DataViewerMessageRequest;
4443
use crate::data_viewer::message::DataViewerMessageResponse;
4544
use crate::data_viewer::message::DataViewerRowRequest;
4645
use crate::data_viewer::message::DataViewerRowResponse;
46+
use crate::r_task;
4747

4848
pub struct RDataViewer {
4949
title: String,
@@ -195,7 +195,7 @@ impl DataSet {
195195
}
196196

197197
pub fn from_object(id: String, title: String, object: RObject) -> Result<Self, anyhow::Error> {
198-
r_lock! {
198+
r_task(|| unsafe {
199199
let row_count = {
200200
if r_is_data_frame(*object) {
201201
let row_names = Rf_getAttrib(*object, R_RowNamesSymbol);
@@ -214,10 +214,10 @@ impl DataSet {
214214
Ok(Self {
215215
id: id.clone(),
216216
title: title.clone(),
217-
columns: columns,
218-
row_count: row_count
217+
columns,
218+
row_count,
219219
})
220-
}
220+
})
221221
}
222222

223223
fn slice_data(&self, start: usize, size: usize) -> Result<Vec<DataColumn>, anyhow::Error> {

crates/ark/src/environment/r_environment.rs

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use harp::environment::Environment;
1616
use harp::exec::RFunction;
1717
use harp::exec::RFunctionExt;
1818
use harp::object::RObject;
19-
use harp::r_lock;
2019
use harp::utils::r_assert_type;
2120
use harp::vector::CharacterVector;
2221
use harp::vector::Vector;
@@ -40,6 +39,7 @@ use crate::environment::message::EnvironmentMessageUpdate;
4039
use crate::environment::message::EnvironmentMessageView;
4140
use crate::environment::variable::EnvironmentVariable;
4241
use crate::lsp::events::EVENTS;
42+
use crate::r_task;
4343

4444
/**
4545
* The R Environment handler provides the server side of Positron's Environment
@@ -218,14 +218,13 @@ impl REnvironment {
218218
fn refresh(&mut self, request_id: Option<String>) {
219219
let mut variables: Vec<EnvironmentVariable> = vec![];
220220

221-
r_lock! {
221+
r_task(|| {
222222
self.update_bindings(self.bindings());
223223

224224
for binding in &self.current_bindings {
225225
variables.push(EnvironmentVariable::new(binding));
226226
}
227-
228-
}
227+
});
229228

230229
// TODO: Avoid serializing the full list of variables if it exceeds a
231230
// certain threshold
@@ -244,8 +243,7 @@ impl REnvironment {
244243
*/
245244
fn clear(&mut self, include_hidden_objects: bool, request_id: Option<String>) {
246245
// try to rm(<env>, list = ls(envir = <env>, all.names = TRUE)))
247-
let result: Result<(), harp::error::Error> = r_lock! {
248-
246+
let result: Result<(), harp::error::Error> = r_task(|| unsafe {
249247
let mut list = RFunction::new("base", "ls")
250248
.param("envir", *self.env)
251249
.param("all.names", Rf_ScalarLogical(include_hidden_objects as i32))
@@ -264,7 +262,7 @@ impl REnvironment {
264262
.call()?;
265263

266264
Ok(())
267-
};
265+
});
268266

269267
if let Err(_err) = result {
270268
error!("Failed to clear the environment");
@@ -281,8 +279,8 @@ impl REnvironment {
281279
* Clear the environment. Uses rm(envir = <env>, list = ls(<env>, all.names = TRUE))
282280
*/
283281
fn delete(&mut self, variables: Vec<String>, request_id: Option<String>) {
284-
r_lock! {
285-
let variables : Vec<&str> = variables.iter().map(|s| s as &str).collect();
282+
r_task(|| unsafe {
283+
let variables: Vec<&str> = variables.iter().map(|s| s as &str).collect();
286284

287285
let result = RFunction::new("base", "rm")
288286
.param("list", CharacterVector::create(variables).cast())
@@ -292,16 +290,15 @@ impl REnvironment {
292290
if let Err(_) = result {
293291
error!("Failed to delete variables from the environment");
294292
}
295-
}
293+
});
296294

297295
// and then update
298296
self.update(request_id);
299297
}
300298

301299
fn clipboard_format(&mut self, path: &Vec<String>, format: String, request_id: Option<String>) {
302-
let clipped = r_lock! {
303-
EnvironmentVariable::clip(RObject::view(*self.env), &path, &format)
304-
};
300+
let clipped =
301+
r_task(|| EnvironmentVariable::clip(RObject::view(*self.env), &path, &format));
305302

306303
let msg = match clipped {
307304
Ok(content) => {
@@ -319,9 +316,7 @@ impl REnvironment {
319316
}
320317

321318
fn inspect(&mut self, path: &Vec<String>, request_id: Option<String>) {
322-
let inspect = r_lock! {
323-
EnvironmentVariable::inspect(RObject::view(*self.env), &path)
324-
};
319+
let inspect = r_task(|| EnvironmentVariable::inspect(RObject::view(*self.env), &path));
325320
let msg = match inspect {
326321
Ok(children) => {
327322
let length = children.len();
@@ -340,9 +335,8 @@ impl REnvironment {
340335
}
341336

342337
fn view(&mut self, path: &Vec<String>, request_id: Option<String>) {
343-
let data = r_lock! {
344-
EnvironmentVariable::resolve_data_object(RObject::view(*self.env), &path)
345-
};
338+
let data =
339+
r_task(|| EnvironmentVariable::resolve_data_object(RObject::view(*self.env), &path));
346340

347341
let msg = match data {
348342
Ok(data) => {
@@ -386,7 +380,7 @@ impl REnvironment {
386380
let old_bindings = &self.current_bindings;
387381
let mut new_bindings = vec![];
388382

389-
r_lock! {
383+
r_task(|| {
390384
new_bindings = self.bindings();
391385

392386
let mut old_iter = old_bindings.iter();
@@ -396,25 +390,20 @@ impl REnvironment {
396390
let mut new_next = new_iter.next();
397391

398392
loop {
399-
400393
match (old_next, new_next) {
401394
// nothing more to do
402-
(None, None) => {
403-
break
404-
},
395+
(None, None) => break,
405396

406397
// No more old, collect last new into added
407398
(None, Some(mut new)) => {
408399
loop {
409-
assigned.push(
410-
EnvironmentVariable::new(&new)
411-
);
400+
assigned.push(EnvironmentVariable::new(&new));
412401

413402
match new_iter.next() {
414403
Some(x) => {
415404
new = x;
416405
},
417-
None => break
406+
None => break,
418407
};
419408
}
420409
break;
@@ -429,7 +418,7 @@ impl REnvironment {
429418
Some(x) => {
430419
old = x;
431420
},
432-
None => break
421+
None => break,
433422
};
434423
}
435424

@@ -439,25 +428,21 @@ impl REnvironment {
439428
(Some(old), Some(new)) => {
440429
if old.name == new.name {
441430
if old.value != new.value {
442-
assigned.push(
443-
EnvironmentVariable::new(&new)
444-
);
431+
assigned.push(EnvironmentVariable::new(&new));
445432
}
446433
old_next = old_iter.next();
447434
new_next = new_iter.next();
448435
} else if old.name < new.name {
449436
removed.push(old.name.to_string());
450437
old_next = old_iter.next();
451438
} else {
452-
assigned.push(
453-
EnvironmentVariable::new(&new)
454-
);
439+
assigned.push(EnvironmentVariable::new(&new));
455440
new_next = new_iter.next();
456441
}
457-
}
442+
},
458443
}
459444
}
460-
}
445+
});
461446

462447
if assigned.len() > 0 || removed.len() > 0 || request_id.is_some() {
463448
// only update the bindings (and the version)

0 commit comments

Comments
 (0)