From: headius@... Date: 2021-04-01T18:25:06+00:00 Subject: [ruby-core:103155] [Ruby master Bug#17771] String#start_with? should not construct MatchData or set $~ Issue #17771 has been updated by headius (Charles Nutter). I see this behavior was explicitly blessed by matz in #13712 but I still believe this is not the best choice. Around the same time as that discussion, another boolean query method `match?` was added that explicitly does *not* set the last match frame variable. I feel this is inconsistent and the boolean query methods that accept a Regexp should be as fast as possible. If you want a MatchData use methods that provide it. ---------------------------------------- Bug #17771: String#start_with? should not construct MatchData or set $~ https://bugs.ruby-lang.org/issues/17771#change-91227 * Author: headius (Charles Nutter) * Status: Open * Priority: Normal * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN ---------------------------------------- I am working on making $~ more thread-safe in JRuby and came across this unexpected behavior: ```ruby $ rvm ruby-3.0 do ruby -e '"foo".start_with?(/foo/); p $~' # ``` The `start_with?` method was added 11 years ago in https://bugs.ruby-lang.org/issues/3388 but I do not think the set of $~ was an intended feature. The `start_with?` method could be much faster and more thread-safe if it did not use the frame-local backref slot and did not allocate a MatchData. Compare with `match?` which was added specifically (without MatchData or backref setting) to provide a fast way to check if a Regexp matches. I propose that `start_with?` stop constructing MatchData, stop setting backref, and provide only its boolean result in the same way as `match?`. -- https://bugs.ruby-lang.org/ Unsubscribe: