From pdalgd at gmail.com  Fri Aug 27 15:04:10 2010
From: pdalgd at gmail.com (peter dalgaard)
Date: Fri, 27 Aug 2010 15:04:10 +0200
Subject: [Rd] Is it safe not to coerce matrices with as.double() in .C()?
In-Reply-To: <B10BAA7D28D88B45AF82813C4A6FFA93B323DA@usctmx1157.merck.com>
References: <B10BAA7D28D88B45AF82813C4A6FFA93B323DA@usctmx1157.merck.com>
Message-ID: <B70A8DAE-0879-4E71-9792-359CF433240B@gmail.com>


On Aug 27, 2010, at 2:44 PM, Liaw, Andy wrote:

> I'd very much appreciate guidance on this.  A user reported that the
> as.double() coercion used inside the .C() call for a function in my 
> package (specifically, randomForest:::predict.randomForest()) is 
> taking up significant amount of time when called repeatedly, and
> Removing some of these reduced run time by 30-40% in some cases.
> These arguments are components of the fitted model (thus do not 
> change), and are matrices.  Some basic tests show no difference in
> The result when the coercions are removed (other than faster run time).
> What I like to know is whether this is safe to do, or is it likely to
> lead
> to trouble in the future?

In a word: yes. It is safe as long as you are absolutely sure that the argument has the right mode. The unsafeness comes in when people might unwittingly use, say, an integer vector where a double was expected, causing memory overruns and general mayhem. 

Notice, BTW, that if you switch to .Call or .External, then you have much more scope for handling such details on the C-side. E.g. you could coerce only if the object has the wrong mode, avoid duplicating things you won't be modifying anyway, etc. 

> 
> Best,
> Andy
> Notice:  This e-mail message, together with any attachme...{{dropped:14}}
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



[Rd] Is it safe not to coerce matrices with as.double() in .C()?
peter dalgaard