ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:106135] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap
       [not found] <redmine.issue-16035.20190801190200.7941@ruby-lang.org>
@ 2021-11-18 12:18 ` headius (Charles Nutter)
  2021-11-18 12:21 ` [ruby-core:106136] " byroot (Jean Boussier)
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 4+ messages in thread
From: headius (Charles Nutter) @ 2021-11-18 12:18 UTC (permalink / raw
  To: ruby-core

Issue #16035 has been updated by headius (Charles Nutter).


I am behind the times here, but it is worth noting that implementations which cannot guarantee idempotency of fixnum and flonum-ranged Integers and Floats will have trouble implementing the spirit of this change. On JRuby, it is not possible to treat two fixnums created separately as the same object, since the WeakMap implementation needs to compare by object identity and JRuby represents all fixnums and flonums as full objectswith their own identities.

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

* Author: byroot (Jean Boussier)
* Status: Closed
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
----------------------------------------
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:106136] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap
       [not found] <redmine.issue-16035.20190801190200.7941@ruby-lang.org>
  2021-11-18 12:18 ` [ruby-core:106135] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap headius (Charles Nutter)
@ 2021-11-18 12:21 ` byroot (Jean Boussier)
  2021-11-18 14:04 ` [ruby-core:106139] " headius (Charles Nutter)
  2021-11-19 20:35 ` [ruby-core:106183] " Eregon (Benoit Daloze)
  3 siblings, 0 replies; 4+ messages in thread
From: byroot (Jean Boussier) @ 2021-11-18 12:21 UTC (permalink / raw
  To: ruby-core

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


It's fine, even on MRI you'll get that with BigNum:

```ruby
>> (2**68).equal?(2**68)
=> false
```

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

* Author: byroot (Jean Boussier)
* Status: Closed
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
----------------------------------------
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:106139] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap
       [not found] <redmine.issue-16035.20190801190200.7941@ruby-lang.org>
  2021-11-18 12:18 ` [ruby-core:106135] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap headius (Charles Nutter)
  2021-11-18 12:21 ` [ruby-core:106136] " byroot (Jean Boussier)
@ 2021-11-18 14:04 ` headius (Charles Nutter)
  2021-11-19 20:35 ` [ruby-core:106183] " Eregon (Benoit Daloze)
  3 siblings, 0 replies; 4+ messages in thread
From: headius (Charles Nutter) @ 2021-11-18 14:04 UTC (permalink / raw
  To: ruby-core

Issue #16035 has been updated by headius (Charles Nutter).


@byroot True, and this has indeed always been a known behavior difference on JRuby that users just deal with (don't use bare integers as keys, mostly). I really just want to point out that this difference extends to key identity in WeakMap and other identity maps.

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

* Author: byroot (Jean Boussier)
* Status: Closed
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
----------------------------------------
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:106183] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap
       [not found] <redmine.issue-16035.20190801190200.7941@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2021-11-18 14:04 ` [ruby-core:106139] " headius (Charles Nutter)
@ 2021-11-19 20:35 ` Eregon (Benoit Daloze)
  3 siblings, 0 replies; 4+ messages in thread
From: Eregon (Benoit Daloze) @ 2021-11-19 20:35 UTC (permalink / raw
  To: ruby-core

Issue #16035 has been updated by Eregon (Benoit Daloze).


We implemented this correctly in TruffleRuby.
It's a little bit tricky as then indeed some wrapper with custom equals() is needed for primitives:
https://github.com/oracle/truffleruby/commit/e839f3f5887bdd04b855e286e9f74443fbcccf53
That means the exact same semantics as `equal?` on CRuby.

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

* Author: byroot (Jean Boussier)
* Status: Closed
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
----------------------------------------
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:[~2021-11-19 20:36 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.7941@ruby-lang.org>
2021-11-18 12:18 ` [ruby-core:106135] [Ruby master Feature#16035] Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap headius (Charles Nutter)
2021-11-18 12:21 ` [ruby-core:106136] " byroot (Jean Boussier)
2021-11-18 14:04 ` [ruby-core:106139] " headius (Charles Nutter)
2021-11-19 20:35 ` [ruby-core:106183] " Eregon (Benoit Daloze)

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