Skip to content

Conversation

@trinistr
Copy link
Contributor

@trinistr trinistr commented Nov 21, 2025

From #1265

GC.config added to allow setting configuration variables on the Garbage
Collector. [Feature #20443]

Includes generic tests and tests for MRI's default implementation of GC.

If a Ruby implementation does not want to expose implementation or configuration, a minimal method can be this:

def GC.config(options = nil)
  return { implementation: "none" } if options.nil?

  raise ArgumentError unless Hash === options
  if options.key?(:implementation)
    raise ArgumentError, 'Attempting to set read-only key "Implementation"'
  end

  {}
end

This is the reference: https://github.com/ruby/ruby/blob/2289961b485b1cbf7b1012693722c16a6cdb4cda/gc.rb#L445

@trinistr trinistr force-pushed the 3.4-gc-config branch 2 times, most recently from 6be2052 to 0bd6e6e Compare November 21, 2025 21:47
Includes generic tests and tests for MRI's default implementation of GC.

If a Ruby implementation does not want to expose implementation or configuration, a minimal method can be this:
```ruby
def GC.config(options = nil)
  return { implementation: "none" } if options.nil?

  raise ArgumentError unless Hash === options
  if options.key?(:implementation)
    raise ArgumentError, 'Attempting to set read-only key "Implementation"'
  end

  {}
end
```
@trinistr trinistr marked this pull request as ready for review November 21, 2025 21:55
Comment on lines 54 to 56
-> { GC.config([]) }.should raise_error(ArgumentError, "ArgumentError")
-> { GC.config("default") }.should raise_error(ArgumentError, "ArgumentError")
-> { GC.config(1) }.should raise_error(ArgumentError, "ArgumentError")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if specifying the message makes sense if there is no message. Maybe this is overspecification?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it's better to not specify the message if it's empty/missing or clearly wrong, could you remove those?


it "includes the name of currently loaded GC implementation as a global key" do
GC.config.should include(:implementation)
GC.config[:implementation].should be_kind_of(String)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out, the string uses Encoding::US_ASCII, not UTF-8. Should this be specified?

Copy link
Member

@eregon eregon Nov 23, 2025

Choose a reason for hiding this comment

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

Doesn't matter much, they are interchangeable (so I think not)

@eregon
Copy link
Member

eregon commented Nov 23, 2025

The docs at https://github.com/ruby/ruby/blob/2289961b485b1cbf7b1012693722c16a6cdb4cda/gc.rb#L398 say:

This method is implementation-specific to CRuby.

which sounds like maybe we shouldn't spec this, or explicitly allow the method to not be defined by Ruby implementations.

OTOH, it's a public method on a public class, so it's not unlikely some gems might rely on it and indeed it seems better to just implement as you have shown in the description for increased compatibility.
It'd be nice for TruffleRuby & JRuby to show the GC name in implementation for example, although I'm not sure that value can be read from inside Java.

Copy link
Member

@eregon eregon left a comment

Choose a reason for hiding this comment

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

Looks goods, thanks!

@eregon eregon merged commit 713af0a into ruby:master Nov 23, 2025
13 checks passed
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