Skip to content

Commit

Permalink
Lazy initialize volatile fields
Browse files Browse the repository at this point in the history
Eagerly initializing these adds memory churn to the allocation of
many RubyModule types that will never even access these fields.
Instead we lazily initialize them using an atomic update.
  • Loading branch information
headius committed Feb 11, 2025
1 parent 521d9dd commit 777c69e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 116 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/IncludedModuleWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public IncludedModuleWrapper(Ruby runtime, RubyClass superClass, RubyModule orig
public IncludedModuleWrapper(Ruby runtime, RubyClass superClass, RubyModule origin, RubyModule methodsHolder) {
super(runtime, superClass, origin);
origin.addIncludingHierarchy(this);
// force writeable methods table so we see future updates
methods = methodsHolder.getMethodsForWrite();
}

Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/org/jruby/PrependedModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ public PrependedModule(Ruby runtime, RubyClass superClass, RubyModule prependedC
classIndex(superClass.getClassIndex()); // use same ClassIndex as metaclass, since we're technically still of that type
}
this.methods = prependedClass.methods;
prependedClass.methods = Collections.EMPTY_MAP;
prependedClass.methods = null;
prependedClass.methodLocation = this;

Map<String, DynamicMethod> methods = getMethods();
for (Map.Entry<String, DynamicMethod> entry : methods.entrySet()) {
DynamicMethod method = entry.getValue();
if (moveRefinedMethod(entry.getKey(), method, prependedClass)) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@ void addInvalidatorsAndFlush(InvalidatorList invalidators) {
invalidators.add(methodInvalidator);

// if we're not at boot time, don't bother fully clearing caches
if (!runtime.isBootingCore()) cachedMethods.clear();
if (!runtime.isBootingCore()) getCachedMethods().clear();

getSubclassesForRead().forEachClass(invalidators);
}
Expand Down
Loading

0 comments on commit 777c69e

Please sign in to comment.