Commit 0d39d52
committed
Optimize Class#subclasses
There were several issues with the old implementation:
* Iterating by creating a Set and an Iterator adds significant
allocation overhead. Better to use Map#forEach.
* ConcurrentWeakHashMap is a very old implementation that has
several inefficiencies as well as no custom implementation of
Map#forEach (it falls back on the default impl which creates a
Set and Iterator). Instead we use a synchronized WeakHashMap.
* The initial array was created with a default size of 4 elements.
For many classes this will be insufficient, leading to excessive
array allocation and copying as it grows. Instead we use the
current class's "subclasses" size as an initial estimate.
* When no subclasses appear to be present, bail out early with
an empty array that does minimal allocation.
Performance is significantly better for zero, small, and large
subclass lists.
1M * Object.subclasses, with 83 elements:
Before:
2.020000 0.040000 2.060000 ( 1.823539)
1.980000 0.030000 2.010000 ( 1.737808)
1.830000 0.010000 1.840000 ( 1.738772)
1.730000 0.020000 1.750000 ( 1.740675)
1.720000 0.000000 1.720000 ( 1.707344)
After:
1.340000 0.020000 1.360000 ( 1.034745)
0.930000 0.000000 0.930000 ( 0.918107)
0.920000 0.010000 0.930000 ( 0.914828)
0.920000 0.000000 0.920000 ( 0.922137)
0.920000 0.000000 0.920000 ( 0.915440)
10M * Numeric.subclasses, with 4 elements:
Before:
0.930000 0.030000 0.960000 ( 0.789997)
0.640000 0.010000 0.650000 ( 0.621404)
0.620000 0.010000 0.630000 ( 0.614404)
0.630000 0.000000 0.630000 ( 0.629492)
0.630000 0.010000 0.640000 ( 0.608538)
After:
0.720000 0.010000 0.730000 ( 0.559470)
0.460000 0.000000 0.460000 ( 0.454176)
0.510000 0.000000 0.510000 ( 0.429875)
0.430000 0.010000 0.440000 ( 0.434487)
0.430000 0.000000 0.430000 ( 0.427971)
Fixes jruby#84571 parent 8b022a9 commit 0d39d52
1 file changed
+19
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
96 | | - | |
| 96 | + | |
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| |||
1019 | 1019 | | |
1020 | 1020 | | |
1021 | 1021 | | |
1022 | | - | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
1023 | 1030 | | |
1024 | 1031 | | |
1025 | 1032 | | |
| |||
1081 | 1088 | | |
1082 | 1089 | | |
1083 | 1090 | | |
1084 | | - | |
| 1091 | + | |
1085 | 1092 | | |
1086 | 1093 | | |
1087 | | - | |
1088 | | - | |
1089 | | - | |
1090 | | - | |
1091 | | - | |
1092 | | - | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
1093 | 1101 | | |
1094 | | - | |
1095 | | - | |
| 1102 | + | |
1096 | 1103 | | |
1097 | 1104 | | |
1098 | 1105 | | |
| |||
1112 | 1119 | | |
1113 | 1120 | | |
1114 | 1121 | | |
1115 | | - | |
| 1122 | + | |
1116 | 1123 | | |
1117 | 1124 | | |
1118 | 1125 | | |
| |||
0 commit comments