IRkernel / repr

String and byte representations for all kinds of R objects
https://irkernel.github.io/docs/repr/
GNU General Public License v3.0
54 stars 33 forks source link

improve short-circuit logic #73

Open flying-sheep opened 8 years ago

flying-sheep commented 8 years ago

i just checked the code of capabilities(), and what @randy3k did in #62 is probably not that fast after all. this is the code:

capabilities <- function (what = NULL) {
    z <- .Internal(capabilities())
    if (!is.null(what)) 
        z <- z[match(what, names(z), 0L)]
    if (.Platform$OS.type == 'windows') 
        return(z)
    nas <- names(z[is.na(z)])
    if (any(nas %in% c('X11', 'jpeg', 'png', 'tiff'))) {
        z[nas] <- tryCatch(.Internal(capabilitiesX11()), error = function(e) FALSE)
    }
    z
}

so there are 1-2 internal calls:

  1. .Internal(capabilities())
  2. .Internal(capabilitiesX11()) (if any(nas %in% c('X11', 'jpeg', 'png', 'tiff')))
randy3k commented 8 years ago

Perhaps, just separate the X11 and non-X11 checking.

jankatins commented 8 years ago

Yea, first all non-X11 in one go, then X11...

BTW: why don't we do the checks on startup instead of each time? the only problem I can think of is when user installs something during the session and things like check_capability('png') can't be changed from within the session, as all these are compiled into R, aren't they?

flying-sheep commented 8 years ago

We could do it on startup, but we'll have to use an environment I think. In .onLoad I think?

Simply assigning to a variable at top level happens at compile time, so the check would return whatever capabilities the building system had at build time.