Skip to content

Commit 09bcd0d

Browse files
committed
Avoid size calculation for subclass list
Getting this accurate is not important, but most weak collections will attempt to get an accurate count at increased expense. This patch just saves the last list size and uses that for future list allocations.
1 parent 0d39d52 commit 09bcd0d

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

core/src/main/java/org/jruby/RubyClass.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,14 +1019,9 @@ protected void setModuleSuperClass(RubyClass superClass) {
10191019

10201020
@JRubyMethod
10211021
public IRubyObject subclasses(ThreadContext context) {
1022-
Map<RubyClass, Object> subclasses = this.subclasses;
1023-
int sizeEstimate = subclasses == null ? 0 : subclasses.size();
1024-
1025-
if (sizeEstimate == 0) {
1026-
return RubyArray.newEmptyArray(context.runtime);
1027-
}
1022+
int subclassEstimate = this.subclassEstimate;
10281023

1029-
RubyArray<RubyClass> subs = RubyArray.newArray(context.runtime, sizeEstimate);
1024+
RubyArray<RubyClass> subs = RubyArray.newArray(context.runtime, subclassEstimate == -1 ? 4 : subclassEstimate);
10301025

10311026
concreteSubclasses(subs);
10321027

@@ -3093,6 +3088,7 @@ public IRubyObject invokeFrom(ThreadContext context, CallType callType, IRubyObj
30933088
private ObjectAllocator allocator; // the default allocator
30943089
protected ObjectMarshal marshal;
30953090
private volatile Map<RubyClass, Object> subclasses;
3091+
private int subclassEstimate = -1;
30963092
public static final int CS_IDX_INITIALIZE = 0;
30973093
public enum CS_NAMES {
30983094
INITIALIZE("initialize");

0 commit comments

Comments
 (0)