HenrikBengtsson / profmem

🔧 R package: profmem - Simple Memory Profiling for R
https://cran.r-project.org/package=profmem
35 stars 2 forks source link

Support for nested `profmem()` calls #6

Closed HenrikBengtsson closed 6 years ago

HenrikBengtsson commented 6 years ago

Wish

To be able to do:

p1 <- profmem({
  x <- 1:1000
  p2 <- profmem({
     y <- as.double(x)
  })
  z <- x * y
})

Where p1 should hold all memory allocations including those done during p2.

Issue

Rprofmem() itself can only handle a single file; from ?Rprofmem:

Enabling profiling automatically disables any existing profiling to another or the same file.

which is also confirmed when inspecting the internal code.

In develop, profmem_begin/end() now protects against the stack from being greater than one level, in order to avoid overwriting existing profiling logs by mistake.

Solution

It should not be impossible to have the profmem package to orchestrate a stack of files. One idea is to have profmem_begin() parse existing profile buffer and stack it before starting over producing a fresh one. Calls to profmem_end() will consume the current buffer and append it any previous one existing at the same level. If not at the top level, then it'll resume profiling to a fresh profile file.