Skip to content

Commit 4ed331b

Browse files
committed
Optimize code loading and trigger const_added callback only if it's defined
1 parent fbb9154 commit 4ed331b

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/main/java/org/truffleruby/RubyContext.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ public final class RubyContext {
159159
public long nativeArgv = 0L;
160160
public long nativeArgvLength = -1L;
161161

162+
// Whether or not a custom Module.const_added callback was defined.
163+
// Optimization to avoid calling a default (empty) callback if there
164+
// are no user-defined callbacks.
165+
private volatile boolean constAddedDefined = false;
166+
162167
private final AssumedValue<Boolean> warningCategoryDeprecated;
163168
private final AssumedValue<Boolean> warningCategoryExperimental;
164169

@@ -444,7 +449,7 @@ private TruffleNFIPlatform createNativePlatform() {
444449

445450
@TruffleBoundary
446451
public static Object send(Node currentNode, Object receiver, String methodName, Object... arguments) {
447-
if (currentNode.isAdoptable()) {
452+
if (currentNode != null && currentNode.isAdoptable()) {
448453
final EncapsulatingNodeReference callNodeRef = EncapsulatingNodeReference.getCurrent();
449454
final Node prev = callNodeRef.set(currentNode);
450455
try {
@@ -779,4 +784,12 @@ public void initializeMainScriptName(String mainScriptName) {
779784
public ImmutableRubyString getMainScriptName() {
780785
return this.mainScriptName;
781786
}
787+
788+
public boolean isConstAddedEverDefined() {
789+
return constAddedDefined;
790+
}
791+
792+
public void constAddedIsDefined() {
793+
constAddedDefined = true;
794+
}
782795
}

src/main/java/org/truffleruby/core/module/ModuleFields.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.truffleruby.RubyLanguage;
3131
import org.truffleruby.collections.ConcurrentOperations;
3232
import org.truffleruby.collections.ConcurrentWeakSet;
33-
import org.truffleruby.core.CoreLibrary;
3433
import org.truffleruby.core.encoding.Encodings;
3534
import org.truffleruby.core.encoding.TStringUtils;
3635
import org.truffleruby.core.kernel.KernelNodes;
@@ -496,8 +495,7 @@ private RubyConstant setConstantInternal(RubyContext context, Node currentNode,
496495
invalidateConstantIncludedBy(name);
497496
}
498497

499-
final CoreLibrary coreLibrary = context.getCoreLibrary();
500-
if (currentNode != null && coreLibrary != null && coreLibrary.isLoaded()) {
498+
if (context.isConstAddedEverDefined()) {
501499
final RubySymbol nameSymbol = context.getLanguageSlow().getSymbol(name);
502500
RubyContext.send(currentNode, rubyModule, "const_added", nameSymbol);
503501
}
@@ -579,6 +577,11 @@ public void addMethod(RubyContext context, Node currentNode, InternalMethod meth
579577
}
580578
}
581579
}
580+
581+
// track if ever a custom Module.const_add callback is defined and ignore a default one
582+
if (context.getCoreLibrary().isLoaded() && method.getName().equals("const_added")) {
583+
RubyLanguage.getCurrentContext().constAddedIsDefined();
584+
}
582585
}
583586

584587
@TruffleBoundary

0 commit comments

Comments
 (0)