Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PERFORMANCE: Intern Multiple String Constants #4754

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 19 additions & 20 deletions core/src/main/java/org/jruby/RubyClass.java
Expand Up @@ -30,21 +30,6 @@
***** END LICENSE BLOCK *****/
package org.jruby;

import org.jruby.javasupport.JavaClass;
import org.jruby.runtime.Arity;
import org.jruby.runtime.JavaSites;
import org.jruby.runtime.callsite.CachingCallSite;
import org.jruby.runtime.callsite.RespondToCallSite;
import org.jruby.runtime.ivars.VariableAccessor;
import static org.jruby.util.CodegenUtils.ci;
import static org.jruby.util.CodegenUtils.p;
import static org.jruby.util.CodegenUtils.sig;
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ACC_STATIC;
import static org.objectweb.asm.Opcodes.ACC_SUPER;
import static org.objectweb.asm.Opcodes.ACC_VARARGS;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -60,7 +45,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.compiler.impl.SkinnyMethodAdapter;
Expand All @@ -69,35 +53,50 @@
import org.jruby.java.codegen.RealClassGenerator;
import org.jruby.java.codegen.Reified;
import org.jruby.javasupport.Java;
import org.jruby.runtime.Helpers;
import org.jruby.javasupport.JavaClass;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.CallSite;
import org.jruby.runtime.CallType;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.JavaSites;
import org.jruby.runtime.MethodIndex;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ObjectMarshal;
import org.jruby.runtime.ThreadContext;
import static org.jruby.runtime.Visibility.*;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.callsite.CacheEntry;
import org.jruby.runtime.callsite.CachingCallSite;
import org.jruby.runtime.callsite.RespondToCallSite;
import org.jruby.runtime.ivars.VariableAccessor;
import org.jruby.runtime.ivars.VariableAccessorField;
import org.jruby.runtime.ivars.VariableTableManager;
import org.jruby.runtime.marshal.MarshalStream;
import org.jruby.runtime.marshal.UnmarshalStream;
import org.jruby.runtime.opto.Invalidator;
import org.jruby.util.ArraySupport;
import org.jruby.util.OneShotClassLoader;
import org.jruby.util.ClassDefiningClassLoader;
import org.jruby.util.CodegenUtils;
import org.jruby.util.JavaNameMangler;
import org.jruby.util.OneShotClassLoader;
import org.jruby.util.collections.WeakHashSet;
import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;

import static org.jruby.runtime.Visibility.PRIVATE;
import static org.jruby.util.CodegenUtils.ci;
import static org.jruby.util.CodegenUtils.p;
import static org.jruby.util.CodegenUtils.sig;
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ACC_STATIC;
import static org.objectweb.asm.Opcodes.ACC_SUPER;
import static org.objectweb.asm.Opcodes.ACC_VARARGS;

/**
*
* @author jpetersen
Expand Down Expand Up @@ -319,7 +318,7 @@ public String[] getVariableNames() {
}

public Map<String, VariableAccessor> getVariableTableCopy() {
return new HashMap<String, VariableAccessor>(getVariableAccessorsForRead());
return new HashMap<>(getVariableAccessorsForRead());
}

@Override
Expand Down
19 changes: 4 additions & 15 deletions core/src/main/java/org/jruby/RubyModule.java
Expand Up @@ -519,7 +519,7 @@ public String getBaseName() {
* @param name the new base name of the class
*/
public void setBaseName(String name) {
baseName = name;
baseName = name.intern();
cachedName = null;
}

