Skip to content

Commit

Permalink
Refactor KeywordArgumentsDescriptorManager.EMPTY and move it to Keywo…
Browse files Browse the repository at this point in the history
…rdArgumentsDescriptor
  • Loading branch information
andrykonchin committed Oct 8, 2024
1 parent f9b3036 commit f0370c2
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/main/java/org/truffleruby/cext/CExtNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
import org.truffleruby.language.arguments.ArgumentsDescriptor;
import org.truffleruby.language.arguments.NoKeywordArgumentsDescriptor;
import org.truffleruby.language.arguments.KeywordArgumentsDescriptor;
import org.truffleruby.language.arguments.KeywordArgumentsDescriptorManager;
import org.truffleruby.language.arguments.RubyArguments;
import org.truffleruby.language.backtrace.Backtrace;
import org.truffleruby.language.constants.GetConstantNode;
Expand Down Expand Up @@ -396,7 +395,7 @@ Object sendWithoutCExtLock(VirtualFrame frame, Object receiver, RubySymbol metho
dispatchNode, PRIVATE, ownedProfile);
} else {
return sendWithoutCExtLock(frame, receiver, method, block,
KeywordArgumentsDescriptorManager.EMPTY, args,
KeywordArgumentsDescriptor.EMPTY, args,
dispatchNode, PRIVATE, ownedProfile);
}
}
Expand Down Expand Up @@ -434,7 +433,7 @@ Object sendWithoutCExtLock(VirtualFrame frame, Object receiver, RubySymbol metho
dispatchNode, PUBLIC, ownedProfile);
} else {
return sendWithoutCExtLock(frame, receiver, method, block,
KeywordArgumentsDescriptorManager.EMPTY, args,
KeywordArgumentsDescriptor.EMPTY, args,
dispatchNode, PUBLIC, ownedProfile);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* arguments were passed. The callee must handle that for now. */
public final class KeywordArgumentsDescriptor extends ArgumentsDescriptor {

public static final KeywordArgumentsDescriptor EMPTY = new KeywordArgumentsDescriptor(
StringUtils.EMPTY_STRING_ARRAY);

@CompilationFinal(dimensions = 1) private final String[] keywords;

/** Use {@link KeywordArgumentsDescriptorManager} to get an instance. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@

public final class KeywordArgumentsDescriptorManager {

public static final KeywordArgumentsDescriptor EMPTY = new KeywordArgumentsDescriptor(
StringUtils.EMPTY_STRING_ARRAY);

private final WeakValueCache<Key, KeywordArgumentsDescriptor> CANONICAL_KEYWORD_DESCRIPTORS = new WeakValueCache<>();

public KeywordArgumentsDescriptorManager() {
CANONICAL_KEYWORD_DESCRIPTORS.put(new Key(StringUtils.EMPTY_STRING_ARRAY), EMPTY);
CANONICAL_KEYWORD_DESCRIPTORS.put(new Key(StringUtils.EMPTY_STRING_ARRAY), KeywordArgumentsDescriptor.EMPTY);
}

public KeywordArgumentsDescriptor getArgumentsDescriptor(String[] keywords) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.truffleruby.language.RubyRootNode;
import org.truffleruby.language.SpecialVariablesSendingNode;
import org.truffleruby.language.arguments.ArgumentsDescriptor;
import org.truffleruby.language.arguments.KeywordArgumentsDescriptor;
import org.truffleruby.language.arguments.NoKeywordArgumentsDescriptor;
import org.truffleruby.language.arguments.KeywordArgumentsDescriptorManager;
import org.truffleruby.language.arguments.RubyArguments;
import org.truffleruby.language.methods.CallInternalMethodNode;
import org.truffleruby.language.methods.InternalMethod;
Expand Down Expand Up @@ -149,7 +149,7 @@ public Object callWithKeywords(Object receiver, String method, Object arg1,
final Object[] rubyArgs = RubyArguments.allocate(2);
RubyArguments.setSelf(rubyArgs, receiver);
RubyArguments.setBlock(rubyArgs, nil);
RubyArguments.setDescriptor(rubyArgs, KeywordArgumentsDescriptorManager.EMPTY);
RubyArguments.setDescriptor(rubyArgs, KeywordArgumentsDescriptor.EMPTY);
RubyArguments.setArgument(rubyArgs, 0, arg1);
RubyArguments.setArgument(rubyArgs, 1, keywords);
return execute(null, receiver, method, rubyArgs, PRIVATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import org.truffleruby.language.arguments.NoKeywordArgumentsDescriptor;
import org.truffleruby.language.arguments.KeywordArgumentsDescriptor;
import org.truffleruby.language.arguments.KeywordArgumentsDescriptorManager;

/** A literal call site in Ruby code: one of foo(), super or yield. */
public abstract class LiteralCallNode extends RubyContextSourceNode {
Expand Down Expand Up @@ -58,7 +57,7 @@ protected ArgumentsDescriptor getArgumentsDescriptorAndCheckRuby2KeywordsHash(Ob
RubyHash hash = (RubyHash) lastArgument;
if (hash.ruby2_keywords) { // both branches profiled
copyRuby2KeywordsHashBoundary(args, hash);
return KeywordArgumentsDescriptorManager.EMPTY;
return KeywordArgumentsDescriptor.EMPTY;
} else {
if (!notRuby2KeywordsHashProfile) {
CompilerDirectives.transferToInterpreterAndInvalidate();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/truffleruby/parser/YARPTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
import org.truffleruby.language.RubyRootNode;
import org.truffleruby.language.RubyTopLevelRootNode;
import org.truffleruby.language.arguments.ArgumentsDescriptor;
import org.truffleruby.language.arguments.KeywordArgumentsDescriptorManager;
import org.truffleruby.language.arguments.KeywordArgumentsDescriptor;
import org.truffleruby.language.arguments.NoKeywordArgumentsDescriptor;
import org.truffleruby.language.arguments.ProfileArgumentNodeGen;
import org.truffleruby.language.arguments.ReadSelfNode;
Expand Down Expand Up @@ -809,7 +809,7 @@ private ArgumentsDescriptor getKeywordArgumentsDescriptor(Nodes.Node[] arguments
Nodes.Node last = ArrayUtils.getLast(arguments);
// if `...` then we know there are no literal keyword arguments, only **kwrest
if (last instanceof Nodes.ForwardingArgumentsNode) {
return KeywordArgumentsDescriptorManager.EMPTY;
return KeywordArgumentsDescriptor.EMPTY;
}
// if there are no keywords the descriptor is NoKeywordArgumentsDescriptor.INSTANCE
if (!(last instanceof Nodes.KeywordHashNode keywords)) {
Expand Down Expand Up @@ -1826,7 +1826,7 @@ public RubyNode visitForwardingSuperNode(Nodes.ForwardingSuperNode node) {
final RubyNode[] reloadSequence = reloadTranslator.reload(parametersNode);

var descriptor = (parametersNode.keywords.length > 0 || parametersNode.keyword_rest != null)
? KeywordArgumentsDescriptorManager.EMPTY
? KeywordArgumentsDescriptor.EMPTY
: NoKeywordArgumentsDescriptor.INSTANCE;
final int restParamIndex = reloadTranslator.getRestParameterIndex();
final RubyNode arguments = new ReadZSuperArgumentsNode(restParamIndex, reloadSequence);
Expand Down

0 comments on commit f0370c2

Please sign in to comment.