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=-2.8 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_PASS 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 2E16420248 for ; Fri, 12 Apr 2019 04:12:52 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 41AF112124F; Fri, 12 Apr 2019 13:12:44 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 98C4812124F for ; Fri, 12 Apr 2019 13:12:41 +0900 (JST) Received: by filter0151p3mdw1.sendgrid.net with SMTP id filter0151p3mdw1-18698-5CB01036-19 2019-04-12 04:12:38.532603788 +0000 UTC m=+120233.887320354 Received: from herokuapp.com (unknown [54.89.200.84]) by ismtpd0010p1iad2.sendgrid.net (SG) with ESMTP id SZljFD-7RvmHydGHdcUfjg for ; Fri, 12 Apr 2019 04:12:38.476 +0000 (UTC) Date: Fri, 12 Apr 2019 04:12:38 +0000 (UTC) From: alanwucanada@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 67737 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15765 X-Redmine-Issue-Author: alanwu X-Redmine-Sender: alanwu 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?jxWKqnXOckdYE3zELIumel1PrOP8HeovWI9f2iebhllqPVl7LMgil9ZI03Wc3T?= =?us-ascii?Q?SDqqe1+3coFYwcGak6RjwvdVkcTmf=2FBbmne+WLP?= =?us-ascii?Q?COfTMJrbXxbcp429mzNGBRX=2FFd6UtYdOnXL3Sgo?= =?us-ascii?Q?2WmEThJ3oheHbBv2+eJt1daoEL9WxirRT=2FVRjP5?= =?us-ascii?Q?iFvrvLQMgwTmdzTWKjllaFjExttTdaPYI4Q=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92253 Subject: [ruby-core:92253] [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 reported by alanwu (Alan Wu). ---------------------------------------- Feature #15765: [PATCH] Module#name without global constant search https://bugs.ruby-lang.org/issues/15765 * Author: alanwu (Alan Wu) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Hello! 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. ### Behavior changes and caveats: ```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 p mod::BeforeToS2.name p mod::AfterToS.name p mod::AfterToS2.name ``` 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/