From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-2.6 required=3.0 tests=AWL,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 73F1D1F4B4 for ; Fri, 2 Apr 2021 10:28:08 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 7BA741209D6; Fri, 2 Apr 2021 19:27:07 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id DB5521209A7 for ; Fri, 2 Apr 2021 19:27:05 +0900 (JST) Received: by filterdrecv-p3iad2-7d7c446bd4-mj5b4 with SMTP id filterdrecv-p3iad2-7d7c446bd4-mj5b4-18-6066F1B2-7C 2021-04-02 10:28:02.951158813 +0000 UTC m=+834907.409212916 Received: from herokuapp.com (unknown) by ismtpd0173p1iad2.sendgrid.net (SG) with ESMTP id kfOwHi40TKqWIrOubPxl6w for ; Fri, 02 Apr 2021 10:28:02.936 +0000 (UTC) Date: Fri, 02 Apr 2021 10:28:02 +0000 (UTC) From: eregontp@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 79219 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17771 X-Redmine-Issue-Author: headius X-Redmine-Sender: Eregon X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?KippOI8ZHtTweq7XfQzW93937kJ4QNWwSBuHnaMEcr0BhyUv3YHLx5X0MTDUz1?= =?us-ascii?Q?txdAVgFMdM5P3rFKcYS3HUW1BH214=2FFVOiw23z0?= =?us-ascii?Q?rKPuN49IMIBb9LOE7JFRIygI7zDhDuOCaVUQUvZ?= =?us-ascii?Q?IigmmH4vliiYehCFvLDEWBVstuC41oZXvRS2oMI?= =?us-ascii?Q?JcXshWiEPN1C3=2F1cOEl2OWaxcmRwufeawsiZIej?= =?us-ascii?Q?Oq+Ss6W1sXQ=2Fi79xE=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 103176 Subject: [ruby-core:103176] [Ruby master Feature#17771] String#start_with? should not construct MatchData or set $~ X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #17771 has been updated by Eregon (Benoit Daloze). I don't think there is a rule that predicate methods only return a boolean and never set `$~`. It is the case for `String#match` vs `String#match?`, but it doesn't mean it holds for other Regexp methods. I see it a bit like the use of `!`, which in the core library is generally only used if there is also a non-`!` variant (e.g., `Array#delete`). `String#start_with?` enables to match a regexp without the need to manually build another regexp like `/\A#{regexp}/` (from the user point of view, there might be internal caching depending on the regexp engine), so I think that is a valid use case for using `start_with?` and accessing the MatchData after. StringScanner has a similar functionality for matching a regexp from the start, as if there was a `\A`, but does not expose `$~` directly: `ruby -rstrscan -e 's = StringScanner.new("test string"); s.scan(/(\w)\w+/); p s[1]'` => `"t"`. That said, I'm not against no longer setting $~ for String#start_with?, but I do worry about the compatibility issue here, especially since it might be quite hard to debug why $~ is suddenly `nil` or the previous MatchData in the Ruby version changing this behavior. ---------------------------------------- Feature #17771: String#start_with? should not construct MatchData or set $~ https://bugs.ruby-lang.org/issues/17771#change-91251 * Author: headius (Charles Nutter) * Status: Open * Priority: Normal ---------------------------------------- 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/