Skip to content

Commit 7c411d9

Browse files
committed
SharedObjects.isShared() does not need the context
* If an object has a shared Shape then the option must be enabled.
1 parent a4a46b7 commit 7c411d9

File tree

10 files changed

+27
-42
lines changed

10 files changed

+27
-42
lines changed

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

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

1212
import java.lang.reflect.Array;
1313

14-
import org.truffleruby.RubyContext;
1514
import org.truffleruby.core.array.library.ArrayStoreLibrary;
1615
import org.truffleruby.core.array.library.DelegatedArrayStorage;
1716
import org.truffleruby.core.array.library.NativeArrayStorage;
@@ -35,16 +34,14 @@ public static boolean verifyStore(RubyArray array) {
3534
backingStore instanceof int[] || backingStore instanceof long[] || backingStore instanceof double[] ||
3635
backingStore.getClass() == Object[].class : backingStore;
3736

38-
final RubyContext context = array.getLogicalClass().fields.getContext();
39-
if (SharedObjects.isShared(context, array)) {
37+
if (SharedObjects.isShared(array)) {
4038
final Object store = array.store;
4139

4240
if (store.getClass() == Object[].class) {
4341
final Object[] objectArray = (Object[]) store;
4442

4543
for (Object element : objectArray) {
4644
assert SharedObjects.assertPropagateSharing(
47-
context,
4845
array,
4946
element) : "unshared element in shared Array: " + element;
5047
}
@@ -56,7 +53,6 @@ public static boolean verifyStore(RubyArray array) {
5653
for (int i = delegated.offset; i < delegated.offset + delegated.length; i++) {
5754
final Object element = objectArray[i];
5855
assert SharedObjects.assertPropagateSharing(
59-
context,
6056
array,
6157
element) : "unshared element in shared copy-on-write Array: " + element;
6258
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,9 @@ public static boolean verifyStore(RubyContext context, RubyHash hash) {
5959

6060
while (entry != null) {
6161
assert SharedObjects.assertPropagateSharing(
62-
context,
6362
hash,
6463
entry.getKey()) : "unshared key in shared Hash: " + entry.getKey();
6564
assert SharedObjects.assertPropagateSharing(
66-
context,
6765
hash,
6866
entry.getValue()) : "unshared value in shared Hash: " + entry.getValue();
6967

@@ -119,8 +117,8 @@ public static boolean verifyStore(RubyContext context, RubyHash hash) {
119117
final Object key = PackedArrayStrategy.getKey(packedStore, n);
120118
final Object value = PackedArrayStrategy.getValue(packedStore, n);
121119

122-
assert SharedObjects.assertPropagateSharing(context, hash, key) : "unshared key in shared Hash: " + key;
123-
assert SharedObjects.assertPropagateSharing(context, hash, value) : "unshared value in shared Hash: " +
120+
assert SharedObjects.assertPropagateSharing(hash, key) : "unshared key in shared Hash: " + key;
121+
assert SharedObjects.assertPropagateSharing(hash, value) : "unshared value in shared Hash: " +
124122
value;
125123
}
126124

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ protected Object removeInstanceVariable(RubyDynamicObject object, String name) {
11021102
final String ivar = SymbolTable.checkInstanceVariableName(getContext(), name, object, this);
11031103
final Object value = DynamicObjectLibrary.getUncached().getOrDefault(object, ivar, nil);
11041104

1105-
if (SharedObjects.isShared(getContext(), object)) {
1105+
if (SharedObjects.isShared(object)) {
11061106
synchronized (object) {
11071107
removeField(object, name);
11081108
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ public void addMethod(RubyContext context, Node currentNode, InternalMethod meth
405405

406406
checkFrozen(context, currentNode);
407407

408-
if (SharedObjects.isShared(context, rubyModule)) {
408+
if (SharedObjects.isShared(rubyModule)) {
409409
Set<Object> adjacent = ObjectGraph.newObjectSet();
410410
ObjectGraph.addProperty(adjacent, method);
411411
for (Object object : adjacent) {

src/main/java/org/truffleruby/debug/TruffleDebugNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,13 @@ public abstract static class IsSharedNode extends CoreMethodArrayArgumentsNode {
363363
limit = "getCacheLimit()")
364364
protected boolean isSharedCached(RubyDynamicObject object,
365365
@Cached("object.getShape()") Shape cachedShape,
366-
@Cached("isShared(getContext(), cachedShape)") boolean shared) {
366+
@Cached("cachedShape.isShared()") boolean shared) {
367367
return shared;
368368
}
369369

370370
@Specialization(replaces = "isSharedCached")
371371
protected boolean isShared(RubyDynamicObject object) {
372-
return SharedObjects.isShared(getContext(), object);
372+
return SharedObjects.isShared(object);
373373
}
374374

375375
@Specialization

src/main/java/org/truffleruby/language/objects/ShapeCachingGuards.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
package org.truffleruby.language.objects;
1212

13-
import org.truffleruby.RubyLanguage;
1413
import org.truffleruby.language.RubyDynamicObject;
1514
import org.truffleruby.language.objects.shared.SharedObjects;
1615

@@ -24,7 +23,7 @@ public static boolean updateShape(RubyDynamicObject object) {
2423
// TODO (eregon, 14 July 2020): review callers, once they use the library they should not need to update the Shape manually anymore
2524
boolean updated = DynamicObjectLibrary.getUncached().updateShape(object);
2625
if (updated) {
27-
assert !SharedObjects.isShared(RubyLanguage.getCurrentContext(), object);
26+
assert !SharedObjects.isShared(object);
2827
}
2928
return updated;
3029
}

src/main/java/org/truffleruby/language/objects/shared/IsSharedNode.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected boolean isShareCached(RubyDynamicObject object,
4343
@Cached("object.getShape()") Shape cachedShape,
4444
@CachedContext(RubyLanguage.class) TruffleLanguage.ContextReference<RubyContext> contextReference,
4545
@Cached("contextReference.get()") RubyContext cachedContext,
46-
@Cached("isShared(cachedContext, cachedShape)") boolean shared) {
46+
@Cached("cachedShape.isShared()") boolean shared) {
4747
return shared;
4848
}
4949

@@ -55,11 +55,7 @@ protected boolean updateShapeAndIsShared(RubyDynamicObject object) {
5555
@Specialization(replaces = { "isShareCached", "updateShapeAndIsShared" })
5656
protected boolean isSharedUncached(RubyDynamicObject object,
5757
@CachedContext(RubyLanguage.class) RubyContext context) {
58-
return SharedObjects.isShared(context, object);
59-
}
60-
61-
protected boolean isShared(RubyContext context, Shape shape) {
62-
return SharedObjects.isShared(context, shape);
58+
return context.getOptions().SHARED_OBJECTS_ENABLED && SharedObjects.isShared(object);
6359
}
6460

6561
}

src/main/java/org/truffleruby/language/objects/shared/ShareObjectNode.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ protected void shareCached(RubyDynamicObject object,
5959
assert object.getShape() == sharedShape;
6060

6161
// Share the logical class
62-
if (!SharedObjects.isShared(getContext(), object.getLogicalClass().getShape())) {
62+
if (!object.getLogicalClass().getShape().isShared()) {
6363
// The logical class is fixed for a given Shape and only needs to be shared once
6464
CompilerDirectives.transferToInterpreterAndInvalidate();
6565
SharedObjects.writeBarrier(getContext(), object.getLogicalClass());
6666
}
6767

6868
// Share the metaclass. Note that the metaclass might refer to `object` via `attached`,
6969
// so it is important to share the object first.
70-
if (!SharedObjects.isShared(getContext(), object.getMetaClass().getShape())) {
70+
if (!object.getMetaClass().getShape().isShared()) {
7171
// The metaclass is fixed for a given Shape and only needs to be shared once
7272
CompilerDirectives.transferToInterpreterAndInvalidate();
7373
SharedObjects.writeBarrier(getContext(), object.getMetaClass());
@@ -84,7 +84,7 @@ protected void shareCached(RubyDynamicObject object,
8484

8585
private boolean allFieldsAreShared(RubyDynamicObject object) {
8686
for (Object value : ObjectGraph.getAdjacentObjects(object)) {
87-
assert SharedObjects.isShared(getContext(), value) : "unshared field in shared object: " + value;
87+
assert SharedObjects.isShared(value) : "unshared field in shared object: " + value;
8888
}
8989

9090
return true;
@@ -126,7 +126,7 @@ protected ReadAndShareFieldNode[] createReadAndShareFieldNodes(List<Property> pr
126126
}
127127

128128
protected Shape createSharedShape(Shape cachedShape) {
129-
if (SharedObjects.isShared(getContext(), cachedShape)) {
129+
if (cachedShape.isShared()) {
130130
throw new UnsupportedOperationException(
131131
"Thread-safety bug: the object is already shared. This means another thread marked the object as shared concurrently.");
132132
} else {

src/main/java/org/truffleruby/language/objects/shared/SharedObjects.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
2626
import com.oracle.truffle.api.object.DynamicObjectLibrary;
27-
import com.oracle.truffle.api.object.Shape;
2827
import com.oracle.truffle.api.source.SourceSection;
2928

3029
public class SharedObjects {
@@ -112,38 +111,39 @@ private static void shareObject(RubyContext context, RubyDynamicObject value) {
112111
shareObjects(context, stack);
113112
}
114113

115-
public static boolean isShared(RubyContext context, Object object) {
114+
/** Callers of this should be careful, this method will return true for RubySymbol even if the
115+
* SHARED_OBJECTS_ENABLED option is false. */
116+
public static boolean isShared(Object object) {
116117
return object instanceof RubySymbol ||
117-
(object instanceof RubyDynamicObject && isShared(context, ((RubyDynamicObject) object).getShape()));
118+
(object instanceof RubyDynamicObject && isShared((RubyDynamicObject) object));
118119
}
119120

120-
public static boolean isShared(RubyContext context, Shape shape) {
121-
return context.getOptions().SHARED_OBJECTS_ENABLED && shape.isShared();
121+
public static boolean isShared(RubyDynamicObject object) {
122+
return object.getShape().isShared();
122123
}
123124

124-
public static boolean assertPropagateSharing(RubyContext context, RubyDynamicObject source, Object value) {
125-
if (isShared(context, source) && value instanceof RubyDynamicObject) {
126-
return isShared(context, value);
125+
public static boolean assertPropagateSharing(RubyDynamicObject source, Object value) {
126+
if (isShared(source) && value instanceof RubyDynamicObject) {
127+
return isShared(value);
127128
} else {
128129
return true;
129130
}
130131
}
131132

132133
public static void writeBarrier(RubyContext context, Object value) {
133-
if (context.getOptions().SHARED_OBJECTS_ENABLED && value instanceof RubyDynamicObject &&
134-
!isShared(context, value)) {
134+
if (context.getOptions().SHARED_OBJECTS_ENABLED && value instanceof RubyDynamicObject && !isShared(value)) {
135135
shareObject(context, (RubyDynamicObject) value);
136136
}
137137
}
138138

139139
public static void propagate(RubyContext context, RubyDynamicObject source, Object value) {
140-
if (isShared(context, source)) {
140+
if (isShared(source)) {
141141
writeBarrier(context, value);
142142
}
143143
}
144144

145145
private static boolean share(RubyContext context, RubyDynamicObject object) {
146-
if (isShared(context, object)) {
146+
if (isShared(object)) {
147147
return false;
148148
}
149149

src/main/java/org/truffleruby/language/objects/shared/WriteBarrierNode.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected void writeBarrierCached(RubyDynamicObject value,
5151
@Cached("value.getShape()") Shape cachedShape,
5252
@CachedContext(RubyLanguage.class) ContextReference<RubyContext> contextReference,
5353
@Cached("contextReference.get()") RubyContext cachedContext,
54-
@Cached("isShared(cachedContext, cachedShape)") boolean alreadyShared,
54+
@Cached("cachedShape.isShared()") boolean alreadyShared,
5555
@Cached("createShareObjectNode(alreadyShared)") ShareObjectNode shareObjectNode) {
5656
if (!alreadyShared) {
5757
shareObjectNode.executeShare(value);
@@ -73,10 +73,6 @@ protected void writeBarrierUncached(RubyDynamicObject value,
7373
protected void noWriteBarrier(Object value) {
7474
}
7575

76-
protected boolean isShared(RubyContext context, Shape shape) {
77-
return SharedObjects.isShared(context, shape);
78-
}
79-
8076
protected ShareObjectNode createShareObjectNode(boolean alreadyShared) {
8177
if (!alreadyShared) {
8278
return ShareObjectNodeGen.create(getDepth() + 1);

0 commit comments

Comments
 (0)