From jon.clayden at gmail.com  Tue Aug 11 18:51:46 2009
From: jon.clayden at gmail.com (Jon Clayden)
Date: Tue, 11 Aug 2009 17:51:46 +0100
Subject: [Rd] readBin() arg check has unnecessary overhead (patch included)
Message-ID: <68e086180908110951j2f684448jbb31707b8e02ad0f@mail.gmail.com>

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
wrong.

Regards,
Jon

--- 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
LONDON  WC1N 1EH
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



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