Skip to content

Nested functions are not hashed #155

@DavZim

Description

@DavZim

Not sure if this is on purpose or too complicate to implement, but I wanted to document it here (as I couldn't find existing documentation/issues on it).

When we memoise a function that calls another function, the code of "other function" is not taken into account when creating the hash.

Eg.

cache <- cachem::cache_mem()


#####################################
# Base case ==============
foo <- function(x) {
  bar(x)
}
bar <- function(x) {
  x + 1
}
foo_mem <- memoise::memoise(foo, cache = cache)

memoise::has_cache(foo_mem)(1)
#> [1] FALSE
# expected!

foo_mem(1)
memoise::has_cache(foo_mem)(1)
#> [1] TRUE
# expected!


#####################################
# Case 1: change foo ===========
foo <- function(x) {
  print("Running foo") # this was added
  bar(x)
}
bar <- function(x) {
  x + 1
}
foo_mem <- memoise::memoise(foo, cache = cache)

# foo_mem(1) is not cached because foo has changed: this is expected!
memoise::has_cache(foo_mem)(1)
#> [1] FALSE
# expected!

foo_mem(1)
memoise::has_cache(foo_mem)(1)
#> [1] TRUE
# expected!


#####################################
# Case 2: dependent function changes - change bar ========
foo <- function(x) {
  print("Running foo")
  bar(x)
}
bar <- function(x) {
  print("Running bar") # this was added
  x + 1
}
foo_mem <- memoise::memoise(foo, cache = cache)
memoise::has_cache(foo_mem)(1)
#> [1] TRUE
# NOT EXPECTED!

Ideally, I would expect the last foo_mem() to not have a cache, as bar() (inside foo()) has changed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions