ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: nobu@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92768] [Ruby trunk Feature#15765] [PATCH] Module#name without global constant search
Date: Wed, 22 May 2019 07:11:38 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-78133.20190522071138.38938568794a99f5@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15765.20190412041238@ruby-lang.org

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   # "#<Module:0x0000563494b1cca8>::AfterToS" on trunk, nil after patch
p mod::AfterToS2.name  # "#<Module:0x0000563494b1cca8>::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/

      parent reply	other threads:[~2019-05-22  7:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15765.20190412041238@ruby-lang.org>
2019-04-12  4:12 ` [ruby-core:92253] [Ruby trunk Feature#15765] [PATCH] Module#name without global constant search alanwucanada
2019-04-12  4:17 ` [ruby-core:92254] " alanwucanada
2019-05-22  7:11 ` nobu [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-78133.20190522071138.38938568794a99f5@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).