Skip to content

Commit

Permalink
Use polymorphism for Operand.getOperandType.
Browse files Browse the repository at this point in the history
getOperandType is only used by the persistence engine, so there's
no impact to interpreter performance by doing this, and for a
startup-interpreted 'rails c' this reduces memory use by over 5MB.
headius committed Aug 5, 2015
1 parent b3bc8d5 commit ce91559
Showing 41 changed files with 231 additions and 65 deletions.
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Array.java
Original file line number Diff line number Diff line change
@@ -26,11 +26,16 @@ public Array() {
}

public Array(Operand[] elts) {
super(OperandType.ARRAY);
super();

this.elts = elts == null ? EMPTY_ARRAY : elts;
}

@Override
public OperandType getOperandType() {
return OperandType.ARRAY;
}

public boolean isBlank() {
return elts.length == 0;
}
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/AsString.java
Original file line number Diff line number Diff line change
@@ -16,11 +16,16 @@ public class AsString extends Operand {
final private Operand source;

public AsString(Operand source) {
super(OperandType.AS_STRING);
super();

this.source = source == null ? StringLiteral.EMPTY_STRING : source;
}

@Override
public OperandType getOperandType() {
return OperandType.AS_STRING;
}

@Override
public Object retrieve(ThreadContext context, IRubyObject self, StaticScope currScope, DynamicScope currDynScope, Object[] temp) {
return ((IRubyObject) source.retrieve(context, self, currScope, currDynScope, temp)).asString();
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Backref.java
Original file line number Diff line number Diff line change
@@ -17,10 +17,15 @@ public class Backref extends Reference {
final public char type;

public Backref(char t) {
super(OperandType.BACKREF, "$" + t);
super("$" + t);
type = t;
}

@Override
public OperandType getOperandType() {
return OperandType.BACKREF;
}

public Object retrieve(ThreadContext context, IRubyObject self, StaticScope currScope, DynamicScope currDynScope, Object[] temp) {
IRubyObject backref = context.getBackRef();

7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Bignum.java
Original file line number Diff line number Diff line change
@@ -32,10 +32,15 @@ public class Bignum extends ImmutableLiteral {
final public BigInteger value;

public Bignum(BigInteger value) {
super(OperandType.BIGNUM);
super();
this.value = value;
}

@Override
public OperandType getOperandType() {
return OperandType.BIGNUM;
}

@Override
public Object createCacheObject(ThreadContext context) {
return RubyBignum.newBignum(context.runtime, value);
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Boolean.java
Original file line number Diff line number Diff line change
@@ -9,11 +9,16 @@ public class Boolean extends ImmutableLiteral {
private final boolean truthy;

public Boolean(boolean truthy) {
super(OperandType.BOOLEAN);
super();

this.truthy = truthy;
}

@Override
public OperandType getOperandType() {
return OperandType.BOOLEAN;
}

@Override
public Object createCacheObject(ThreadContext context) {
return context.runtime.newBoolean(isTrue());
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Complex.java
Original file line number Diff line number Diff line change
@@ -13,11 +13,16 @@ public class Complex extends ImmutableLiteral {
private final ImmutableLiteral number;

public Complex(ImmutableLiteral number) {
super(OperandType.COMPLEX);
super();

this.number = number;
}

@Override
public OperandType getOperandType() {
return OperandType.COMPLEX;
}

@Override
public Object createCacheObject(ThreadContext context) {
return RubyComplex.newComplexRaw(context.runtime,
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/CurrentScope.java
Original file line number Diff line number Diff line change
@@ -24,10 +24,15 @@ public static CurrentScope ScopeFor(int depth) {
private final int scopeNestingDepth;

public CurrentScope(int scopeNestingDepth) {
super(OperandType.CURRENT_SCOPE);
super();
this.scopeNestingDepth = scopeNestingDepth;
}

@Override
public OperandType getOperandType() {
return OperandType.CURRENT_SCOPE;
}

@Override
public void addUsedVariables(List<Variable> l) {
/* Nothing to do */
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/DynamicSymbol.java
Original file line number Diff line number Diff line change
@@ -17,11 +17,16 @@ public class DynamicSymbol extends Operand {
final private Operand symbolName;

public DynamicSymbol(Operand n) {
super(OperandType.DYNAMIC_SYMBOL);
super();

symbolName = n;
}

@Override
public OperandType getOperandType() {
return OperandType.DYNAMIC_SYMBOL;
}

public Operand getSymbolName() {
return symbolName;
}
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Filename.java
Original file line number Diff line number Diff line change
@@ -10,7 +10,12 @@
*/
public class Filename extends StringLiteral {
public Filename(ByteList filename) {
super(OperandType.FILENAME, filename, 0);
super(filename, 0);
}

@Override
public OperandType getOperandType() {
return OperandType.FILENAME;
}

@Override
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Fixnum.java
Original file line number Diff line number Diff line change
@@ -31,10 +31,15 @@ public class Fixnum extends ImmutableLiteral {
final public long value;

public Fixnum(long val) {
super(OperandType.FIXNUM);
super();
value = val;
}

@Override
public OperandType getOperandType() {
return OperandType.FIXNUM;
}

public Fixnum(BigInteger val) {
this(val.longValue());
}
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Float.java
Original file line number Diff line number Diff line change
@@ -9,11 +9,16 @@ public class Float extends ImmutableLiteral {
final public double value;

public Float(double value) {
super(OperandType.FLOAT);
super();

this.value = value;
}

@Override
public OperandType getOperandType() {
return OperandType.FLOAT;
}

@Override
public Object createCacheObject(ThreadContext context) {
return context.runtime.newFloat(value);
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/FrozenString.java
Original file line number Diff line number Diff line change
@@ -21,7 +21,12 @@ public class FrozenString extends StringLiteral {
* Used by persistence and by .freeze optimization
*/
public FrozenString(ByteList byteList, int cr) {
super(OperandType.FROZEN_STRING, byteList, cr);
super(byteList, cr);
}

@Override
public OperandType getOperandType() {
return OperandType.FROZEN_STRING;
}

/**
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/GlobalVariable.java
Original file line number Diff line number Diff line change
@@ -10,7 +10,12 @@

public class GlobalVariable extends Reference {
public GlobalVariable(String name) {
super(OperandType.GLOBAL_VARIABLE, name);
super(name);
}

@Override
public OperandType getOperandType() {
return OperandType.GLOBAL_VARIABLE;
}

public int compareTo(Object arg0) {
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Hash.java
Original file line number Diff line number Diff line change
@@ -30,12 +30,17 @@ public class Hash extends Operand {
final public boolean isKWArgsHash;

public Hash(List<KeyValuePair<Operand, Operand>> pairs, boolean isKWArgsHash) {
super(OperandType.HASH);
super();

this.pairs = pairs;
this.isKWArgsHash = isKWArgsHash;
}

@Override
public OperandType getOperandType() {
return OperandType.HASH;
}

public Hash(List<KeyValuePair<Operand, Operand>> pairs) {
this(pairs, false);
}
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/IRException.java
Original file line number Diff line number Diff line change
@@ -14,11 +14,16 @@ public class IRException extends Operand {
private final RubyLocalJumpError.Reason type;

protected IRException(RubyLocalJumpError.Reason type) {
super(OperandType.IR_EXCEPTION);
super();

this.type = type;
}

@Override
public OperandType getOperandType() {
return OperandType.IR_EXCEPTION;
}

public RubyLocalJumpError.Reason getType() {
return type;
}
Original file line number Diff line number Diff line change
@@ -26,8 +26,8 @@
public abstract class ImmutableLiteral extends Operand {
private Object cachedObject = null;

public ImmutableLiteral(OperandType type) {
super(type);
public ImmutableLiteral() {
super();
}

@Override
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Label.java
Original file line number Diff line number Diff line change
@@ -24,12 +24,17 @@ public class Label extends Operand {
public static Label getGlobalEnsureBlockLabel() { return GLOBAL_ENSURE_BLOCK_LABEL.clone(); }

public Label(String prefix, int id) {
super(OperandType.LABEL);
super();

this.prefix = prefix;
this.id = id;
}

@Override
public OperandType getOperandType() {
return OperandType.LABEL;
}

@Override
public String toString() {
return prefix + "_" + id + ":" + targetPC;
12 changes: 6 additions & 6 deletions core/src/main/java/org/jruby/ir/operands/LocalVariable.java
Original file line number Diff line number Diff line change
@@ -15,19 +15,19 @@ public class LocalVariable extends Variable implements DepthCloneable {
protected final int offset;
protected final int hcode;

// FIXME: We should resolve to an index into an array but localvariable has no allocator
public LocalVariable(String name, int scopeDepth, int location) {
this(OperandType.LOCAL_VARIABLE, name, scopeDepth, location);
}

protected LocalVariable(OperandType type, String name, int scopeDepth, int location) {
super(type);
super();
this.name = name;
this.scopeDepth = scopeDepth;
this.offset = location;
this.hcode = (name + ":" + offset).hashCode();
}

@Override
public OperandType getOperandType() {
return OperandType.LOCAL_VARIABLE;
}

public boolean isSameDepth(LocalVariable other) {
return getScopeDepth() == other.getScopeDepth();
}
8 changes: 4 additions & 4 deletions core/src/main/java/org/jruby/ir/operands/Nil.java
Original file line number Diff line number Diff line change
@@ -13,12 +13,12 @@
*/
public class Nil extends ImmutableLiteral {
public Nil() {
super(OperandType.NIL);
super();
}

// For UnexecutableNil
protected Nil(OperandType type) {
super(type);
@Override
public OperandType getOperandType() {
return OperandType.LOCAL_VARIABLE;
}

@Override
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/NthRef.java
Original file line number Diff line number Diff line change
@@ -17,10 +17,15 @@ public class NthRef extends Reference {
final public int matchNumber;

public NthRef(int matchNumber) {
super(OperandType.NTH_REF, "$" + matchNumber);
super("$" + matchNumber);
this.matchNumber = matchNumber;
}

@Override
public OperandType getOperandType() {
return OperandType.NTH_REF;
}

@Override
public Object retrieve(ThreadContext context, IRubyObject self, StaticScope currScope, DynamicScope currDynScope, Object[] temp) {
return IRRuntimeHelpers.nthMatch(context, matchNumber);
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/NullBlock.java
Original file line number Diff line number Diff line change
@@ -14,7 +14,12 @@ public class NullBlock extends ImmutableLiteral {
public static final NullBlock INSTANCE = new NullBlock();

private NullBlock() {
super(OperandType.NULL_BLOCK);
super();
}

@Override
public OperandType getOperandType() {
return OperandType.NULL_BLOCK;
}

@Override
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/ObjectClass.java
Original file line number Diff line number Diff line change
@@ -11,7 +11,12 @@

public class ObjectClass extends Operand {
public ObjectClass() {
super(OperandType.OBJECT_CLASS);
super();
}

@Override
public OperandType getOperandType() {
return OperandType.OBJECT_CLASS;
}

@Override
8 changes: 2 additions & 6 deletions core/src/main/java/org/jruby/ir/operands/Operand.java
Original file line number Diff line number Diff line change
@@ -14,15 +14,11 @@

public abstract class Operand {
public static final Operand[] EMPTY_ARRAY = new Operand[0];
private final OperandType type;

public Operand(OperandType type) {
this.type = type;
public Operand() {
}

public final OperandType getOperandType() {
return type;
}
public abstract OperandType getOperandType();

/**
* Do we know the value of this operand at compile-time?
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Rational.java
Original file line number Diff line number Diff line change
@@ -11,12 +11,17 @@ public class Rational extends ImmutableLiteral {
private final long denominator;

public Rational(long numerator, long denominator) {
super(OperandType.RATIONAL);
super();

this.numerator = numerator;
this.denominator = denominator;
}

@Override
public OperandType getOperandType() {
return OperandType.RATIONAL;
}

@Override
public Object createCacheObject(ThreadContext context) {
return context.runtime.newRational(numerator, denominator);
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/ir/operands/Reference.java
Original file line number Diff line number Diff line change
@@ -10,8 +10,8 @@
public abstract class Reference extends Operand {
final private String name;

public Reference(OperandType type, String name) {
super(type);
public Reference(String name) {
super();

this.name = name;
}
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Regexp.java
Original file line number Diff line number Diff line change
@@ -16,12 +16,17 @@ public class Regexp extends ImmutableLiteral {
final private ByteList source;

public Regexp(ByteList source, RegexpOptions options) {
super(OperandType.REGEXP);
super();

this.source = source;
this.options = options;
}

@Override
public OperandType getOperandType() {
return OperandType.REGEXP;
}

public ByteList getSource() {
return source;
}
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/SValue.java
Original file line number Diff line number Diff line change
@@ -27,11 +27,16 @@ public class SValue extends Operand {
final private Operand array;

public SValue(Operand array) {
super(OperandType.SVALUE);
super();

this.array = array;
}

@Override
public OperandType getOperandType() {
return OperandType.SVALUE;
}

public Operand getArray() {
return array;
}
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/ScopeModule.java
Original file line number Diff line number Diff line change
@@ -28,11 +28,16 @@ public static ScopeModule ModuleFor(int depth) {
private final int scopeModuleDepth;

public ScopeModule(int scopeModuleDepth) {
super(OperandType.SCOPE_MODULE);
super();

this.scopeModuleDepth = scopeModuleDepth;
}

@Override
public OperandType getOperandType() {
return OperandType.SCOPE_MODULE;
}

@Override
public void addUsedVariables(List<Variable> l) {
/* Do nothing */
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Self.java
Original file line number Diff line number Diff line change
@@ -13,7 +13,12 @@ public class Self extends LocalVariable {
private static final String NAME = "%self";

private Self() {
super(OperandType.SELF, NAME, 0, 0);
super(NAME, 0, 0);
}

@Override
public OperandType getOperandType() {
return OperandType.SELF;
}

public boolean isSelf() {
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Splat.java
Original file line number Diff line number Diff line change
@@ -20,10 +20,15 @@ public class Splat extends Operand implements DepthCloneable {
final private Operand array;

public Splat(Operand array) {
super(OperandType.SPLAT);
super();
this.array = array;
}

@Override
public OperandType getOperandType() {
return OperandType.SPLAT;
}

@Override
public String toString() {
return "*(unsplat)" + array;
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/StandardError.java
Original file line number Diff line number Diff line change
@@ -9,7 +9,12 @@
// for when the rescue block doesn't specify an exception object class
public class StandardError extends Operand {
public StandardError() {
super(OperandType.STANDARD_ERROR);
super();
}

@Override
public OperandType getOperandType() {
return OperandType.STANDARD_ERROR;
}

@Override
18 changes: 9 additions & 9 deletions core/src/main/java/org/jruby/ir/operands/StringLiteral.java
Original file line number Diff line number Diff line change
@@ -33,16 +33,11 @@ public class StringLiteral extends Operand {
final public int coderange;

public StringLiteral(ByteList val, int coderange) {
this(OperandType.STRING_LITERAL, val, coderange);
this(internedStringFromByteList(val), val, coderange);
}

protected StringLiteral(OperandType type, ByteList val, int coderange) {
this(type, internedStringFromByteList(val), val, coderange);

}

protected StringLiteral(OperandType type, String string, ByteList bytelist, int coderange) {
super(type);
protected StringLiteral(String string, ByteList bytelist, int coderange) {
super();

this.bytelist = bytelist;
this.coderange = coderange;
@@ -64,13 +59,18 @@ public StringLiteral(String s) {
}

private StringLiteral(String string, ByteList byteList) {
super(OperandType.STRING_LITERAL);
super();

this.bytelist = byteList;
this.string = string;
this.coderange = StringSupport.CR_7BIT;
}

@Override
public OperandType getOperandType() {
return OperandType.STRING_LITERAL;
}

@Override
public boolean hasKnownValue() {
return true;
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Symbol.java
Original file line number Diff line number Diff line change
@@ -15,12 +15,17 @@ public class Symbol extends ImmutableLiteral {
private final Encoding encoding;

public Symbol(String name, Encoding encoding) {
super(OperandType.SYMBOL);
super();

this.name = name;
this.encoding = encoding;
}

@Override
public OperandType getOperandType() {
return OperandType.SYMBOL;
}

public String getName() {
return name;
}
Original file line number Diff line number Diff line change
@@ -4,7 +4,12 @@

public abstract class TemporaryVariable extends Variable {
public TemporaryVariable() {
super(OperandType.TEMPORARY_VARIABLE);
super();
}

@Override
public OperandType getOperandType() {
return OperandType.TEMPORARY_VARIABLE;
}

/**
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/UnboxedBoolean.java
Original file line number Diff line number Diff line change
@@ -12,11 +12,16 @@ public class UnboxedBoolean extends ImmutableLiteral {
public static final UnboxedBoolean FALSE = new UnboxedBoolean(false);

public UnboxedBoolean(boolean truthy) {
super(OperandType.UNBOXED_BOOLEAN);
super();

this.truthy = truthy;
}

@Override
public OperandType getOperandType() {
return OperandType.UNBOXED_BOOLEAN;
}

@Override
public Object createCacheObject(ThreadContext context) {
return context.runtime.newBoolean(isTrue());
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/UnboxedFixnum.java
Original file line number Diff line number Diff line change
@@ -27,10 +27,15 @@ public class UnboxedFixnum extends ImmutableLiteral {
final public long value;

public UnboxedFixnum(long val) {
super(OperandType.UNBOXED_FIXNUM);
super();
value = val;
}

@Override
public OperandType getOperandType() {
return OperandType.UNBOXED_FIXNUM;
}

@Override
public Object createCacheObject(ThreadContext context) {
return context.runtime.newFixnum(value);
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/UnboxedFloat.java
Original file line number Diff line number Diff line change
@@ -7,11 +7,16 @@ public class UnboxedFloat extends ImmutableLiteral {
final public double value;

public UnboxedFloat(double value) {
super(OperandType.UNBOXED_FLOAT);
super();

this.value = value;
}

@Override
public OperandType getOperandType() {
return OperandType.UNBOXED_FLOAT;
}

@Override
public boolean hasKnownValue() {
return true;
7 changes: 6 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/UndefinedValue.java
Original file line number Diff line number Diff line change
@@ -25,7 +25,12 @@ public class UndefinedValue extends Operand implements IRubyObject {
public static final UndefinedValue UNDEFINED = new UndefinedValue();

private UndefinedValue() {
super(OperandType.UNDEFINED_VALUE);
super();
}

@Override
public OperandType getOperandType() {
return OperandType.UNDEFINED_VALUE;
}

@Override
Original file line number Diff line number Diff line change
@@ -16,7 +16,12 @@ public class UnexecutableNil extends Nil {
public static final UnexecutableNil U_NIL = new UnexecutableNil();

private UnexecutableNil() {
super(OperandType.UNEXECUTABLE_NIL);
super();
}

@Override
public OperandType getOperandType() {
return OperandType.UNEXECUTABLE_NIL;
}

@Override
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/ir/operands/Variable.java
Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@
import java.util.Map;

public abstract class Variable extends Operand implements Comparable {
public Variable(OperandType type) {
super(type);
public Variable() {
super();
}

public abstract String getName();
Original file line number Diff line number Diff line change
@@ -18,12 +18,17 @@ public class WrappedIRClosure extends Operand {
private final IRClosure closure;

public WrappedIRClosure(Variable self, IRClosure closure) {
super(OperandType.WRAPPED_IR_CLOSURE);
super();

this.self = self;
this.closure = closure;
}

@Override
public OperandType getOperandType() {
return OperandType.WRAPPED_IR_CLOSURE;
}

@Override
public void addUsedVariables(List<Variable> l) {
l.add(self);

0 comments on commit ce91559

Please sign in to comment.