Skip to content

Commit 85e0648

Browse files
committed
[GR-30927] Intrinsify _collections.deque and _queue.SimpleQueue.
PullRequest: graalpython/1766
2 parents f7b276e + 3db2d89 commit 85e0648

File tree

20 files changed

+2142
-693
lines changed

20 files changed

+2142
-693
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@
2424
*graalpython.lib-python.3.test.test_deque.TestBasic.test_index
2525
*graalpython.lib-python.3.test.test_deque.TestBasic.test_index_bug_24913
2626
*graalpython.lib-python.3.test.test_deque.TestBasic.test_init
27+
*graalpython.lib-python.3.test.test_deque.TestBasic.test_insert
28+
*graalpython.lib-python.3.test.test_deque.TestBasic.test_insert_bug_26194
2729
*graalpython.lib-python.3.test.test_deque.TestBasic.test_len
2830
*graalpython.lib-python.3.test.test_deque.TestBasic.test_long_steadystate_queue_popleft
2931
*graalpython.lib-python.3.test.test_deque.TestBasic.test_long_steadystate_queue_popright
32+
*graalpython.lib-python.3.test.test_deque.TestBasic.test_maxlen
3033
*graalpython.lib-python.3.test.test_deque.TestBasic.test_maxlen_attribute
3134
*graalpython.lib-python.3.test.test_deque.TestBasic.test_maxlen_zero
3235
*graalpython.lib-python.3.test.test_deque.TestBasic.test_mul
36+
*graalpython.lib-python.3.test.test_deque.TestBasic.test_pickle
37+
*graalpython.lib-python.3.test.test_deque.TestBasic.test_pickle_recursive
38+
*graalpython.lib-python.3.test.test_deque.TestBasic.test_print
3339
*graalpython.lib-python.3.test.test_deque.TestBasic.test_remove
40+
*graalpython.lib-python.3.test.test_deque.TestBasic.test_repr
3441
*graalpython.lib-python.3.test.test_deque.TestBasic.test_reverse
3542
*graalpython.lib-python.3.test.test_deque.TestBasic.test_reversed
3643
*graalpython.lib-python.3.test.test_deque.TestBasic.test_reversed_new
@@ -55,6 +62,7 @@
5562
*graalpython.lib-python.3.test.test_deque.TestSequence.test_index
5663
*graalpython.lib-python.3.test.test_deque.TestSequence.test_len
5764
*graalpython.lib-python.3.test.test_deque.TestSequence.test_minmax
65+
*graalpython.lib-python.3.test.test_deque.TestSequence.test_pickle
5866
*graalpython.lib-python.3.test.test_deque.TestSequence.test_repeat
5967
*graalpython.lib-python.3.test.test_deque.TestSequence.test_subscript
6068
*graalpython.lib-python.3.test.test_deque.TestSequence.test_truth

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
*graalpython.lib-python.3.test.test_queue.CQueueTest.test_shrinking_queue
2020
*graalpython.lib-python.3.test.test_queue.CSimpleQueueTest.test_basic
2121
*graalpython.lib-python.3.test.test_queue.CSimpleQueueTest.test_is_default
22+
*graalpython.lib-python.3.test.test_queue.CSimpleQueueTest.test_many_threads
23+
*graalpython.lib-python.3.test.test_queue.CSimpleQueueTest.test_many_threads_nonblock
24+
*graalpython.lib-python.3.test.test_queue.CSimpleQueueTest.test_many_threads_timeout
2225
*graalpython.lib-python.3.test.test_queue.CSimpleQueueTest.test_negative_timeout_raises_exception
2326
*graalpython.lib-python.3.test.test_queue.CSimpleQueueTest.test_order
2427
*graalpython.lib-python.3.test.test_queue.CSimpleQueueTest.test_reentrancy
@@ -43,6 +46,9 @@
4346
*graalpython.lib-python.3.test.test_queue.PyQueueTest.test_queue_task_done
4447
*graalpython.lib-python.3.test.test_queue.PyQueueTest.test_shrinking_queue
4548
*graalpython.lib-python.3.test.test_queue.PySimpleQueueTest.test_basic
49+
*graalpython.lib-python.3.test.test_queue.PySimpleQueueTest.test_many_threads
50+
*graalpython.lib-python.3.test.test_queue.PySimpleQueueTest.test_many_threads_nonblock
51+
*graalpython.lib-python.3.test.test_queue.PySimpleQueueTest.test_many_threads_timeout
4652
*graalpython.lib-python.3.test.test_queue.PySimpleQueueTest.test_negative_timeout_raises_exception
4753
*graalpython.lib-python.3.test.test_queue.PySimpleQueueTest.test_order
4854
*graalpython.lib-python.3.test.test_queue.PySimpleQueueTest.test_references

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@
142142
import com.oracle.graal.python.builtins.objects.cell.CellBuiltins;
143143
import com.oracle.graal.python.builtins.objects.code.CodeBuiltins;
144144
import com.oracle.graal.python.builtins.objects.complex.ComplexBuiltins;
145+
import com.oracle.graal.python.builtins.objects.deque.DequeBuiltins;
146+
import com.oracle.graal.python.builtins.objects.deque.DequeIterBuiltins;
145147
import com.oracle.graal.python.builtins.objects.dict.DictBuiltins;
146148
import com.oracle.graal.python.builtins.objects.dict.DictReprBuiltin;
147149
import com.oracle.graal.python.builtins.objects.dict.DictValuesBuiltins;
@@ -191,6 +193,7 @@
191193
import com.oracle.graal.python.builtins.objects.posix.DirEntryBuiltins;
192194
import com.oracle.graal.python.builtins.objects.posix.ScandirIteratorBuiltins;
193195
import com.oracle.graal.python.builtins.objects.property.PropertyBuiltins;
196+
import com.oracle.graal.python.builtins.objects.queue.SimpleQueueBuiltins;
194197
import com.oracle.graal.python.builtins.objects.random.RandomBuiltins;
195198
import com.oracle.graal.python.builtins.objects.range.RangeBuiltins;
196199
import com.oracle.graal.python.builtins.objects.referencetype.ReferenceTypeBuiltins;
@@ -294,7 +297,6 @@ private static String[] initializeCoreFiles() {
294297
"termios",
295298
"zipimport",
296299
"mmap",
297-
"_queue",
298300
"_ast",
299301
"java",
300302
"pyio_patches",
@@ -438,6 +440,8 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
438440
new ErrnoModuleBuiltins(),
439441
new CodecsModuleBuiltins(),
440442
new CodecsTruffleModuleBuiltins(),
443+
new DequeBuiltins(),
444+
new DequeIterBuiltins(),
441445
new CollectionsModuleBuiltins(),
442446
new JavaModuleBuiltins(),
443447
new JArrayModuleBuiltins(),
@@ -482,6 +486,7 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
482486
new MMapModuleBuiltins(),
483487
new FcntlModuleBuiltins(),
484488
new MMapBuiltins(),
489+
new SimpleQueueBuiltins(),
485490
new QueueModuleBuiltins(),
486491
new ThreadModuleBuiltins(),
487492
new ThreadBuiltins(),

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
package com.oracle.graal.python.builtins;
2727

2828
import static com.oracle.graal.python.nodes.BuiltinNames.BUILTINS;
29+
import static com.oracle.graal.python.nodes.BuiltinNames.DEQUE;
30+
import static com.oracle.graal.python.nodes.BuiltinNames.DEQUE_ITER;
31+
import static com.oracle.graal.python.nodes.BuiltinNames.DEQUE_REV_ITER;
2932
import static com.oracle.graal.python.nodes.BuiltinNames.DICT_ITEMITERATOR;
3033
import static com.oracle.graal.python.nodes.BuiltinNames.DICT_ITEMS;
3134
import static com.oracle.graal.python.nodes.BuiltinNames.DICT_KEYITERATOR;
@@ -38,6 +41,7 @@
3841
import static com.oracle.graal.python.nodes.BuiltinNames.FOREIGN;
3942
import static com.oracle.graal.python.nodes.BuiltinNames.MEMBER_DESCRIPTOR;
4043
import static com.oracle.graal.python.nodes.BuiltinNames.PROPERTY;
44+
import static com.oracle.graal.python.nodes.BuiltinNames.SIMPLE_QUEUE;
4145

4246
import java.util.Arrays;
4347
import java.util.HashSet;
@@ -92,6 +96,9 @@ public enum PythonBuiltinClassType implements TruffleObject {
9296
PByteArray("bytearray", BUILTINS),
9397
PBytes("bytes", BUILTINS),
9498
PCell("cell", Flags.PRIVATE_DERIVED_WODICT),
99+
PDeque(DEQUE, "_collections", Flags.PUBLIC_BASE_WODICT),
100+
PDequeIter(DEQUE_ITER, "_collections", Flags.PUBLIC_DERIVED_WODICT),
101+
PDequeRevIter(DEQUE_REV_ITER, "_collections", Flags.PUBLIC_DERIVED_WODICT),
95102
PComplex("complex", BUILTINS),
96103
PDict("dict", BUILTINS),
97104
PDictItemIterator(DICT_ITEMITERATOR, Flags.PRIVATE_DERIVED_WODICT),
@@ -120,6 +127,7 @@ public enum PythonBuiltinClassType implements TruffleObject {
120127
PNone("NoneType", Flags.PRIVATE_DERIVED_WODICT),
121128
PNotImplemented("NotImplementedType", Flags.PRIVATE_DERIVED_WODICT),
122129
PProperty(PROPERTY, BUILTINS, Flags.PUBLIC_BASE_WODICT),
130+
PSimpleQueue(SIMPLE_QUEUE, "_queue", Flags.PUBLIC_BASE_WODICT),
123131
PRandom("Random", "_random"),
124132
PRange("range", BUILTINS, Flags.PUBLIC_DERIVED_WODICT),
125133
PReferenceType("ReferenceType", "_weakref"),
@@ -284,6 +292,8 @@ public enum PythonBuiltinClassType implements TruffleObject {
284292

285293
IOUnsupportedOperation("UnsupportedOperation", "io", Flags.EXCEPTION),
286294

295+
Empty("Empty", "_queue", Flags.EXCEPTION),
296+
287297
// warnings
288298
Warning("Warning", BUILTINS, Flags.EXCEPTION),
289299
BytesWarning("BytesWarning", BUILTINS, Flags.EXCEPTION),
@@ -600,6 +610,8 @@ public final Shape getInstanceShape(PythonLanguage lang) {
600610
PFileIO.base = PRawIOBase;
601611
PTextIOWrapper.base = PTextIOBase;
602612
PStringIO.base = PTextIOBase;
613+
614+
Empty.base = Exception;
603615
}
604616

605617
// Proxy InteropLibrary messages to the PythonBuiltinClass

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

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,131 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43-
import java.util.ArrayList;
4443
import java.util.List;
4544

45+
import com.oracle.graal.python.builtins.Builtin;
4646
import com.oracle.graal.python.builtins.CoreFunctions;
47+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4748
import com.oracle.graal.python.builtins.PythonBuiltins;
49+
import com.oracle.graal.python.builtins.objects.PNone;
50+
import com.oracle.graal.python.builtins.objects.deque.DequeIterBuiltins.DequeIterNextNode;
51+
import com.oracle.graal.python.builtins.objects.deque.PDeque;
52+
import com.oracle.graal.python.builtins.objects.deque.PDequeIter;
53+
import com.oracle.graal.python.builtins.objects.function.PKeyword;
54+
import com.oracle.graal.python.lib.PyNumberIndexNode;
55+
import com.oracle.graal.python.nodes.BuiltinNames;
56+
import com.oracle.graal.python.nodes.ErrorMessages;
4857
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
58+
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
59+
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
60+
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
61+
import com.oracle.truffle.api.CompilerDirectives;
62+
import com.oracle.truffle.api.dsl.Cached;
63+
import com.oracle.truffle.api.dsl.Cached.Shared;
64+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4965
import com.oracle.truffle.api.dsl.NodeFactory;
66+
import com.oracle.truffle.api.dsl.Specialization;
67+
import com.oracle.truffle.api.frame.VirtualFrame;
5068

5169
@CoreFunctions(defineModule = "_collections")
5270
public class CollectionsModuleBuiltins extends PythonBuiltins {
5371
@Override
5472
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
55-
return new ArrayList<>();
73+
return CollectionsModuleBuiltinsFactory.getFactories();
74+
}
75+
76+
// _collections.deque
77+
@Builtin(name = BuiltinNames.DEQUE, minNumOfPositionalArgs = 1, constructsClass = PythonBuiltinClassType.PDeque, takesVarArgs = true, takesVarKeywordArgs = true)
78+
@GenerateNodeFactory
79+
abstract static class DequeNode extends PythonVarargsBuiltinNode {
80+
81+
@Override
82+
public Object varArgExecute(VirtualFrame frame, Object self, Object[] arguments, PKeyword[] keywords) throws VarargsBuiltinDirectInvocationNotSupported {
83+
if (arguments.length >= 1) {
84+
return doGeneric(arguments[0], null, null);
85+
}
86+
CompilerDirectives.transferToInterpreterAndInvalidate();
87+
throw new VarargsBuiltinDirectInvocationNotSupported();
88+
}
89+
90+
@Specialization
91+
@SuppressWarnings("unused")
92+
PDeque doGeneric(Object cls, Object[] args, PKeyword[] kwargs) {
93+
return factory().createDeque(cls);
94+
}
95+
}
96+
97+
// _collections._deque_iterator
98+
@Builtin(name = BuiltinNames.DEQUE_ITER, constructsClass = PythonBuiltinClassType.PDequeIter, //
99+
minNumOfPositionalArgs = 2, parameterNames = {"$self", "iterable", "index"})
100+
@GenerateNodeFactory
101+
abstract static class DequeIterNode extends PythonTernaryBuiltinNode {
102+
103+
@Specialization(guards = "isNoValue(index)")
104+
PDequeIter doDeque(@SuppressWarnings("unused") Object cls, PDeque deque, @SuppressWarnings("unused") PNone index) {
105+
return factory().createDequeIter(deque);
106+
}
107+
108+
@Specialization
109+
PDequeIter doDequeInt(@SuppressWarnings("unused") Object cls, PDeque deque, int index,
110+
@Shared("getNextNode") @Cached DequeIterNextNode getNextNode) {
111+
PDequeIter dequeIter = factory().createDequeIter(deque);
112+
for (int i = 0; i < index; i++) {
113+
getNextNode.execute(dequeIter);
114+
}
115+
return dequeIter;
116+
}
117+
118+
@Specialization(replaces = {"doDeque", "doDequeInt"})
119+
PDequeIter doGeneric(VirtualFrame frame, @SuppressWarnings("unused") Object cls, Object deque, Object indexObj,
120+
@Cached PyNumberIndexNode toIndexNode,
121+
@Cached CastToJavaIntExactNode castToJavaIntExactNode,
122+
@Shared("getNextNode") @Cached DequeIterNextNode getNextNode) {
123+
if (deque instanceof PDeque) {
124+
if (indexObj != PNone.NO_VALUE) {
125+
int index = castToJavaIntExactNode.execute(toIndexNode.execute(frame, indexObj));
126+
return doDequeInt(cls, (PDeque) deque, index, getNextNode);
127+
}
128+
return doDeque(cls, (PDeque) deque, PNone.NO_VALUE);
129+
}
130+
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.EXPECTED_OBJ_TYPE_S_GOT_P, BuiltinNames.DEQUE, deque);
131+
}
132+
}
133+
134+
// _collections._deque_reverse_iterator
135+
@Builtin(name = BuiltinNames.DEQUE_REV_ITER, constructsClass = PythonBuiltinClassType.PDequeRevIter, //
136+
minNumOfPositionalArgs = 2, parameterNames = {"$self", "iterable", "index"})
137+
@GenerateNodeFactory
138+
abstract static class DequeRevIterNode extends PythonTernaryBuiltinNode {
139+
140+
@Specialization(guards = "isNoValue(index)")
141+
PDequeIter doDeque(@SuppressWarnings("unused") Object cls, PDeque deque, @SuppressWarnings("unused") PNone index) {
142+
return factory().createDequeRevIter(deque);
143+
}
144+
145+
@Specialization
146+
PDequeIter doDequeInt(@SuppressWarnings("unused") Object cls, PDeque deque, int index,
147+
@Shared("getNextNode") @Cached DequeIterNextNode getNextNode) {
148+
PDequeIter dequeIter = factory().createDequeRevIter(deque);
149+
for (int i = 0; i < index; i++) {
150+
getNextNode.execute(dequeIter);
151+
}
152+
return dequeIter;
153+
}
154+
155+
@Specialization(replaces = {"doDeque", "doDequeInt"})
156+
PDequeIter doGeneric(VirtualFrame frame, @SuppressWarnings("unused") Object cls, Object deque, Object indexObj,
157+
@Cached PyNumberIndexNode toIndexNode,
158+
@Cached CastToJavaIntExactNode castToJavaIntExactNode,
159+
@Shared("getNextNode") @Cached DequeIterNextNode getNextNode) {
160+
if (deque instanceof PDeque) {
161+
if (indexObj != PNone.NO_VALUE) {
162+
int index = castToJavaIntExactNode.execute(toIndexNode.execute(frame, indexObj));
163+
return doDequeInt(cls, (PDeque) deque, index, getNextNode);
164+
}
165+
return doDeque(cls, (PDeque) deque, PNone.NO_VALUE);
166+
}
167+
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.EXPECTED_OBJ_TYPE_S_GOT_P, BuiltinNames.DEQUE, deque);
168+
}
56169
}
57170
}

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,44 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43-
import java.util.ArrayList;
4443
import java.util.List;
4544

45+
import com.oracle.graal.python.builtins.Builtin;
4646
import com.oracle.graal.python.builtins.CoreFunctions;
47+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4748
import com.oracle.graal.python.builtins.PythonBuiltins;
49+
import com.oracle.graal.python.builtins.objects.queue.PSimpleQueue;
50+
import com.oracle.graal.python.nodes.BuiltinNames;
4851
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
52+
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
53+
import com.oracle.graal.python.runtime.PythonCore;
54+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4955
import com.oracle.truffle.api.dsl.NodeFactory;
56+
import com.oracle.truffle.api.dsl.Specialization;
5057

5158
@CoreFunctions(defineModule = "_queue")
5259
public class QueueModuleBuiltins extends PythonBuiltins {
5360
@Override
5461
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
55-
return new ArrayList<>();
62+
return QueueModuleBuiltinsFactory.getFactories();
63+
}
64+
65+
@Override
66+
public void initialize(PythonCore core) {
67+
super.initialize(core);
68+
builtinConstants.put(BuiltinNames.EMPTY, core.lookupType(PythonBuiltinClassType.Empty));
69+
}
70+
71+
// _queue.SimpleQueue
72+
@Builtin(name = BuiltinNames.SIMPLE_QUEUE, constructsClass = PythonBuiltinClassType.PSimpleQueue, //
73+
minNumOfPositionalArgs = 1, //
74+
doc = "SimpleQueue()\n--\n\nSimple, unbounded, reentrant FIFO queue.")
75+
@GenerateNodeFactory
76+
abstract static class SimpleQueueNode extends PythonUnaryBuiltinNode {
77+
78+
@Specialization
79+
PSimpleQueue doGeneric(Object cls) {
80+
return factory().createSimpleQueue(cls);
81+
}
5682
}
5783
}

0 commit comments

Comments
 (0)