Skip to content

Class#subclasses slows down with larger sets #8457

@headius

Description

@headius

When Class#subclasses returns a longer list, we appear to slow down more relative to CRuby.

With the four classes from Numeric:

$ ruby -rbenchmark -e '5.times { puts Benchmark.measure { 10_000_000.times { Numeric.subclasses } } }'            
  0.840000   0.040000   0.880000 (  0.719652)
  0.610000   0.000000   0.610000 (  0.597529)
  0.610000   0.010000   0.620000 (  0.598173)
  0.600000   0.000000   0.600000 (  0.600692)
  0.650000   0.010000   0.660000 (  0.600609)
$ chruby-exec 3.3 -- ruby -rbenchmark -e '5.times { puts Benchmark.measure { 10_000_000.times { Numeric.subclasses } } }'
  0.892543   0.003522   0.896065 (  0.896127)
  0.880852   0.002772   0.883624 (  0.883620)
  0.902136   0.003467   0.905603 (  0.905592)
  0.902449   0.003406   0.905855 (  0.905844)
  0.888070   0.003159   0.891229 (  0.891218)

And the 83 subclasses (82 in CRuby) of Object (note reduced iterations in benchmark):

$ ruby -rbenchmark -e '5.times { puts Benchmark.measure { 1_000_000.times { Object.subclasses } } }'         
  2.090000   0.020000   2.110000 (  1.885031)
  1.780000   0.020000   1.800000 (  1.813361)
  1.800000   0.010000   1.810000 (  1.821201)
  1.760000   0.010000   1.770000 (  1.754643)
  1.810000   0.010000   1.820000 (  1.777603)
$ chruby-exec 3.3 -- ruby -rbenchmark -e '5.times { puts Benchmark.measure { 1_000_000.times { Object.subclasses } } }'
  1.006108   0.018142   1.024250 (  1.024307)
  0.989379   0.014869   1.004248 (  1.004562)
  1.006066   0.018692   1.024758 (  1.024845)
  1.022598   0.022539   1.045137 (  1.091699)
  0.985969   0.019927   1.005896 (  1.006522)

Ignoring the overhead of the benchmark, it appears CRuby slows down about 11x and we slow down 30x for an increased class count of 20x.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions