ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:94101] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap
       [not found] <redmine.issue-16035.20190801190200@ruby-lang.org>
@ 2019-08-01 19:02 ` jean.boussier
  2019-08-29  6:31 ` [ruby-core:94648] " ko1
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 4+ messages in thread
From: jean.boussier @ 2019-08-01 19:02 UTC (permalink / raw
  To: ruby-core

Issue #16035 has been reported by byroot (Jean Boussier).

----------------------------------------
Feature #16035: Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap
https://bugs.ruby-lang.org/issues/16035

* Author: byroot (Jean Boussier)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
This goes one step farther than what @nobu did in https://bugs.ruby-lang.org/issues/13498

With this patch, special objects such as static symbols, integers, etc can be used as either key or values inside WeakMap. They simply don't have a finalizer defined on them.

This is useful if you need to deduplicate value objects, e.g. some minimal use case:

```ruby
class Money
  REGISTRY = ObjectSpace::WeakMap.new
  private_constant :REGISTRY

  def self.new(amount)
    REGISTRY[amount] ||= super.freeze
  end

  def initialize(amount)
    @amount = amount
  end
end

if Money.new(42).eql?(Money.new(42))
  puts "Same instance"
else
  puts "Different instances"
end
```

This is a very simple example, but more complex examples can create use a dynamically created symbol as deduplication key, etc.

It also removes one weirdness introduced in the mentioned patch:

```ruby
wmap = ObjectSpace::WeakMap.new
wmap["foo".to_sym] = Object.new # works fine with dynamic symbols
wmap[:bar] = Object.new # cannot define finalizer for Symbol (ArgumentError)
```

Proposed patch: https://github.com/ruby/ruby/pull/2313



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

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

* [ruby-core:94648] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap
       [not found] <redmine.issue-16035.20190801190200@ruby-lang.org>
  2019-08-01 19:02 ` [ruby-core:94101] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap jean.boussier
@ 2019-08-29  6:31 ` ko1
  2019-08-29  6:41 ` [ruby-core:94649] " ko1
  2019-08-29  6:44 ` [ruby-core:94650] " matz
  3 siblings, 0 replies; 4+ messages in thread
From: ko1 @ 2019-08-29  6:31 UTC (permalink / raw
  To: ruby-core

Issue #16035 has been updated by ko1 (Koichi Sasada).


`42` never be collected. Is it intentional?


----------------------------------------
Feature #16035: Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap
https://bugs.ruby-lang.org/issues/16035#change-81246

* Author: byroot (Jean Boussier)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
This goes one step farther than what @nobu did in https://bugs.ruby-lang.org/issues/13498

With this patch, special objects such as static symbols, integers, etc can be used as either key or values inside WeakMap. They simply don't have a finalizer defined on them.

This is useful if you need to deduplicate value objects, e.g. some minimal use case:

```ruby
class Money
  REGISTRY = ObjectSpace::WeakMap.new
  private_constant :REGISTRY

  def self.new(amount)
    REGISTRY[amount] ||= super.freeze
  end

  def initialize(amount)
    @amount = amount
  end
end

if Money.new(42).eql?(Money.new(42))
  puts "Same instance"
else
  puts "Different instances"
end
```

This is a very simple example, but more complex examples can create use a dynamically created symbol as deduplication key, etc.

It also removes one weirdness introduced in the mentioned patch:

```ruby
wmap = ObjectSpace::WeakMap.new
wmap["foo".to_sym] = Object.new # works fine with dynamic symbols
wmap[:bar] = Object.new # cannot define finalizer for Symbol (ArgumentError)
```

Proposed patch: https://github.com/ruby/ruby/pull/2313



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

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

* [ruby-core:94649] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap
       [not found] <redmine.issue-16035.20190801190200@ruby-lang.org>
  2019-08-01 19:02 ` [ruby-core:94101] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap jean.boussier
  2019-08-29  6:31 ` [ruby-core:94648] " ko1
@ 2019-08-29  6:41 ` ko1
  2019-08-29  6:44 ` [ruby-core:94650] " matz
  3 siblings, 0 replies; 4+ messages in thread
From: ko1 @ 2019-08-29  6:41 UTC (permalink / raw
  To: ruby-core

Issue #16035 has been updated by ko1 (Koichi Sasada).

Assignee set to nobu (Nobuyoshi Nakada)
Status changed from Feedback to Assigned

sorry, value is collectable (my misunderstand).

----------------------------------------
Feature #16035: Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap
https://bugs.ruby-lang.org/issues/16035#change-81248

* Author: byroot (Jean Boussier)
* Status: Assigned
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
This goes one step farther than what @nobu did in https://bugs.ruby-lang.org/issues/13498

With this patch, special objects such as static symbols, integers, etc can be used as either key or values inside WeakMap. They simply don't have a finalizer defined on them.

This is useful if you need to deduplicate value objects, e.g. some minimal use case:

```ruby
class Money
  REGISTRY = ObjectSpace::WeakMap.new
  private_constant :REGISTRY

  def self.new(amount)
    REGISTRY[amount] ||= super.freeze
  end

  def initialize(amount)
    @amount = amount
  end
end

if Money.new(42).eql?(Money.new(42))
  puts "Same instance"
else
  puts "Different instances"
end
```

This is a very simple example, but more complex examples can create use a dynamically created symbol as deduplication key, etc.

It also removes one weirdness introduced in the mentioned patch:

```ruby
wmap = ObjectSpace::WeakMap.new
wmap["foo".to_sym] = Object.new # works fine with dynamic symbols
wmap[:bar] = Object.new # cannot define finalizer for Symbol (ArgumentError)
```

Proposed patch: https://github.com/ruby/ruby/pull/2313



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

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

* [ruby-core:94650] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap
       [not found] <redmine.issue-16035.20190801190200@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-08-29  6:41 ` [ruby-core:94649] " ko1
@ 2019-08-29  6:44 ` matz
  3 siblings, 0 replies; 4+ messages in thread
From: matz @ 2019-08-29  6:44 UTC (permalink / raw
  To: ruby-core

Issue #16035 has been updated by matz (Yukihiro Matsumoto).


I think this is the required behavior for `WeakMap` to implement a cache for example. Accepted.

Matz.


----------------------------------------
Feature #16035: Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap
https://bugs.ruby-lang.org/issues/16035#change-81249

* Author: byroot (Jean Boussier)
* Status: Assigned
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
This goes one step farther than what @nobu did in https://bugs.ruby-lang.org/issues/13498

With this patch, special objects such as static symbols, integers, etc can be used as either key or values inside WeakMap. They simply don't have a finalizer defined on them.

This is useful if you need to deduplicate value objects, e.g. some minimal use case:

```ruby
class Money
  REGISTRY = ObjectSpace::WeakMap.new
  private_constant :REGISTRY

  def self.new(amount)
    REGISTRY[amount] ||= super.freeze
  end

  def initialize(amount)
    @amount = amount
  end
end

if Money.new(42).eql?(Money.new(42))
  puts "Same instance"
else
  puts "Different instances"
end
```

This is a very simple example, but more complex examples can create use a dynamically created symbol as deduplication key, etc.

It also removes one weirdness introduced in the mentioned patch:

```ruby
wmap = ObjectSpace::WeakMap.new
wmap["foo".to_sym] = Object.new # works fine with dynamic symbols
wmap[:bar] = Object.new # cannot define finalizer for Symbol (ArgumentError)
```

Proposed patch: https://github.com/ruby/ruby/pull/2313



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

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

end of thread, other threads:[~2019-08-29  6:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <redmine.issue-16035.20190801190200@ruby-lang.org>
2019-08-01 19:02 ` [ruby-core:94101] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap jean.boussier
2019-08-29  6:31 ` [ruby-core:94648] " ko1
2019-08-29  6:41 ` [ruby-core:94649] " ko1
2019-08-29  6:44 ` [ruby-core:94650] " matz

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).