r-lib/sparsevctrs

fix bug in altrep_sparse_real_Dataptr()

Closed this issue · 3 comments

The following code should not throw an error, as setting attributes shouldn't force materialization

aaa <- 1:1000000000000

# No materialization 
attributes(aaa) <- list(h = 3)

# Forced materialization
aaa[]
#> Error: vector memory exhausted (limit reached?)
library(sparsevctrs)
  
options("sparsevctrs.verbose_materialize" = TRUE)
  
aaa <- new_sparse_real(1, 5, 10)

attributes(aaa) <- list(h = 3)
#> sparsevctrs: Sparse vector materialized

per https://svn.r-project.org/R/branches/ALTREP/ALTREP.html#some_guidelines,

A common practice in C code in the base R implementation is to duplicate an object and modify the copy in place. ALTREP classes need to handle this in an appropriate way. For example, this is how unary arithmetic functions produce their return value. For most SEXP types, in particular all vector types, if a Duplicate method is provided that returns a non-NULL value, then that value should be freshly allocated with no references. This will allow attributes to be modified. If the class value can provide a writable data pointer then this also supports modifying the data.

add R_set_altrep_Duplicate_method() per #5

Fixed in #46

library(sparsevctrs)
  
options("sparsevctrs.verbose_materialize" = TRUE)
  
aaa <- sparse_double(1, 5, 10)

attributes(aaa) <- list(h = 3)

Created on 2024-05-13 with reprex v2.1.0