How to profile the loading of an R package

回眸只為那壹抹淺笑 提交于 2019-12-01 15:25:17

问题


I have written this R package that takes ages (> 10s, sometimes up to 20-30s!) to load.

Every time the package loads, such as when building the package at the step "** testing if installed package can be loaded", or directly calling library("my.package"), nothing happens for 20s.

This makes everything painfully slow during development: building documentation, building the package, running R check...

Of course I have my suspicions (looking at you, dodgy dependency), but I need to gather evidence before axing it.

Is there a way to profile the loading of the package, in order to identify the cause? Or more generally, how can I figure out what is happening under the hood?


回答1:


So an issue with using the detach method from @davide-lorino is that if there are entangled depends or imports, it will fail, and fail hard.

A better method is to use a future backend that loads each of the imports in a clean R session, and time how long it takes to load them via library.

I implemented this in a package that might be useful to others: https://github.com/rmflight/importedPackageTimings




回答2:


You can determine which library takes the longest to load by benchmarking a call to load each of the libraries that you are testing.

The key is to make sure that you unload the libraries. If you keep the libraries loaded before re-loading them, the library() function will determine that the library has loaded and return out. On a typical benchmark of 100 runs, 1 of them will represent the time it took to load your library, and the remaining 99 will represent the time it took library() to figure out that the library is loaded. The result (duration) will then be an aggregate of the 100 runs, yielding a very small number and almost no variance between the results, like so:

When what you really want looks more like:

Giving us a less surprising result for our efforts.

P.s. the detach_package() function is implemented like this:

detach_package <- function(pkg, character.only = FALSE)
{
  if(!character.only)
  {
    pkg <- deparse(substitute(pkg))
  }
  search_item <- paste("package", pkg, sep = ":")
  while(search_item %in% search())
  {
    detach(search_item, unload = TRUE, character.only = TRUE)
  }
}


来源:https://stackoverflow.com/questions/57054120/how-to-profile-the-loading-of-an-r-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!