Skip to content

[Rcpp-devel] Armor, Shield and Shelter.

5 messages · Romain Francois, Dirk Eddelbuettel, Smith, Dale (Norcross) +1 more

#
Hello,

As usual when I work on some packge that use Rcpp, I come up with ideas. 
On dplyrRcpp I've defined these classes to handle stack based protection 
of objects from the GC. These are thin wrappers around PROTECT / 
UNPROTECT / PROTECT_WITH_INDEX

Sometimes, the Rcpp api classes don't quite cut it and one has to 
directly manipulate SEXP. When we do that we have to surrender to use 
the ugly PROTECT/UNPROTECT macros and whenever I use them, I feel 
unsafe, because later on I will forget to correctly unprotect, ...

Anyway, I'd like to propose adding Shield, Armor and Shleter to Rcpp. 
For now they live in the tools directory of dplyrRcpp.

Shield
======

That is simplest. When a Shield object is constructed, PROTECT is 
called, when it is destructed UNPROTECT(1) is called. Using it looks 
like this:

Shield<SEXP> x( ... ) ;

Shield has a conversion to SEXP operator that returns its SEXP so we can 
pass a Shield<SEXP> to any function that takes a SEXP.

Armor
=====

This is similar but it uses the less known PROTECT_WITH_INDEX and 
REPROTECT.

It should be preferred to Shield when we change the object we want to 
protect, for example in a loop.

When it is constructed, it protects its SEXP and records its index in 
the protection stack, when something is assigned to it, the new object 
takes its place so the previously protected object might no longer be.

Armor<SEXP> x( ... ) ;
x = ... ;


Shelter
=======

Shelter is a bit different as it protects several objects in the same 
scope. It has an function call like operator that protects its argument 
and bumps up the number of objects it currently protects. Whe it is 
destructed, it unprotects that many objects.

Looks like this for now:

Shelter<SEXP> ___ ;
SEXP x = ___(...) ;

There are cases when this is useful and for which I could not use either 
Armor or Shield. They are used throughout dplyrRcpp and it makes perfect 
sense to use them. That's another clear win of C++ against macros.



They are all templated, but it really only makes sense to use SEXP as 
the parameter, maybe I'll enforce that. It just looks nice to me:

Shield<SEXP>
Armor<SEXP>
Shekter<SEXP>

It conveys the idea of protecting a SEXP.


I will add them to Rcpp11, and I would like to add them to Rcpp too. So 
I wanted to start a discussion on them here.

With these, we should never need to call PROTECT / UNPROTECT macros ...

Please let me know what you think about that. Getting no reply means 
"nobody cares", so please comment on this if you want to see them in Rcpp.

This is starting to be the way I approach contributing to Rcpp, start a 
discussion on a feature and add it if people care. This is somewhat 
different from the approach I used to take which was put the code in 
anyway and let people know afterwards.

This is a non disruptive proposal as the template classes I propose 
don't interract with the rest of the code. We might not use them in 
Rcpp, but we should. There are currently 155 calls to UNPROTECT in the 
.h and .cpp of Rcpp. That is that many macro calls we could get rid of.

Romain
#
Romain,
On 11 October 2013 at 13:03, Romain Francois wrote:
| Anyway, I'd like to propose adding Shield, Armor and Shleter to Rcpp. 

Sure.

| This is a non disruptive proposal as the template classes I propose 
| don't interract with the rest of the code. We might not use them in 
| Rcpp, but we should. There are currently 155 calls to UNPROTECT in the 
| .h and .cpp of Rcpp. That is that many macro calls we could get rid of.

(UN)PROTECT calls in code internal to Rcpp are less of a worry (at least on
the user list); another question is how often Rcpp users need to resort to
this in their code, and how often it would help?  I don't have a good idea.

And OTOH as this seems to non-disruptive and orthogonal to existing code, why
not?  

And the names are very cute indeed. I'd say go for it.

Thanks,  Dirk
#
Le 11/10/13 13:26, Dirk Eddelbuettel a ?crit :
Sure. I'll make my case in the appropriate channel if we decide to add 
the feature.
They fit perfectly in what I do with dplyrRcpp. This replaces the usual 
reflex of "I can't use an api class so i'll just use PROTECT/UNPROTECT"

We can get an idea using some grepping for UNPROTECT in packages that 
depend on Rcpp.
It is. That would just be 3 template class, all using inline functions. 
The code produced by the compiler should be equivalent to using 
PROTECT/UNPROTECT ...

The code written by the developper will be nicer.
Kudos to http://thesaurus.com/browse/protector

I still want to see comments from users.

One thing I'm not certain about is the interface of Shelter, We might be 
able to make something more natural.
#
Romain,

I don't think I have any use for your proposed code at this time. However, I would like to say that reducing the number of macro calls is a worthy goal. My own attitude is "build it and they will use it".

Dale Smith, Ph.D.
Senior Financial Quantitative Analyst
Financial & Risk Management Solutions
Fiserv
Office: 678-375-5315
www.fiserv.com

-----Original Message-----
From: rcpp-devel-bounces at r-forge.wu-wien.ac.at [mailto:rcpp-devel-bounces at r-forge.wu-wien.ac.at] On Behalf Of Romain Francois
Sent: Friday, October 11, 2013 7:42 AM
To: Dirk Eddelbuettel
Cc: Rcpp-devel at r-forge.wu-wien.ac.at
Subject: Re: [Rcpp-devel] Armor, Shield and Shelter.

Le 11/10/13 13:26, Dirk Eddelbuettel a ?crit :
Sure. I'll make my case in the appropriate channel if we decide to add the feature.
They fit perfectly in what I do with dplyrRcpp. This replaces the usual reflex of "I can't use an api class so i'll just use PROTECT/UNPROTECT"

We can get an idea using some grepping for UNPROTECT in packages that depend on Rcpp.
It is. That would just be 3 template class, all using inline functions. 
The code produced by the compiler should be equivalent to using PROTECT/UNPROTECT ...

The code written by the developper will be nicer.
Kudos to http://thesaurus.com/browse/protector

I still want to see comments from users.

One thing I'm not certain about is the interface of Shelter, We might be able to make something more natural.

--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

_______________________________________________
Rcpp-devel mailing list
Rcpp-devel at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
#
I agree with Dale; it would be good to see these functions in Rcpp.

-Kevin

On Fri, Oct 11, 2013 at 6:57 AM, Smith, Dale (Norcross)
<Dale.Smith at fiserv.com> wrote: