Skip to content

Commit 02654d3

Browse files
fangereransalond
authored andcommitted
Make PCells more lightweight.
(cherry picked from commit 6eefbd8)
1 parent 2a2609d commit 02654d3

File tree

4 files changed

+62
-30
lines changed

4 files changed

+62
-30
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cell/PCell.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -40,24 +40,16 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.cell;
4242

43-
import java.util.ArrayList;
44-
import java.util.List;
45-
46-
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
47-
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
43+
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
4844
import com.oracle.truffle.api.Assumption;
4945
import com.oracle.truffle.api.CompilerAsserts;
50-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
46+
import com.oracle.truffle.api.CompilerDirectives;
5147
import com.oracle.truffle.api.Truffle;
5248

53-
public class PCell extends PythonBuiltinObject {
54-
private final Assumption effectivelyFinal = Truffle.getRuntime().createAssumption("cell is effectively final");
49+
public final class PCell extends PythonAbstractObject {
50+
private Assumption effectivelyFinal;
5551
private Object ref;
5652

57-
public PCell() {
58-
super(PythonBuiltinClassType.PCell);
59-
}
60-
6153
public Object getRef() {
6254
return ref;
6355
}
@@ -67,7 +59,7 @@ public void clearRef() {
6759
}
6860

6961
public void setRef(Object ref) {
70-
if (effectivelyFinal.isValid()) {
62+
if (effectivelyFinal != null && effectivelyFinal.isValid()) {
7163
if (this.ref != null) {
7264
effectivelyFinal.invalidate();
7365
}
@@ -76,17 +68,13 @@ public void setRef(Object ref) {
7668
}
7769

7870
public Assumption isEffectivelyFinalAssumption() {
71+
CompilerAsserts.neverPartOfCompilation();
72+
if (effectivelyFinal == null) {
73+
effectivelyFinal = Truffle.getRuntime().createAssumption("cell is effectively final");
74+
}
7975
return effectivelyFinal;
8076
}
8177

82-
@Override
83-
@TruffleBoundary
84-
public List<String> getAttributeNames() {
85-
ArrayList<String> arrayList = new ArrayList<>();
86-
arrayList.add("cell_contents");
87-
return arrayList;
88-
}
89-
9078
@Override
9179
public String toString() {
9280
CompilerAsserts.neverPartOfCompilation();
@@ -95,4 +83,10 @@ public String toString() {
9583
}
9684
return String.format("<cell at %s: %s object at %s>", hashCode(), ref.getClass().getSimpleName(), ref.hashCode());
9785
}
86+
87+
@Override
88+
public int compareTo(Object o) {
89+
CompilerDirectives.transferToInterpreter();
90+
throw new UnsupportedOperationException();
91+
}
9892
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/cell/WriteLocalCellNode.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@
3333
import com.oracle.graal.python.nodes.frame.WriteIdentifierNode;
3434
import com.oracle.graal.python.nodes.statement.StatementNode;
3535
import com.oracle.truffle.api.CompilerDirectives;
36+
import com.oracle.truffle.api.dsl.Cached;
3637
import com.oracle.truffle.api.dsl.NodeChild;
3738
import com.oracle.truffle.api.dsl.Specialization;
3839
import com.oracle.truffle.api.frame.FrameSlot;
3940
import com.oracle.truffle.api.frame.VirtualFrame;
41+
import com.oracle.truffle.api.nodes.Node;
4042
import com.oracle.truffle.api.nodes.NodeInfo;
43+
import com.oracle.truffle.api.profiles.ConditionProfile;
4144

4245
@NodeInfo(shortName = "write_cell")
4346
@NodeChild(value = "rhs", type = ExpressionNode.class)
@@ -63,15 +66,12 @@ public void doWrite(VirtualFrame frame, Object value) {
6366
public abstract void executeWithValue(VirtualFrame frame, Object value);
6467

6568
@Specialization
66-
void writeObject(VirtualFrame frame, Object value) {
69+
void writeObject(VirtualFrame frame, Object value,
70+
@Cached WriteToCellNode writeToCellNode,
71+
@Cached("createBinaryProfile()") ConditionProfile profile) {
6772
Object localValue = readLocal.execute(frame);
68-
if (localValue instanceof PCell) {
69-
PCell cell = (PCell) localValue;
70-
if (value == NO_VALUE) {
71-
cell.clearRef();
72-
} else {
73-
cell.setRef(value);
74-
}
73+
if (profile.profile(localValue instanceof PCell)) {
74+
writeToCellNode.execute((PCell) localValue, value);
7575
return;
7676
}
7777
CompilerDirectives.transferToInterpreter();
@@ -82,4 +82,24 @@ void writeObject(VirtualFrame frame, Object value) {
8282
public Object getIdentifier() {
8383
return frameSlot.getIdentifier();
8484
}
85+
86+
abstract static class WriteToCellNode extends Node {
87+
88+
public abstract void execute(PCell cell, Object value);
89+
90+
@Specialization(guards = "cell == cachedCell", limit = "1")
91+
void doWriteCached(@SuppressWarnings("unused") PCell cell, Object value,
92+
@Cached("cell") PCell cachedCell) {
93+
doWriteGeneric(cachedCell, value);
94+
}
95+
96+
@Specialization(replaces = "doWriteCached")
97+
void doWriteGeneric(PCell cell, Object value) {
98+
if (value == NO_VALUE) {
99+
cell.clearRef();
100+
} else {
101+
cell.setRef(value);
102+
}
103+
}
104+
}
85105
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/object/GetClassNode.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.oracle.graal.python.builtins.objects.PEllipsis;
4646
import com.oracle.graal.python.builtins.objects.PNone;
4747
import com.oracle.graal.python.builtins.objects.PNotImplemented;
48+
import com.oracle.graal.python.builtins.objects.cell.PCell;
4849
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.GetNativeClassNode;
4950
import com.oracle.graal.python.builtins.objects.cext.PythonAbstractNativeObject;
5051
import com.oracle.graal.python.builtins.objects.cext.PythonNativeVoidPtr;
@@ -113,6 +114,17 @@ protected PythonBuiltinClass getIt(@SuppressWarnings("unused") GetSetDescriptor
113114
return contextRef.get().getCore().lookupType(PythonBuiltinClassType.GetSetDescriptor);
114115
}
115116

117+
@Specialization(assumptions = "singleContextAssumption()")
118+
protected PythonBuiltinClass getIt(@SuppressWarnings("unused") PCell object,
119+
@Cached("getIt(object)") PythonBuiltinClass klass) {
120+
return klass;
121+
}
122+
123+
@Specialization
124+
protected PythonBuiltinClass getIt(@SuppressWarnings("unused") PCell object) {
125+
return contextRef.get().getCore().lookupType(PythonBuiltinClassType.PCell);
126+
}
127+
116128
@Specialization(assumptions = "singleContextAssumption()")
117129
protected PythonBuiltinClass getIt(@SuppressWarnings("unused") PNone object,
118130
@Cached("getIt(object)") PythonBuiltinClass klass) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/object/GetLazyClassNode.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.oracle.graal.python.builtins.objects.PEllipsis;
4545
import com.oracle.graal.python.builtins.objects.PNone;
4646
import com.oracle.graal.python.builtins.objects.PNotImplemented;
47+
import com.oracle.graal.python.builtins.objects.cell.PCell;
4748
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.GetNativeClassNode;
4849
import com.oracle.graal.python.builtins.objects.cext.PythonAbstractNativeObject;
4950
import com.oracle.graal.python.builtins.objects.cext.PythonNativeObject;
@@ -128,6 +129,11 @@ protected static LazyPythonClass getIt(@SuppressWarnings("unused") GetSetDescrip
128129
return PythonBuiltinClassType.GetSetDescriptor;
129130
}
130131

132+
@Specialization
133+
protected static LazyPythonClass getIt(@SuppressWarnings("unused") PCell object) {
134+
return PythonBuiltinClassType.PCell;
135+
}
136+
131137
@Specialization
132138
protected static LazyPythonClass getIt(@SuppressWarnings("unused") PNone object) {
133139
return PythonBuiltinClassType.PNone;

0 commit comments

Comments
 (0)