Expand Down Expand Up @@ -1599,6 +1599,8 @@ public void addModuleFunction(String name, DynamicMethod method) {
*/
public synchronized void defineAlias(String name, String oldName) {
testFrozen("module");
name = name.intern();
oldName = oldName.intern();
if (oldName.equals(name)) return;

DynamicMethod method = searchForAliasMethod(getRuntime(), oldName);
Expand All @@ -1609,20 +1611,6 @@ public synchronized void defineAlias(String name, String oldName) {
methodLocation.invalidateCacheDescendants();
}

public synchronized void defineAliases(List<String> aliases, String oldName) {
testFrozen("module");
DynamicMethod method = searchForAliasMethod(getRuntime(), oldName);

for (String name: aliases) {
if (oldName.equals(name)) continue;

putMethod(name, new AliasMethod(this, method, oldName));
}

methodLocation.invalidateCoreClasses();
methodLocation.invalidateCacheDescendants();
}

private DynamicMethod searchForAliasMethod(Ruby runtime, String name) {
DynamicMethod method = deepMethodSearch(name, runtime);

Expand Down Expand Up @@ -3848,6 +3836,7 @@ public IRubyObject setConstant(String name, IRubyObject value, boolean hidden) {
* @return The result of setting the variable.
*/
private IRubyObject setConstantCommon(String name, IRubyObject value, boolean hidden, boolean warn) {
name = name.intern();
IRubyObject oldValue = fetchConstant(name);

setParentForModule(name, value);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/ArgumentNode.java
Expand Up @@ -93,7 +93,7 @@ public int getIndex() {
}

public String getName() {
return StringSupport.byteListAsString(identifier);
return StringSupport.byteListAsString(identifier).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/AttrAssignNode.java
Expand Up @@ -83,7 +83,7 @@ public Object accept(NodeVisitor visitor) {
* @return name
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/BlockArgNode.java
Expand Up @@ -88,7 +88,7 @@ public int getCount() {
* @return it's name
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/CallNode.java
Expand Up @@ -124,7 +124,7 @@ public Node setArgsNode(Node argsNode) {
* @return name
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/ClassVarAsgnNode.java
Expand Up @@ -77,7 +77,7 @@ public <T> T accept(NodeVisitor<T> iVisitor) {
* @return Returns a String
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/ClassVarDeclNode.java
Expand Up @@ -75,7 +75,7 @@ public <T> T accept(NodeVisitor<T> iVisitor) {
* @return Returns a String
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/ClassVarNode.java
Expand Up @@ -72,7 +72,7 @@ public <T> T accept(NodeVisitor<T> iVisitor) {
* @return Returns a String
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/Colon3Node.java
Expand Up @@ -77,7 +77,7 @@ public <T> T accept(NodeVisitor<T> iVisitor) {
* @return Returns a String
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/ConstDeclNode.java
Expand Up @@ -77,7 +77,7 @@ public <T> T accept(NodeVisitor<T> iVisitor) {
* @return name
*/
public String getName() {
return name == null ? constNode.getName() : StringSupport.byteListAsString(name);
return name == null ? constNode.getName() : StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/ast/ConstNode.java
Expand Up @@ -42,7 +42,7 @@
/**
* The access to a Constant.
*/
public class ConstNode extends Node implements INameNode {
public final class ConstNode extends Node implements INameNode {
private ByteList name;

public ConstNode(ISourcePosition position, ByteList name) {
Expand Down Expand Up @@ -72,7 +72,7 @@ public <T> T accept(NodeVisitor<T> iVisitor) {
* @return Returns a String
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/DAsgnNode.java
Expand Up @@ -78,7 +78,7 @@ public <T> T accept(NodeVisitor<T> iVisitor) {
* @return Returns a String
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/DVarNode.java
Expand Up @@ -97,7 +97,7 @@ public int getIndex() {
* @return Returns a String
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/FCallNode.java
Expand Up @@ -119,7 +119,7 @@ public Node setArgsNode(Node argsNode) {
* @return Returns a String
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/GlobalAsgnNode.java
Expand Up @@ -75,7 +75,7 @@ public <T> T accept(NodeVisitor<T> iVisitor) {
* @return Returns a String
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/InstAsgnNode.java
Expand Up @@ -78,7 +78,7 @@ public <T> T accept(NodeVisitor<T> iVisitor) {
* @return Returns a String
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/LocalAsgnNode.java
Expand Up @@ -78,7 +78,7 @@ public <T> T accept(NodeVisitor<T> iVisitor) {
* Name of the local assignment.
**/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/MethodDefNode.java
Expand Up @@ -89,7 +89,7 @@ public Node getBodyNode() {
* @return Returns a String
*/
public String getName() {
return StringSupport.byteListAsString(name);
return StringSupport.byteListAsString(name).intern();
}

public ByteList getByteName() {
Expand Down
Expand Up @@ -31,7 +31,6 @@
package org.jruby.internal.runtime.methods;

import java.lang.reflect.Method;
import java.util.Arrays;
import org.jruby.MetaClass;
import org.jruby.PrependedModule;
import org.jruby.RubyModule;
Expand Down Expand Up @@ -497,7 +496,7 @@ public String getName() {
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
this.name = name.intern();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/ir/IRScope.java
Expand Up @@ -340,7 +340,7 @@ public String getName() {
}

public void setName(String name) { // This is for IRClosure and IRMethod ;(
this.name = name;
this.name = name.intern();
}

public void setFileName(String filename) {
Expand Down Expand Up @@ -1101,6 +1101,7 @@ public List<IRClosure> getEndBlocks() {

// Enebo: We should just make n primitive int and not take the hash hit
protected int allocateNextPrefixedName(String prefix) {
prefix = prefix.intern();
int index = getPrefixCountSize(prefix);

nextVarIndex.put(prefix, index + 1);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ir/instructions/CallBase.java
Expand Up @@ -43,7 +43,7 @@ protected CallBase(Operation op, CallType callType, String name, Operand receive
this.callSiteId = callSiteCounter++;
argsCount = args.length;
hasClosure = closure != null;
this.name = name;
this.name = name.intern();
this.callType = callType;
this.callSite = getCallSiteFor(callType, name, potentiallyRefined);
splatMap = IRRuntimeHelpers.buildSplatMap(args);
Expand Down
Expand Up @@ -20,7 +20,7 @@
// - looks up lexical scopes
// - then inheritance hierarchy if lexical search fails
// - then invokes const_missing if inheritance search fails
public class SearchConstInstr extends OneOperandResultBaseInstr implements FixedArityInstr {
public final class SearchConstInstr extends OneOperandResultBaseInstr implements FixedArityInstr {
private final String constName;
private final boolean noPrivateConsts;

Expand Down Expand Up @@ -57,9 +57,9 @@ public Instr clone(CloneInfo ii) {
@Override
public void encode(IRWriterEncoder e) {
super.encode(e);
e.encode(getConstName());
e.encode(constName);
e.encode(getStartingScope());
e.encode(isNoPrivateConsts());
e.encode(noPrivateConsts);
}

public static SearchConstInstr decode(IRReaderDecoder d) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/ir/operands/Label.java
Expand Up @@ -27,7 +27,7 @@ public class Label extends Operand {
public Label(String prefix, int id) {
super();

this.prefix = prefix;
this.prefix = prefix.intern();
this.id = id;
}

Expand Down Expand Up @@ -101,7 +101,7 @@ public static Label decode(IRReaderDecoder d) {
// Check if this label was already created
// Important! Program would not be interpreted correctly
// if new name will be created every time
String fullLabel = prefix + '_' + id;
final String fullLabel = (prefix + '_' + id).intern();
if (d.getVars().containsKey(fullLabel)) {
return (Label) d.getVars().get(fullLabel);
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ir/operands/Reference.java
Expand Up @@ -13,7 +13,7 @@ public abstract class Reference extends Operand {
public Reference(String name) {
super();

this.name = name;
this.name = name.intern();
}

public String getName() {
Expand Down