Copy-on-modify

  • If you modify a value bound to multiple names, it is ‘copy-on-modify’
  • If you modify a value bound to a single name, it is ‘modify-in-place’
  • Use tracemem() to see when a name’s value changes
x <- c(1, 2, 3)
cat(tracemem(x), "\n")
#> <0x560e54534fa8>
y <- x
y[[3]] <- 4L  # Changes (copy-on-modify)
#> tracemem[0x560e54534fa8 -> 0x560e545f1c58]: eval eval withVisible withCallingHandlers eval eval with_handlers doWithOneRestart withOneRestart withRestartList doWithOneRestart withOneRestart withRestartList doWithOneRestart withOneRestart withRestartList withRestarts <Anonymous> evaluate in_dir in_input_dir eng_r block_exec call_block process_group withCallingHandlers <Anonymous> process_file <Anonymous> <Anonymous> render_cur_session <Anonymous>
y[[3]] <- 5L  # Doesn't change (modify-in-place)

Turn off tracemem() with untracemem()

Can also use ref(x) to get the address of the value bound to a given name