-
-
Notifications
You must be signed in to change notification settings - Fork 213
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
Rcpp is not using R_PreserveObject
/R_ReleaseObject` as intended
#1081
Comments
Thanks for the detailed post, and the worked suggestion using Now, is there by chance a truly minimally reproducible example that would not involve the ggplot2 stack, tibbles, and a number of other packages (including one not on CRAN) ? |
On Sun, 17 May 2020, Dirk Eddelbuettel wrote:
Thanks for the detailed post, and the worked suggestion using Rcpp_precious
(love the name ;-) ) and a simple list. For disruptive changes I appreciate
the minimalism. If this works (as expected) we could look into a hashed
structure later.
Now, is there by chance a truly minimally reproducible example that would
not involve the ggplot2 stack, tibbles, and a number of other packages?
Not from me. I don't know if there is something these other packages
are doing that from Rcpp's perspective they shouldn't, but that is
between you and them. Rcpp is making a ton of calls to
R_PreserveObject, and that isn't the way that mechanism is intended to
be used.
It does look from your NEWS file like there is already some work in
progress in this direction; maybe it would be good to accelerate that.
But other than looking over a design document, if you have one, I'm
stepping back from this.
…
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, orunsubscribe.[AA55UVCUQJORGFQC64J2S2TRSABDHA5CNFSM4NDNY5UKYY3PNVWWK3TUL52HS4
DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEWFEHTQ.gif]
--
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa Phone: 319-335-3386
Department of Statistics and Fax: 319-335-3017
Actuarial Science
241 Schaeffer Hall email: luke-tierney@uiowa.edu
Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
|
Use of And we rather happily relied on this base R feature all those years. But presumably all use cases up to now were only 'lower-case s' stress testing where @clauswilke and @bhive01 seem to have found a way to put a capital S there. Or, quite possibly, other use cases that may have seen failures were less determined to drill down. You all now did, so thanks for that. Resource management in the context of a dynamically managed system can have its challenges. Relying on this mechanism worked really well so far. Let's see if you can turn it up one notch. Looking at the code now, requires some tweaks. |
On Sun, 17 May 2020, Dirk Eddelbuettel wrote:
Use of R_PreserveObject (and the matching release) goes back 15 years to the
very first C++ class wrappers around SEXP objects. By using C++ facilities
like the constructor and destructor we can generally ensure proper resource
management (cf the RTTI idiom in C++ parlance).
And we rather happily relied on this base R feature all those years.
But presumably all use cases up to now were only 'lower-case s' stress
testing where @clauswilke and @bhive01 seem to have found a way to put a
capital S there. Or, quite possibly, other use cases that may have seen
failures were less determined to drill down. You all now did, so thanks for
that.
Same for the 20-or-so-year old recursive code in
R_ReleaseObject. Works fine until someone throws 300K objects at it
and tries to release the first one ...
… Resource management in the context of a dynamically managed system can have
its challenges. Relying on this mechanism worked really well so far. Let's
see if you can turn it up one notch. Looking at the code now, requires some
tweaks.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, orunsubscribe.[AA55UVBLSGYEJICAC7SUZHTRSAGFLA5CNFSM4NDNY5UKYY3PNVWWK3TUL52HS4
DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEWFFYHY.gif]
--
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa Phone: 319-335-3386
Department of Statistics and Fax: 319-335-3017
Actuarial Science
241 Schaeffer Hall email: luke-tierney@uiowa.edu
Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
|
This would probably function as a reproducible example (depending on stack size +
(Note: reproducibility may depend on whether your C++ standard library implementation destructs elements from first-to-last, or last-to-first. AFAIK this isn't explicitly mandated by the standard) The problem occurs because of order of destruction for vector elements doesn't match the order in which they were created, leading to this deep recursion into the precious list. Ultimately, the goal of the Rcpp constructors and destructors is to protect the associated object from the GC for the lifetime of that object, and unprotect it once collection is safe. I think using a hash table is worth considering just because it would insulate us from other issues (e.g. performance) related to order-of-destruction issues. |
Correct, and we were on the same calling path. |
While the suggested design can be added to the package fairly easily, it does not account for use from |
I have been trying to create a simpler reprex, but I'm not sure I've been successful. The code below attempts to simulate what I think is causing the issues in the ggplot2 example, and it crashes my R session if I run it twice. But the error is
|
(But that is slightly different. |
Ah, Ok. I'm the user here, but I may not know what I'm doing. :-) The gridtext package definitely wraps things into |
@eddelbuettel Could you provide a minimal correct example using |
Sorry, I need to rephrase that. The XPtr generally has a default finaliser too so I stated that poorly, my bad. I use them a lot myself when I also rely on a default cleanup. It's just that maybe in this example ensuring a cleanup at end of scope may help. To be seen. |
In my reprex, you could just replace |
Simplifying is good. And apologies again for the comment two up---I also rely on the default finalizer (see Other (standard) XPtr use (I do a ton myself) is to ... do some work, set up some data structures or pointers to persist ... do some other stuff ... have a code block relying on the XPtr allocated objects ... some other things ... and then end-of-scope (end of subroutine say) to clean things up. That works of course as intended. "It just gets very slow": Yes, we always say that R objects we wrap should not use |
Ok, needed to take a break, clear my head, work on something else ... to then come back and now have the (basic idea and solution of) Luke's proposal implemented (in a slightly different way because of the way Rcpp is called etc pp). Still passes unit tests, but (once I installed package |
y1;5202;0cOn Sun, 17 May 2020, Dirk Eddelbuettel wrote:
Ok, needed to take a break, clear my head, work on something else ... to
then come back and now have the (basic idea and solution of) Luke's proposal
implemented (in a slightly different way because of the way Rcpp is called
etc pp). Still passes unit tests, but (once I installed package ggtext) it
does not appear to run the example above any better: it still slows to halt
in loop and then spends minutes sitting there. (R 4.0.0, Ubuntu 19.10,
everything current from CRAN).
If you are using a linked list then that is what I would expect. But
you should have gotten rid of the stack overflow segfault that you
would still be getting in R 4.0.0 (you would need R-devel/R-patched to
avoid that without doing something in Rcpp).
…
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, orunsubscribe.[AA55UVC3KZVXQQWLAUFH2ADRSBLXXA5CNFSM4NDNY5UKYY3PNVWWK3TUL52HS4
DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEWFP34A.gif]
--
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa Phone: 319-335-3386
Department of Statistics and Fax: 319-335-3017
Actuarial Science
241 Schaeffer Hall email: luke-tierney@uiowa.edu
Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
|
It never crashed for me before or after. R 4.0.0, standard Ubuntu build via Michael Rutter based on my standard Debian package for R. No tricks, no patches. Also Ubuntu 19.10, standard kernel, but more memory than I had on past machines but nothing outrageous. I may push this branch later for @kevinushey to look at (and/or @clauswilke to try too). |
@kevinushey to understand the conditions elaborating on your example: one would hope that this is not something that anyone would actually write, since all the protection is unnecessary and wasteful. Simply allocating an R list would need only a single protection for the list itself and no other protection as all elements are already protected by the enclosing object. So is there any hope of identifying where those excessive preserve/release calls come from? I'd say that is really where the problem lies - R package code almost never needs explicit protections, since only the result object needs to be protected and all other objects are transient with direct transfer of ownership. I think Rcpp would be in a great position to provide tools here - it could warn if the pool gets large. (This is assuming that Rcpp itself is not creating those unnecessary protections and that it provides proper ownership transfer when assigning R objects.) |
Step back a second: Rcpp lets you create object, so we simply started 'by setting the protect bit' to make them comply with standard behavior. That worked wonderfully well since the very beginning, and this is really the first report I can think of identifying side effects. And with that, again thanks for already suggesting one alternative. And the thought of a singleton instance hosting such a 'pool', possibly via hashing, also crossed my mind today. We'll see if anybody has appetite and time to work on it. |
I'm not sure I understand - there is no "protect bit" in R - there is only object ownership. If an object is not contained in (owned by) any other object, it is considered deletable and gets garbage-collected - that's how R works. There are two objects are that never collected: the protect stack and the precious list. The former is where As Luke said, if you had any official description of the |
It's not very complicated, and the sources are out there for everybody to look at. |
Taking a closer look, this code looks suspect: Rcpp/inst/include/Rcpp/String.h Lines 530 to 538 in 5e5de18
Normally, So I think this may ultimately be a bug in the Rcpp
|
Hm, OTOH was that not exactly the standard pattern: mark an object returned to R as protected? I just cleaned up the branches; I had accidentally dropped the earlier commit on master which was of course a mistake (reset and force pushed, master back to where it was) and the work based on Luke's suggestion in its own branch. |
The String Rcpp/inst/include/Rcpp/internal/wrap.h Lines 929 to 934 in 95d0854
which really does just give you an unprotected R object from a |
Hm, indeed, point taken. Sadly, "empirically" on the problem by @clauswilke none of it matters. Still runs ~15 mins for me with all the slowdowns in the 2nd run. Using the code in the new branch. |
I have instrumented R[1] to record modifications of the precious list and their source. The result is that @clauswilke's example for While analyzing the log, one thing that struck me is that objects get protected many times - out of the 805,500 protections 517,299 are duplicates (i.e. protecting already protected object). Here is the breakdown of multiplicity (i.e. how many entries of the object are still left on the precious list) and type of the leaked objects for that example with
As I was trying to point out, Rcpp is actually in a position to track such things itself and address it and/or provide guidance to package authors since it controls the abstraction. This has really nothing to with R, but with the memory management model of Rcpp. [1] - https://gist.github.com/s-u/68423081a8ba9b33765f3742f75ea890 |
These tests are slow, and I didn''t quite internalize how these choice of preserving/removing objects affect packages And for now it looks like the branch by @kevinushey wins. The example runs in about 10 minutes, so faster than the old solution and faster than the suggested mod by @ltierney which I put in first. Ultimately we probably want something else. To be discussed. |
I was digging deeper and I really didn't realize that we're essentially just debugging
Note that the PR above has no effect on the leaks for me. It is certainly sensible, just doesn't seem to reduce the allocations in |
To be clear: There may very well be problems in the gridtext code. We may also be dealing with multiple different issues at once. The original problem was a stack overflow (wilkelab/gridtext#9), and we determined that that was not under my control. If gridtext is leaking memory, that would be a separate issue. |
Sorry, most of that speed gain was a different test in the 'bug' script where I replaced |
(The commit message by @kevinushey was overeagerly interepreted by GH's bot as a fixed. Reopening). PR #1082 is now in Rcpp 1.0.4.11 which you can install from the |
Regarding the memory model -- Rcpp's objects are basically like C++ Given that, we could probably do a lot better by using our own internal reference counting implementation, and only preserve / release objects when the reference count changes between 0 and 1. I agree it also makes sense to use our own hash table rather than pushing everything directly onto the |
On Mon, 18 May 2020, Kevin Ushey wrote:
Regarding the memory model -- Rcpp's objects are basically like C++
shared_ptrs, in that multiple Rcpp objects can reference the same R object,
and that object should remain alive (protected from garbage collection) as
long as any Rcpp object referencing that data is alive.
Given that, we could probably do a lot better by using our own internal
reference counting implementation, and only preserve / release objects when
the reference count changes between 0 and 1.
I agree it also makes sense to use our own hash table rather than pushing
everything directly onto the R_PreciousList.
An alternative that might be worth exploring (not thought through
carefully so there may be issues):
Instead of, or in addition to, storing the R SEXP in your Rccp object,
store a CONS cell with the R SEXP in the TAG field. Use the CAR and
CDR fields to link the cell into a doubly-linked list, with the head
registered with R_PreservObject. When there are no longer Rccp
references to the R SEXP, the point where you are now using
R_DeleteObject, just unlink the cell. That would give O(1)
performance.
…
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, orunsubscribe.[AA55UVCYLLFHSQ5IOFGSIZDRSFR5JA5CNFSM4NDNY5UKYY3PNVWWK3TUL52HS4
DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEWI4K2Q.gif]
--
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa Phone: 319-335-3386
Department of Statistics and Fax: 319-335-3017
Actuarial Science
241 Schaeffer Hall email: luke-tierney@uiowa.edu
Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
|
If this is the case, does managing the resource via a shared_ptr with custom alloc/finaliser make sense - as opposed to implementing your own reference counting? |
I don't think this helps. Rcpp objects just have a reference to the underlying |
I mean if you construct the (Rcpp) object from a SEXP you
Then move or copy of an Rcpp object doesnt need to call protect or preserve on the SEXP, just move/copy the shared_ptr and it will take care of the ref counting. |
Hi @clauswilke I see per CRANberries that ggtext is now on CRAN -- congrats. Did the issue discussed here affecting interactions between ggtext, gridtext, Rcpp and R get sorted out or circumvented? Was there anything we can learn from it? |
Dirk, I can tell you as the end-user of ggtext/gridtext and the originator
of this issue that Luke's edits to R-devel solved the crashing problem, but
at significant cost to run-time under non-crashing circumstances. I don't
understand enough of the underlying systems to comment on how to fix the
performance, but if any of the comments above can help in that regard I
would certainly welcome those improvements as well.
…On Thu, Jun 4, 2020 at 5:50 AM Dirk Eddelbuettel ***@***.***> wrote:
Hi @clauswilke <https://github.com/clauswilke> I see per CRANberries that ggtext
is now on CRAN
<http://dirk.eddelbuettel.com/cranberries/2020/06/04#ggtext_0.1.0> --
congrats. Did the issue discussed here affecting interactions between
ggtext, gridtext, Rcpp and R get sorted out or circumvented? Was there
anything we can learn from it?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1081 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADANXKBWOIHELZPGSFJX6DRU6KCTANCNFSM4NDNY5UA>
.
|
The performance issue is well understood -- it is the linear search for the object to be removed. At this point I don't think R-core can afford to do anything about that, as performance is fine if the facility is used as intended (i.e.sparingly). There is an outside chance that an improvement might come as part of other improvements, but I wouldn't count on it. If Rccp has not resolved this yet, I am fairly confident that the doubly-linked list approach would be the best option. A hash table would be an alternative, but pointer hashing has some risks. |
We tried, it's all documented in this thread. And redoing this is not an entirely simple fix either. If used in a normal manner it still works as it has for the previous dozen years, and by now 1950 packages on CRAN. |
Yeah, I think this is a particularly unusual use case, with gridtext making some poor choices, Rcpp making some poor choices, and @bhive01 wanting to generate thousands of plots in a single RMarkdown chunk. I think it's fine if in some weird corner cases performance is poor. Eventually, I plan to rewrite gridtext completely, since I've learned a lot from my first implementation, and hopefully this issue will go away then. |
That was my read from your code too. There wasn't yet an obvious way to help---but a simpler fix may just be a simple stack for strings we could do 'outside' of the core Rcpp operations relying on simpler and well-tested STL semantics. This really is a corner of a corner case, and I am not quite willing to rock the general stability for this. Those ideas and rough sketches are all good, as are tests so if someone has time and this itch to scratch I will help with solid testing. But sadly I am not going to have time for rewrite of this myself. |
Basically my take as well for the R side, but we both know it only takes one case from someone with a large twitter following ... :-) |
Well thanks all for your help, especially to Luke for getting me going again with R-devel patch. Better to be running and slower than not running at all. Hopefully, improvements down the line will resolve this issue entirely. |
I'll also point out that I wrote my code as weirdly as I did because otherwise there were encoding issues on Windows. If at some point in the future we can use utf-8 on Windows then C++ code relying on extensive string manipulations can simply use native C++ strings and then many of these issues will go away. |
Here's a simple example: #include <Rcpp.h>
using namespace Rcpp;
class SimpleClass {
public:
IntegerVector x;
SimpleClass(IntegerVector x) : x(x) {}
};
typedef std::vector<SimpleClass> SimpleClassVec;
// [[Rcpp::export]]
SEXP new_simple_class(IntegerVector x) {
XPtr<SimpleClassVec> ptr(new SimpleClassVec());
for (int i = 0; i < 1e5; i++) {
ptr->push_back(SimpleClass(x));
}
return ptr;
}
/*** R
system.time(for (i in 1:100) {
message("Iteration: ", i)
new_simple_class(1:1e5)
})
*/ In my computer, this stalls at iteration 2-3, and I have to kill the R session. The babsim.hospital package uses simmer to run thousands of simulations and is hitting this issue consistently (cc @olafmersmann). The numbers in my example above are deliberately exaggerated to hit the issue soon, but, comparatively, this #include "cpp11.hpp"
#include <vector>
using namespace cpp11;
class SimpleClass {
public:
integers x;
SimpleClass(integers x) : x(x) {}
};
typedef std::vector<SimpleClass> SimpleClassVec;
[[cpp11::register]]
SEXP new_simple_class(integers x) {
external_pointer<SimpleClassVec> ptr(new SimpleClassVec());
for (int i = 0; i < 1e5; i++) {
ptr->push_back(SimpleClass(x));
}
return ptr;
} saved as cpp11::cpp_source("test_cpp11.cpp")
system.time(for (i in 1:100) {
message("Iteration: ", i)
new_simple_class(1:1e5)
}) takes just 7 seconds in my computer. EDIT: I see the example is similar to #1081 (comment) (sorry, long thread :) ). |
with thanks to @ltierney for the report in RcppCore#1081 as well as a suggested alternative this branch reworks his idea somewhat to better fit how Rcpp sets itself up
For completeness, with the PR #1033 we'll likely merge tomorrow they both take the same time (where I took the message printing out and simplified the outer loop): > Rcpp::sourceCpp("/tmp/exRcpp.cpp")
> system.time(replicate(100, new_simple_class_rcpp(1:1e5)))
user system elapsed
2.639 0.012 2.651
>
> cpp11::cpp_source("/tmp/exCpp11.cpp")
> system.time(replicate(100, new_simple_class_cpp11(1:1e5)))
user system elapsed
2.711 0.064 2.774
> |
This is now taken care of in Rcpp 1.0.6.2 via #1133. Big thanks to everybody who helped with this, it is a very nice fix. |
This is based on an issue found by @bhive01 and discussed with him
and @clauswilke in wilkelab/gridtext#9.
Running the code included at the end in R versions up through 4.0.0
with the C stack set to 1M will segfault with a C stack overflow. The
same happens (for me on Ubuntu at least) with the default 8M stack if
you change the value of N to 1000.
Running this code as is (i.e. N == 500) and with an 8M stack will not
fail, but takes 14 minutes due to several multi-minute-long pauses in
the second loop.
The problem is that this code, via calls to
R_PreserveObject
inRcpp, puts around 300K objects into the preserved object list. This is
a simple linked list, so has to be searched linearly to remove objects
pushed on early. That is the reason for the long pauses. The segfault
happens because the code for
R_ReleaseObject
was using recursion towork down the list. R-devel and R-patched now use iteration and no
longer segfault, but that doesn't help anyone running R 4.0.0 or
earlier.
The R Extensions Manual says about the
R_PreserveObject
/R_ReleaseObject
mechanism:The linked list data structure, and even the recursive delete, are
reasonable for this usage but are not suited for handling 300K objects
that may need to be removed in random or first in/first-out order.
Assuming you need to keep alive R objects, a better way to do this is
to create an appropriate data structure that you control, and protect
only that data structure with
R_PreserveObject
(and maybe release iton unload with
R_ReleseObject
). That way you can use a moreappropriate data structure, such as a hash table. Using a hash table
the code that currently takes 14 minutes would only take about 1.5
minutes. The improvement is even larger for N == 1000.
Even if you don't want to go to a hash table now it would be a good
idea to switch to using your own list so users of R 4.0.0 or earlier
don't run the risk of a segfault.
For using your own list you need something like this
Then replace
R_PreserveObject
andR_ReleaseObject
withwhere (this is now in R_devel/R_patched)
This maintains your own list, which is protected by being in the
CDR
field of the protected
Rcpp_precious
list cell. This will give youthe same reliability and performance across all R versions. [Code is
not tested, so there may be typos, but that is design I recommend].
If you want better performance for a use pattern as exhibited in the
example here you could look at using a hash table stored in a
VECSXP
placed in one of the cells of
Rcpp_precious
. In a quick and dirtyexperiment a simple hash table did much better, but the code is of
course more complicated.
All that said, I have a feeling that it should be possible to do
better by using a combination of the protected fields in external
pointer objects and weak references. If you have a document describing
the design of your memory management I could take a look and see if I
can suggest something.
Here is the code to run, from @clauswilke in wilkelab/gridtext#9.
The text was updated successfully, but these errors were encountered: