Forum: Ruby-core [ruby-trunk - Feature #8948][Open] Frozen regex

43421679c356f438c69bf6947e62c4ec?d=identicon&s=25 Tsuyoshi S. (tsuyoshi_s)
on 2013-09-24 21:03
(Received via mailing list)
Issue #8948 has been reported by sawa (Tsuyoshi Sawada).

----------------------------------------
Feature #8948: Frozen regex
https://bugs.ruby-lang.org/issues/8948

Author: sawa (Tsuyoshi Sawada)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin
I see that frozen string was accepted for Ruby 2.1, and frozen array and
hash are proposed in https://bugs.ruby-lang.org/issues/8909. I feel
there is even more use case for a frozen regex, i.e., a regex literal
that generates a regex only once. It is frequent to have a regex within
a frequently repeated portion of code, and generating the same regex
each time is a waste of resource. At the moment, we can have a code
like:

    class Foo
      RE1 = /pattern1/
      RE2 = /pattern1/
      RE3 = /pattern1/
      def classify
        case self
        when RE1 then 1
        when RE2 then 2
        when RE3 then 3
        else 4
        end
      end
    end

but suppose we have a frozen `Regexp` literal `//f`. Then we can write
like:

    class Foo
      def classify
        case self
        when /pattern1/f then 1
        when /pattern1/f then 2
        when /pattern1/f then 3
        else 4
        end
      end
    end

=end
43421679c356f438c69bf6947e62c4ec?d=identicon&s=25 Tsuyoshi S. (tsuyoshi_s)
on 2013-09-24 21:09
(Received via mailing list)
Issue #8948 has been updated by sawa (Tsuyoshi Sawada).


Sorry, there was a mistake in the above. The three regexes with the same
content `/pattern1/` (or `/pattern1/f`) in the respective examples are
supposed to represent different patterns.
----------------------------------------
Feature #8948: Frozen regex
https://bugs.ruby-lang.org/issues/8948#change-41957

Author: sawa (Tsuyoshi Sawada)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin
I see that frozen string was accepted for Ruby 2.1, and frozen array and
hash are proposed in https://bugs.ruby-lang.org/issues/8909. I feel
there is even more use case for a frozen regex, i.e., a regex literal
that generates a regex only once. It is frequent to have a regex within
a frequently repeated portion of code, and generating the same regex
each time is a waste of resource. At the moment, we can have a code
like:

    class Foo
      RE1 = /pattern1/
      RE2 = /pattern1/
      RE3 = /pattern1/
      def classify
        case self
        when RE1 then 1
        when RE2 then 2
        when RE3 then 3
        else 4
        end
      end
    end

but suppose we have a frozen `Regexp` literal `//f`. Then we can write
like:

    class Foo
      def classify
        case self
        when /pattern1/f then 1
        when /pattern1/f then 2
        when /pattern1/f then 3
        else 4
        end
      end
    end

=end
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 Eregon (Benoit Daloze) (Guest)
on 2013-09-24 23:00
(Received via mailing list)
Issue #8948 has been updated by Eregon (Benoit Daloze).


We already have immutable (created only once) regexps: it is always the
case for literal regexps and for dynamic regexps you need the 'o' flag:
/a#{2}b/o.

So there are in practice immutable, but currently not #frozen?. Do you
want to request it? I think it makes sense.

You can check with #object_id to know if 2 references reference the same
object.

> def r; /ab/; end
> r.object_id
=> 2160323760
> r.object_id
=> 2160323760

> def s; /a#{2}b/; end
> s.object_id
=> 2153197860
> s.object_id
=> 2160163740

> def t; /a#{2}b/o; end
> t.object_id
=> 2160181200
> t.object_id
=> 2160181200
----------------------------------------
Feature #8948: Frozen regex
https://bugs.ruby-lang.org/issues/8948#change-41958

Author: sawa (Tsuyoshi Sawada)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin
I see that frozen string was accepted for Ruby 2.1, and frozen array and
hash are proposed in https://bugs.ruby-lang.org/issues/8909. I feel
there is even more use case for a frozen regex, i.e., a regex literal
that generates a regex only once. It is frequent to have a regex within
a frequently repeated portion of code, and generating the same regex
each time is a waste of resource. At the moment, we can have a code
like:

    class Foo
      RE1 = /pattern1/
      RE2 = /pattern1/
      RE3 = /pattern1/
      def classify
        case self
        when RE1 then 1
        when RE2 then 2
        when RE3 then 3
        else 4
        end
      end
    end

but suppose we have a frozen `Regexp` literal `//f`. Then we can write
like:

    class Foo
      def classify
        case self
        when /pattern1/f then 1
        when /pattern1/f then 2
        when /pattern1/f then 3
        else 4
        end
      end
    end

=end
43421679c356f438c69bf6947e62c4ec?d=identicon&s=25 Tsuyoshi S. (tsuyoshi_s)
on 2013-09-25 01:43
(Received via mailing list)
Issue #8948 has been updated by sawa (Tsuyoshi Sawada).


Eregon, thank you for the information.
----------------------------------------
Feature #8948: Frozen regex
https://bugs.ruby-lang.org/issues/8948#change-41963

Author: sawa (Tsuyoshi Sawada)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin
I see that frozen string was accepted for Ruby 2.1, and frozen array and
hash are proposed in https://bugs.ruby-lang.org/issues/8909. I feel
there is even more use case for a frozen regex, i.e., a regex literal
that generates a regex only once. It is frequent to have a regex within
a frequently repeated portion of code, and generating the same regex
each time is a waste of resource. At the moment, we can have a code
like:

    class Foo
      RE1 = /pattern1/
      RE2 = /pattern1/
      RE3 = /pattern1/
      def classify
        case self
        when RE1 then 1
        when RE2 then 2
        when RE3 then 3
        else 4
        end
      end
    end

but suppose we have a frozen `Regexp` literal `//f`. Then we can write
like:

    class Foo
      def classify
        case self
        when /pattern1/f then 1
        when /pattern1/f then 2
        when /pattern1/f then 3
        else 4
        end
      end
    end

=end
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 Eregon (Benoit Daloze) (Guest)
on 2013-09-25 14:47
(Received via mailing list)
Issue #8948 has been updated by Eregon (Benoit Daloze).

Status changed from Open to Feedback

sawa: do you want to request Regexp to always be #frozen? or should the
issue be closed?
----------------------------------------
Feature #8948: Frozen regex
https://bugs.ruby-lang.org/issues/8948#change-41974

Author: sawa (Tsuyoshi Sawada)
Status: Feedback
Priority: Normal
Assignee:
Category:
Target version:


=begin
I see that frozen string was accepted for Ruby 2.1, and frozen array and
hash are proposed in https://bugs.ruby-lang.org/issues/8909. I feel
there is even more use case for a frozen regex, i.e., a regex literal
that generates a regex only once. It is frequent to have a regex within
a frequently repeated portion of code, and generating the same regex
each time is a waste of resource. At the moment, we can have a code
like:

    class Foo
      RE1 = /pattern1/
      RE2 = /pattern1/
      RE3 = /pattern1/
      def classify
        case self
        when RE1 then 1
        when RE2 then 2
        when RE3 then 3
        else 4
        end
      end
    end

but suppose we have a frozen `Regexp` literal `//f`. Then we can write
like:

    class Foo
      def classify
        case self
        when /pattern1/f then 1
        when /pattern1/f then 2
        when /pattern1/f then 3
        else 4
        end
      end
    end

=end
Fe5660da5930df2d7b34b6066c2d16bb?d=identicon&s=25 Jens Wille (jwille)
on 2013-09-25 16:47
(Received via mailing list)
Issue #8948 has been updated by jwille (Jens Wille).


=begin
besides regexps being frozen, there might still be a use case for regexp
literals that would only be allocated once:

  def r1; /ab/; end; r1.object_id  #=> 70043421664620
  def r2; /ab/; end; r2.object_id  #=> 70043421398060

  def r3; /ab/f; end; r3.object_id  #=> 70043421033140
  def r4; /ab/f; end; r4.object_id  #=> 70043421033140

i think it's in the same vein as #8579 and #8909.
=end

----------------------------------------
Feature #8948: Frozen regex
https://bugs.ruby-lang.org/issues/8948#change-41976

Author: sawa (Tsuyoshi Sawada)
Status: Feedback
Priority: Normal
Assignee:
Category:
Target version:


=begin
I see that frozen string was accepted for Ruby 2.1, and frozen array and
hash are proposed in https://bugs.ruby-lang.org/issues/8909. I feel
there is even more use case for a frozen regex, i.e., a regex literal
that generates a regex only once. It is frequent to have a regex within
a frequently repeated portion of code, and generating the same regex
each time is a waste of resource. At the moment, we can have a code
like:

    class Foo
      RE1 = /pattern1/
      RE2 = /pattern1/
      RE3 = /pattern1/
      def classify
        case self
        when RE1 then 1
        when RE2 then 2
        when RE3 then 3
        else 4
        end
      end
    end

but suppose we have a frozen `Regexp` literal `//f`. Then we can write
like:

    class Foo
      def classify
        case self
        when /pattern1/f then 1
        when /pattern1/f then 2
        when /pattern1/f then 3
        else 4
        end
      end
    end

=end
43421679c356f438c69bf6947e62c4ec?d=identicon&s=25 Tsuyoshi S. (tsuyoshi_s)
on 2013-09-26 07:48
(Received via mailing list)
Issue #8948 has been updated by sawa (Tsuyoshi Sawada).


jwille, I agree with the use case, but it would be difficult to tell
which regexes are intended to be the same, so I would not request that
feature.

Probably, it makes sense to have all static regexes frozen, and have the
`f` flag freeze dynamic regexes as well. I can't think of a use case for
a regex that is immutable but not frozen. I am actually not clear about
the difference.
----------------------------------------
Feature #8948: Frozen regex
https://bugs.ruby-lang.org/issues/8948#change-41987

Author: sawa (Tsuyoshi Sawada)
Status: Feedback
Priority: Normal
Assignee:
Category:
Target version:


=begin
I see that frozen string was accepted for Ruby 2.1, and frozen array and
hash are proposed in https://bugs.ruby-lang.org/issues/8909. I feel
there is even more use case for a frozen regex, i.e., a regex literal
that generates a regex only once. It is frequent to have a regex within
a frequently repeated portion of code, and generating the same regex
each time is a waste of resource. At the moment, we can have a code
like:

    class Foo
      RE1 = /pattern1/
      RE2 = /pattern1/
      RE3 = /pattern1/
      def classify
        case self
        when RE1 then 1
        when RE2 then 2
        when RE3 then 3
        else 4
        end
      end
    end

but suppose we have a frozen `Regexp` literal `//f`. Then we can write
like:

    class Foo
      def classify
        case self
        when /pattern1/f then 1
        when /pattern1/f then 2
        when /pattern1/f then 3
        else 4
        end
      end
    end

=end
Fe5660da5930df2d7b34b6066c2d16bb?d=identicon&s=25 Jens Wille (jwille)
on 2013-09-27 13:15
(Received via mailing list)
Issue #8948 has been updated by jwille (Jens Wille).


=begin
((*> but it would be difficult to tell which regexes are intended to be
the same*))

i'm not sure i understand. how is

  def r1; /ab/f; end
  def r2; /ab/f; end

different from

  def s1; 'ab'f; end
  def s2; 'ab'f; end

?
=end

----------------------------------------
Feature #8948: Frozen regex
https://bugs.ruby-lang.org/issues/8948#change-42040

Author: sawa (Tsuyoshi Sawada)
Status: Feedback
Priority: Normal
Assignee:
Category:
Target version:


=begin
I see that frozen string was accepted for Ruby 2.1, and frozen array and
hash are proposed in https://bugs.ruby-lang.org/issues/8909. I feel
there is even more use case for a frozen regex, i.e., a regex literal
that generates a regex only once. It is frequent to have a regex within
a frequently repeated portion of code, and generating the same regex
each time is a waste of resource. At the moment, we can have a code
like:

    class Foo
      RE1 = /pattern1/
      RE2 = /pattern1/
      RE3 = /pattern1/
      def classify
        case self
        when RE1 then 1
        when RE2 then 2
        when RE3 then 3
        else 4
        end
      end
    end

but suppose we have a frozen `Regexp` literal `//f`. Then we can write
like:

    class Foo
      def classify
        case self
        when /pattern1/f then 1
        when /pattern1/f then 2
        when /pattern1/f then 3
        else 4
        end
      end
    end

=end
43421679c356f438c69bf6947e62c4ec?d=identicon&s=25 Tsuyoshi S. (tsuyoshi_s)
on 2013-09-29 11:36
(Received via mailing list)
Issue #8948 has been updated by sawa (Tsuyoshi Sawada).


jwille,

My understanding with the case of string in your example is that the two
strings would count as different strings, but for respective method
calls would not create new strings. It would mean one of the string can
be `"ab"` and the other a different string such as `"cd"`.

If that is what you intended for your regex examples, then there is no
difference.
----------------------------------------
Feature #8948: Frozen regex
https://bugs.ruby-lang.org/issues/8948#change-42077

Author: sawa (Tsuyoshi Sawada)
Status: Feedback
Priority: Normal
Assignee:
Category:
Target version:


=begin
I see that frozen string was accepted for Ruby 2.1, and frozen array and
hash are proposed in https://bugs.ruby-lang.org/issues/8909. I feel
there is even more use case for a frozen regex, i.e., a regex literal
that generates a regex only once. It is frequent to have a regex within
a frequently repeated portion of code, and generating the same regex
each time is a waste of resource. At the moment, we can have a code
like:

    class Foo
      RE1 = /pattern1/
      RE2 = /pattern1/
      RE3 = /pattern1/
      def classify
        case self
        when RE1 then 1
        when RE2 then 2
        when RE3 then 3
        else 4
        end
      end
    end

but suppose we have a frozen `Regexp` literal `//f`. Then we can write
like:

    class Foo
      def classify
        case self
        when /pattern1/f then 1
        when /pattern1/f then 2
        when /pattern1/f then 3
        else 4
        end
      end
    end

=end
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 ko1 (Koichi Sasada) (Guest)
on 2013-09-30 05:12
(Received via mailing list)
Issue #8948 has been updated by ko1 (Koichi Sasada).

Category set to syntax
Assignee set to matz (Yukihiro Matsumoto)
Target version set to next minor

I like to freeze normal regexp literal that Eregon said.

2.2 matter?

Anyone set instance variable for each regexp? :)

----------------------------------------
Feature #8948: Frozen regex
https://bugs.ruby-lang.org/issues/8948#change-42086

Author: sawa (Tsuyoshi Sawada)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: next minor


=begin
I see that frozen string was accepted for Ruby 2.1, and frozen array and
hash are proposed in https://bugs.ruby-lang.org/issues/8909. I feel
there is even more use case for a frozen regex, i.e., a regex literal
that generates a regex only once. It is frequent to have a regex within
a frequently repeated portion of code, and generating the same regex
each time is a waste of resource. At the moment, we can have a code
like:

    class Foo
      RE1 = /pattern1/
      RE2 = /pattern1/
      RE3 = /pattern1/
      def classify
        case self
        when RE1 then 1
        when RE2 then 2
        when RE3 then 3
        else 4
        end
      end
    end

but suppose we have a frozen `Regexp` literal `//f`. Then we can write
like:

    class Foo
      def classify
        case self
        when /pattern1/f then 1
        when /pattern1/f then 2
        when /pattern1/f then 3
        else 4
        end
      end
    end

=end
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 Eregon (Benoit Daloze) (Guest)
on 2013-09-30 10:26
(Received via mailing list)
Issue #8948 has been updated by Eregon (Benoit Daloze).


ko1 (Koichi Sasada) wrote:
> 2.2 matter?

2.1 would make sense to me, so it goes along with other frozen literals.

> Anyone set instance variable for each regexp? :)

I highly doubt it.
----------------------------------------
Feature #8948: Frozen regex
https://bugs.ruby-lang.org/issues/8948#change-42089

Author: sawa (Tsuyoshi Sawada)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: next minor


=begin
I see that frozen string was accepted for Ruby 2.1, and frozen array and
hash are proposed in https://bugs.ruby-lang.org/issues/8909. I feel
there is even more use case for a frozen regex, i.e., a regex literal
that generates a regex only once. It is frequent to have a regex within
a frequently repeated portion of code, and generating the same regex
each time is a waste of resource. At the moment, we can have a code
like:

    class Foo
      RE1 = /pattern1/
      RE2 = /pattern1/
      RE3 = /pattern1/
      def classify
        case self
        when RE1 then 1
        when RE2 then 2
        when RE3 then 3
        else 4
        end
      end
    end

but suppose we have a frozen `Regexp` literal `//f`. Then we can write
like:

    class Foo
      def classify
        case self
        when /pattern1/f then 1
        when /pattern1/f then 2
        when /pattern1/f then 3
        else 4
        end
      end
    end

=end
This topic is locked and can not be replied to.