[Rd] Use of C++ in Packages

Gabriel Becker g@bembecker @end|ng |rom gm@||@com
Fri Mar 29 23:20:07 CET 2019


Hi Jim (et al.),

Comments inline (and assume any offense was unintended, these kinds of
things can be tricky to talk about).

On Fri, Mar 29, 2019 at 8:19 AM Jim Hester <james.f.hester using gmail.com> wrote:

> First, thank you to Tomas for writing his recent post[0] on the R
> developer blog. It raised important issues in interfacing R's C API
> and C++ code.
>
> However I do _not_ think the conclusion reached in the post is helpful
>   > don’t use C++ to interface with R
>

I was a bit surprised a the the strength of this too but its understandable
given the content/motivation of the post.

My personal take away, without putting any words in Tomas' or R-core's
mouths at all, is that the crux here is that using c++ in R packages safely
is a LOT less trivial than people in the wider R community think it is,
these days. Or rather, there are things you can do safely quite easily when
using c++ in an R package, and things you can't, but that distincton a)
isn't really on many people's radar, and b) isn't super trivial to identify
at any given time, and c) depends on internal implementation details so
isn't stable / safe to rely on across time anyway. There are a lot of
reasons for a), and none of them, nor anything else I'm about to say,
constitute criticisms of Rcpp or its developers.

I've always thought that we as tool/software developers in this space
should make things seem as easy and convenient to users as they
can/intrinsically are, *but not easier*. I don't know how popular that
second part I put in there is generally, but personally I think its true
and pretty important not to leave off. I read Tomas' past as suggesting
that as a community, without pointing fingers or laying any individual
blame,  have unintentionally crossed "as easy as it actually is/can be to
do right" line when it comes to the impression we give to novice/journeyman
package developers regarding using c++to interact with the R internals. I
honestly claim little familiarity with c++ but it seems like Tomas is the
relevant expert on both it and hard-core details about how aspects of the R
internals work so if he tells us that that has happened, we should probably
listen.


> There are now more than 1,600 packages on CRAN using C++, the time is
> long past when that type of warning is going to be useful to the R
> community.
>

Here I disagree here pretty strongly. I think the warning is very useful -
unless these issues were widely known before the post (my impression is
that they weren't) - and ignoring its contents or encouraging others to do
so as influential members of the R community would be irresponsible.

I mean, the reality of the situation as it exists now is more or less (I'd
assume a great deal 'more' than 'less', personally) what Tomas described,
right? Furthermore, regardless of what changes may come in the future, it
seems very unlikely any of them will be in this coming release (since grand
feature freeze is like, today?) so we're talking a year out, at LEAST.
Given that, this advice, or at least a more nuanced stance that gives the
information from the post proper weight and is different from the
prevailing sentiment now, basically has to be realistic in the short term.

At the very least I think the post tells us that we need to be really
careful as a community with the "you want speed throw some c++ in your
package at it, you can learn how in a day and it's super easy and basically
free" messaging. The reality is more nuanced than that, at best, even if
ultimately in many situations that is a valid/reasonable approach.


>
> These same issues will also occur with any newer language (such as
> Rust or Julia[1]) which uses RAII to manage resources and tries to
> interface with R. It doesn't seem a productive way forward for R to
> say it can't interface with these languages without first doing
> expensive copies into an intermediate heap.
>
> The advice to avoid C++ is also antithetical to John Chambers vision
> of first S and R as a interface language (from Extending R [2])
>
>   > The *interface* principle has always been central to R and to S
> before. An interface to subroutines was _the_ way to extend the first
> version of S. Subroutine interfaces have continued to be central to R.
>
> The book also has extensive sections on both C++ (via Rcpp) and Julia,
> so clearly John thinks these are legitimate ways to extend R.
>
> So if 'don't use C++' is not realistic and the current R API does not
> allow safe use of C++ exceptions what are the alternatives?
>

Again, nothing is going to change about this for a year*, at least *(AFAIK,
not on R-core) so we have to make it at least somewhat realistic; perhaps
not the blanket moratorium that Tomas advocated - though IMHO statements
from R-core about what is safe/supported when operating in R arena should
be granted *a lot *of weight - but certainly not the prevailing sentiment
it was responding to, either. That is true even if we commit to also
looking for ways to improve the situation in the longer term.


>
> One thing we could do is look how this is handled in other languages
> written in C which also use longjmp for errors.
>
> Lua is one example, they provide an alternative interface;
> lua_pcall[3] and lua_cpcall[4] which wrap a normal lua call and return
> an error code rather long jumping. These interfaces can then be safely
> wrapped by RAII - exception based languages.
>

So there's the function that Simon mentioned, which would work at least for
evaluating R code, though it doesn't necessarily help when you want to hit
the C api directly I think. Because of ALTREP, a LOT of things can
allocate, and thus error, now. That was necessary to get what we needed
without an amount of work/refactoring that would have tanked the whole
project (I think), but it is a thing.


>
> This alternative error code interface is not just useful for C++, but
> also for resource cleanup in C, it is currently non-trivial to handle
> cleanup in all the possible cases a longjmp can occur (interrupts,
> warnings, custom conditions, timeouts any allocation etc.) even with R
> finalizers.
>
> It is past time for R to consider a non-jumpy C interface, so it can
> continue to be used as an effective interface to programming routines
> in the years to come.
>

I mean I totally get this desire, and don't even disagree necessarily in
principle, but that's a pretty easy thing to say, right? My impression,
without really knowing the details of what all that would entail is that it
would/will be a seriously non-trivial amount of work for a group of people
who are already very busy maintaining an extremely widely used, extremely
complex piece of software.

Best,
~G

>
>

	[[alternative HTML version deleted]]



More information about the R-devel mailing list