Skip to content

Commit e23ced2

Browse files
authored
Merge pull request #9095 from headius/smaller_flags
Eliminate global flags
2 parents 95c9078 + 5280fbf commit e23ced2

17 files changed

Lines changed: 249 additions & 216 deletions

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,50 @@ public interface ObjectFlags {
88

99
// These flags must be registered from top of hierarchy down to maintain order.
1010
// TODO: Replace these during the build with their calculated values.
11+
@Deprecated(since = "10.0.3.0")
1112
int FALSE_F = registry.newFlag(RubyBasicObject.class);
13+
@Deprecated(since = "10.0.3.0")
1214
int NIL_F = registry.newFlag(RubyBasicObject.class);
15+
@Deprecated(since = "10.0.3.0")
1316
int FROZEN_F = registry.newFlag(RubyBasicObject.class);
1417

18+
@Deprecated(since = "10.0.3.0")
1519
int CACHEPROXY_F = registry.newFlag(RubyModule.class);
20+
@Deprecated(since = "10.0.3.0")
1621
int NEEDSIMPL_F = registry.newFlag(RubyModule.class);
22+
@Deprecated(since = "10.0.3.0")
1723
int REFINED_MODULE_F = registry.newFlag(RubyModule.class);
24+
@Deprecated(since = "10.0.3.0")
1825
int IS_OVERLAID_F = registry.newFlag(RubyModule.class);
1926
int OMOD_SHARED = registry.newFlag(RubyModule.class);
27+
@Deprecated(since = "10.0.3.0")
2028
int INCLUDED_INTO_REFINEMENT = registry.newFlag(RubyModule.class);
29+
@Deprecated(since = "10.0.3.0")
2130
int TEMPORARY_NAME = registry.newFlag(RubyModule.class);
2231

2332
// order is important here; CR_7BIT_f needs to be 16 and CR_VALID_F needs to be 32 to match values in Prism parser
33+
@Deprecated(since = "10.0.3.0")
2434
int FSTRING = registry.newFlag(RubyString.class);
35+
@Deprecated(since = "10.0.3.0")
2536
int CR_7BIT_F = registry.newFlag(RubyString.class);
37+
@Deprecated(since = "10.0.3.0")
2638
int CR_VALID_F = registry.newFlag(RubyString.class);
39+
@Deprecated(since = "10.0.3.0")
2740
int CHILLED_LITERAL_F = registry.newFlag(RubyString.class);
41+
@Deprecated(since = "10.0.3.0")
2842
int CHILLED_SYMBOL_TO_S_F = registry.newFlag(RubyString.class);
2943

44+
@Deprecated(since = "10.0.3.0")
3045
int MATCH_BUSY = registry.newFlag(RubyMatchData.class);
3146

47+
@Deprecated(since = "10.0.3.0")
3248
int COMPARE_BY_IDENTITY_F = registry.newFlag(RubyHash.class);
49+
@Deprecated(since = "10.0.3.0")
3350
int KEYWORD_REST_ARGUMENTS_F = registry.newFlag(RubyHash.class);
51+
@Deprecated(since = "10.0.3.0")
3452
int PROCDEFAULT_HASH_F = registry.newFlag(RubyHash.class);
53+
@Deprecated(since = "10.0.3.0")
3554
int KEYWORD_ARGUMENTS_F = registry.newFlag(RubyHash.class);
55+
@Deprecated(since = "10.0.3.0")
3656
int RUBY2_KEYWORD_F = registry.newFlag(RubyHash.class);
3757
}

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,6 @@ public static RubyArray newArrayNoCopyLight(Ruby runtime, IRubyObject[] args) {
403403
public static final int ARRAY_DEFAULT_SIZE = 16;
404404
private static final int SMALL_ARRAY_LEN = 16;
405405

406-
private static final int TMPLOCK_ARR_F = 1 << 9;
407-
private static final int TMPLOCK_OR_FROZEN_ARR_F = TMPLOCK_ARR_F | FROZEN_F;
408-
409406
private volatile boolean isShared = false;
410407

411408
protected IRubyObject[] values;
@@ -646,10 +643,7 @@ protected final void modifyCheck() {
646643
*
647644
*/
648645
protected final void modifyCheck(ThreadContext context) {
649-
if ((flags & TMPLOCK_OR_FROZEN_ARR_F) != 0) {
650-
if ((flags & FROZEN_F) != 0) throw context.runtime.newFrozenError(this);
651-
if ((flags & TMPLOCK_ARR_F) != 0) throw typeError(context, "can't modify array during iteration");
652-
}
646+
if (isFrozen()) throw context.runtime.newFrozenError(this);
653647
}
654648

655649
@Deprecated(since = "10.0.0.0")
@@ -1725,15 +1719,6 @@ public RubyBoolean include_p(ThreadContext context, IRubyObject item) {
17251719
return asBoolean(context, includes(context, item));
17261720
}
17271721

1728-
/** rb_ary_frozen_p
1729-
*
1730-
*/
1731-
@JRubyMethod(name = "frozen?")
1732-
@Override
1733-
public RubyBoolean frozen_p(ThreadContext context) {
1734-
return asBoolean(context, isFrozen() || (flags & TMPLOCK_ARR_F) != 0);
1735-
}
1736-
17371722
/**
17381723
* Variable arity version for compatibility. Not bound to a Ruby method.
17391724
* @deprecated Use the versions with zero, one, or two args.

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

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
import org.jruby.runtime.ivars.VariableTableManager;
6262
import org.jruby.runtime.marshal.CoreObjectType;
6363
import org.jruby.util.ArraySupport;
64-
import org.jruby.util.ByteList;
6564
import org.jruby.util.IdUtil;
6665
import org.jruby.util.TypeConverter;
6766

@@ -168,7 +167,7 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
168167
protected transient RubyClass metaClass;
169168

170169
/** object flags */
171-
protected int flags;
170+
protected byte flags;
172171

173172
/** variable table, lazily allocated as needed (if needed) */
174173
public transient Object[] varTable;
@@ -182,8 +181,15 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
182181
*/
183182
public static final String ERR_INSECURE_SET_INST_VAR = "Insecure: can't modify instance variable";
184183

184+
static final byte FALSE = 0b00000001;
185+
static final byte NIL = 0b00000010;
186+
static final byte FROZEN = 0b00000100;
187+
188+
@Deprecated(since = "10.0.3.0")
185189
public static final int ALL_F = -1;
186-
public static final int FALSE_F = ObjectFlags.FALSE_F;
190+
/** @deprecated external access to global object flags is going away */
191+
@Deprecated(since = "10.0.3.0")
192+
public static final int FALSE_F = FALSE;
187193
/**
188194
* This flag is a bit funny. It's used to denote that this value
189195
* is nil. It's a bit counterintuitive for a Java programmer to
@@ -193,8 +199,12 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
193199
* that it gives a good speed boost to make it monomorphic and
194200
* final. It turns out using a flag for this actually gives us
195201
* better performance than having a polymorphic {@link #isNil()} method.
202+
*
203+
* @deprecated external access to global object flags is going away
196204
*/
197-
public static final int NIL_F = ObjectFlags.NIL_F;
205+
@Deprecated(since = "10.0.3.0")
206+
public static final int NIL_F = NIL;
207+
@Deprecated(since = "10.0.3.0")
198208
public static final int FROZEN_F = ObjectFlags.FROZEN_F;
199209

200210
/**
@@ -310,40 +320,28 @@ protected final void testFrozen() {
310320
}
311321

312322
/**
313-
* Sets or unsets a flag on this object. The only flags that are
314-
* guaranteed to be valid to use as the first argument is:
315-
*
316-
* <ul>
317-
* <li>{@link #FALSE_F}</li>
318-
* <li>{@link #NIL_F}</li>
319-
* <li>{@link #FROZEN_F}</li>
320-
* </ul>
321-
*
322-
* @param flag the actual flag to set or unset.
323-
* @param set if true, the flag will be set, if false, the flag will be unset.
323+
* @deprecated external access to global object flags is going away
324324
*/
325+
@Deprecated(since = "10.0.3.0")
325326
public final void setFlag(int flag, boolean set) {
326-
if (set) {
327+
if (flag == FROZEN) {
328+
setFrozen(set);
329+
} else if (set) {
327330
flags |= flag;
328331
} else {
329332
flags &= ~flag;
330333
}
331334
}
332335

333336
/**
334-
* Get the value of a custom flag on this object. The only
335-
* guaranteed flags that can be sent in to this method is:
336-
*
337-
* <ul>
338-
* <li>{@link #FALSE_F}</li>
339-
* <li>{@link #NIL_F}</li>
340-
* <li>{@link #FROZEN_F}</li>
341-
* </ul>
342-
*
343-
* @param flag the flag to get
344-
* @return true if the flag is set, false otherwise
337+
* @deprecated external access to global object flags is going away
345338
*/
339+
@Deprecated(since = "10.0.3.0")
346340
public final boolean getFlag(int flag) {
341+
if (flag == FROZEN) {
342+
return isFrozen();
343+
}
344+
347345
return (flags & flag) != 0;
348346
}
349347

@@ -413,54 +411,55 @@ public final IRubyObject callMethod(ThreadContext context, String name, IRubyObj
413411
}
414412

415413
/**
416-
* Does this object represent nil? See the docs for the {@link
417-
* #NIL_F} flag for more information.
414+
* Does this object represent nil?
415+
*
416+
* @return true if nil
418417
*/
419418
@Override
420419
public final boolean isNil() {
421-
return (flags & NIL_F) != 0;
420+
return (flags & NIL) != 0;
422421
}
423422

424423
/**
425-
* Is this value a truthy value or not? Based on the {@link #FALSE_F} flag.
426-
* @return true it truthy
424+
* Is this value a truthy value or not?
425+
*
426+
* @return true if truthy
427427
*/
428428
@Override
429429
public final boolean isTrue() {
430-
return (flags & FALSE_F) == 0;
430+
return (flags & FALSE) == 0;
431431
}
432432

433433
/**
434-
* Is this value a falsey value or not? Based on the {@link #FALSE_F} flag.
435-
* @return true is false
434+
* Is this value a falsey value or not?
435+
*
436+
* @return true if falsey
436437
*/
437438
public final boolean isFalse() {
438-
return (flags & FALSE_F) != 0;
439+
return (flags & FALSE) != 0;
439440
}
440441

441-
/**
442-
* Is this value frozen or not? Shortcut for doing
443-
* getFlag(FROZEN_F).
442+
/** whether the object has been frozen */ /**
443+
* Is this value frozen or not?
444444
*
445445
* @return true if this object is frozen, false otherwise
446446
*/
447447
@Override
448448
public boolean isFrozen() {
449-
return (flags & FROZEN_F) != 0;
449+
return (flags & FROZEN) != 0;
450450
}
451451

452452
/**
453-
* Sets whether this object is frozen or not. Shortcut for doing
454-
* setFlag(FROZEN_F, frozen).
453+
* Sets whether this object is frozen or not.
455454
*
456455
* @param frozen should this object be frozen?
457456
*/
458457
@Override
459458
public void setFrozen(boolean frozen) {
460459
if (frozen) {
461-
flags |= FROZEN_F;
460+
flags |= FROZEN;
462461
} else {
463-
flags &= ~FROZEN_F;
462+
flags &= ~FROZEN;
464463
}
465464
}
466465

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public class RubyBoolean extends RubyObject implements Constantizable, Appendabl
5858
(value ? runtime.getTrueClass() : runtime.getFalseClass()),
5959
false); // Don't put in object space
6060

61-
if (!value) flags = FALSE_F;
61+
if (!value) flags = FALSE;
62+
setFrozen(true);
6263

6364
if (RubyInstanceConfig.CONSISTENT_HASHING_ENABLED) {
6465
// default to a fixed value
@@ -76,7 +77,7 @@ public class RubyBoolean extends RubyObject implements Constantizable, Appendabl
7677

7778
@Override
7879
public ClassIndex getNativeClassIndex() {
79-
return (flags & FALSE_F) == 0 ? ClassIndex.TRUE : ClassIndex.FALSE;
80+
return (flags & FALSE) == 0 ? ClassIndex.TRUE : ClassIndex.FALSE;
8081
}
8182

8283
@Override
@@ -135,8 +136,6 @@ public void appendIntoString(RubyString target) {
135136
public static class False extends RubyBoolean {
136137
False(Ruby runtime) {
137138
super(runtime, false);
138-
139-
flags = FALSE_F | FROZEN_F;
140139
}
141140

142141
@JRubyMethod(name = "&")
@@ -184,7 +183,7 @@ public static class True extends RubyBoolean {
184183
True(Ruby runtime) {
185184
super(runtime, true);
186185

187-
flags |= FROZEN_F;
186+
setFrozen(true);
188187
}
189188

190189
@JRubyMethod(name = "&")
@@ -243,7 +242,7 @@ public int hashCode() {
243242
@Deprecated(since = "10.0.3.0")
244243
@Override
245244
public RubyFixnum id() {
246-
if ((flags & FALSE_F) == 0) {
245+
if ((flags & FALSE) == 0) {
247246
return metaClass.runtime.getTrueID();
248247
} else {
249248
return RubyFixnum.zero(metaClass.runtime);

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
import static org.jruby.api.Convert.asBoolean;
6464
import static org.jruby.api.Convert.asFixnum;
6565
import static org.jruby.api.Convert.asFloat;
66-
import static org.jruby.api.Convert.asInteger;
6766
import static org.jruby.api.Convert.toInt;
6867
import static org.jruby.api.Convert.toLong;
6968
import static org.jruby.api.Create.newArray;
@@ -122,13 +121,13 @@ public RubyFixnum(Ruby runtime) {
122121
public RubyFixnum(Ruby runtime, long value) {
123122
super(runtime.getFixnum());
124123
this.value = value;
125-
this.flags |= FROZEN_F;
124+
this.setFrozen(true);
126125
}
127126

128127
private RubyFixnum(RubyClass klazz, long value) {
129128
super(klazz);
130129
this.value = value;
131-
this.flags |= FROZEN_F;
130+
this.setFrozen(true);
132131
}
133132

134133
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ public RubyFloat(Ruby runtime) {
141141
public RubyFloat(Ruby runtime, double value) {
142142
super(runtime.getFloat());
143143
this.value = value;
144-
this.flags |= FROZEN_F;
144+
this.setFrozen(true);
145145
}
146146

147147
private RubyFloat(RubyClass klass, double value) {
148148
super(klass);
149149
this.value = value;
150-
this.flags |= FROZEN_F;
150+
this.setFrozen(true);
151151
}
152152

153153
public RubyClass singletonClass(ThreadContext context) {

0 commit comments

Comments
 (0)