Bug #21558
closedRegexp.union with multiple regexps returns a non-shareable object (should be shareable if possible)
Description
Regexp.union returns a non-shareable object when given multiple Regex arguments. Is this expected behavior?
It would be preferable if the resulting Regexp were shareable, since single-argument Regexp.union does produce a shareable object.
irb(main):001> a = /a/
=> /a/
irb(main):002> b = Regexp.union(a)
=> /a/
irb(main):003> c = Regexp.union(a, a)
=> /(?-mix:a)|(?-mix:a)/
irb(main):004> Ractor.shareable?(a)
=> true
irb(main):005> Ractor.shareable?(b)
=> true
irb(main):006> Ractor.shareable?(c)
=> false
Updated by nobu (Nobuyoshi Nakada) 10 months ago
- Status changed from Open to Feedback
Regexp.union creates a new instance, except for a single Regexp argument case.
And it is impossible to revert a Ractor-shareable object to non-Ractor-shareable.
So it feels natural that the new instance is not Ractor-shareable as well as String.new and others, to me.
Updated by nobu (Nobuyoshi Nakada) 10 months ago
Another example is Range.
All instances of Range are automatically frozen so they are Ractor-shareable.
Personally, I think freezing all Regexp instances also may be nice, but that's a different story than Regexp.union.
Updated by niku (niku _) 10 months ago
Regexp.union creates a new instance, except for a single Regexp argument case.
Ah, that makes perfect sense now. Thank you for the detailed explanation.
irb(main):001> a = /a/
=> /a/
irb(main):002> a.__id__
=> 7232
irb(main):003> b = Regexp.union(a)
=> /a/
irb(main):004> b.__id__
=> 7232
irb(main):005> c = Regexp.union(a, a)
=> /(?-mix:a)|(?-mix:a)/
irb(main):006> c.__id__
=> 17912
Personally, I think freezing all Regexp instances also may be nice, but that's a different story than Regexp.union.
I agree with your opinion. I'll create a new issue.
Updated by niku (niku _) 10 months ago
I've found an issue that already talked about it https://bugs.ruby-lang.org/issues/17256.
Updated by znz (Kazuhiro NISHIYAMA) 10 months ago
- Related to Feature #17256: Freeze all Regexp objects added
Updated by Eregon (Benoit Daloze) 10 months ago
- Related to Feature #8948: Frozen regex added
Updated by Eregon (Benoit Daloze) 10 months ago
There is also #8948. I didn't get time to try it, but anyone is welcome to try and find if there are any failures in CI/the test suites and if so how to fix them.