Skip to content

Commit 6a633b2

Browse files
committed
replace RubyLibrary with FreezeNode and IsFrozenNode
1 parent 7958712 commit 6a633b2

28 files changed

+237
-324
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Performance:
1515
Changes:
1616

1717

18+
Memory Footprint:
19+
20+
* Replaced `RubyLibrary` with `FreezeNode` and `IsFrozenNode` (@horakivo).
21+
22+
1823
# 23.0.0
1924

2025
New features:

src/main/java/org/truffleruby/Layouts.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
public abstract class Layouts {
1515

16+
public static final int FROZEN_FLAG = 1;
17+
1618
// Special variables
1719

1820
public static final String TEMP_PREFIX = "%";

src/main/java/org/truffleruby/core/array/RubyArray.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.truffleruby.language.RubyDynamicObject;
2222
import org.truffleruby.language.RubyGuards;
2323
import org.truffleruby.language.dispatch.DispatchNode;
24-
import org.truffleruby.language.library.RubyLibrary;
24+
import org.truffleruby.language.objects.IsFrozenNode;
2525
import org.truffleruby.language.objects.ObjectGraph;
2626
import org.truffleruby.language.objects.ObjectGraphNode;
2727

@@ -126,20 +126,20 @@ public boolean isArrayElementReadable(long index) {
126126

127127
@ExportMessage
128128
public boolean isArrayElementModifiable(long index,
129-
@CachedLibrary("this") RubyLibrary rubyLibrary) {
130-
return !rubyLibrary.isFrozen(this) && inBounds(index);
129+
@Cached IsFrozenNode isFrozenNode) {
130+
return !isFrozenNode.execute(this) && inBounds(index);
131131
}
132132

133133
@ExportMessage
134134
public boolean isArrayElementRemovable(long index,
135-
@CachedLibrary("this") RubyLibrary rubyLibrary) {
136-
return !rubyLibrary.isFrozen(this) && inBounds(index);
135+
@Cached IsFrozenNode isFrozenNode) {
136+
return !isFrozenNode.execute(this) && inBounds(index);
137137
}
138138

139139
@ExportMessage
140140
public boolean isArrayElementInsertable(long index,
141-
@CachedLibrary("this") RubyLibrary rubyLibrary) {
142-
return !rubyLibrary.isFrozen(this) && RubyGuards.fitsInInteger(index) && index >= size;
141+
@Cached IsFrozenNode isFrozenNode) {
142+
return !isFrozenNode.execute(this) && RubyGuards.fitsInInteger(index) && index >= size;
143143
}
144144
// endregion
145145

src/main/java/org/truffleruby/core/format/read/bytes/ReadStringPointerNode.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.truffleruby.extra.ffi.Pointer;
2020
import org.truffleruby.language.Nil;
2121
import org.truffleruby.language.control.RaiseException;
22-
import org.truffleruby.language.library.RubyLibrary;
2322

2423
import com.oracle.truffle.api.dsl.NodeChild;
2524
import com.oracle.truffle.api.dsl.Specialization;
@@ -46,7 +45,6 @@ protected MissingValue decode(Nil nil) {
4645

4746
@Specialization
4847
protected RubyString read(VirtualFrame frame, long address,
49-
@CachedLibrary(limit = "getRubyLibraryCacheLimit()") RubyLibrary rubyLibrary,
5048
@CachedLibrary(limit = "1") InteropLibrary interop) {
5149
final Pointer pointer = new Pointer(getContext(), address);
5250
checkAssociated(

src/main/java/org/truffleruby/core/hash/RubyHash.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.truffleruby.language.Nil;
3636
import org.truffleruby.language.RubyDynamicObject;
3737
import org.truffleruby.language.dispatch.DispatchNode;
38-
import org.truffleruby.language.library.RubyLibrary;
38+
import org.truffleruby.language.objects.IsFrozenNode;
3939
import org.truffleruby.language.objects.ObjectGraph;
4040
import org.truffleruby.language.objects.ObjectGraphNode;
4141

@@ -133,15 +133,15 @@ public final boolean isHashEntryExisting(Object key,
133133
@ExportMessage(name = "isHashEntryRemovable")
134134
public boolean isHashEntryModifiableAndRemovable(Object key,
135135
@CachedLibrary("this") InteropLibrary interop,
136-
@CachedLibrary("this") RubyLibrary rubyLibrary) {
137-
return !rubyLibrary.isFrozen(this) && interop.isHashEntryExisting(this, key);
136+
@Cached IsFrozenNode isFrozenNode) {
137+
return !isFrozenNode.execute(this) && interop.isHashEntryExisting(this, key);
138138
}
139139

140140
@ExportMessage
141141
public boolean isHashEntryInsertable(Object key,
142142
@CachedLibrary("this") InteropLibrary interop,
143-
@CachedLibrary("this") RubyLibrary rubyLibrary) {
144-
return !rubyLibrary.isFrozen(this) && !interop.isHashEntryExisting(this, key);
143+
@Cached IsFrozenNode isFrozenNode) {
144+
return !isFrozenNode.execute(this) && !interop.isHashEntryExisting(this, key);
145145
}
146146

147147
@ExportMessage(limit = "hashStrategyLimit()")
@@ -168,10 +168,10 @@ public Object readHashValueOrDefault(Object key, Object defaultValue,
168168
@ExportMessage
169169
public void writeHashEntry(Object key, Object value,
170170
@Cached @Exclusive DispatchNode set,
171-
@CachedLibrary("this") RubyLibrary rubyLibrary,
171+
@Cached IsFrozenNode isFrozenNode,
172172
@Cached @Shared ForeignToRubyNode toRuby)
173173
throws UnsupportedMessageException {
174-
if (rubyLibrary.isFrozen(this)) {
174+
if (isFrozenNode.execute(this)) {
175175
throw UnsupportedMessageException.create();
176176
}
177177
set.call(this, "[]=", toRuby.executeConvert(key), value);
@@ -180,11 +180,11 @@ public void writeHashEntry(Object key, Object value,
180180
@ExportMessage
181181
public void removeHashEntry(Object key,
182182
@Cached @Exclusive DispatchNode delete,
183-
@CachedLibrary("this") RubyLibrary rubyLibrary,
183+
@Cached IsFrozenNode isFrozenNode,
184184
@CachedLibrary("this") InteropLibrary interop,
185185
@Cached @Shared ForeignToRubyNode toRuby)
186186
throws UnsupportedMessageException, UnknownKeyException {
187-
if (rubyLibrary.isFrozen(this)) {
187+
if (isFrozenNode.execute(this)) {
188188
throw UnsupportedMessageException.create();
189189
}
190190
if (!interop.isHashEntryExisting(this, key)) {

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
118118
import org.truffleruby.language.dispatch.RubyCallNode;
119119
import org.truffleruby.language.loader.EvalLoader;
120120
import org.truffleruby.language.globals.ReadGlobalVariableNodeGen;
121-
import org.truffleruby.language.library.RubyLibrary;
122121
import org.truffleruby.language.library.RubyStringLibrary;
123122
import org.truffleruby.language.loader.RequireNode;
124123
import org.truffleruby.language.loader.RequireNodeGen;
@@ -128,9 +127,11 @@
128127
import org.truffleruby.annotations.Split;
129128
import org.truffleruby.language.objects.AllocationTracing;
130129
import org.truffleruby.language.objects.CheckIVarNameNode;
130+
import org.truffleruby.language.objects.FreezeNode;
131131
import org.truffleruby.language.objects.IsANode;
132132
import org.truffleruby.language.objects.IsCopyableObjectNode;
133133
import org.truffleruby.language.objects.IsCopyableObjectNodeGen;
134+
import org.truffleruby.language.objects.IsFrozenNode;
134135
import org.truffleruby.language.objects.LogicalClassNode;
135136
import org.truffleruby.language.objects.MetaClassNode;
136137
import org.truffleruby.language.objects.ShapeCachingGuards;
@@ -553,15 +554,15 @@ public abstract static class CloneNode extends PrimitiveArrayArgumentsNode {
553554
@Child SingletonClassNode singletonClassNode;
554555
private final BranchProfile cantUnfreezeErrorProfile = BranchProfile.create();
555556

556-
@Specialization(guards = "isCopyableObjectNode.execute(object)", limit = "getRubyLibraryCacheLimit()")
557+
@Specialization(guards = "isCopyableObjectNode.execute(object)")
557558
protected RubyDynamicObject copyable(Object object, Object freeze,
558559
@Cached MetaClassNode metaClassNode,
559560
@Cached CopyNode copyNode,
560561
@Cached DispatchNode initializeCloneNode,
561562
@Cached ConditionProfile isSingletonProfile,
562563
@Cached HashingNodes.ToHashByHashCode hashNode,
563-
@CachedLibrary("object") RubyLibrary rubyLibrary,
564-
@CachedLibrary(limit = "getRubyLibraryCacheLimit()") RubyLibrary rubyLibraryFreeze) {
564+
@Cached IsFrozenNode isFrozenNode,
565+
@Cached FreezeNode freezeNode) {
565566
final RubyDynamicObject newObject = copyNode.executeCopy(object);
566567

567568
// Copy the singleton class if any.
@@ -582,8 +583,8 @@ protected RubyDynamicObject copyable(Object object, Object freeze,
582583
}
583584

584585
// Default behavior - is just to copy the frozen state of the original object
585-
if (forceFrozen(freeze) || (copyFrozen && rubyLibrary.isFrozen(object))) { // Profiled through lazy usage of rubyLibraryFreeze
586-
rubyLibraryFreeze.freeze(newObject);
586+
if (forceFrozen(freeze) || (copyFrozen && isFrozenNode.execute(object))) { // Profiled through lazy usage of rubyLibraryFreeze
587+
freezeNode.execute(newObject);
587588
}
588589

589590
return newObject;
@@ -839,10 +840,10 @@ protected Object freeze(Object self,
839840
@GenerateUncached
840841
@CoreMethod(names = "frozen?", alwaysInlined = true)
841842
public abstract static class KernelFrozenNode extends AlwaysInlinedMethodNode {
842-
@Specialization(limit = "getRubyLibraryCacheLimit()")
843+
@Specialization
843844
protected boolean isFrozen(Frame callerFrame, Object self, Object[] rubyArgs, RootCallTarget target,
844-
@CachedLibrary("self") RubyLibrary rubyLibrary) {
845-
return rubyLibrary.isFrozen(self);
845+
@Cached IsFrozenNode isFrozenNode) {
846+
return isFrozenNode.execute(self);
846847
}
847848
}
848849

src/main/java/org/truffleruby/core/module/ModuleFields.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.truffleruby.language.control.RaiseException;
4949
import org.truffleruby.language.loader.ReentrantLockFreeingMap;
5050
import org.truffleruby.language.methods.InternalMethod;
51+
import org.truffleruby.language.objects.IsFrozenNodeGen;
5152
import org.truffleruby.language.objects.ObjectGraph;
5253
import org.truffleruby.language.objects.ObjectGraphNode;
5354
import org.truffleruby.language.objects.classvariables.ClassVariableStorage;
@@ -241,7 +242,7 @@ public void initCopy(RubyModule from) {
241242
}
242243

243244
public void checkFrozen(RubyContext context, Node currentNode) {
244-
if (context.getCoreLibrary() != null && rubyModule.isFrozenUncached()) {
245+
if (context.getCoreLibrary() != null && IsFrozenNodeGen.getUncached().execute(rubyModule)) {
245246
String name;
246247
Object receiver = rubyModule;
247248
if (rubyModule instanceof RubyClass) {

src/main/java/org/truffleruby/core/module/ModuleNodes.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@
102102
import org.truffleruby.language.control.ReturnID;
103103
import org.truffleruby.language.dispatch.DispatchNode;
104104
import org.truffleruby.language.loader.EvalLoader;
105-
import org.truffleruby.language.library.RubyLibrary;
106105
import org.truffleruby.language.library.RubyStringLibrary;
107106
import org.truffleruby.language.loader.CodeLoader;
108107
import org.truffleruby.language.methods.Arity;
@@ -114,6 +113,7 @@
114113
import org.truffleruby.language.methods.UsingNode;
115114
import org.truffleruby.language.objects.AllocationTracing;
116115
import org.truffleruby.language.objects.IsANode;
116+
import org.truffleruby.language.objects.IsFrozenNode;
117117
import org.truffleruby.language.objects.SingletonClassNode;
118118
import org.truffleruby.language.objects.WriteObjectFieldNode;
119119
import org.truffleruby.language.objects.classvariables.CheckClassVariableNameNode;
@@ -447,9 +447,9 @@ protected Object notObject(Frame callerFrame, Object self, Object[] rubyArgs, Ro
447447
@GenerateUncached
448448
public abstract static class GeneratedWriterNode extends AlwaysInlinedMethodNode {
449449

450-
@Specialization(guards = "!rubyLibrary.isFrozen(self)")
450+
@Specialization(guards = "!isFrozenNode.execute(self)", limit = "1")
451451
protected Object writer(Frame callerFrame, Object self, Object[] rubyArgs, RootCallTarget target,
452-
@CachedLibrary(limit = "getRubyLibraryCacheLimit()") RubyLibrary rubyLibrary,
452+
@Cached IsFrozenNode isFrozenNode,
453453
@Cached WriteObjectFieldNode writeObjectFieldNode) {
454454
final String ivarName = RubyRootNode.of(target).getSharedMethodInfo().getNotes();
455455
CompilerAsserts.partialEvaluationConstant(ivarName);
@@ -459,9 +459,9 @@ protected Object writer(Frame callerFrame, Object self, Object[] rubyArgs, RootC
459459
return value;
460460
}
461461

462-
@Specialization(guards = "rubyLibrary.isFrozen(self)")
462+
@Specialization(guards = "isFrozenNode.execute(self)", limit = "1")
463463
protected Object frozen(Frame callerFrame, Object self, Object[] rubyArgs, RootCallTarget target,
464-
@CachedLibrary(limit = "getRubyLibraryCacheLimit()") RubyLibrary rubyLibrary) {
464+
@Cached IsFrozenNode isFrozenNode) {
465465
throw new RaiseException(getContext(), coreExceptions().frozenError(self, this));
466466
}
467467
}

src/main/java/org/truffleruby/core/range/RubyObjectRange.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@
1111

1212
import java.util.Set;
1313

14-
import com.oracle.truffle.api.library.ExportLibrary;
15-
import com.oracle.truffle.api.library.ExportMessage;
1614
import com.oracle.truffle.api.object.Shape;
1715
import org.truffleruby.core.klass.RubyClass;
1816
import org.truffleruby.language.Nil;
1917
import org.truffleruby.language.RubyDynamicObject;
20-
import org.truffleruby.language.library.RubyLibrary;
2118
import org.truffleruby.language.objects.ObjectGraph;
2219
import org.truffleruby.language.objects.ObjectGraphNode;
2320

24-
@ExportLibrary(RubyLibrary.class)
2521
public final class RubyObjectRange extends RubyDynamicObject implements ObjectGraphNode {
2622

2723
public boolean excludedEnd;
@@ -43,16 +39,6 @@ public RubyObjectRange(
4339
this.frozen = frozen;
4440
}
4541

46-
@ExportMessage
47-
protected void freeze() {
48-
frozen = true;
49-
}
50-
51-
@ExportMessage
52-
protected boolean isFrozen() {
53-
return frozen;
54-
}
55-
5642
public boolean isBoundless() {
5743
return begin == Nil.INSTANCE && end == Nil.INSTANCE;
5844
}

src/main/java/org/truffleruby/core/string/RubyString.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.truffleruby.core.klass.RubyClass;
2828
import org.truffleruby.language.RubyBaseNode;
2929
import org.truffleruby.language.RubyDynamicObject;
30-
import org.truffleruby.language.library.RubyLibrary;
3130

3231
import com.oracle.truffle.api.dsl.Cached;
3332
import com.oracle.truffle.api.library.ExportMessage;
@@ -36,7 +35,6 @@
3635

3736
import java.util.Objects;
3837

39-
@ExportLibrary(RubyLibrary.class)
4038
@ExportLibrary(InteropLibrary.class)
4139
@ImportStatic(RubyBaseNode.class)
4240
public final class RubyString extends RubyDynamicObject {
@@ -105,17 +103,13 @@ public RubyEncoding getEncodingUnprofiled() {
105103
return encoding;
106104
}
107105

108-
// region RubyLibrary messages
109-
@ExportMessage
110106
public void freeze() {
111107
frozen = true;
112108
}
113109

114-
@ExportMessage
115110
public boolean isFrozen() {
116111
return frozen;
117112
}
118-
// endregion
119113

120114
// region String messages
121115
@ExportMessage

0 commit comments

Comments
 (0)