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=-4.1 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_PASS 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 01A5820248 for ; Wed, 27 Feb 2019 17:36:46 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 120121216F4; Thu, 28 Feb 2019 02:36:42 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 0B323120B99 for ; Thu, 28 Feb 2019 02:36:38 +0900 (JST) Received: by filter0103p3iad2.sendgrid.net with SMTP id filter0103p3iad2-6904-5C76CAA5-3 2019-02-27 17:36:37.019964731 +0000 UTC m=+301026.612489241 Received: from herokuapp.com (ec2-54-91-101-91.compute-1.amazonaws.com [54.91.101.91]) by ismtpd0030p1iad2.sendgrid.net (SG) with ESMTP id 7ky-CBzsQ6eUomMw3RcEwA for ; Wed, 27 Feb 2019 17:36:37.039 +0000 (UTC) Date: Wed, 27 Feb 2019 17:36:38 +0000 (UTC) From: nelhage@nelhage.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 67082 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15625 X-Redmine-Issue-Author: nelhage X-Redmine-Sender: nelhage 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: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS4P3Lvb0o0U+4yUBcKiade21IwKjei6jpb+V4 W5e6LiRSH7zKikfahl7hI1PxdZ1ph9PCinBAVziR9H+RTrxHCsOQ7rr6CmjrjWKhYDP7ZAJ84TT4UE OldNVB3THYJmVt8HzRN3ejA9cFuALMCUi4z8WAPt/iYT5gU/MuSux7+8Aw== X-ML-Name: ruby-core X-Mail-Count: 91632 Subject: [ruby-core:91632] [Ruby trunk Bug#15625] Module#name performance has exponential-time worst case 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 #15625 has been reported by nelhage (Nelson Elhage). ---------------------------------------- Bug #15625: Module#name performance has exponential-time worst case https://bugs.ruby-lang.org/issues/15625 * Author: nelhage (Nelson Elhage) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin18] * Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- It's well-known (c.f #11119) that `Module#name` has poor performance on anonymous classes, due to searching the entire constant namespace of the VM. However, we recently discovered that it's even worse than that. In the presence of aliased constants (e.g. `module M; end; Alias = M`), the `find_class_path` method actually searches every *path* to a given module (e.g. it will visit that module under both the `M` and `Alias` names). This can lead to exponential-time behavior in cases of repeated aliases. See the attached test case, where performance gets twice as slow with every additional layer of aliasing: On my laptop: ``` user system total real before 2.183899 0.012151 2.196050 ( 2.355187) user system total real a19 5.614596 0.024394 5.638990 ( 5.702878) user system total real a10 9.204399 0.052817 9.257216 ( 9.391010) user system total real a11 16.184035 0.060854 16.244889 ( 16.561101) ``` Our house style makes extensive use of aliases as an import-like mechanism to provide short names within a given scope. We discovered this bug while exploring an actual performance problem -- this is not just a theoretical report. ---Files-------------------------------- classname.rb (854 Bytes) -- https://bugs.ruby-lang.org/