From maechler at stat.math.ethz.ch  Wed Aug 12 14:57:27 2009
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Wed, 12 Aug 2009 14:57:27 +0200
Subject: [Rd] readBin() arg check has unnecessary overhead (patch
	included)
In-Reply-To: <68e086180908110951j2f684448jbb31707b8e02ad0f@mail.gmail.com>
References: <68e086180908110951j2f684448jbb31707b8e02ad0f@mail.gmail.com>
Message-ID: <19074.48183.102932.170100@lynne.math.ethz.ch>

>>>>> Jon Clayden <jon.clayden at gmail.com>
>>>>>     on Tue, 11 Aug 2009 17:51:46 +0100 writes:

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

actually,  %in%  is liked by programmeRs for its inherent
robustness combined with "expressiveness" (<-> readability)
inspite of its potential efficiency loss wrt to  '=='
... 
and indeed, your patch fails in one case where the original code works:

  readBin(., NA_character_, ...)

However that case can also be checked explicitly,
and I will implement the corresponding patch.

Martin Maechler, ETH Zurich


    > 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

    > ______________________________________________
    > R-devel at r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel



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