Skip to content

Commit 3179da6

Browse files
committed
Keep the name in ReadSimpleGlobalVariableNode and WriteSimpleGlobalVariableNode
* Enables looking up again the storage if needed.
1 parent cf92d36 commit 3179da6

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.io.IOException;
1313

14+
import com.oracle.truffle.api.dsl.ImportStatic;
1415
import com.oracle.truffle.api.profiles.ConditionProfile;
1516

1617
import org.truffleruby.Layouts;
@@ -107,34 +108,28 @@ private RubySource loadFile(String feature) throws IOException {
107108

108109
// Only used internally with a constant literal name, does not trigger hooks
109110
@Primitive(name = "global_variable_set")
111+
@ImportStatic(Layouts.class)
110112
public abstract static class WriteGlobalVariableNode extends CoreMethodArrayArgumentsNode {
111113

112114
@Specialization(guards = { "isRubySymbol(cachedName)", "name == cachedName" }, limit = "1")
113115
public Object write(DynamicObject name, Object value,
114116
@Cached("name") DynamicObject cachedName,
115-
@Cached("create(getStorage(cachedName))") WriteSimpleGlobalVariableNode writeNode) {
117+
@Cached("create(SYMBOL.getString(cachedName))") WriteSimpleGlobalVariableNode writeNode) {
116118
return writeNode.execute(value);
117119
}
118-
119-
protected GlobalVariableStorage getStorage(DynamicObject name) {
120-
return coreLibrary().getGlobalVariables().getStorage(Layouts.SYMBOL.getString(name));
121-
}
122120
}
123121

124122
// Only used internally with a constant literal name, does not trigger hooks
125123
@Primitive(name = "global_variable_get")
124+
@ImportStatic(Layouts.class)
126125
public abstract static class ReadGlobalVariableNode extends CoreMethodArrayArgumentsNode {
127126

128127
@Specialization(guards = { "isRubySymbol(cachedName)", "name == cachedName" }, limit = "1")
129128
public Object read(DynamicObject name,
130129
@Cached("name") DynamicObject cachedName,
131-
@Cached("create(getStorage(cachedName))") ReadSimpleGlobalVariableNode readNode) {
130+
@Cached("create(SYMBOL.getString(cachedName))") ReadSimpleGlobalVariableNode readNode) {
132131
return readNode.execute();
133132
}
134-
135-
protected GlobalVariableStorage getStorage(DynamicObject name) {
136-
return coreLibrary().getGlobalVariables().getStorage(Layouts.SYMBOL.getString(name));
137-
}
138133
}
139134

140135
@CoreMethod(names = "define_hooked_variable_with_is_defined", isModuleFunction = true, required = 4)

src/main/java/org/truffleruby/language/globals/ReadGlobalVariableNode.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@
2121

2222
public abstract class ReadGlobalVariableNode extends RubyNode {
2323

24-
private final String name;
24+
protected final String name;
2525
@Child private IsDefinedGlobalVariableNode definedNode;
2626

2727
public ReadGlobalVariableNode(String name) {
2828
this.name = name;
2929
}
3030

3131
@Specialization(guards = "storage.isSimple()")
32-
public Object read(@Cached("getStorage()") GlobalVariableStorage storage,
33-
@Cached("create(storage)") ReadSimpleGlobalVariableNode simpleNode) {
32+
public Object read(
33+
@Cached("getStorage()") GlobalVariableStorage storage,
34+
@Cached("create(name)") ReadSimpleGlobalVariableNode simpleNode) {
3435
return simpleNode.execute();
3536
}
3637

src/main/java/org/truffleruby/language/globals/ReadSimpleGlobalVariableNode.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,33 @@
77

88
public abstract class ReadSimpleGlobalVariableNode extends RubyBaseNode {
99

10-
protected final GlobalVariableStorage storage;
10+
protected final String name;
1111

12-
public static ReadSimpleGlobalVariableNode create(GlobalVariableStorage storage) {
13-
return ReadSimpleGlobalVariableNodeGen.create(storage);
12+
public static ReadSimpleGlobalVariableNode create(String name) {
13+
return ReadSimpleGlobalVariableNodeGen.create(name);
1414
}
1515

16-
public ReadSimpleGlobalVariableNode(GlobalVariableStorage storage) {
17-
this.storage = storage;
16+
public ReadSimpleGlobalVariableNode(String name) {
17+
this.name = name;
1818
}
1919

2020
public abstract Object execute();
2121

2222
@Specialization(assumptions = "storage.getUnchangedAssumption()")
2323
public Object readConstant(
24+
@Cached("getStorage()") GlobalVariableStorage storage,
2425
@Cached("storage.getValue()") Object value) {
2526
return value;
2627
}
2728

2829
@Specialization
29-
public Object read() {
30+
public Object read(
31+
@Cached("getStorage()") GlobalVariableStorage storage) {
3032
return storage.getValue();
3133
}
3234

35+
protected GlobalVariableStorage getStorage() {
36+
return coreLibrary().getGlobalVariables().getStorage(name);
37+
}
38+
3339
}

src/main/java/org/truffleruby/language/globals/WriteGlobalVariableNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
@NodeChild(value = "value")
2323
public abstract class WriteGlobalVariableNode extends RubyNode {
2424

25-
private final String name;
25+
protected final String name;
2626

2727
public WriteGlobalVariableNode(String name) {
2828
this.name = name;
@@ -31,7 +31,7 @@ public WriteGlobalVariableNode(String name) {
3131
@Specialization(guards = "storage.isSimple()")
3232
public Object write(VirtualFrame frame, Object value,
3333
@Cached("getStorage()") GlobalVariableStorage storage,
34-
@Cached("create(storage)") WriteSimpleGlobalVariableNode simpleNode) {
34+
@Cached("create(name)") WriteSimpleGlobalVariableNode simpleNode) {
3535
simpleNode.execute(value);
3636
return value;
3737
}
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.truffleruby.language.globals;
22

3+
import com.oracle.truffle.api.object.DynamicObject;
4+
import org.truffleruby.Layouts;
35
import org.truffleruby.core.basicobject.BasicObjectNodes.ReferenceEqualNode;
46
import org.truffleruby.language.RubyBaseNode;
57
import org.truffleruby.language.objects.shared.WriteBarrierNode;
@@ -9,22 +11,23 @@
911

1012
public abstract class WriteSimpleGlobalVariableNode extends RubyBaseNode {
1113

12-
protected final GlobalVariableStorage storage;
14+
protected final String name;
1315
@Child protected ReferenceEqualNode referenceEqualNode = ReferenceEqualNode.create();
1416
@Child protected WriteBarrierNode writeBarrierNode = WriteBarrierNode.create();
1517

16-
public static WriteSimpleGlobalVariableNode create(GlobalVariableStorage storage) {
17-
return WriteSimpleGlobalVariableNodeGen.create(storage);
18+
public static WriteSimpleGlobalVariableNode create(String name) {
19+
return WriteSimpleGlobalVariableNodeGen.create(name);
1820
}
1921

20-
public WriteSimpleGlobalVariableNode(GlobalVariableStorage storage) {
21-
this.storage = storage;
22+
public WriteSimpleGlobalVariableNode(String name) {
23+
this.name = name;
2224
}
2325

2426
public abstract Object execute(Object value);
2527

2628
@Specialization(guards = "referenceEqualNode.executeReferenceEqual(value, previousValue)", assumptions = "storage.getUnchangedAssumption()")
2729
public Object writeTryToKeepConstant(Object value,
30+
@Cached("getStorage()") GlobalVariableStorage storage,
2831
@Cached("storage.getValue()") Object previousValue) {
2932
// NOTE: we still do the volatile write to get the proper memory barrier,
3033
// as the global variable could be used as a publication mechanism.
@@ -33,7 +36,8 @@ public Object writeTryToKeepConstant(Object value,
3336
}
3437

3538
@Specialization(guards = "storage.isAssumeConstant()", assumptions = "storage.getUnchangedAssumption()")
36-
public Object writeAssumeConstant(Object value) {
39+
public Object writeAssumeConstant(Object value,
40+
@Cached("getStorage()") GlobalVariableStorage storage) {
3741
if (getContext().getSharedObjects().isSharing()) {
3842
writeBarrierNode.executeWriteBarrier(value);
3943
}
@@ -43,12 +47,17 @@ public Object writeAssumeConstant(Object value) {
4347
}
4448

4549
@Specialization(guards = "!storage.isAssumeConstant()", replaces = "writeAssumeConstant")
46-
public Object write(Object value) {
50+
public Object write(Object value,
51+
@Cached("getStorage()") GlobalVariableStorage storage) {
4752
if (getContext().getSharedObjects().isSharing()) {
4853
writeBarrierNode.executeWriteBarrier(value);
4954
}
5055
storage.setValueInternal(value);
5156
return value;
5257
}
5358

59+
protected GlobalVariableStorage getStorage() {
60+
return coreLibrary().getGlobalVariables().getStorage(name);
61+
}
62+
5463
}

0 commit comments

Comments
 (0)