Skip to content

Commit 6bb18b2

Browse files
committed
Make better use of context-cached "immediates".
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.
1 parent f48399a commit 6bb18b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+204
-204
lines changed

core/src/main/java/org/jruby/AbstractRubyMethod.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ public IRubyObject parameters(ThreadContext context) {
145145
}
146146

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

150150
DynamicMethod newMethod = superClass.searchMethod(methodName);
151-
if (newMethod == UndefinedMethod.INSTANCE) return context.runtime.getNil();
151+
if (newMethod == UndefinedMethod.INSTANCE) return context.nil;
152152

153153
if (receiver == null) {
154154
return RubyUnboundMethod.newUnboundMethod(superClass, methodName, superClass, originName, newMethod);

core/src/main/java/org/jruby/RubyArgsFile.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public static IRubyObject each_line(ThreadContext context, IRubyObject recv, IRu
476476
if (!block.isGiven()) return RubyEnumerator.enumeratorize(context.runtime, recv, "each_line");
477477
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);
478478

479-
if (!data.next_argv(context)) return context.runtime.getNil();
479+
if (!data.next_argv(context)) return context.nil;
480480

481481
if (!(data.currentFile instanceof RubyIO)) {
482482
if (!data.next_argv(context)) return recv;
@@ -615,7 +615,7 @@ public static IRubyObject rewind(ThreadContext context, IRubyObject recv) {
615615
public static IRubyObject eof(ThreadContext context, IRubyObject recv) {
616616
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);
617617

618-
if (!data.inited) return context.runtime.getTrue();
618+
if (!data.inited) return context.tru;
619619

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

631-
if (!data.inited) return context.runtime.getTrue();
631+
if (!data.inited) return context.tru;
632632

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

663663
while(true) {
664-
if (!data.next_argv(context)) return context.runtime.getNil();
664+
if (!data.next_argv(context)) return context.nil;
665665

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

745745
while(true) {
746-
if (!data.next_argv(context)) return context.runtime.getNil();
746+
if (!data.next_argv(context)) return context.nil;
747747

748748
IRubyObject bt;
749749
if (!(data.currentFile instanceof RubyFile)) {

core/src/main/java/org/jruby/RubyArray.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,13 +2026,13 @@ public IRubyObject checkArrayType(){
20262026
@JRubyMethod(name = "==", required = 1)
20272027
@Override
20282028
public IRubyObject op_equal(ThreadContext context, IRubyObject obj) {
2029-
if (this == obj) return context.runtime.getTrue();
2029+
if (this == obj) return context.tru;
20302030

20312031
if (!(obj instanceof RubyArray)) {
2032-
if (obj == context.nil) return context.runtime.getFalse();
2032+
if (obj == context.nil) return context.fals;
20332033

20342034
if (!sites(context).respond_to_to_ary.respondsTo(context, obj, obj)) {
2035-
return context.runtime.getFalse();
2035+
return context.fals;
20362036
}
20372037
return Helpers.rbEqual(context, obj, this);
20382038
}
@@ -2041,25 +2041,25 @@ public IRubyObject op_equal(ThreadContext context, IRubyObject obj) {
20412041

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

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

20492049
RubyArray ary = (RubyArray) other;
20502050

2051-
if (realLength != ary.realLength) return context.runtime.getFalse();
2051+
if (realLength != ary.realLength) return context.fals;
20522052

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

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

2059-
if (!site.call(context, a, a, b).isTrue()) return context.runtime.getFalse();
2059+
if (!site.call(context, a, a, b).isTrue()) return context.fals;
20602060
}
20612061

2062-
return context.runtime.getTrue();
2062+
return context.tru;
20632063
}
20642064

20652065
/** rb_ary_eql
@@ -2068,7 +2068,7 @@ public RubyBoolean compare(ThreadContext context, CallSite site, IRubyObject oth
20682068
@JRubyMethod(name = "eql?", required = 1)
20692069
public IRubyObject eql(ThreadContext context, IRubyObject obj) {
20702070
if(!(obj instanceof RubyArray)) {
2071-
return context.runtime.getFalse();
2071+
return context.fals;
20722072
}
20732073
return RecursiveComparator.compare(context, sites(context).eql, this, obj);
20742074
}
@@ -3321,7 +3321,7 @@ private RubyHash makeHash(ThreadContext context, RubyHash hash, Block block) {
33213321
*/
33223322
public IRubyObject uniq_bang(ThreadContext context) {
33233323
RubyHash hash = makeHash();
3324-
if (realLength == hash.size()) return context.runtime.getNil();
3324+
if (realLength == hash.size()) return context.nil;
33253325

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

33423342
if (!block.isGiven()) return uniq_bang(context);
33433343
RubyHash hash = makeHash(context, block);
3344-
if (realLength == hash.size()) return context.runtime.getNil();
3344+
if (realLength == hash.size()) return context.nil;
33453345

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

37483748
long times = RubyNumeric.num2long(arg);
3749-
if (times <= 0) return context.runtime.getNil();
3749+
if (times <= 0) return context.nil;
37503750

37513751
return cycleCommon(context, times, block);
37523752
}
@@ -3757,7 +3757,7 @@ private IRubyObject cycleCommon(ThreadContext context, long n, Block block) {
37573757
block.yield(context, eltOk(i));
37583758
}
37593759
}
3760-
return context.runtime.getNil();
3760+
return context.nil;
37613761
}
37623762

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

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

4386-
return context.runtime.getTrue();
4386+
return context.tru;
43874387
}
43884388

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

4394-
return context.runtime.getTrue();
4394+
return context.tru;
43954395
}
43964396

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

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

4408-
return context.runtime.getFalse();
4408+
return context.fals;
44094409
}
44104410

44114411
private IRubyObject any_pBlockless(ThreadContext context, IRubyObject[] args) {
44124412
IRubyObject pattern = args.length > 0 ? args[0] : null;
44134413
if (pattern == null) {
44144414
for (int i = 0; i < realLength; i++) {
4415-
if (eltOk(i).isTrue()) return context.runtime.getTrue();
4415+
if (eltOk(i).isTrue()) return context.tru;
44164416
}
44174417
} else {
44184418
for (int i = 0; i < realLength; i++) {
4419-
if (pattern.callMethod(context, "===", eltOk(i)).isTrue()) return context.runtime.getTrue();
4419+
if (pattern.callMethod(context, "===", eltOk(i)).isTrue()) return context.tru;
44204420
}
44214421
}
44224422

4423-
return context.runtime.getFalse();
4423+
return context.fals;
44244424
}
44254425

44264426
@JRubyMethod

core/src/main/java/org/jruby/RubyBasicObject.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ public int compareTo(IRubyObject other) {
12941294
@Override
12951295
@JRubyMethod(name = "==")
12961296
public IRubyObject op_equal(ThreadContext context, IRubyObject obj) {
1297-
return this == obj ? context.runtime.getTrue() : context.runtime.getFalse();
1297+
return this == obj ? context.tru : context.fals;
12981298
}
12991299

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

20012001
@Deprecated
@@ -2155,7 +2155,7 @@ public IRubyObject display(ThreadContext context, IRubyObject[] args) {
21552155

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

2158-
return context.runtime.getNil();
2158+
return context.nil;
21592159
}
21602160

21612161
/** rb_obj_tainted
@@ -2253,11 +2253,11 @@ public RubyBoolean frozen_p(ThreadContext context) {
22532253
*/
22542254
public RubyBoolean instance_of_p(ThreadContext context, IRubyObject type) {
22552255
if (type() == type) {
2256-
return context.runtime.getTrue();
2256+
return context.tru;
22572257
} else if (!(type instanceof RubyModule)) {
22582258
throw context.runtime.newTypeError("class or module required");
22592259
} else {
2260-
return context.runtime.getFalse();
2260+
return context.fals;
22612261
}
22622262
}
22632263

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

27742774
/** rb_obj_pattern_match
@@ -2781,11 +2781,11 @@ public IRubyObject nil_p(ThreadContext context) {
27812781
* pattern-match semantics.
27822782
*/
27832783
public IRubyObject op_match(ThreadContext context, IRubyObject arg) {
2784-
return context.runtime.getFalse();
2784+
return context.fals;
27852785
}
27862786

27872787
public IRubyObject op_match19(ThreadContext context, IRubyObject arg) {
2788-
return context.runtime.getNil();
2788+
return context.nil;
27892789
}
27902790

27912791
/**
@@ -2825,9 +2825,9 @@ public IRubyObject op_not_match(ThreadContext context, IRubyObject arg) {
28252825
*/
28262826
public IRubyObject instance_variable_defined_p(ThreadContext context, IRubyObject name) {
28272827
if (variableTableContains(validateInstanceVariable(name, name.asJavaString()))) {
2828-
return context.runtime.getTrue();
2828+
return context.tru;
28292829
}
2830-
return context.runtime.getFalse();
2830+
return context.fals;
28312831
}
28322832

28332833
/** rb_obj_ivar_get
@@ -2855,7 +2855,7 @@ public IRubyObject instance_variable_get(ThreadContext context, IRubyObject name
28552855
if ((value = variableTableFetch(validateInstanceVariable(name, name.asJavaString()))) != null) {
28562856
return (IRubyObject)value;
28572857
}
2858-
return context.runtime.getNil();
2858+
return context.nil;
28592859
}
28602860

28612861
/** rb_obj_ivar_set

core/src/main/java/org/jruby/RubyBignum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
999999
} else if (other instanceof RubyFloat) {
10001000
double a = ((RubyFloat) other).getDoubleValue();
10011001
if (Double.isNaN(a)) {
1002-
return context.runtime.getFalse();
1002+
return context.fals;
10031003
}
10041004
return RubyBoolean.newBoolean(context.runtime, a == big2dbl(this));
10051005
} else {

core/src/main/java/org/jruby/RubyComparable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public static IRubyObject invcmp(final ThreadContext context, final IRubyObject
129129
private static final ThreadContext.RecursiveFunctionEx DEFAULT_INVCMP = new ThreadContext.RecursiveFunctionEx<IRubyObject>() {
130130
@Override
131131
public IRubyObject call(ThreadContext context, IRubyObject recv, IRubyObject other, boolean recur) {
132-
if (recur || !sites(context).respond_to_op_cmp.respondsTo(context, other, other)) return context.runtime.getNil();
132+
if (recur || !sites(context).respond_to_op_cmp.respondsTo(context, other, other)) return context.nil;
133133
return sites(context).op_cmp.call(context, other, other, recv);
134134
}
135135
};
@@ -154,7 +154,7 @@ public static IRubyObject invcmp(final ThreadContext context, ThreadContext.Recu
154154
*/
155155
@JRubyMethod(name = "==", required = 1)
156156
public static IRubyObject op_equal(ThreadContext context, IRubyObject recv, IRubyObject other) {
157-
return callCmpMethod(context, recv, other, context.runtime.getFalse());
157+
return callCmpMethod(context, recv, other, context.fals);
158158
}
159159

160160
@Deprecated

core/src/main/java/org/jruby/RubyComplex.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ public IRubyObject conjugate(ThreadContext context) {
769769
@JRubyMethod(name = "real?")
770770
@Override
771771
public IRubyObject real_p(ThreadContext context) {
772-
return context.runtime.getFalse();
772+
return context.fals;
773773
}
774774

775775
@Override
@@ -780,7 +780,7 @@ public IRubyObject real_p(ThreadContext context) {
780780
*/
781781
// @JRubyMethod(name = "complex?")
782782
public IRubyObject complex_p(ThreadContext context) {
783-
return context.runtime.getTrue();
783+
return context.tru;
784784
}
785785

786786
/** nucomp_exact_p

core/src/main/java/org/jruby/RubyContinuation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public IRubyObject enter(ThreadContext context, IRubyObject yielded, Block block
109109
} catch (Continuation c) {
110110
if (c == continuation) {
111111
if (continuation.args.length == 0) {
112-
return context.runtime.getNil();
112+
return context.nil;
113113
} else if (continuation.args.length == 1) {
114114
return continuation.args[0];
115115
} else {

core/src/main/java/org/jruby/RubyDir.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ public IRubyObject set_pos(IRubyObject newPos) {
757757

758758
@JRubyMethod(name = {"path", "to_path"})
759759
public IRubyObject path(ThreadContext context) {
760-
return path == null ? context.runtime.getNil() : path.strDup(context.runtime);
760+
return path == null ? context.nil : path.strDup(context.runtime);
761761
}
762762

763763
@JRubyMethod

core/src/main/java/org/jruby/RubyException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public static IRubyObject op_eqq(ThreadContext context, IRubyObject recv, IRubyO
103103
Object object = ((ConcreteJavaProxy)other).getObject();
104104
if (object instanceof Throwable && !(object instanceof FlowControlException)) {
105105
if (recv == runtime.getException() || object instanceof java.lang.Exception) {
106-
return context.runtime.getTrue();
106+
return context.tru;
107107
}
108108
}
109109
}
@@ -273,7 +273,7 @@ public RubyString inspect(ThreadContext context) {
273273
@Override
274274
@JRubyMethod(name = "==")
275275
public RubyBoolean op_equal(ThreadContext context, IRubyObject other) {
276-
if (this == other) return context.runtime.getTrue();
276+
if (this == other) return context.tru;
277277

278278
boolean equal = context.runtime.getException().isInstance(other) &&
279279
getMetaClass().getRealClass() == other.getMetaClass().getRealClass() &&

0 commit comments

Comments
 (0)