library(cheapr)
library(bench)
df <- new_df(x = sample.int(10^4))
# Note the memory allocation
mark(shallow_copy(df), iterations = 1)
mark(deep_copy(df), iterations = 1)
# In both cases the address of df changes
address(df);address(shallow_copy(df));address(deep_copy(df))
# When shallow-copying attributes are not duplicated
address(attr(df, "names"));address(attr(shallow_copy(df), "names"))
# They are when deep-copying
address(attr(df, "names"));address(attr(deep_copy(df), "names"))
# Adding an attribute in place with and without shallow copy
invisible(attrs_add(df, key = TRUE, .set = TRUE))
attr(df, "key")
# Remove attribute in-place
invisible(attrs_add(df, key = NULL, .set = TRUE))
# With shallow copy
invisible(attrs_add(shallow_copy(df), key = TRUE, .set = TRUE))
# 'key' attr was only added to the shallow copy, and not the original df
attr(df, "key")
Run the code above in your browser using DataLab