Skip to content

Commit c8122e8

Browse files
committed
[GR-38880] Refactor contextvars to be closer to cpython and faster
PullRequest: graalpython/2276
2 parents b56497a + a03b50f commit c8122e8

File tree

4 files changed

+21
-29
lines changed

4 files changed

+21
-29
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ContextvarsModuleBuiltins.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,9 @@
5050
import com.oracle.graal.python.builtins.PythonBuiltins;
5151
import com.oracle.graal.python.builtins.objects.PNone;
5252
import com.oracle.graal.python.builtins.objects.contextvars.PContextVar;
53-
import com.oracle.graal.python.builtins.objects.function.PKeyword;
5453
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5554
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5655
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
57-
import com.oracle.graal.python.nodes.statement.ImportNode;
58-
import com.oracle.graal.python.util.PythonUtils;
5956
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6057
import com.oracle.truffle.api.dsl.NodeFactory;
6158
import com.oracle.truffle.api.dsl.Specialization;
@@ -88,12 +85,7 @@ protected Object construct(VirtualFrame frame, Object cls, String name, PNone de
8885

8986
@Specialization(guards = "!isPNone(def)")
9087
protected Object constructDef(VirtualFrame frame, Object cls, String name, Object def) {
91-
Object local = factory().createThreadLocal(PythonBuiltinClassType.PThreadLocal, PythonUtils.EMPTY_OBJECT_ARRAY, PKeyword.EMPTY_KEYWORDS);
92-
return factory().createContextVar(name, def, local);
93-
}
94-
95-
protected ImportNode.ImportExpression createImportThreading() {
96-
return ImportNode.createAsExpression("threading");
88+
return factory().createContextVar(name, def);
9789
}
9890
}
9991

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/contextvars/ContextVarBuiltins.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,8 @@
4949
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5050
import com.oracle.graal.python.builtins.PythonBuiltins;
5151
import com.oracle.graal.python.builtins.objects.PNone;
52-
import com.oracle.graal.python.lib.PyObjectLookupAttr;
53-
import com.oracle.graal.python.lib.PyObjectSetAttr;
5452
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5553
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
56-
import com.oracle.truffle.api.dsl.Cached;
5754
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
5855
import com.oracle.truffle.api.dsl.NodeFactory;
5956
import com.oracle.truffle.api.dsl.Specialization;
@@ -71,16 +68,14 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
7168
@GenerateNodeFactory
7269
public abstract static class GetNode extends PythonBinaryBuiltinNode {
7370
@Specialization
74-
Object get(VirtualFrame frame, PContextVar self, PNone def,
75-
@Cached PyObjectLookupAttr lookupAtrrNode) {
76-
return get(frame, self, PContextVar.NO_DEFAULT, lookupAtrrNode);
71+
Object get(VirtualFrame frame, PContextVar self, PNone def) {
72+
return get(frame, self, PContextVar.NO_DEFAULT);
7773
}
7874

7975
@Specialization(guards = "!isPNone(def)")
80-
Object get(VirtualFrame frame, PContextVar self, Object def,
81-
@Cached PyObjectLookupAttr lookupAtrrNode) {
82-
Object value = lookupAtrrNode.execute(frame, self.getLocal(), "value");
83-
if (value != PNone.NO_VALUE) {
76+
Object get(VirtualFrame frame, PContextVar self, Object def) {
77+
Object value = self.getValue();
78+
if (value != null) {
8479
return value;
8580
}
8681
if (def != PContextVar.NO_DEFAULT) {
@@ -97,9 +92,8 @@ Object get(VirtualFrame frame, PContextVar self, Object def,
9792
@GenerateNodeFactory
9893
public abstract static class SetNode extends PythonBinaryBuiltinNode {
9994
@Specialization
100-
static Object get(VirtualFrame frame, PContextVar self, Object value,
101-
@Cached PyObjectSetAttr setAtrrNode) {
102-
setAtrrNode.execute(frame, self.getLocal(), "value", value);
95+
static Object get(VirtualFrame frame, PContextVar self, Object value) {
96+
self.setValue(value);
10397
return PNone.NONE;
10498
}
10599
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/contextvars/PContextVar.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,21 @@
4141
package com.oracle.graal.python.builtins.objects.contextvars;
4242

4343
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
44+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4445
import com.oracle.truffle.api.object.Shape;
4546

4647
public final class PContextVar extends PythonBuiltinObject {
4748
private final String name;
4849
private final Object def;
49-
private final Object local;
50+
private final ThreadLocal<Object> value;
5051

5152
public static final Object NO_DEFAULT = new Object();
5253

53-
public PContextVar(Object cls, Shape instanceShape, String name, Object def, Object local) {
54+
public PContextVar(Object cls, Shape instanceShape, String name, Object def) {
5455
super(cls, instanceShape);
5556
this.name = name;
5657
this.def = def;
57-
this.local = local;
58+
this.value = new ThreadLocal<>();
5859
}
5960

6061
public String getName() {
@@ -65,8 +66,13 @@ public Object getDefault() {
6566
return def;
6667
}
6768

68-
public Object getLocal() {
69-
return local;
69+
@TruffleBoundary
70+
public Object getValue() {
71+
return value.get();
7072
}
7173

74+
@TruffleBoundary
75+
public void setValue(Object value) {
76+
this.value.set(value);
77+
}
7278
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ public final PDebugHandle createDebugHandle(GraalHPyHandle handle) {
14351435
return trace(new PDebugHandle(PythonBuiltinClassType.DebugHandle, getShape(PythonBuiltinClassType.DebugHandle), handle));
14361436
}
14371437

1438-
public final PContextVar createContextVar(String name, Object def, Object local) {
1439-
return trace(new PContextVar(PythonBuiltinClassType.ContextVar, getShape(PythonBuiltinClassType.ContextVar), name, def, local));
1438+
public final PContextVar createContextVar(String name, Object def) {
1439+
return trace(new PContextVar(PythonBuiltinClassType.ContextVar, getShape(PythonBuiltinClassType.ContextVar), name, def));
14401440
}
14411441
}

0 commit comments

Comments
 (0)