-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2.3 modify on copy (rules of copy seem to have changed) #1753
Comments
I get the same results just modifying x. This doesn't match up with section 2.5 modify in-place which says
|
This part content may be not properl : https://advanced-r-solutions.rbind.io/names-and-values.html#modify-in-place |
Hi everyone, section 2.3 actually gives much information, but perhaps it doesn't put enough emphasis on the fact that the copy-on-replace behaviour is tightly linked to the number of references to the object. To troubleshoot the above examples: first and foremost, please run "bare R", not through RStudio, which actually adds references to objects for the purpose of the GUI. See http://adv-r.had.co.nz/memory.html#modification from the 1st edition to read more about that. For me (R version 4.3.1 on my x86_64-pc-linux-gnu (64-bit) running Debian GNU/Linux), the following example (same as @jxu and @HyacinthMeng above) works well, without copy:
Another interesting thing I discovered is that apparently, when we run something like
This entails the interesting behaviour that when using the shorthand "1:3", the very first replacement seems to make a copy, while the subsequent ones (even when extending the object, provided memory management allows for enough room at that particular place) do not create a copy:
But sometimes, when you extend the vector, the memory management will go find enough room somewhere else:
Perhaps @hadley could add something on this stuff in the book, and close this issue? I also read that the way R deals with references to objects in undergoing some work, so perhaps what we say here may be outdated soon...? |
You mean like this? 😄
|
Yes, thank you Hadley, I had read that, but perhaps the OP didn't... ;) Still, IMHO there is something to explain about that number of references seemingly equal to 2^16 - 1 for a "promise"(?) like |
@jean-baka there's a good reason that the second edition doesn't use |
Hi,
it seems that the rules for copy-on-modify had changed.
Every time y is modified, y is copied to a new address like you see just below (cf. section 2.31 tracemem() of the book)
Matrix products: default
locale:
[1] LC_COLLATE=French_France.utf8 LC_CTYPE=French_France.utf8
[3] LC_MONETARY=French_France.utf8 LC_NUMERIC=C
[5] LC_TIME=French_France.utf8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.2.2 tools_4.2.2
The text was updated successfully, but these errors were encountered: