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=-2.2 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_BL_SPAMCOP_NET,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 568DD1F5AE for ; Tue, 21 Jul 2020 11:27:40 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 7A0C4120A5C; Tue, 21 Jul 2020 20:27:05 +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 6993C120A5B for ; Tue, 21 Jul 2020 20:27:01 +0900 (JST) Received: by filterdrecv-p3iad2-5b55dcd864-9xqm9 with SMTP id filterdrecv-p3iad2-5b55dcd864-9xqm9-19-5F16D10F-28 2020-07-21 11:27:11.312614516 +0000 UTC m=+2139470.271699657 Received: from herokuapp.com (unknown) by ismtpd0021p1iad2.sendgrid.net (SG) with ESMTP id 8FXkBZZBQ8qoS82iiKIfHA for ; Tue, 21 Jul 2020 11:27:11.239 +0000 (UTC) Date: Tue, 21 Jul 2020 11:27:11 +0000 (UTC) From: scivola20@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 75037 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Bug X-Redmine-Issue-Id: 17030 X-Redmine-Issue-Author: marcandre X-Redmine-Sender: scivola20 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?u8hf4OtRrg=2FO8nePzVvmvIhAkN2S0lOrMpOKRf2gTMIRA+zm7XwYIAqkU6PIh4?= =?us-ascii?Q?YcIH6pI6wcotbV5v8fgo2PKq6Cljx5HTmVE2UcS?= =?us-ascii?Q?Z6lF9RApzVkH89oJJKqe0yXNa4mSZ0z4FJbTu3P?= =?us-ascii?Q?0D1HkT+EBPY3gEKW8M8uaV8MSGwWnnhBxFsU0vH?= =?us-ascii?Q?RlOGTgXW1V5x9pR1+Byw+R9x30yDMTeOLTlmYm+?= =?us-ascii?Q?ihdsDnLbI82eGOnSY=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 99248 Subject: [ruby-core:99248] [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 scivola20 (sciv ola). I have an idea to solve it without any compatibility problem. [1] Introduce such a Regexp object that `===` method is same as `match?`. [2] Introduce regexp literal option that makes the Regexp object as [1]. If the option is `'f'`, we can write as `/o/f`, and `grep(/o/f)` is faster than `grep(/o/)`. This speed up not only `grep` but also `all?`, `any?`, `case` and so on. Many people have written like this: ```rb IO.foreach("foo.txt") do |line| case line when /^#/ # do nothing when /^(\d+)/ # using $1 when /xxx/ # using $& when /yyy/ # not using $& else # ... end end ``` This is slow because of the above mentioned problem. Replacing `/^#/` with `/^#/f`, and `/yyy/` with `/yyy/f` will make it faster. ---------------------------------------- Bug #17030: Enumerable#grep{_v} should be optimized for Regexp https://bugs.ruby-lang.org/issues/17030#change-86632 * 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/