Project

General

Profile

Feature #16035

Allow non-finalizable objects such as Integer, static Symbol etc in ObjectSpace::WeakMap

Added by byroot (Jean Boussier) about 2 months ago. Updated 19 days ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:94101]

Description

This goes one step farther than what nobu (Nobuyoshi Nakada) 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:

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:

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

Associated revisions

Revision a4a19b11
Added by byroot (Jean Boussier) 19 days ago

Allow non-finalizable objects in ObjectSpace::WeakMap

[feature #16035]

This goes one step farther than what nobu did in [feature #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

Revision 79117d4a
Added by nobu (Nobuyoshi Nakada) 19 days ago

NEWS: [Feature #16035] [ci skip]

History

Updated by ko1 (Koichi Sasada) 20 days ago

42 never be collected. Is it intentional?

#2

Updated by ko1 (Koichi Sasada) 20 days ago

  • Status changed from Open to Feedback

Updated by ko1 (Koichi Sasada) 20 days ago

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

sorry, value is collectable (my misunderstand).

Updated by matz (Yukihiro Matsumoto) 20 days ago

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

Matz.

#5

Updated by byroot (Jean Boussier) 19 days ago

  • Status changed from Assigned to Closed

Applied in changeset git|a4a19b114ba94b8f28d5a91aee5d595a516006d5.


Allow non-finalizable objects in ObjectSpace::WeakMap

[feature #16035]

This goes one step farther than what nobu did in [feature #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

Also available in: Atom PDF