Skip to content

Commit

Permalink
Remove context from coverage in translator
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed Mar 23, 2021
1 parent 8d44fc7 commit 98a1c67
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
24 changes: 20 additions & 4 deletions src/main/java/org/truffleruby/RubyLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;

Expand Down Expand Up @@ -148,9 +149,10 @@ public final class RubyLanguage extends TruffleLanguage<RubyContext> {

private final CyclicAssumption tracingCyclicAssumption = new CyclicAssumption("object-space-tracing");
@CompilationFinal private volatile Assumption tracingAssumption = tracingCyclicAssumption.getAssumption();
public final Assumption singleContextAssumption = Truffle
.getRuntime()
.createAssumption("single RubyContext per RubyLanguage instance");

@CompilationFinal public boolean singleContext = true;
@CompilationFinal public Optional<RubyContext> contextIfSingleContext;

public final CyclicAssumption traceFuncUnusedAssumption = new CyclicAssumption("set_trace_func is not used");

private final ReentrantLock safepointLock = new ReentrantLock();
Expand Down Expand Up @@ -305,7 +307,13 @@ public void resetSafepointAssumption() {
protected void initializeMultipleContexts() {
// TODO Make Symbol.all_symbols per context, by having a SymbolTable per context and creating new symbols with
// the per-language SymbolTable.
singleContextAssumption.invalidate();

if (contextIfSingleContext == null) { // before first context created
this.singleContext = false;
} else {
throw CompilerDirectives.shouldNotReachHere("RubyLanguage#initializeMultipleContexts() called after" +
" context created and areOptionsCompatible() returned false");
}
}

@Override
Expand All @@ -330,6 +338,11 @@ public RubyContext createContext(Env env) {
// TODO CS 3-Dec-16 need to parse RUBYOPT here if it hasn't been already?
final RubyContext context = new RubyContext(this, env);
Metrics.printTime("after-create-context");
if (singleContext) {
contextIfSingleContext = Optional.of(context);
} else {
contextIfSingleContext = Optional.empty();
}
return context;
}

Expand Down Expand Up @@ -544,6 +557,9 @@ private static Shape createShape(Class<? extends RubyDynamicObject> layoutClass)

@Override
protected boolean areOptionsCompatible(OptionValues firstOptions, OptionValues newOptions) {
if (singleContext) {
return false;
}
return LanguageOptions.areOptionsCompatible(firstOptions, newOptions);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/truffleruby/parser/BodyTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3089,7 +3089,7 @@ private RubyNode addNewlineIfNeeded(ParseNode jrubyNode, RubyNode node) {
return node;
}

if (language.singleContextAssumption.isValid() && context.getCoverageManager().isEnabled()) {
if (environment.getParseEnvironment().isCoverageEnabled()) {
node.unsafeSetIsCoverageLine();
context.getCoverageManager().setLineHasCode(source, current.toSourceSection(source).getStartLine());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/truffleruby/parser/MethodTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public MethodTranslator(
this.argsNode = argsNode;
this.methodNameForBlock = methodNameForBlock;

if (parserContext == ParserContext.EVAL || context.getCoverageManager().isEnabled()) {
if (parserContext == ParserContext.EVAL || environment.getParseEnvironment().isCoverageEnabled()) {
shouldLazyTranslate = false;
} else if (language.getSourcePath(source).startsWith(language.coreLoadPath)) {
shouldLazyTranslate = language.options.LAZY_TRANSLATION_CORE;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/truffleruby/parser/ParseEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ public class ParseEnvironment {
private boolean dynamicConstantLookup = false;
public boolean allowTruffleRubyPrimitives = false;
private final String corePath;
private final boolean coverageEnabled;

public ParseEnvironment(RubyLanguage language) {
corePath = language.corePath;
coverageEnabled = language.contextIfSingleContext.map(c -> c.getCoverageManager().isEnabled()).orElse(false);
}

public String getCorePath() {
Expand All @@ -40,6 +42,11 @@ public LexicalScope getLexicalScope() {
return lexicalScope;
}

/** Returns false if the AST is shared */
public boolean isCoverageEnabled() {
return coverageEnabled;
}

public LexicalScope pushLexicalScope() {
return lexicalScope = new LexicalScope(getLexicalScope());
}
Expand Down

0 comments on commit 98a1c67

Please sign in to comment.