Skip to content

Commit

Permalink
Make better use of context-cached "immediates".
Browse files Browse the repository at this point in the history
This commit is a global search and replace of all code that
traverses through context.runtime to get at nil, true, and false
objects rather than using the cached versions on context itself.
The context is thread-local and probably very likely to be in the
CPU cache, which means these references should be in cache also.
  • Loading branch information
headius committed Apr 12, 2018
1 parent f48399a commit 6bb18b2
Show file tree
Hide file tree
Showing 65 changed files with 204 additions and 204 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/AbstractRubyMethod.java
Expand Up @@ -145,10 +145,10 @@ public IRubyObject parameters(ThreadContext context) {
}

protected IRubyObject super_method(ThreadContext context, IRubyObject receiver, RubyModule superClass) {
if (superClass == null) return context.runtime.getNil();
if (superClass == null) return context.nil;

DynamicMethod newMethod = superClass.searchMethod(methodName);
if (newMethod == UndefinedMethod.INSTANCE) return context.runtime.getNil();
if (newMethod == UndefinedMethod.INSTANCE) return context.nil;

if (receiver == null) {
return RubyUnboundMethod.newUnboundMethod(superClass, methodName, superClass, originName, newMethod);
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/org/jruby/RubyArgsFile.java
Expand Up @@ -476,7 +476,7 @@ public static IRubyObject each_line(ThreadContext context, IRubyObject recv, IRu
if (!block.isGiven()) return RubyEnumerator.enumeratorize(context.runtime, recv, "each_line");
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);

if (!data.next_argv(context)) return context.runtime.getNil();
if (!data.next_argv(context)) return context.nil;

if (!(data.currentFile instanceof RubyIO)) {
if (!data.next_argv(context)) return recv;
Expand Down Expand Up @@ -615,7 +615,7 @@ public static IRubyObject rewind(ThreadContext context, IRubyObject recv) {
public static IRubyObject eof(ThreadContext context, IRubyObject recv) {
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);

if (!data.inited) return context.runtime.getTrue();
if (!data.inited) return context.tru;

if (!(data.currentFile instanceof RubyIO)) {
return data.currentFile.callMethod(context, "eof");
Expand All @@ -628,7 +628,7 @@ public static IRubyObject eof(ThreadContext context, IRubyObject recv) {
public static IRubyObject eof_p(ThreadContext context, IRubyObject recv) {
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);

if (!data.inited) return context.runtime.getTrue();
if (!data.inited) return context.tru;

if (!(data.currentFile instanceof RubyIO)) {
return data.currentFile.callMethod(context, "eof?");
Expand Down Expand Up @@ -661,7 +661,7 @@ public static IRubyObject getbyte(ThreadContext context, IRubyObject recv) {
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);

while(true) {
if (!data.next_argv(context)) return context.runtime.getNil();
if (!data.next_argv(context)) return context.nil;

IRubyObject bt;
if (!(data.currentFile instanceof RubyFile)) {
Expand Down Expand Up @@ -743,7 +743,7 @@ public static IRubyObject getc(ThreadContext context, IRubyObject recv) {
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);

while(true) {
if (!data.next_argv(context)) return context.runtime.getNil();
if (!data.next_argv(context)) return context.nil;

IRubyObject bt;
if (!(data.currentFile instanceof RubyFile)) {
Expand Down
44 changes: 22 additions & 22 deletions core/src/main/java/org/jruby/RubyArray.java
Expand Up @@ -2026,13 +2026,13 @@ public IRubyObject checkArrayType(){
@JRubyMethod(name = "==", required = 1)
@Override
public IRubyObject op_equal(ThreadContext context, IRubyObject obj) {
if (this == obj) return context.runtime.getTrue();
if (this == obj) return context.tru;

if (!(obj instanceof RubyArray)) {
if (obj == context.nil) return context.runtime.getFalse();
if (obj == context.nil) return context.fals;

if (!sites(context).respond_to_to_ary.respondsTo(context, obj, obj)) {
return context.runtime.getFalse();
return context.fals;
}
return Helpers.rbEqual(context, obj, this);
}
Expand All @@ -2041,25 +2041,25 @@ public IRubyObject op_equal(ThreadContext context, IRubyObject obj) {

public RubyBoolean compare(ThreadContext context, CallSite site, IRubyObject other) {
if (!(other instanceof RubyArray)) {
if (!sites(context).respond_to_to_ary.respondsTo(context, other, other)) return context.runtime.getFalse();
if (!sites(context).respond_to_to_ary.respondsTo(context, other, other)) return context.fals;

return Helpers.rbEqual(context, other, this);
}

RubyArray ary = (RubyArray) other;

if (realLength != ary.realLength) return context.runtime.getFalse();
if (realLength != ary.realLength) return context.fals;

for (int i = 0; i < realLength; i++) {
IRubyObject a = elt(i);
IRubyObject b = ary.elt(i);

if (a == b) continue; // matching MRI opt. mock frameworks can throw errors if we don't

if (!site.call(context, a, a, b).isTrue()) return context.runtime.getFalse();
if (!site.call(context, a, a, b).isTrue()) return context.fals;
}

return context.runtime.getTrue();
return context.tru;
}

/** rb_ary_eql
Expand All @@ -2068,7 +2068,7 @@ public RubyBoolean compare(ThreadContext context, CallSite site, IRubyObject oth
@JRubyMethod(name = "eql?", required = 1)
public IRubyObject eql(ThreadContext context, IRubyObject obj) {
if(!(obj instanceof RubyArray)) {
return context.runtime.getFalse();
return context.fals;
}
return RecursiveComparator.compare(context, sites(context).eql, this, obj);
}
Expand Down Expand Up @@ -3321,7 +3321,7 @@ private RubyHash makeHash(ThreadContext context, RubyHash hash, Block block) {
*/
public IRubyObject uniq_bang(ThreadContext context) {
RubyHash hash = makeHash();
if (realLength == hash.size()) return context.runtime.getNil();
if (realLength == hash.size()) return context.nil;

// TODO: (CON) This could be a no-op for packed arrays if size does not change
unpack();
Expand All @@ -3341,7 +3341,7 @@ public IRubyObject uniq_bang19(ThreadContext context, Block block) {

if (!block.isGiven()) return uniq_bang(context);
RubyHash hash = makeHash(context, block);
if (realLength == hash.size()) return context.runtime.getNil();
if (realLength == hash.size()) return context.nil;

// after evaluating the block, a new modify check is needed
modifyCheck();
Expand Down Expand Up @@ -3746,7 +3746,7 @@ public IRubyObject cycle(ThreadContext context, IRubyObject arg, Block block) {
if (!block.isGiven()) return enumeratorizeWithSize(context, this, "cycle", new IRubyObject[] {arg}, cycleSizeFn(context));

long times = RubyNumeric.num2long(arg);
if (times <= 0) return context.runtime.getNil();
if (times <= 0) return context.nil;

return cycleCommon(context, times, block);
}
Expand All @@ -3757,7 +3757,7 @@ private IRubyObject cycleCommon(ThreadContext context, long n, Block block) {
block.yield(context, eltOk(i));
}
}
return context.runtime.getNil();
return context.nil;
}

private SizeFn cycleSizeFn(final ThreadContext context) {
Expand Down Expand Up @@ -4380,47 +4380,47 @@ public IRubyObject all_p(ThreadContext context, IRubyObject[] args, Block block)
if (!block.isGiven()) return all_pBlockless(context);

for (int i = 0; i < realLength; i++) {
if (!block.yield(context, eltOk(i)).isTrue()) return context.runtime.getFalse();
if (!block.yield(context, eltOk(i)).isTrue()) return context.fals;
}

return context.runtime.getTrue();
return context.tru;
}

private IRubyObject all_pBlockless(ThreadContext context) {
for (int i = 0; i < realLength; i++) {
if (!eltOk(i).isTrue()) return context.runtime.getFalse();
if (!eltOk(i).isTrue()) return context.fals;
}

return context.runtime.getTrue();
return context.tru;
}

@JRubyMethod(name = "any?", optional = 1)
public IRubyObject any_p(ThreadContext context, IRubyObject[] args, Block block) {
if (isEmpty()) return context.runtime.getFalse();
if (isEmpty()) return context.fals;
if (!isBuiltin("each")) return RubyEnumerable.any_pCommon(context, this, args, block);
boolean patternGiven = args.length > 0;
if (!block.isGiven() || patternGiven) return any_pBlockless(context, args);

for (int i = 0; i < realLength; i++) {
if (block.yield(context, eltOk(i)).isTrue()) return context.runtime.getTrue();
if (block.yield(context, eltOk(i)).isTrue()) return context.tru;
}

return context.runtime.getFalse();
return context.fals;
}

private IRubyObject any_pBlockless(ThreadContext context, IRubyObject[] args) {
IRubyObject pattern = args.length > 0 ? args[0] : null;
if (pattern == null) {
for (int i = 0; i < realLength; i++) {
if (eltOk(i).isTrue()) return context.runtime.getTrue();
if (eltOk(i).isTrue()) return context.tru;
}
} else {
for (int i = 0; i < realLength; i++) {
if (pattern.callMethod(context, "===", eltOk(i)).isTrue()) return context.runtime.getTrue();
if (pattern.callMethod(context, "===", eltOk(i)).isTrue()) return context.tru;
}
}

return context.runtime.getFalse();
return context.fals;
}

@JRubyMethod
Expand Down
22 changes: 11 additions & 11 deletions core/src/main/java/org/jruby/RubyBasicObject.java
Expand Up @@ -1294,7 +1294,7 @@ public int compareTo(IRubyObject other) {
@Override
@JRubyMethod(name = "==")
public IRubyObject op_equal(ThreadContext context, IRubyObject obj) {
return this == obj ? context.runtime.getTrue() : context.runtime.getFalse();
return this == obj ? context.tru : context.fals;
}

@Deprecated
Expand Down Expand Up @@ -1995,7 +1995,7 @@ private void callFinalizer(IRubyObject finalizer) {
*/
@JRubyMethod(name = "equal?", required = 1)
public IRubyObject equal_p(ThreadContext context, IRubyObject other) {
return this == other ? context.runtime.getTrue() : context.runtime.getFalse();
return this == other ? context.tru : context.fals;
}

@Deprecated
Expand Down Expand Up @@ -2155,7 +2155,7 @@ public IRubyObject display(ThreadContext context, IRubyObject[] args) {

port.callMethod(context, "write", this);

return context.runtime.getNil();
return context.nil;
}

/** rb_obj_tainted
Expand Down Expand Up @@ -2253,11 +2253,11 @@ public RubyBoolean frozen_p(ThreadContext context) {
*/
public RubyBoolean instance_of_p(ThreadContext context, IRubyObject type) {
if (type() == type) {
return context.runtime.getTrue();
return context.tru;
} else if (!(type instanceof RubyModule)) {
throw context.runtime.newTypeError("class or module required");
} else {
return context.runtime.getFalse();
return context.fals;
}
}

Expand Down Expand Up @@ -2768,7 +2768,7 @@ public IRubyObject send19(ThreadContext context, IRubyObject[] args, Block block
* Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
*/
public IRubyObject nil_p(ThreadContext context) {
return context.runtime.getFalse();
return context.fals;
}

/** rb_obj_pattern_match
Expand All @@ -2781,11 +2781,11 @@ public IRubyObject nil_p(ThreadContext context) {
* pattern-match semantics.
*/
public IRubyObject op_match(ThreadContext context, IRubyObject arg) {
return context.runtime.getFalse();
return context.fals;
}

public IRubyObject op_match19(ThreadContext context, IRubyObject arg) {
return context.runtime.getNil();
return context.nil;
}

/**
Expand Down Expand Up @@ -2825,9 +2825,9 @@ public IRubyObject op_not_match(ThreadContext context, IRubyObject arg) {
*/
public IRubyObject instance_variable_defined_p(ThreadContext context, IRubyObject name) {
if (variableTableContains(validateInstanceVariable(name, name.asJavaString()))) {
return context.runtime.getTrue();
return context.tru;
}
return context.runtime.getFalse();
return context.fals;
}

/** rb_obj_ivar_get
Expand Down Expand Up @@ -2855,7 +2855,7 @@ public IRubyObject instance_variable_get(ThreadContext context, IRubyObject name
if ((value = variableTableFetch(validateInstanceVariable(name, name.asJavaString()))) != null) {
return (IRubyObject)value;
}
return context.runtime.getNil();
return context.nil;
}

/** rb_obj_ivar_set
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyBignum.java
Expand Up @@ -999,7 +999,7 @@ public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
} else if (other instanceof RubyFloat) {
double a = ((RubyFloat) other).getDoubleValue();
if (Double.isNaN(a)) {
return context.runtime.getFalse();
return context.fals;
}
return RubyBoolean.newBoolean(context.runtime, a == big2dbl(this));
} else {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/RubyComparable.java
Expand Up @@ -129,7 +129,7 @@ public static IRubyObject invcmp(final ThreadContext context, final IRubyObject
private static final ThreadContext.RecursiveFunctionEx DEFAULT_INVCMP = new ThreadContext.RecursiveFunctionEx<IRubyObject>() {
@Override
public IRubyObject call(ThreadContext context, IRubyObject recv, IRubyObject other, boolean recur) {
if (recur || !sites(context).respond_to_op_cmp.respondsTo(context, other, other)) return context.runtime.getNil();
if (recur || !sites(context).respond_to_op_cmp.respondsTo(context, other, other)) return context.nil;
return sites(context).op_cmp.call(context, other, other, recv);
}
};
Expand All @@ -154,7 +154,7 @@ public static IRubyObject invcmp(final ThreadContext context, ThreadContext.Recu
*/
@JRubyMethod(name = "==", required = 1)
public static IRubyObject op_equal(ThreadContext context, IRubyObject recv, IRubyObject other) {
return callCmpMethod(context, recv, other, context.runtime.getFalse());
return callCmpMethod(context, recv, other, context.fals);
}

@Deprecated
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/RubyComplex.java
Expand Up @@ -769,7 +769,7 @@ public IRubyObject conjugate(ThreadContext context) {
@JRubyMethod(name = "real?")
@Override
public IRubyObject real_p(ThreadContext context) {
return context.runtime.getFalse();
return context.fals;
}

@Override
Expand All @@ -780,7 +780,7 @@ public IRubyObject real_p(ThreadContext context) {
*/
// @JRubyMethod(name = "complex?")
public IRubyObject complex_p(ThreadContext context) {
return context.runtime.getTrue();
return context.tru;
}

/** nucomp_exact_p
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyContinuation.java
Expand Up @@ -109,7 +109,7 @@ public IRubyObject enter(ThreadContext context, IRubyObject yielded, Block block
} catch (Continuation c) {
if (c == continuation) {
if (continuation.args.length == 0) {
return context.runtime.getNil();
return context.nil;
} else if (continuation.args.length == 1) {
return continuation.args[0];
} else {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyDir.java
Expand Up @@ -757,7 +757,7 @@ public IRubyObject set_pos(IRubyObject newPos) {

@JRubyMethod(name = {"path", "to_path"})
public IRubyObject path(ThreadContext context) {
return path == null ? context.runtime.getNil() : path.strDup(context.runtime);
return path == null ? context.nil : path.strDup(context.runtime);
}

@JRubyMethod
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/RubyException.java
Expand Up @@ -103,7 +103,7 @@ public static IRubyObject op_eqq(ThreadContext context, IRubyObject recv, IRubyO
Object object = ((ConcreteJavaProxy)other).getObject();
if (object instanceof Throwable && !(object instanceof FlowControlException)) {
if (recv == runtime.getException() || object instanceof java.lang.Exception) {
return context.runtime.getTrue();
return context.tru;
}
}
}
Expand Down Expand Up @@ -273,7 +273,7 @@ public RubyString inspect(ThreadContext context) {
@Override
@JRubyMethod(name = "==")
public RubyBoolean op_equal(ThreadContext context, IRubyObject other) {
if (this == other) return context.runtime.getTrue();
if (this == other) return context.tru;

boolean equal = context.runtime.getException().isInstance(other) &&
getMetaClass().getRealClass() == other.getMetaClass().getRealClass() &&
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyFileStat.java
Expand Up @@ -565,6 +565,6 @@ private IRubyObject getWorldMode(ThreadContext context, int mode) {
return RubyNumeric.int2fix(context.runtime,
(stat.mode() & (S_IRUGO | S_IWUGO | S_IXUGO) ));
}
return context.runtime.getNil();
return context.nil;
}
}
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/RubyFileTest.java
Expand Up @@ -319,14 +319,14 @@ public static RubyBoolean zero_p(ThreadContext context, IRubyObject recv, IRubyO
public static IRubyObject worldReadable(ThreadContext context, IRubyObject recv, IRubyObject filename) {
RubyFileStat stat = getRubyFileStat(context, filename);

return stat == null ? context.runtime.getNil() : stat.worldReadable(context);
return stat == null ? context.nil : stat.worldReadable(context);
}

@JRubyMethod(name = "world_writable?", required = 1, module = true)
public static IRubyObject worldWritable(ThreadContext context, IRubyObject recv, IRubyObject filename) {
RubyFileStat stat = getRubyFileStat(context, filename);

return stat == null ? context.runtime.getNil() : stat.worldWritable(context);
return stat == null ? context.nil : stat.worldWritable(context);
}

/**
Expand Down

0 comments on commit 6bb18b2

Please sign in to comment.