Skip to content

Slow try in combination with do.call

3 messages · Alexander Kaever, Martin Maechler

#
Hi,

It seems like a try(do.call(f, args)) can be very slow on error depending on the args size. This is related to a complete deparse of the call using deparse(call)[1L] within the try function. How about replacing deparse(call)[1L] by deparse(call, nlines = 1)?

Best,
Alex


Example:

fun <- function(x) {
  stop("testing")
}
d <- rep(list(mtcars), 10000)
object.size(d)
# 72MB

system.time({
  try(do.call(fun, args = list(x = d)))
})
# 8s


Unsere Informationen zum Datenschutz finden Sie hier<https://www.evotec.com/de/about/site-information/datenschutzbestimmungen>.

Evotec International GmbH, Hamburg. Amtsgericht Hamburg HRB 72242
Gesch?ftsf?hrung: Dr. Cord Dohrmann, Dr. Craig Johnstone, Enno Spillner

STATEMENT OF CONFIDENTIALITY.

This email and any attachments may contain confidential, proprietary, privileged and/or private information.  
If received in error, please notify us immediately by reply email and then delete this email and any attachments from your system. Thank you.
#
> Hi,
    > It seems like a try(do.call(f, args)) can be very slow on error depending on the args size. This is related to a complete deparse of the call using deparse(call)[1L] within the try function. How about replacing deparse(call)[1L] by deparse(call, nlines = 1)?

    > Best,
    > Alex

an *excellent* idea!

I have checked that the resulting try() object continues to contain the
long large call; indeed that is not the problem, but the
deparse()ing  *is* as you say above.

{The experts typically use  tryCatch() directly, instead of  try() ,
 which may be the reason other experienced R developers have not
 stumbled over this ...}

Thanks a lot, notably also for the clear  repr.ex. below.

Best regards,
Martin


    > Example:

    > fun <- function(x) {
    > stop("testing")
    > }
    > d <- rep(list(mtcars), 10000)
    > object.size(d)
    > # 72MB

    > system.time({
    > try(do.call(fun, args = list(x = d)))
    > })
    > # 8s


    > Unsere Informationen zum Datenschutz finden Sie hier<https://www.evotec.com/de/about/site-information/datenschutzbestimmungen>.

    > Evotec International GmbH, Hamburg. Amtsgericht Hamburg HRB 72242
    > Gesch?ftsf?hrung: Dr. Cord Dohrmann, Dr. Craig Johnstone, Enno Spillner

    > STATEMENT OF CONFIDENTIALITY.

    > This email and any attachments may contain confidential, proprietary, privileged and/or private information.  
    > If received in error, please notify us immediately by reply email and then delete this email and any attachments from your system. Thank you.
    > ______________________________________________
    > R-devel at r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel
#
>> Hi,
    >> It seems like a try(do.call(f, args)) can be very slow on error depending on the args size. This is related to a complete deparse of the call using deparse(call)[1L] within the try function. How about replacing deparse(call)[1L] by deparse(call, nlines = 1)?

    >> Best,
    >> Alex

    > an *excellent* idea!

    > I have checked that the resulting try() object continues to contain the
    > long large call; indeed that is not the problem, but the
    > deparse()ing  *is* as you say above.

    > {The experts typically use  tryCatch() directly, instead of  try() ,
    > which may be the reason other experienced R developers have not
    > stumbled over this ...}

    > Thanks a lot, notably also for the clear  repr.ex. below.

    > Best regards,
    > Martin

OTOH, I find so many cases  of   deparse(*)[1]  (or similar) in
R's own sources, I'm wondering
if I'm forgetting something ... and using nlines=* is not always
faster & equivalent and hence better ??

Martin




    >> Example:

    >> fun <- function(x) {
    >> stop("testing")
    >> }
    >> d <- rep(list(mtcars), 10000)
    >> object.size(d)
    >> # 72MB

    >> system.time({
    >> try(do.call(fun, args = list(x = d)))
    >> })
    >> # 8s