Skip to content

Commit

Permalink
Make Symbol an ImmutableLiteral instead of a simple Reference.
Browse files Browse the repository at this point in the history
This will improve symbol performance in two ways:
1. In interpreter the operand caches symbol value vs digging from global symbol table
2. Since it is a ImmutableLiteral opts like OptTempVars can propagate its value instead
   of indirecting through a temp var.
  • Loading branch information
enebo committed Mar 17, 2015
1 parent c9a5cca commit 8e3137a
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions core/src/main/java/org/jruby/ir/operands/Symbol.java
Expand Up @@ -6,22 +6,30 @@
import org.jruby.ir.IRVisitor;
import org.jruby.ir.persistence.IRReaderDecoder;
import org.jruby.ir.persistence.IRWriterEncoder;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

public class Symbol extends Reference {
public class Symbol extends ImmutableLiteral {
public static final Symbol KW_REST_ARG_DUMMY = new Symbol("", ASCIIEncoding.INSTANCE);

private final String name;
private final Encoding encoding;

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

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

public String getName() {
return name;
}

@Override
public Object createCacheObject(ThreadContext context) {
return RubySymbol.newSymbol(context.runtime, getName(), encoding);
}

@Override
public boolean canCopyPropagate() {
return true;
Expand All @@ -31,11 +39,6 @@ public Encoding getEncoding() {
return encoding;
}

@Override
public Object retrieve(ThreadContext context, IRubyObject self, StaticScope currScope, DynamicScope currDynScope, Object[] temp) {
return RubySymbol.newSymbol(context.runtime, getName(), encoding);
}

@Override
public String toString() {
return ":'" + getName() + "'";
Expand All @@ -44,6 +47,7 @@ public String toString() {
@Override
public void encode(IRWriterEncoder e) {
super.encode(e);
e.encode(getName());
e.encode(getEncoding());
}

Expand Down

0 comments on commit 8e3137a

Please sign in to comment.