Skip to content

Commit

Permalink
Use a record to hold the compound symbol pair
Browse files Browse the repository at this point in the history
Records have optimization characteristics that may be helpful
here.
  • Loading branch information
headius committed Mar 28, 2024
1 parent 4d4f7f5 commit 711ceb5
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions core/src/main/java/org/jruby/RubySymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ public static RubySymbol newHardSymbol(Ruby runtime, String name) {
}

public static RubySymbol newMethodSymbolFromCompound(Ruby runtime, String compoundName) {
return runtime.getSymbolTable().getCompoundSymbol(compoundName).getKey();
return runtime.getSymbolTable().getMethodSymbolFromCompound(compoundName);
}

public static RubySymbol newCalleeSymbolFromCompound(Ruby runtime, String compoundName) {
return runtime.getSymbolTable().getCompoundSymbol(compoundName).getValue();
return runtime.getSymbolTable().getCalleeSymbolFromCompound(compoundName);
}

/**
Expand Down Expand Up @@ -990,7 +990,7 @@ public static final class SymbolTable {

private final ReentrantLock tableLock = new ReentrantLock();
private volatile SymbolEntry[] symbolTable;
private Map<String, AbstractMap.SimpleImmutableEntry<RubySymbol, RubySymbol>> compoundSymbolTable = new ConcurrentHashMap<>();
private Map<String, CompoundSymbol> compoundSymbolTable = new ConcurrentHashMap<>();
private int size;
private int threshold;
private final float loadFactor;
Expand Down Expand Up @@ -1105,6 +1105,29 @@ public RubySymbol getSymbol(ByteList bytes, ObjBooleanConsumer<RubySymbol> handl
return symbol;
}

/**
* Get the method name symbol from a compound name.
*
* @see #getCompoundSymbol(String)
* @param compoundName the compound name
* @return the method component of the compound name, as a symbol
*/
public RubySymbol getMethodSymbolFromCompound(String compoundName) {
return getCompoundSymbol(compoundName).method;
}


/**
* Get the callee name symbol from a compound name.
*
* @see #getCompoundSymbol(String)
* @param compoundName the compound name
* @return the callee component of the compound name, as a symbol
*/
public RubySymbol getCalleeSymbolFromCompound(String compoundName) {
return getCompoundSymbol(compoundName).callee;
}

/**
* Get a pair of symbols associated with the given compound method name, used by aliases to pass both the callee
* name and the original method name on the stack. This avoids re-parsing the compoundName and constructing new
Expand All @@ -1113,13 +1136,15 @@ public RubySymbol getSymbol(ByteList bytes, ObjBooleanConsumer<RubySymbol> handl
* @param compoundName the compound name used for a combination of alias and method
* @return a Map.Entry representing the __method__ and __callee__ symbols for that compound name as key and value
*/
public Map.Entry<RubySymbol, RubySymbol> getCompoundSymbol(String compoundName) {
private CompoundSymbol getCompoundSymbol(String compoundName) {
return compoundSymbolTable.computeIfAbsent(compoundName, (cname) ->
new AbstractMap.SimpleImmutableEntry(
new CompoundSymbol(
getSymbol(Helpers.getSuperNameFromCompositeName(cname), true),
getSymbol(Helpers.getCalleeNameFromCompositeName(cname), true)));
}

private record CompoundSymbol(RubySymbol method, RubySymbol callee){}

private RubySymbol findSymbol(ByteList bytes, int hash, boolean hard) {
RubySymbol symbol = null;

Expand Down

0 comments on commit 711ceb5

Please sign in to comment.