Project

General

Profile

Bug #13113

Performance issue, const_set Module.new is too slow in Ruby 2.4

Added by pocke (Masataka Kuwabara) about 2 years ago. Updated about 2 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]
[ruby-core:79007]

Description

In Ruby 2.4, the following code is too slow.

5000000.times { Module.new.const_set(:X, Module.new) }

In Ruby 2.4, it took 115.68 seconds to run this code.
However, in Ruby 2.3.3, it took 7.36 seconds.

And if const_set value is not Module.new, it is not slow. For example

5000000.times { Module.new.const_set(:X, 2) }

Ruby versions

ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]

See also. https://github.com/rspec/rspec-core/issues/2365

Associated revisions

Revision 8e980027
Added by nobu (Nobuyoshi Nakada) about 2 years ago

benchmarks for [Bug #13113] [ci skip]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57283 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 57283
Added by nobu (Nobuyoshi Nakada) about 2 years ago

benchmarks for [Bug #13113] [ci skip]

Revision 57283
Added by nobu (Nobuyoshi Nakada) about 2 years ago

benchmarks for [Bug #13113] [ci skip]

Revision 57283
Added by nobu (Nobuyoshi Nakada) about 2 years ago

benchmarks for [Bug #13113] [ci skip]

Revision faf472a2
Added by nobu (Nobuyoshi Nakada) about 2 years ago

variable.c: resolve permanent name only

  • variable.c (rb_const_set): resolve and cache class name immediately only if the outer class/module has the name, otherwise just set the ID. [ruby-core:79007] [Bug #13113]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57284 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 57284
Added by nobu (Nobuyoshi Nakada) about 2 years ago

variable.c: resolve permanent name only

  • variable.c (rb_const_set): resolve and cache class name immediately only if the outer class/module has the name, otherwise just set the ID. [ruby-core:79007] [Bug #13113]

Revision 57284
Added by nobu (Nobuyoshi Nakada) about 2 years ago

variable.c: resolve permanent name only

  • variable.c (rb_const_set): resolve and cache class name immediately only if the outer class/module has the name, otherwise just set the ID. [ruby-core:79007] [Bug #13113]

Revision 57284
Added by nobu (Nobuyoshi Nakada) about 2 years ago

variable.c: resolve permanent name only

  • variable.c (rb_const_set): resolve and cache class name immediately only if the outer class/module has the name, otherwise just set the ID. [ruby-core:79007] [Bug #13113]

Revision d4f4ba6f
Added by naruse (Yui NARUSE) about 2 years ago

merge revision(s) 57283,57284: [Backport #13113]

benchmarks for [Bug #13113] [ci skip]
variable.c: resolve permanent name only

* variable.c (rb_const_set): resolve and cache class name
  immediately only if the outer class/module has the name,
  otherwise just set the ID.  [ruby-core:79007] [Bug #13113]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@57850 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 57850
Added by naruse (Yui NARUSE) about 2 years ago

merge revision(s) 57283,57284: [Backport #13113]

benchmarks for [Bug #13113] [ci skip]
variable.c: resolve permanent name only

* variable.c (rb_const_set): resolve and cache class name
  immediately only if the outer class/module has the name,
  otherwise just set the ID.  [ruby-core:79007] [Bug #13113]

History

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

  • Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN to 2.2: DONTNEED, 2.3: DONTNEED, 2.4: REQUIRED

Since r53376, anonymous module is resolved immediately by assignment to a constant, including the outer module.

#2

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

  • Status changed from Open to Closed

Applied in changeset r57283.


benchmarks for [Bug #13113] [ci skip]

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

benchmark results:
Execution time (sec)

name 2.3.3 r57281 r57284
loop_whileloop2 0.146 0.135 0.106
vm2_module_ann_const_set* 9.072 107.850 6.889
vm2_module_const_set* 3.954 13.374 3.468

Speedup ratio: compare with the result of `2.3.3' (greater is better)

name r57281 r57284
loop_whileloop2 1.075 1.373
vm2_module_ann_const_set* 0.084 1.317
vm2_module_const_set* 0.296 1.140

Updated by ko1 (Koichi Sasada) about 2 years ago

中田さん:

(1) ann_ って何?
(2) Module.new.const_set(:X, Module.new) だと、
  Module.new が支配的だと思うんだけど、何が計りたいの?

On 2017/01/08 10:46, nobu wrote:

nobu 2017-01-08 10:46:10 +0900 (Sun, 08 Jan 2017)

New Revision: 57283

https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57283

Log:
benchmarks for [Bug #13113] [ci skip]

Added files:
trunk/benchmark/bm_vm2_module_ann_const_set.rb
trunk/benchmark/bm_vm2_module_const_set.rb

Index: benchmark/bm_vm2_module_ann_const_set.rb

--- benchmark/bm_vm2_module_ann_const_set.rb (revision 0)
+++ benchmark/bm_vm2_module_ann_const_set.rb (revision 57283)
@@ -0,0 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/bm_vm2_module_ann_const_set.rb#L1
+i = 0
+while i<6_000_000 # benchmark loop 2

  • i += 1
  • Module.new.const_set(:X, Module.new) +end

Property changes on: benchmark/bm_vm2_module_ann_const_set.rb


Added: svn:eol-style

  • LF

Index: benchmark/bm_vm2_module_const_set.rb

--- benchmark/bm_vm2_module_const_set.rb (revision 0)
+++ benchmark/bm_vm2_module_const_set.rb (revision 57283)
@@ -0,0 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/bm_vm2_module_const_set.rb#L1
+i = 0
+module M
+end
+$VERBOSE = nil
+while i<6_000_000 # benchmark loop 2

  • i += 1
  • M.const_set(:X, Module.new) +end

Property changes on: benchmark/bm_vm2_module_const_set.rb


Added: svn:eol-style

  • LF

--
ML: ruby-changes@quickml.atdot.net
Info: http://www.atdot.net/~ko1/quickml/

--
// SASADA Koichi at atdot dot net

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

On 2017/01/19 10:40, SASADA Koichi wrote:

中田さん:

(1) ann_ って何?

ANoNymous

(2) Module.new.const_set(:X, Module.new) だと、
  Module.new が支配的だと思うんだけど、何が計りたいの?

無名モジュールの名前解決がそれ以上に支配的だった、というのが問題なので
Module.newだけが支配的になればそれで充分。

--
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
中田 伸悦

Updated by ko1 (Koichi Sasada) about 2 years ago

On 2017/01/19 10:47, Nobuyoshi Nakada wrote:

(1) ann_ って何?

ANoNymous

わかんねーです。
せめてコメントとか。

(2) Module.new.const_set(:X, Module.new) だと、
  Module.new が支配的だと思うんだけど、何が計りたいの?

無名モジュールの名前解決がそれ以上に支配的だった、というのが問題なので
Module.newだけが支配的になればそれで充分。

うーん、ベンチマークじゃなくて、テストに入れても良さそうな...。

--
// SASADA Koichi at atdot dot net

Updated by naruse (Yui NARUSE) about 2 years ago

  • Backport changed from 2.2: DONTNEED, 2.3: DONTNEED, 2.4: REQUIRED to 2.2: DONTNEED, 2.3: DONTNEED, 2.4: DONE

ruby_2_4 r57850 merged revision(s) 57283,57284.

Also available in: Atom PDF