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 8D5D81F66E for ; Wed, 26 Aug 2020 20:43:57 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id DAA5C120A4D; Thu, 27 Aug 2020 05:43:21 +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 75030120A4B for ; Thu, 27 Aug 2020 05:43:20 +0900 (JST) Received: by filterdrecv-p3las1-84969bf46d-r6ht7 with SMTP id filterdrecv-p3las1-84969bf46d-r6ht7-20-5F46C980-2F 2020-08-26 20:43:44.788986716 +0000 UTC m=+10578.483292591 Received: from herokuapp.com (unknown) by ismtpd0040p1iad2.sendgrid.net (SG) with ESMTP id f8WVt75BTl6KXQhsjiz-Jw for ; Wed, 26 Aug 2020 20:43:44.574 +0000 (UTC) Date: Wed, 26 Aug 2020 20:43:44 +0000 (UTC) From: daniel@dan42.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 75549 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Bug X-Redmine-Issue-Id: 17030 X-Redmine-Issue-Author: marcandre X-Redmine-Sender: Dan0042 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?8sy4RigFvRTdBfCVJrT9zb2J88PC92TMQwdNgaWYaq46lHq50Fn6s7KE+YQuhj?= =?us-ascii?Q?6YRkLKQ+Jy+kstvxi4MBuSPlNc4RdEDdRIrBjvt?= =?us-ascii?Q?TqQbFSdmapBltDcbwh+yZjyKYtKsy6hxrZbKaiI?= =?us-ascii?Q?b8gMoe8Ay0yILY+kq3f5ZJzsSfPrZGIVQwT8QwS?= =?us-ascii?Q?UzCnKMF9OZh2k0r1GazbAzrmXZ6ITbFG3BwCgrd?= =?us-ascii?Q?B0vAXh9efPRqXOVKA=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 99716 Subject: [ruby-core:99716] [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 Dan0042 (Daniel DeLorme). What about this? ```ruby 2.times do p $~ # depends on match *below* rx =~ str end ``` Now imagine if `2.times` is replaced by `foo`; a priori we can't know if or how many times the block will be executed. So what I was trying to say is that flow control can lead to all kinds of code paths where it's extremely difficult to know which matching operations a pseudo-global may depend on. Maybe not impossible, but personally I wouldn't want to code that kind of analysis when a simple approach is enough for >90% of cases, and guaranteed to be bug-free. > There will be other false positives: `str.gsub(regexp, &block)`. That's not a real issue, simply assume that `block` will want access to `Regexp.last_match`. Actually... `block` does not have access to `Regexp.last_match` (unless you created the block in the same scope as the gsub operation, but that would be unusual) ---------------------------------------- Bug #17030: Enumerable#grep{_v} should be optimized for Regexp https://bugs.ruby-lang.org/issues/17030#change-87206 * 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/