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=-3.9 required=3.0 tests=AWL,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,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 DE6231F462 for ; Wed, 22 May 2019 07:11:43 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 500D4120A1B; Wed, 22 May 2019 16:11:39 +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 6DBEE120A1B for ; Wed, 22 May 2019 16:11:37 +0900 (JST) Received: by filter0092p3mdw1.sendgrid.net with SMTP id filter0092p3mdw1-20531-5CE4F62A-38 2019-05-22 07:11:38.590745377 +0000 UTC m=+549189.056825969 Received: from herokuapp.com (unknown [3.81.19.97]) by ismtpd0036p1iad1.sendgrid.net (SG) with ESMTP id Hkr7ys29Q8eUlgKjocwgSw for ; Wed, 22 May 2019 07:11:38.558 +0000 (UTC) Date: Wed, 22 May 2019 07:11:38 +0000 (UTC) From: nobu@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 68254 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15765 X-Redmine-Issue-Author: alanwu X-Redmine-Sender: nobu 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?q8Dly+pU2+3ektTtZVXgZtbJPXwqo7p86jCsvYTW4BxMSxEEtvXbQGmu3Jv+9S?= =?us-ascii?Q?EJrWH9oBiql6nXfXRkc4fD62cOTxOTNIRb9rS6+?= =?us-ascii?Q?oiTjYa7VPu0zCRwYFC6b=2FYrq+cZGqLwd1qfTUKG?= =?us-ascii?Q?94bJ+QDMIyGI0x6WY0eAUtzMScgA9RgoKjJU2Aw?= =?us-ascii?Q?689EuGL8KRxWVWTphSasDk9IYJN=2FqZVFWuQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92768 Subject: [ruby-core:92768] [Ruby trunk Feature#15765] [PATCH] Module#name without global constant search 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 #15765 has been updated by nobu (Nobuyoshi Nakada). Status changed from Open to Closed Closed by b00f280d4b9569e7153365d7e1c522b3d6b3c6cf ---------------------------------------- Feature #15765: [PATCH] Module#name without global constant search https://bugs.ruby-lang.org/issues/15765#change-78133 * Author: alanwu (Alan Wu) * Status: Closed * Priority: Normal * Assignee: * Target version: ---------------------------------------- Hello! The current implementation of `Module#name` is known for having sub-optimal performance when it comes to anonymous modules. (see #11119 and #15625) I have put together a reimplementation of `Module#name`, which works by eagerly naming modules and classes when they are assigned to constants. Besides solving the performance issues for `Module#name` on anonymous modules, there are some other benefits. This patch: - removes more code than it adds - makes normal class and module definition slightly faster (definitions like `class Foo; end`) - slightly reduces memory usage for classes and modules due to the removal of a hidden ivar - improves the performance of defining modules and classes under an anonymous module. This used to execute a global search each time. ### Behavior changes and caveats: Since we already name module and classes declared with the `class` and `module` keyword on trunk, this patch mostly targets anonymous modules. I tried my best keeping the behaviors consistent with the current implementation, but there are some small behavioral changes. ```ruby mod = Module.new mod::BeforeToS = Module.new mod.const_set(:BeforeToS2, Module.new) mod.to_s # on trunk, the VM starts naming modules assigned under mod after calling to_s mod::AfterToS = Module.new mod.const_set(:AfterToS2, Module.new) p mod::BeforeToS.name # nil on both p mod::BeforeToS2.name # nil on both p mod::AfterToS.name # "#::AfterToS" on trunk, nil after patch p mod::AfterToS2.name # "#::AfterToS2" on trunk, nil after patch ``` This prints 4 nils after my patch, as I think the behavior on trunk is unintentional. A few C APIs also have the same effect as calling to_s. They are all changed to be side-effect free. ```ruby m = Module.new m::Child = Module.new Mod = m p Object.send(:remove_const, :Mod)::Child.name ``` This prints nil on trunk and `Mod::Child` under this patch. `rb_name_class` is removed, as it does nothing in this new implementation. Not sure if this is public API. Since the recursive naming is done with a recursive function, when a deeply nested anonymous module is assigned to a constant, it is technically possible for this implementation to throw a `StackError`. I had a version which does heap allocation to deal with this, but I picked this version for performance in the common cases. Anonymous modules are rare as is, and one would have to build a structure nested thousands level deep for this to happen. On my system it can name a module fifty thousand levels deep without problem. I think these changes are fairly minimal and acceptable. ---Files-------------------------------- benchmarks.rb (3.16 KB) 0001-Eagerly-name-modules-and-classes.patch (19.8 KB) -- https://bugs.ruby-lang.org/