Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds debugging functionality #144

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Adds debugging functionality #144

wants to merge 3 commits into from

Conversation

DavZim
Copy link

@DavZim DavZim commented Dec 7, 2022

This PR adds a debug argument to memoise, if set to TRUE, each function call will print out information about the function, arguments, key, time, as well as file size (if cachem::cache_disk is used).

I am not sure if this is something you want to have in the package, but the code was too helpful for me to drop it.
For me this helped me to debug a complex shiny application and find bottlenecks where the caching/memoisation were actually harming the overall performance.

The PR adds no dependencies and if left at its default (FALSE), it will not interfere with the existing functionality. If set to TRUE, it will print out the debug information, see for example

sleeper <- function(x) {
  Sys.sleep(x)
  x
}

memF <- memoise::memoise(sleeper, debug = TRUE)

memF(1)
#> Calling Function memF(x = 1)
#>  hash time: 0.0000s
#>  key: 4147be8bbfbabec5e01b4df9d18936be
#>  = no cache found
#>      eval time: 1.0186s
#>  ==> total time: 1.0186s
#> [1] 1
memF(1)
#> Calling Function memF(x = 1)
#>  hash time: 0.0010s
#>  key: 4147be8bbfbabec5e01b4df9d18936be
#>  = cache found
#>      cache read time: 0.0000s
#>  ==> total time: 0.0000s
#> [1] 1


memFF <- memoise::memoise(sleeper, cache = cachem::cache_disk(), debug = TRUE)
memFF(1)
#> Calling Function memFF(x = 1)
#>  hash time: 0.0000s
#>  key: 4147be8bbfbabec5e01b4df9d18936be
#>  = no cache found
#>      eval time: 1.0135s
#>      cache save time: 0.0120s
#>  new cache size: 102 bytes
#>  ==> total time: 1.0275s
#> [1] 1
memFF(1)
#> Calling Function memFF(x = 1)
#>  hash time: 0.0000s
#>  key: 4147be8bbfbabec5e01b4df9d18936be
#>  = cache found
#>      cache read time: 0.0040s
#>  ==> total time: 0.0040s
#> [1] 1

Created on 2022-12-07 by the reprex package (v2.0.1)

Feel free to change the code if you feel it is missing something or is doing too much!

@DavZim DavZim mentioned this pull request Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant