ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:100777] [Ruby master Feature#17316] On memoization
@ 2020-11-11 10:22 sawadatsuyoshi
  2020-11-11 17:55 ` [ruby-core:100793] " marcandre-ruby-core
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: sawadatsuyoshi @ 2020-11-11 10:22 UTC (permalink / raw)
  To: ruby-core

Issue #17316 has been reported by sawa (Tsuyoshi Sawada).

----------------------------------------
Feature #17316: On memoization
https://bugs.ruby-lang.org/issues/17316

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
I have seem so many attempts to memoize a value in the form:

```ruby
@foo ||= some_heavy_calculation(...)
```

improperly, i.e., even when the value can potentially be falsy. This practice is wide spread, and since in most cases memoization is about efficiency and it would not be critical if it does not work correctly, people do not seem to care so much about correcting the wrong usage.

In such case, the correct form would be:

```ruby
unless instance_variable_defined?(:foo)
  @foo = some_heavy_calculation(...)
end
```

but this looks too long, and perhaps that is keeping people away from using it.

What about allowing `Kernel#instance_variable_set` to take a block instead of the second argument, in which case the assignment should be done only when the instance variable in not defined?

```ruby
instance_variable_set(:foo){some_heavy_calculation(...)}
```

Or, if that does not look right or seems to depart from the original usage of `instance_variable_set`, then what about having a new method?

```ruby
memoize(:foo){some_heavy_calculation(...)}
```



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:100793] [Ruby master Feature#17316] On memoization
  2020-11-11 10:22 [ruby-core:100777] [Ruby master Feature#17316] On memoization sawadatsuyoshi
@ 2020-11-11 17:55 ` marcandre-ruby-core
  2020-11-11 18:36 ` [ruby-core:100794] " sawadatsuyoshi
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: marcandre-ruby-core @ 2020-11-11 17:55 UTC (permalink / raw)
  To: ruby-core

Issue #17316 has been updated by marcandre (Marc-Andre Lafortune).


Memoization is tricky, not just for `nil`/`false` values. What about freezing that object? What about calling `Ractor.make_shareable` on it?

I just released a small gem to deal with memoization that:
- works with `nil`/`false` results.
- works for methods accepting arguments
- works for frozen objects
- is Ractor-ready in that the object can be made Ractor-shareable.

Gem is here: https://github.com/marcandre/ractor-cache
Comments welcome :-)

I think more strategies might be useful, for example accessing the cache via a Ractor/SharedHash, but haven't implemented that.

----------------------------------------
Feature #17316: On memoization
https://bugs.ruby-lang.org/issues/17316#change-88436

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
I have seen so many attempts to memoize a value in the form:

```ruby
@foo ||= some_heavy_calculation(...)
```

improperly, i.e., even when the value can potentially be falsy. This practice is wide spread, and since in most cases memoization is about efficiency and it would not be critical if it does not work correctly, people do not seem to care so much about correcting the wrong usage.

In such case, the correct form would be:

```ruby
unless instance_variable_defined?(:foo)
  @foo = some_heavy_calculation(...)
end
```

but this looks too long, and perhaps that is keeping people away from using it.

What about allowing `Kernel#instance_variable_set` to take a block instead of the second argument, in which case the assignment should be done only when the instance variable is not defined?

```ruby
instance_variable_set(:foo){some_heavy_calculation(...)}
```

Or, if that does not look right or seems to depart from the original usage of `instance_variable_set`, then what about having a new method?

```ruby
memoize(:foo){some_heavy_calculation(...)}
```



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:100794] [Ruby master Feature#17316] On memoization
  2020-11-11 10:22 [ruby-core:100777] [Ruby master Feature#17316] On memoization sawadatsuyoshi
  2020-11-11 17:55 ` [ruby-core:100793] " marcandre-ruby-core
@ 2020-11-11 18:36 ` sawadatsuyoshi
  2020-11-11 19:11 ` [ruby-core:100796] " marcandre-ruby-core
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: sawadatsuyoshi @ 2020-11-11 18:36 UTC (permalink / raw)
  To: ruby-core

Issue #17316 has been updated by sawa (Tsuyoshi Sawada).


marcandre (Marc-Andre Lafortune) wrote in #note-3:
> I just released a small gem to deal with memoization

Looks interesting.

----------------------------------------
Feature #17316: On memoization
https://bugs.ruby-lang.org/issues/17316#change-88437

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
I have seen so many attempts to memoize a value in the form:

```ruby
@foo ||= some_heavy_calculation(...)
```

improperly, i.e., even when the value can potentially be falsy. This practice is wide spread, and since in most cases memoization is about efficiency and it would not be critical if it does not work correctly, people do not seem to care so much about correcting the wrong usage.

In such case, the correct form would be:

```ruby
unless instance_variable_defined?(:foo)
  @foo = some_heavy_calculation(...)
end
```

but this looks too long, and perhaps that is keeping people away from using it.

What about allowing `Kernel#instance_variable_set` to take a block instead of the second argument, in which case the assignment should be done only when the instance variable is not defined?

```ruby
instance_variable_set(:foo){some_heavy_calculation(...)}
```

Or, if that does not look right or seems to depart from the original usage of `instance_variable_set`, then what about having a new method?

```ruby
memoize(:foo){some_heavy_calculation(...)}
```



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:100796] [Ruby master Feature#17316] On memoization
  2020-11-11 10:22 [ruby-core:100777] [Ruby master Feature#17316] On memoization sawadatsuyoshi
  2020-11-11 17:55 ` [ruby-core:100793] " marcandre-ruby-core
  2020-11-11 18:36 ` [ruby-core:100794] " sawadatsuyoshi
@ 2020-11-11 19:11 ` marcandre-ruby-core
  2020-11-12  5:35 ` [ruby-core:100813] " sawadatsuyoshi
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: marcandre-ruby-core @ 2020-11-11 19:11 UTC (permalink / raw)
  To: ruby-core

Issue #17316 has been updated by marcandre (Marc-Andre Lafortune).


> What about allowing Kernel#instance_variable_set to take a block instead of the second argument, in which case the assignment should be done only when the instance variable is not defined?

I would like `Kernel#instance_variable_get` (not `_set`) to accept a block like `Hash#fetch` for when the instance variable is not set.

----------------------------------------
Feature #17316: On memoization
https://bugs.ruby-lang.org/issues/17316#change-88439

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
I have seen so many attempts to memoize a value in the form:

```ruby
@foo ||= some_heavy_calculation(...)
```

improperly, i.e., even when the value can potentially be falsy. This practice is wide spread, and since in most cases memoization is about efficiency and it would not be critical if it does not work correctly, people do not seem to care so much about correcting the wrong usage.

In such case, the correct form would be:

```ruby
unless instance_variable_defined?(:foo)
  @foo = some_heavy_calculation(...)
end
```

but this looks too long, and perhaps that is keeping people away from using it.

What about allowing `Kernel#instance_variable_set` to take a block instead of the second argument, in which case the assignment should be done only when the instance variable is not defined?

```ruby
instance_variable_set(:foo){some_heavy_calculation(...)}
```

Or, if that does not look right or seems to depart from the original usage of `instance_variable_set`, then what about having a new method?

```ruby
memoize(:foo){some_heavy_calculation(...)}
```



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:100813] [Ruby master Feature#17316] On memoization
  2020-11-11 10:22 [ruby-core:100777] [Ruby master Feature#17316] On memoization sawadatsuyoshi
                   ` (2 preceding siblings ...)
  2020-11-11 19:11 ` [ruby-core:100796] " marcandre-ruby-core
@ 2020-11-12  5:35 ` sawadatsuyoshi
  2020-11-30 18:36 ` [ruby-core:101162] " daniel
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: sawadatsuyoshi @ 2020-11-12  5:35 UTC (permalink / raw)
  To: ruby-core

Issue #17316 has been updated by sawa (Tsuyoshi Sawada).


marcandre (Marc-Andre Lafortune) wrote in #note-5:
> > What about allowing Kernel#instance_variable_set to take a block instead of the second argument, in which case the assignment should be done only when the instance variable is not defined?
> 
> I would like `Kernel#instance_variable_get` (not `_set`) to accept a block like `Hash#fetch` for when the instance variable is not set.

That also makes sense. Either is fine with me.

----------------------------------------
Feature #17316: On memoization
https://bugs.ruby-lang.org/issues/17316#change-88454

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
I have seen so many attempts to memoize a value in the form:

```ruby
@foo ||= some_heavy_calculation(...)
```

improperly, i.e., even when the value can potentially be falsy. This practice is wide spread, and since in most cases memoization is about efficiency and it would not be critical if it does not work correctly, people do not seem to care so much about correcting the wrong usage.

In such case, the correct form would be:

```ruby
unless instance_variable_defined?(:foo)
  @foo = some_heavy_calculation(...)
end
```

but this looks too long, and perhaps that is keeping people away from using it.

What about allowing `Kernel#instance_variable_set` to take a block instead of the second argument, in which case the assignment should be done only when the instance variable is not defined?

```ruby
instance_variable_set(:foo){some_heavy_calculation(...)}
```

Or, if that does not look right or seems to depart from the original usage of `instance_variable_set`, then what about having a new method?

```ruby
memoize(:foo){some_heavy_calculation(...)}
```



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:101162] [Ruby master Feature#17316] On memoization
  2020-11-11 10:22 [ruby-core:100777] [Ruby master Feature#17316] On memoization sawadatsuyoshi
                   ` (3 preceding siblings ...)
  2020-11-12  5:35 ` [ruby-core:100813] " sawadatsuyoshi
@ 2020-11-30 18:36 ` daniel
  2020-12-30  7:42 ` [ruby-core:101811] " sebastian.buza1
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: daniel @ 2020-11-30 18:36 UTC (permalink / raw)
  To: ruby-core

Issue #17316 has been updated by Dan0042 (Daniel DeLorme).


marcandre (Marc-Andre Lafortune) wrote in #note-3:
> Gem is here: https://github.com/marcandre/ractor-cache
> Comments welcome :-)

Since you say so... :-)
An additional strategy might to wrap the @cache in a Ractor::LVar (if/once available). I tend to use memoization to cache DB access rather than long calculations, and for a given class I would probably not use all (or even a majority) of memoized methods at once. So pre-computing values before deep-freezing is not a good option for me.

But I find it interesting that this memoization stuff keeps getting reimplemented.
https://rubygems.org/search?utf8=%E2%9C%93&query=memoization
Not to mention all the people (including me) who have implemented this in their private code.
And everyone tends to have a slightly different implementation based on the features they need.

For example my own implementation is compatible with shallow-freezing and falsy values, but not with methods that take arguments; instead I wanted cache-busting based on dependent values. And multiple-assignment aliases.

```ruby
memo ->{id}, #memo-busting lambda
:foo, :bar,  #aliases for foobar[0] and foobar[1]
def foobar
  obj = get_foobar_from_db(id)
  [obj.foo, obj.bar]
end
```

All this to say that since the specifics can vary, it's probably better to leave that level of memoization to gems and individual developers. I can somewhat agree with something simple like `instance_variable_get(:@v){ @v = calc() }` ... but then again we can already do this just as easily now with `return @v if defined? @v; @v = calc()`


----------------------------------------
Feature #17316: On memoization
https://bugs.ruby-lang.org/issues/17316#change-88848

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
I have seen so many attempts to memoize a value in the form:

```ruby
@foo ||= some_heavy_calculation(...)
```

improperly, i.e., even when the value can potentially be falsy. This practice is wide spread, and since in most cases memoization is about efficiency and it would not be critical if it does not work correctly, people do not seem to care so much about correcting the wrong usage.

In such case, the correct form would be:

```ruby
unless instance_variable_defined?(:@foo)
  @foo = some_heavy_calculation(...)
end
```

but this looks too long, and perhaps that is keeping people away from using it.

What about allowing `Kernel#instance_variable_set` to take a block instead of the second argument, in which case the assignment should be done only when the instance variable is not defined?

```ruby
instance_variable_set(:@foo){some_heavy_calculation(...)}
```

Or, if that does not look right or seems to depart from the original usage of `instance_variable_set`, then what about having a new method?

```ruby
memoize(:foo){some_heavy_calculation(...)}
```



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:101811] [Ruby master Feature#17316] On memoization
  2020-11-11 10:22 [ruby-core:100777] [Ruby master Feature#17316] On memoization sawadatsuyoshi
                   ` (4 preceding siblings ...)
  2020-11-30 18:36 ` [ruby-core:101162] " daniel
@ 2020-12-30  7:42 ` sebastian.buza1
  2021-01-05 16:46 ` [ruby-core:101935] " marcandre-ruby-core
  2022-05-13 11:32 ` [ruby-core:108541] " joel@drapper.me (Joel Drapper)
  7 siblings, 0 replies; 9+ messages in thread
From: sebastian.buza1 @ 2020-12-30  7:42 UTC (permalink / raw)
  To: ruby-core

Issue #17316 has been updated by sebyx07 (Sebastian Buza).


IMO there should be an operator in the language directly to keep it more dry.



```ruby
def my_method # current implementation
  return @cache if defined? @cache
  @cache = some_heavy_calculation
end

def my_new_method
  @cache ?= some_heavy_calculation
end
```

----------------------------------------
Feature #17316: On memoization
https://bugs.ruby-lang.org/issues/17316#change-89638

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
I have seen so many attempts to memoize a value in the form:

```ruby
@foo ||= some_heavy_calculation(...)
```

improperly, i.e., even when the value can potentially be falsy. This practice is wide spread, and since in most cases memoization is about efficiency and it would not be critical if it does not work correctly, people do not seem to care so much about correcting the wrong usage.

In such case, the correct form would be:

```ruby
unless instance_variable_defined?(:@foo)
  @foo = some_heavy_calculation(...)
end
```

but this looks too long, and perhaps that is keeping people away from using it.

What about allowing `Kernel#instance_variable_set` to take a block instead of the second argument, in which case the assignment should be done only when the instance variable is not defined?

```ruby
instance_variable_set(:@foo){some_heavy_calculation(...)}
```

Or, if that does not look right or seems to depart from the original usage of `instance_variable_set`, then what about having a new method?

```ruby
memoize(:foo){some_heavy_calculation(...)}
```



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:101935] [Ruby master Feature#17316] On memoization
  2020-11-11 10:22 [ruby-core:100777] [Ruby master Feature#17316] On memoization sawadatsuyoshi
                   ` (5 preceding siblings ...)
  2020-12-30  7:42 ` [ruby-core:101811] " sebastian.buza1
@ 2021-01-05 16:46 ` marcandre-ruby-core
  2022-05-13 11:32 ` [ruby-core:108541] " joel@drapper.me (Joel Drapper)
  7 siblings, 0 replies; 9+ messages in thread
From: marcandre-ruby-core @ 2021-01-05 16:46 UTC (permalink / raw)
  To: ruby-core

Issue #17316 has been updated by marcandre (Marc-Andre Lafortune).


Dan0042 (Daniel DeLorme) wrote in #note-8:
> marcandre (Marc-Andre Lafortune) wrote in #note-3:
> > Gem is here: https://github.com/marcandre/ractor-cache
> > Comments welcome :-)
> 
> Since you say so... :-)
> An additional strategy might to wrap the @cache in a Ractor::LVar (if/once available).

Indeed. I refactored it to use `Ractor.current[]` and a `WeakMap`. I removed the other ways as I can't think of a case where this isn't the best way to go.


----------------------------------------
Feature #17316: On memoization
https://bugs.ruby-lang.org/issues/17316#change-89783

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
I have seen so many attempts to memoize a value in the form:

```ruby
@foo ||= some_heavy_calculation(...)
```

improperly, i.e., even when the value can potentially be falsy. This practice is wide spread, and since in most cases memoization is about efficiency and it would not be critical if it does not work correctly, people do not seem to care so much about correcting the wrong usage.

In such case, the correct form would be:

```ruby
unless instance_variable_defined?(:@foo)
  @foo = some_heavy_calculation(...)
end
```

but this looks too long, and perhaps that is keeping people away from using it.

What about allowing `Kernel#instance_variable_set` to take a block instead of the second argument, in which case the assignment should be done only when the instance variable is not defined?

```ruby
instance_variable_set(:@foo){some_heavy_calculation(...)}
```

Or, if that does not look right or seems to depart from the original usage of `instance_variable_set`, then what about having a new method?

```ruby
memoize(:foo){some_heavy_calculation(...)}
```



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:108541] [Ruby master Feature#17316] On memoization
  2020-11-11 10:22 [ruby-core:100777] [Ruby master Feature#17316] On memoization sawadatsuyoshi
                   ` (6 preceding siblings ...)
  2021-01-05 16:46 ` [ruby-core:101935] " marcandre-ruby-core
@ 2022-05-13 11:32 ` joel@drapper.me (Joel Drapper)
  7 siblings, 0 replies; 9+ messages in thread
From: joel@drapper.me (Joel Drapper) @ 2022-05-13 11:32 UTC (permalink / raw)
  To: ruby-core

Issue #17316 has been updated by joel@drapper.me (Joel Drapper).


I've been experimenting with memoization by passing a block to `attr_reader` / `attr_accessor`, e.g.

```ruby
attr_reader(:foo) { something_slow }
```

or

```ruby
attr_reader :foo do
  something_slow
end
```

I prototyped this in Ruby to get a feel for what it's like to use. https://gist.github.com/joeldrapper/7e35f2f5f906344195c121801ddd28d4

----------------------------------------
Feature #17316: On memoization
https://bugs.ruby-lang.org/issues/17316#change-97585

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
I have seen so many attempts to memoize a value in the form:

```ruby
@foo ||= some_heavy_calculation(...)
```

improperly, i.e., even when the value can potentially be falsy. This practice is wide spread, and since in most cases memoization is about efficiency and it would not be critical if it does not work correctly, people do not seem to care so much about correcting the wrong usage.

In such case, the correct form would be:

```ruby
unless instance_variable_defined?(:@foo)
  @foo = some_heavy_calculation(...)
end
```

but this looks too long, and perhaps that is keeping people away from using it.

What about allowing `Kernel#instance_variable_set` to take a block instead of the second argument, in which case the assignment should be done only when the instance variable is not defined?

```ruby
instance_variable_set(:@foo){some_heavy_calculation(...)}
```

Or, if that does not look right or seems to depart from the original usage of `instance_variable_set`, then what about having a new method?

```ruby
memoize(:foo){some_heavy_calculation(...)}
```



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-05-13 11:32 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-11 10:22 [ruby-core:100777] [Ruby master Feature#17316] On memoization sawadatsuyoshi
2020-11-11 17:55 ` [ruby-core:100793] " marcandre-ruby-core
2020-11-11 18:36 ` [ruby-core:100794] " sawadatsuyoshi
2020-11-11 19:11 ` [ruby-core:100796] " marcandre-ruby-core
2020-11-12  5:35 ` [ruby-core:100813] " sawadatsuyoshi
2020-11-30 18:36 ` [ruby-core:101162] " daniel
2020-12-30  7:42 ` [ruby-core:101811] " sebastian.buza1
2021-01-05 16:46 ` [ruby-core:101935] " marcandre-ruby-core
2022-05-13 11:32 ` [ruby-core:108541] " joel@drapper.me (Joel Drapper)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).