r-lib / rray

Simple Arrays
https://rray.r-lib.org
GNU General Public License v3.0
129 stars 12 forks source link

Call `vec_restore_default()` when the inner types are the same #169

Closed DavisVaughan closed 5 years ago

DavisVaughan commented 5 years ago
vec_restore.vctrs_rray <- function(x, to, ..., i = NULL) {

  # Use the much faster default restore method if the inner types
  # are the same. Otherwise we have to go through the constructor
  # to get the correct type specific `class` restored
  if (typeof(x) == typeof(to)) {
    return(NextMethod())
  }

  x_dim <- rray_dim(x)

  new_rray(
    .data = vec_data(x),
    size = x_dim[1],
    shape = x_dim[-1],
    dim_names = rray_dim_names(x)
  )
}

will require a change to rray_full_like() to just add empty dim names

DavisVaughan commented 5 years ago

related to #162

DavisVaughan commented 5 years ago

can also remove the vec_data() call (avoiding 2 copies on R <3.6) because vctrs nukes attributes for us

DavisVaughan commented 5 years ago

173