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-Status: No, score=-3.8 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=ham 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 AF9661F66E for ; Wed, 26 Aug 2020 17:30:28 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 34517120A2A; Thu, 27 Aug 2020 02:29:56 +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 069B4120A29 for ; Thu, 27 Aug 2020 02:29:53 +0900 (JST) Received: by filterdrecv-p3las1-c889d8879-b4bfh with SMTP id filterdrecv-p3las1-c889d8879-b4bfh-21-5F469C2F-15 2020-08-26 17:30:23.157236224 +0000 UTC m=+79182.663152128 Received: from herokuapp.com (unknown) by geopod-ismtpd-2-3 (SG) with ESMTP id 0zolTz6FSiCiC1QBNL0ezw for ; Wed, 26 Aug 2020 17:30:22.890 +0000 (UTC) Date: Wed, 26 Aug 2020 17:30:23 +0000 (UTC) From: marcandre-ruby-core@marc-andre.ca Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 75541 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Bug X-Redmine-Issue-Id: 17030 X-Redmine-Issue-Author: marcandre X-Redmine-Sender: marcandre 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?6=2FIMxCQLDposcQf5wmbDAtfaKduBAO0bKyhL3BGZtMQ5q7K2TvpbN6A7JIyt9E?= =?us-ascii?Q?aO5E6ksoeOiPxRnagKbws52asfk7uhYt9YVUYWE?= =?us-ascii?Q?2aqr50umGb6b7qHrardLw+5Rb12LXHkX+08L6q5?= =?us-ascii?Q?V4PnY1IYR33lFk6a+RdIfOGPLmyLkrJbWFiPZ1M?= =?us-ascii?Q?aSFukt1pEb=2FUhde2ThJu4Rnfc9D5Z1R5e6qpJx4?= =?us-ascii?Q?nGIMWh24fc6bBzvqQvB4PvbmgdYbm2NDWr1UY6?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 99708 Subject: [ruby-core:99708] [Ruby master Bug#17030] Enumerable#grep{_v} should be optimized for Regexp 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 #17030 has been updated by marcandre (Marc-Andre Lafortune). fatkodima (Dima Fatko) wrote in #note-13: > In many cases, probably yes, but again, `case-when`, when arguments/consts/etc instead of local vars are used - it is hard to tell if them are regexes or not. That's not really what I'm proposing. I'm proposing something like an internal `Regexp.needs_last_match?` that would return `true` or `false` depending on the Ruby code, and that could be used to optimize methods. It would return `true` if any subsequent code could be impacted by `$~` and al. ```ruby def foo /x/ =~ 'x' # needs_last_match? # => false case method when /(foo)/ # needs_last_match? # => false do_something when /(bar)/ # needs_last_match? # => true puts $2 # ... # needs_last_match? # => false end end def bar # ... # needs_last_match? # => true case x when /(foo)/ # needs_last_match? # => true do_something end Regexp.last_match # ... # needs_last_match? # => false (false negative) Regexp.send :last_match # ... # needs_last_match? # => false (false negative) const_get(:Regexp).last_match end ``` ---------------------------------------- Bug #17030: Enumerable#grep{_v} should be optimized for Regexp https://bugs.ruby-lang.org/issues/17030#change-87199 * Author: marcandre (Marc-Andre Lafortune) * Status: Open * Priority: Normal * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN ---------------------------------------- Currently: ```ruby array.select { |e| e.match?(REGEXP) } # about 3x faster and 6x more memory efficient than array.grep(REGEXP) ``` This is because `grep` calls `Regexp#===` which creates useless `MatchData` -- https://bugs.ruby-lang.org/