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,URIBL_BLOCKED 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 03F2A1F66E for ; Thu, 27 Aug 2020 17:16:47 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 82104120AA3; Fri, 28 Aug 2020 02:16:11 +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 34DDE120AA0 for ; Fri, 28 Aug 2020 02:16:09 +0900 (JST) Received: by filterdrecv-p3iad2-56cd58c948-88wkq with SMTP id filterdrecv-p3iad2-56cd58c948-88wkq-19-5F47EA76-C0 2020-08-27 17:16:38.649774332 +0000 UTC m=+68263.424612590 Received: from herokuapp.com (unknown) by geopod-ismtpd-4-0 (SG) with ESMTP id cKOI4wtwTgGINx4CiqYhbg for ; Thu, 27 Aug 2020 17:16:38.595 +0000 (UTC) Date: Thu, 27 Aug 2020 17:16:38 +0000 (UTC) From: merch-redmine@jeremyevans.net Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 75571 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Bug X-Redmine-Issue-Id: 17030 X-Redmine-Issue-Author: marcandre X-Redmine-Sender: jeremyevans0 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?RVE3t853K5scBhbmJHUzZTFFeVC=2FZSUmHZ0Dc+26wcEi2CTgsF1oz0wTSSxGGN?= =?us-ascii?Q?BIJgpO3kQOt0yPMKDBbzdP6aXetf98Os+yE41Lv?= =?us-ascii?Q?XxDBPb2nAvA2ODucRMwOrSgYgNMgFhi28nm9n4M?= =?us-ascii?Q?iYiurRuvndajk9ZU+gQaBrvaQmZuNppeaDo=2FHpF?= =?us-ascii?Q?ihWbeb3hHgdSHcStd7QadyXMJRIl6+03JPW4ZZR?= =?us-ascii?Q?NVhB54V+k0SN=2FxtNs=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 99738 Subject: [ruby-core:99738] [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 jeremyevans0 (Jeremy Evans). Dan0042 (Daniel DeLorme) wrote in #note-21: > Yeah ok, that sentence wasn't very clear, sorry. > > The first thing is that when compiling a method to an iseq, you have to set a flag on the iseq if the method contains any of the "last_match" pseudo-globals (`$~`, `$&`, `$1`, `Regexp.last_match`, ...) > > Then in `rb_reg_match` (aka `Regexp#=~`), you check if the current iseq has the flag set. This is similar to how `rb_backref_get` gets the last_match object from execution context > control frame > normal control frame > ep > svar > backref. If the flag is not set it means you can use a variant of `reg_match_pos` that only returns the position without using `rb_reg_search` to set the last_match, in the same vein as `rb_reg_match_m_p` (aka `Regexp#match?`). > > But I may be missing a few details here, as I don't have a full understanding of the VM. Unfortunately, you can't take this approach for VM optimizations without breaking backwards compatibility unless you also have a deoptimization approach that will handle code such as: ```ruby def a; /(a)/ =~ 'a'; binding; end; a.eval('$1') def a; /(a)/ =~ 'a'; proc{}; end; a.binding.eval('$1') def a(c, m); /(a)/ =~ 'a'; c.send(m); end; a(Regexp, :last_match) ``` ---------------------------------------- Bug #17030: Enumerable#grep{_v} should be optimized for Regexp https://bugs.ruby-lang.org/issues/17030#change-87237 * 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) } ``` is about three times faster and six times more memory efficient than ```ruby array.grep(REGEXP) ``` This is because `grep` calls `Regexp#===`, which creates useless `MatchData`. -- https://bugs.ruby-lang.org/