Skip to content

Provide concrete-only traversal for Class#subclasses#8480

Merged
headius merged 2 commits intojruby:masterfrom
headius:optimize_subclasses_walk
Dec 4, 2024
Merged

Provide concrete-only traversal for Class#subclasses#8480
headius merged 2 commits intojruby:masterfrom
headius:optimize_subclasses_walk

Conversation

@headius
Copy link
Member

@headius headius commented Dec 3, 2024

For classes with many singletons, the Class#subclasses walk will cost significantly more than is warranted for the few concrete classes it will return. This can be eliminated by tracking whether each entry is concrete and adding a separate traversal link to the next concrete class in the chain.

Performance of such a case is significantly better:

Before:

1 thread Numeric.subclasses
                          1.315k (± 4.6%) i/s -      6.612k in   5.040800s
1 thread Object.subclasses
                         56.282 (± 3.6%) i/s -    285.000 in   5.074688s
1 thread Custom.subclasses with 1000 singletons
                          5.588 (± 0.0%) i/s -     28.000 in   5.021174s

After:

1 thread Numeric.subclasses
                          2.402k (± 3.4%) i/s -     11.990k in   4.998757s
1 thread Object.subclasses
                        189.977 (± 6.8%) i/s -    954.000 in   5.068134s
1 thread Custom.subclasses with many singletons
                          3.439k (± 1.0%) i/s -     17.334k in   5.040639s

For classes with many singletons, the Class#subclasses walk will
cost significantly more than is warranted for the few concrete
classes it will return. This can be eliminated by tracking whether
each entry is concrete and adding a separate traversal link to the
next concrete class in the chain.

Performance of such a case is significantly better:

Before:
```
1 thread Numeric.subclasses
                          1.315k (± 4.6%) i/s -      6.612k in   5.040800s
1 thread Object.subclasses
                         56.282 (± 3.6%) i/s -    285.000 in   5.074688s
1 thread Custom.subclasses with 1000 singletons
                          5.588 (± 0.0%) i/s -     28.000 in   5.021174s
```

After:
```
1 thread Numeric.subclasses
                          2.402k (± 3.4%) i/s -     11.990k in   4.998757s
1 thread Object.subclasses
                        189.977 (± 6.8%) i/s -    954.000 in   5.068134s
1 thread Custom.subclasses with many singletons
                          3.439k (± 1.0%) i/s -     17.334k in   5.040639s
```
@headius headius added this to the JRuby 9.4.10.0 milestone Dec 3, 2024
@headius headius requested a review from enebo December 3, 2024 21:40
@headius headius force-pushed the optimize_subclasses_walk branch from c1e9844 to 470dd56 Compare December 3, 2024 21:42
@headius headius merged commit 9d63c22 into jruby:master Dec 4, 2024
@headius headius deleted the optimize_subclasses_walk branch December 4, 2024 01:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant