Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honor MAX_DISPLAY_ENTRIES when inspecting variables #629

Merged
merged 12 commits into from
Nov 21, 2024

Conversation

dfalbel
Copy link
Contributor

@dfalbel dfalbel commented Nov 14, 2024

This PR fixes some performance related issues that involve the variables pane.
Addressing:

The goal is to have the following scenarios working fine in positron:

# create a single string that's very large
a <- paste0(rep(letters, length.out = 1e7), collapse="-")

# create a very large vector, and expand it in the variables pane
b <- 1:100000

# expand a data frame with many columns
d <- as.data.frame(lapply(1:10000, function(x) 1:100000))

# expand a data.frame with many rows
f <- as.data.frame(lapply(1:20, function(x) 1:5e6))

# visualizing large matrixes (many rows and columns)
g <- matrix(0, nrow = 100000, ncol = 10000)

# matrixes with large strings inside
h <- matrix(paste0(rep(letters, length.out = 1e7), collapse="-"), nrow = 10, ncol = 10)

Note: When testing it's important to use a Release version of ark. In my tests, with debug can be 20x slower in such cases - probably for the tail recursion optimizations important for the object size computation.

Not fixed in this PR:

@dfalbel dfalbel marked this pull request as ready for review November 15, 2024 13:50
@dfalbel dfalbel requested a review from lionel- November 15, 2024 13:59
This was referenced Nov 15, 2024
Copy link
Contributor

@lionel- lionel- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

for char in elt.chars() {
if display_value.len() >= MAX_DISPLAY_VALUE_LENGTH {
is_truncated = true;
break 'outer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat syntax for long breaks

let mut out: Vec<Variable> = vec![];
let n = unsafe { Rf_xlength(value) };

let list = List::new(value)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// We need to be careful when truncating the string, we don't want to return invalid
// UTF8 sequences. `chars` makes sure we are not splitting a UTF8 character in half.
// See also https://doc.rust-lang.org/book/ch08-02-strings.html#slicing-strings
fn truncate_chars(value: String, len: usize) -> (bool, String) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC String::truncate()would be unsafe here because it takes a byte offset, not a number of chars? I see in the docs:

Panics if new_len does not lie on a char boundary.

Tricky!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly! We either need to find the char index to truncate using the char_indices() iterator or make a copy like we do here.

@@ -1779,6 +1818,14 @@ mod tests {
})
}

fn inspect_from_expr(code: &str) -> Vec<Variable> {
let env = harp::parse_eval_global("new.env()").unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth adding an Environment method at some point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 840a877, let me know what you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable

crates/ark/src/variables/variable.rs Outdated Show resolved Hide resolved
crates/ark/src/variables/variable.rs Outdated Show resolved Hide resolved
@dfalbel dfalbel merged commit cab11b9 into main Nov 21, 2024
6 checks passed
@dfalbel dfalbel deleted the bugfix/honor-max-display-entries branch November 21, 2024 13:49
@github-actions github-actions bot locked and limited conversation to collaborators Nov 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants