{"action":"created","issue":{"url":"https://api.github.com/repos/RcppCore/Rcpp/issues/355","labels_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/355/labels{/name}","comments_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/355/comments","events_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/355/events","html_url":"https://github.com/RcppCore/Rcpp/issues/355","id":103881268,"number":355,"title":"Move semantics for R in Rcpp function","user":{"login":"tqchen","id":2577440,"avatar_url":"https://avatars.githubusercontent.com/u/2577440?v=3","gravatar_id":"","url":"https://api.github.com/users/tqchen","html_url":"https://github.com/tqchen","followers_url":"https://api.github.com/users/tqchen/followers","following_url":"https://api.github.com/users/tqchen/following{/other_user}","gists_url":"https://api.github.com/users/tqchen/gists{/gist_id}","starred_url":"https://api.github.com/users/tqchen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tqchen/subscriptions","organizations_url":"https://api.github.com/users/tqchen/orgs","repos_url":"https://api.github.com/users/tqchen/repos","events_url":"https://api.github.com/users/tqchen/events{/privacy}","received_events_url":"https://api.github.com/users/tqchen/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-08-29T20:48:59Z","updated_at":"2015-08-29T21:56:48Z","closed_at":null,"body":"Inspired by discussion with @thirdwing and @hetong007.  I am not expert in R and Rcpp. However, we are making machine learning packages for R using c++. So this comes from our use case, and I believe it could be useful in general.\r\n\r\nThe motivation is to create an online update API for some machine learning models. Or in simplified case, recording word counts. We want to create a function that reads document one by one, and update the word count in a matrix.\r\n\r\nA common way to implement word count in C++, is create a vector, and update the vector by adding to the cells as the data get updated.  However, R is a language that normally immutable states. This comes with the cost on having to copy the object each time you write it. So a R function for update will usually looks like\r\n```r\r\nword_count_vector <- update(word_count_vector, data)\r\n```\r\n\r\nInternally, this means copy the word_count_vector, update it with data, and return a new one. This introduces a cost in memory copy, and is not suitable when data is not a lot, and there is only small amount of updates.\r\n\r\nOne way to deal with this problem is to make word_count_vector ***mutable***, which is possible if there is a C++ API and ```word_count_vector``` is implemented with external ptr. \r\n\r\n```r\r\n# update will update word_count_vector\r\nupdate(word_count_vector, data)\r\n```\r\n \r\nHowever, this kind of semantics is not natural to R users. As users usually assume  ```word_count_vector``` is immutable.  More importantly, it is impossible to warn the user when they did something wrong, like treat the word_count_vector as the one that has not been changed.\r\n \r\nAfter some discussion, we think it is interesting to introduce a ***move sematics***, so user can still write\r\n\r\n```r\r\nword_count_vector <- update(word_count_vector, data)\r\n```\r\n\r\nWhich is in the same way as R. However, when word_count_vector is passed into the function, it get ```moved```, and the original ```word_count_vector``` is no longer valid. It is possible to hit an error to the user when they attempt to use the old ```word_count_vector```, if an invalid field, or nullptr is encountered in internal implementation. This allows run-time check for violation of the move semantics, tell the user if they did something wrong.\r\n\r\nIn this way, the move semantics kind of allows mutations in R in a way that R user is familiar with. But still get the benefit of reusing the data structure in C++, so no re-allocation and copy is needed.\r\n\r\nWe believe this could be helpful to introducing online updating functions that continuously updates a state and the old states are dumped. "},"comment":{"url":"https://api.github.com/repos/RcppCore/Rcpp/issues/comments/136056004","html_url":"https://github.com/RcppCore/Rcpp/issues/355#issuecomment-136056004","issue_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/355","id":136056004,"user":{"login":"tqchen","id":2577440,"avatar_url":"https://avatars.githubusercontent.com/u/2577440?v=3","gravatar_id":"","url":"https://api.github.com/users/tqchen","html_url":"https://github.com/tqchen","followers_url":"https://api.github.com/users/tqchen/followers","following_url":"https://api.github.com/users/tqchen/following{/other_user}","gists_url":"https://api.github.com/users/tqchen/gists{/gist_id}","starred_url":"https://api.github.com/users/tqchen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tqchen/subscriptions","organizations_url":"https://api.github.com/users/tqchen/orgs","repos_url":"https://api.github.com/users/tqchen/repos","events_url":"https://api.github.com/users/tqchen/events{/privacy}","received_events_url":"https://api.github.com/users/tqchen/received_events","type":"User","site_admin":false},"created_at":"2015-08-29T21:56:48Z","updated_at":"2015-08-29T21:56:48Z","body":"Interesting. I know see that it is possible by using current interface directly. \r\n\r\nIt might be nice to have a extension of ```Rcpp:XPtr``` that support such semantics, and hit a error automatically when a fetch to the XPtr is made in a state of moved pointer. \r\n\r\nThis kind of semantics might be worth advocating for Rcpp users who want to reuse memory and also be friendly to R interface.\r\n"}}