Skip to content

Implement memoization #6887

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

Closed
wants to merge 12 commits into from
Closed

Implement memoization #6887

wants to merge 12 commits into from

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Jul 18, 2019

Implement memo(...) function which caches its argument on first evaluation
and re-uses the cached value afterwards. The cache is placed next to the
method enclosing the memo(...) call.

memo is a member of package compiletime.

in the parentheses is computed only the first time and is cached for subsequent recalculuations.
The memoized program expands to the following code:
```
class C(x: T) {
Copy link
Contributor

@soronpo soronpo Jul 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing [T] (and in all other examples)

@soronpo
Copy link
Contributor

soronpo commented Jul 21, 2019

(I need this now! 😄)
Since memo hides the caching mechanism, people may use it wrong (in a mutating/impure function) and get unexpected results:

class C[T](x: T) {
  var someState : Boolean = false //somewhere this is modified
  def costly(x : T) : Int = {
    if (someState) ???
    else ???
  }  
  def f(y: Int) = memo(costly(x)) * y
}

How do we prevent this? Is there a way for the compiler to verify the function is pure?
If not, I believe this should be documented as well.

@odersky
Copy link
Contributor Author

odersky commented Jul 26, 2019

@soronpo There is not currently a mechanism to indicate that a function is pure. There might be in the future, but it will take a while to get there.

The problem you outline is exactly the same for a lazy val, so not specific to memo.

odersky added 12 commits July 26, 2019 10:35
Implement `memo(...)` function which caches its argument on first evaluation
and re-uses the cached value afterwards. The cache is placed next to the
method enclosing the memo(...) call.

`memo` is a member of package `compiletime`.
Maybe we should always set the positions of these generated definitions automatically from the symbol's span. It's an easy trap to fall into.
Stale symbol errors can happen when inspecting symbols while comparing
the trees before and after pickling.
... and also make it work on several nested levels together.
Restricting `entered` and `enteredAfter` to class members is more a trap to
fall into than a helpful check.
@odersky
Copy link
Contributor Author

odersky commented Jul 30, 2019

Superseded by #6967

@odersky odersky closed this Jul 30, 2019
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.

2 participants