[Rd] readBin() arg check has unnecessary overhead (patch included)

Jon Clayden jon.clayden at gmail.com
Tue Aug 11 18:51:46 CEST 2009

Dear all,

The version of readBin() in R-devel includes a use of match(), through
`%in%`, which can affect its performance significantly. By using
primitives instead of the rather expensive call to match(), I reduce
the time spent inside readBin() by more than 30% in some of my code
(part of the tractor.base package). A simple patch that does this is
given below. This passes "make check-devel" fine, and I don't see that
it could produce unexpected behaviour -- though I may, of course, be


--- R-devel/src/library/base/R/connections.R	2009-08-07 01:52:16.000000000 +0100
+++ R-devel-mod/src/library/base/R/connections.R	2009-08-11
16:22:30.000000000 +0100
@@ -193,6 +193,6 @@
     swap <- endian != .Platform$endian
     if(!is.character(what) || length(what) != 1L
-    	|| !(what %in% c("numeric", "double", "integer", "int", "logical",
-                         "complex", "character", "raw")))
+    	|| !any(what == c("numeric", "double", "integer", "int", "logical",
+                          "complex", "character", "raw")))
         what <- typeof(what)
     .Internal(readBin(con, what, n, size, signed, swap))

Jonathan D. Clayden, Ph.D.
Research Fellow
Radiology and Physics Unit
UCL Institute of Child Health
30 Guilford Street
United Kingdom

t | +44 (0)20 7905 2708
f | +44 (0)20 7905 2358
w | www.homepages.ucl.ac.uk/~sejjjd2/
w | www.diffusion-mri.org.uk/people/1

More information about the R-devel mailing list