Skip to content

Commit 4833e43

Browse files
Add faster early returns for most vec_restore() operations
1 parent 9e95de5 commit 4833e43

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

R/vctrs.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@
33
# proxy/restore
44
# This is inefficient but seemingly required for vctrs machinery
55
method(vec_proxy, class_vecvec) <- function(x, ...) {
6-
data_frame(x = list(x@x), i = S7_data(x))
6+
data_frame(x = list(x@x), l = sum(lengths(x@x)), i = S7_data(x))
77
}
88
method(vec_restore, class_vecvec) <- function(x, to, ...) {
9+
if (is_vecvec(x)) return(x)
910
if (vec_size(x) == 0L) {
1011
return(S7_class(to)())
1112
}
1213

14+
# Fast path for vecvec without merging - just update the indices
15+
if(identical(sum(slot_len <- lengths(to@x)), slot_grp <- unique(x$l))) {
16+
S7_data(to) <- x$i
17+
# Invoke pruning of slots
18+
out <- to[seq_along(to)]
19+
attributes(out) <- attributes(to)
20+
return(out)
21+
}
22+
1323
# Identify groups of vectors
1424
# TODO - this destroys altrep
1525
grp <- vec_group_loc(x$x)

0 commit comments

Comments
 (0)