Skip to content

Commit e5c92e4

Browse files
author
Adam Hrbac
committed
[GR-40449] implement contextvars
PullRequest: graalpython/2417
2 parents f7a2c06 + ae41301 commit e5c92e4

File tree

15 files changed

+1204
-22
lines changed

15 files changed

+1204
-22
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.modules;
42+
43+
import static org.junit.Assert.assertEquals;
44+
import static org.junit.Assert.assertNull;
45+
import static org.junit.Assert.fail;
46+
47+
import org.junit.After;
48+
import org.junit.Before;
49+
import org.junit.Test;
50+
51+
import com.oracle.graal.python.builtins.objects.contextvars.Hamt;
52+
import com.oracle.graal.python.test.PythonTests;
53+
54+
public class HamtTests {
55+
@Before
56+
public void enter() {
57+
PythonTests.enterContext();
58+
}
59+
60+
@After
61+
public void close() {
62+
PythonTests.closeContext();
63+
}
64+
65+
@Test
66+
public void testHamtConstructed() {
67+
Hamt hamt = new Hamt();
68+
assertEquals("null\n", hamt.dump());
69+
}
70+
71+
@Test
72+
public void testHamtSetLookup() {
73+
Hamt hamt = new Hamt();
74+
hamt = hamt.withEntry(new Hamt.Entry(1, 1, 1));
75+
assertEquals(1, hamt.lookup(1, 1));
76+
hamt = hamt.withEntry(new Hamt.Entry(0, 0, 0));
77+
assertEquals(0, hamt.lookup(0, 0));
78+
assertEquals(1, hamt.lookup(1, 1));
79+
}
80+
81+
@Test
82+
public void testBitmapExtremes() {
83+
Hamt hamt = new Hamt();
84+
hamt = hamt.withEntry(new Hamt.Entry(1, 0, 1));
85+
hamt = hamt.withEntry(new Hamt.Entry(2, 31, 2));
86+
assertEquals(2, hamt.lookup(2, 31));
87+
hamt = new Hamt();
88+
hamt = hamt.withEntry(new Hamt.Entry(1, 31, 1));
89+
hamt = hamt.withEntry(new Hamt.Entry(2, 0, 2));
90+
assertEquals(2, hamt.lookup(2, 0));
91+
assertEquals(1, hamt.lookup(1, 31));
92+
}
93+
94+
@Test
95+
public void testSequentialNumbers() {
96+
int limit = 700;
97+
Hamt hamt = new Hamt();
98+
for (int i = 0; i < limit; ++i) {
99+
hamt = hamt.withEntry(new Hamt.Entry(i, i, i));
100+
for (int j = 0; j < limit; ++j) {
101+
assertEquals(j <= i ? j : null, hamt.lookup(j, j));
102+
}
103+
}
104+
}
105+
106+
@Test
107+
public void testLotsOfEntries() {
108+
Hamt hamt = new Hamt();
109+
for (int i = 0; i < 100000; ++i) {
110+
hamt = hamt.withEntry(new Hamt.Entry(i, i * 3, i * 2));
111+
}
112+
for (int i = 0; i < 100000; ++i) {
113+
assertEquals(i * 2, hamt.lookup(i, i * 3));
114+
}
115+
}
116+
117+
@Test
118+
public void addAndRemoveEntries() {
119+
int limit = 700;
120+
Hamt hamt = new Hamt();
121+
for (int i = 0; i < limit; ++i) {
122+
hamt = hamt.withEntry(new Hamt.Entry(i, i * 3, i));
123+
for (int j = 0; j < limit; ++j) {
124+
assertNull(hamt.without(j, j * 3).lookup(j, j * 3));
125+
}
126+
}
127+
}
128+
129+
@Test
130+
public void addAndRemoveAllEntries() {
131+
int limit = 70000;
132+
Hamt hamt = new Hamt();
133+
for (int i = 0; i < limit; ++i) {
134+
hamt = hamt.withEntry(new Hamt.Entry(i, i * 3, i + 1));
135+
}
136+
Hamt fullHamt = hamt;
137+
for (int i = limit - 1; i >= 0; --i) {
138+
hamt = hamt.without(i, i * 3);
139+
if (hamt.lookup(i, i * 3) != null) {
140+
fail(i + ":" + hamt.dump());
141+
}
142+
}
143+
assertEquals("null\n", hamt.dump());
144+
hamt = fullHamt;
145+
for (int i = 0; i < limit; ++i) {
146+
hamt = hamt.without(i, i * 3);
147+
if (hamt.lookup(i, i * 3) != null) {
148+
fail(i + ":" + hamt.dump());
149+
}
150+
}
151+
assertEquals("null\n", hamt.dump());
152+
}
153+
154+
@Test
155+
public void largeHashVariance() {
156+
int limit = 70000;
157+
Hamt hamt = new Hamt();
158+
for (int i = 0; i < limit; ++i) {
159+
hamt = hamt.withEntry(new Hamt.Entry(i, String.valueOf(i).hashCode(), i + 1));
160+
}
161+
for (int i = 0; i < limit; ++i) {
162+
assertEquals(i + 1, hamt.lookup(i, String.valueOf(i).hashCode()));
163+
}
164+
for (int i = 0; i < limit; ++i) {
165+
hamt = hamt.without(i, String.valueOf(i).hashCode());
166+
}
167+
assertEquals("null\n", hamt.dump());
168+
}
169+
170+
// @Test
171+
public void measureTimeForSmallHamtAcceses() {
172+
Hamt hamt = new Hamt().withEntry(new Hamt.Entry(1, 0, 1)).withEntry(new Hamt.Entry(2, 31, 2));
173+
long start = System.nanoTime();
174+
for (int i = 0; i < 1000000; ++i) {
175+
Hamt requestContext = hamt.withEntry(new Hamt.Entry(2, 31, 1));
176+
requestContext.lookup(2, 31);
177+
requestContext.lookup(1, 0);
178+
}
179+
System.out.println("Took " + (System.nanoTime() - start) + " nanoseconds");
180+
181+
}
182+
}

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_context.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_copy_1
2+
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_get_context_1
3+
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_getset_2
4+
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_getset_5
5+
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_run_1
6+
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_run_2
7+
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_run_3
8+
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_run_5
9+
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_run_6
10+
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_run_7
11+
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_subclassing_1
12+
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_threads_1
13+
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_typerrors_1
14+
*graalpython.lib-python.3.test.test_context.ContextTest.test_contextvar_getitem
115
*graalpython.lib-python.3.test.test_context.ContextTest.test_context_subclassing_1
216
*graalpython.lib-python.3.test.test_context.HamtTest.test_hamt_basics_1
317
*graalpython.lib-python.3.test.test_context.HamtTest.test_hamt_basics_2

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@
217217
import com.oracle.graal.python.builtins.objects.cell.CellBuiltins;
218218
import com.oracle.graal.python.builtins.objects.code.CodeBuiltins;
219219
import com.oracle.graal.python.builtins.objects.complex.ComplexBuiltins;
220+
import com.oracle.graal.python.builtins.objects.contextvars.ContextBuiltins;
220221
import com.oracle.graal.python.builtins.objects.contextvars.ContextVarBuiltins;
222+
import com.oracle.graal.python.builtins.objects.contextvars.TokenBuiltins;
221223
import com.oracle.graal.python.builtins.objects.deque.DequeBuiltins;
222224
import com.oracle.graal.python.builtins.objects.deque.DequeIterBuiltins;
223225
import com.oracle.graal.python.builtins.objects.dict.DefaultDictBuiltins;
@@ -559,6 +561,8 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
559561
new ReferenceTypeBuiltins(),
560562
new WarningsModuleBuiltins(),
561563
new ContextVarBuiltins(),
564+
new ContextBuiltins(),
565+
new TokenBuiltins(),
562566
// exceptions
563567
new SystemExitBuiltins(),
564568
new ImportErrorBuiltins(),

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@
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.nodes.ErrorMessages;
54+
import com.oracle.graal.python.nodes.PRaiseNode;
5355
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5456
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5557
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
58+
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
59+
import com.oracle.graal.python.runtime.PythonContext;
60+
import com.oracle.truffle.api.dsl.Cached;
5661
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
5762
import com.oracle.truffle.api.dsl.NodeFactory;
5863
import com.oracle.truffle.api.dsl.Specialization;
@@ -72,7 +77,8 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
7277
public abstract static class GetDefaultEncodingNode extends PythonBuiltinNode {
7378
@Specialization
7479
protected Object copyCtx() {
75-
throw raise(PythonBuiltinClassType.NotImplementedError);
80+
PythonContext.PythonThreadState threadState = getContext().getThreadState(getLanguage());
81+
return factory().copyContextVarsContext(threadState.getContextVarsContext());
7682
}
7783
}
7884

@@ -90,4 +96,22 @@ protected Object constructDef(VirtualFrame frame, Object cls, TruffleString name
9096
}
9197
}
9298

99+
@Builtin(name = "Context", minNumOfPositionalArgs = 1, constructsClass = PythonBuiltinClassType.ContextVarsContext)
100+
@GenerateNodeFactory
101+
public abstract static class ContextNode extends PythonUnaryBuiltinNode {
102+
@Specialization
103+
Object construct(VirtualFrame frame, Object cls) {
104+
return factory().createContextVarsContext();
105+
}
106+
}
107+
108+
@Builtin(name = "Token", minNumOfPositionalArgs = 1, constructsClass = PythonBuiltinClassType.ContextVarsToken)
109+
@GenerateNodeFactory
110+
public abstract static class TokenNode extends PythonUnaryBuiltinNode {
111+
@Specialization
112+
Object construct(VirtualFrame frame, Object cls,
113+
@Cached PRaiseNode raise) {
114+
throw raise.raise(PythonBuiltinClassType.RuntimeError, ErrorMessages.TOKEN_ONLY_BY_CONTEXTVAR);
115+
}
116+
}
93117
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyContextFunctions.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3756,7 +3756,8 @@ int execute(Object[] arguments,
37563756
if (!(var instanceof PContextVar)) {
37573757
throw raiseNode.raise(TypeError, ErrorMessages.INSTANCE_OF_CONTEXTVAR_EXPECTED);
37583758
}
3759-
Object result = ((PContextVar) var).getValue();
3759+
PythonContext.PythonThreadState threadState = context.getContext().getThreadState(PythonLanguage.get(asContextNode));
3760+
Object result = ((PContextVar) var).getValue(threadState);
37603761
if (result == null) {
37613762
if (def == NULL_HANDLE_DELEGATE) {
37623763
def = ((PContextVar) var).getDefault();
@@ -3801,7 +3802,8 @@ Object execute(Object[] arguments,
38013802
if (!(var instanceof PContextVar)) {
38023803
throw raiseNode.raise(TypeError, ErrorMessages.INSTANCE_OF_CONTEXTVAR_EXPECTED);
38033804
}
3804-
((PContextVar) var).setValue(val);
3805+
PythonContext.PythonThreadState threadState = context.getContext().getThreadState(PythonLanguage.get(asContextNode));
3806+
((PContextVar) var).setValue(threadState, val);
38053807
return asHandleNode.execute(context, PNone.NONE);
38063808
} catch (PException e) {
38073809
transformExceptionToNativeNode.execute(context, e);

0 commit comments

Comments
 (0)