Refactor FormattedVector
for faster formatting of S3 objects
#646
+292
−276
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The variables pane uses the
FormattedVector
API to format vector elements to be displayed. One important assumption that we do when using this API is that it's lazy, in the sense that we create an iterator over the elements of a vector and we only pay the formatting price for the values we actually iterated.This is true for atomic vectors, for which we iterate using R's C API eg, extracting values with
REAL_ELT
and formattinng them in Rust. However, this is not true for any vector that has a class (except factors), in this case we need to use theformat
function, and we were actually formatting the entire vector before being able to iterate over the formatted values.This is very costly if we need for format very large vectors, as when we expand the data.frame in posit-dev/positron#3628 (comment)
This PR refactors the
FormattedVector
API, with the main change being:FormattedVector
iter()
iter()
,iter_n()
,column_iter()
,column_iter_n()
. Those suffixed withn
will only format the requested part of the vector.As a consequence, now creating the
iter()
returns aResult
, so we need to adapt in a few places.Now, most of the times in the variables pane we'll use
iter_n()
to only format the firstN
elements, that are necessaryto create the display value in the variables pane.