Skip to content

Commit 7668187

Browse files
committed
Resource namespaces only after debug() or debugonce() calls
1 parent ad2e1fc commit 7668187

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

crates/ark/src/interface.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2057,7 +2057,10 @@ fn do_resource_namespaces() -> bool {
20572057
let opt: Option<bool> = r_null_or_try_into(harp::get_option("ark.resource_namespaces"))
20582058
.ok()
20592059
.flatten();
2060-
opt.unwrap_or(true)
2060+
2061+
// By default we don't resource namespaces to avoid increased memory usage.
2062+
// https://github.com/posit-dev/positron/issues/5050
2063+
opt.unwrap_or(false)
20612064
}
20622065

20632066
/// Are we auto-printing?

crates/ark/src/modules/positron/hooks.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
register_hooks <- function() {
99
rebind("utils", "View", .ps.view_data_frame, namespace = TRUE)
10+
rebind("base", "debug", new_ark_debug(base::debug), namespace = TRUE)
11+
rebind("base", "debugonce", new_ark_debug(base::debugonce), namespace = TRUE)
1012
register_getHook_hook()
1113
}
1214

crates/ark/src/modules/positron/srcref.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,35 @@ reparse_with_srcref <- function(x, name, uri, line) {
4444
zap_srcref <- function(x) {
4545
.ps.Call("ark_zap_srcref", x)
4646
}
47+
48+
new_ark_debug <- function(fn) {
49+
# Signature of `debug()`:
50+
# function(fun, text = "", condition = NULL, signature = NULL)
51+
52+
body(fn) <- bquote({
53+
local({
54+
# Don't do anything if user has explicitly disabled namespace resourcing
55+
if (!.ps.internal(do_resource_namespaces(default = TRUE))) {
56+
return() # from local()
57+
}
58+
59+
# Enable namespace resourcing for all future loaded namespaces and
60+
# resource already loaded namespaces so we get virtual documents for
61+
# step-debugging.
62+
options(ark.resource_namespaces = TRUE)
63+
.ps.internal(resource_loaded_namespaces())
64+
})
65+
66+
.(body(fn))
67+
})
68+
69+
fn
70+
}
71+
72+
do_resource_namespaces <- function(default) {
73+
getOption("ark.resource_namespaces", default = default)
74+
}
75+
76+
resource_loaded_namespaces <- function() {
77+
.ps.Call("ps_resource_loaded_namespaces")
78+
}

crates/ark/src/srcref.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ pub(crate) fn resource_loaded_namespaces() -> anyhow::Result<()> {
3131
Ok(())
3232
}
3333

34+
#[harp::register]
35+
unsafe extern "C" fn ps_resource_loaded_namespaces() -> anyhow::Result<SEXP> {
36+
resource_loaded_namespaces()?;
37+
Ok(harp::r_null())
38+
}
39+
3440
#[harp::register]
3541
unsafe extern "C" fn ps_ns_populate_srcref(ns_name: SEXP) -> anyhow::Result<SEXP> {
3642
let ns_name: String = RObject::view(ns_name).try_into()?;

0 commit comments

Comments
 (0)