[Rd] R (>= 3.4.0): integer-to-double coercion in comparisons no longer done (a good thing)

Henrik Bengtsson henrik.bengtsson at gmail.com
Sun Jan 28 00:01:14 CET 2018


Hi,

there was a memory improvement done in R going from R 3.3.3 to R 3.4.0
when it comes to comparing an integer 'x' an double 'y' (either may be
scalar or vector).

For example, in R 3.3.3, I get:

> getRversion()
[1] '3.3.3'
> x <- integer(1000)
> y <- double(1000)
> profmem::profmem(z <- (x < y))
Rprofmem memory profiling of:
z <- (x < y)

Memory allocations:
      bytes      calls
1      8040 <internal>
2      4040 <internal>
total 12080
>

and in R 3.4.0, I get:

> getRversion()
[1] '3.4.0'
> x <- integer(1000)
> y <- double(1000)
> profmem::profmem(z <- (x < y))
Rprofmem memory profiling of:
z <- (x < y)

Memory allocations:
      bytes      calls
1      4040 <internal>
total  4040

Note how in R (<= 3.3.3), the (x < y) comparison will cause an
internal coercion of integer vector 'x' into a double, which then can
be compared to double 'y'.  In R (>= 3.4.0), it appears that this
coercion is done per element and will therefore avoid introducing a
new, temporary copy internally.  The same is observed with when
comparing with (x == y).

This is a great improvement that I think deserves more credit.  I
couldn't find any mentioning of it in the R 3.4.0 NEWS
(https://cran.r-project.org/doc/manuals/r-release/NEWS.html).  Does
anyone know whether this was a specific improvement for such
comparison, or a side effect of something else, e.g. an improved byte
compiler?

Thanks,

Henrik



More information about the R-devel mailing list