Skip to content

Commit dacf741

Browse files
author
Adam Hrbac
committed
Improve the way contextvars.Context is copied
1 parent 53adfc9 commit dacf741

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ public abstract static class GetDefaultEncodingNode extends PythonBuiltinNode {
7878
@Specialization
7979
protected Object copyCtx() {
8080
PythonContext.PythonThreadState threadState = getContext().getThreadState(getLanguage());
81-
PContextVarsContext ret = factory().createContextVarsContext();
82-
ret.contextVarValues = threadState.getContextVarsContext().contextVarValues;
83-
return ret;
81+
return factory().copyContextVarsContext(threadState.getContextVarsContext());
8482
}
8583
}
8684

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import com.oracle.truffle.api.object.Shape;
4949

5050
public class PContextVarsContext extends PythonBuiltinObject {
51-
public Hamt contextVarValues = new Hamt();
51+
Hamt contextVarValues;
5252
private PContextVarsContext previousContext = null;
5353

5454
public void enter(PythonContext.PythonThreadState threadState) {
@@ -68,6 +68,16 @@ public void leave(PythonContext.PythonThreadState threadState) {
6868
}
6969

7070
public PContextVarsContext(Object cls, Shape instanceShape) {
71+
this(new Hamt(), cls, instanceShape);
72+
}
73+
74+
public PContextVarsContext(PContextVarsContext original, Object cls, Shape instanceShape) {
75+
this(original.contextVarValues, cls, instanceShape);
76+
}
77+
78+
private PContextVarsContext(Hamt contextVarValues, Object cls, Shape instanceShape) {
7179
super(cls, instanceShape);
80+
this.contextVarValues = contextVarValues;
81+
7282
}
7383
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,10 @@ public final PContextVarsContext createContextVarsContext() {
14521452
return trace(new PContextVarsContext(PythonBuiltinClassType.ContextVarsContext, getShape(PythonBuiltinClassType.ContextVarsContext)));
14531453
}
14541454

1455+
public final PContextVarsContext copyContextVarsContext(PContextVarsContext original) {
1456+
return trace(new PContextVarsContext(original, PythonBuiltinClassType.ContextVarsContext, getShape(PythonBuiltinClassType.ContextVarsContext)));
1457+
}
1458+
14551459
public final PContextVarsToken createContextVarsToken(PContextVar var, Object oldValue) {
14561460
return trace(new PContextVarsToken(var, oldValue == null ? PContextVarsToken.MISSING : oldValue, PythonBuiltinClassType.ContextVarsToken, getShape(PythonBuiltinClassType.ContextVarsToken)));
14571461
}

0 commit comments

Comments
 (0)