Skip to content

Commit 8b8e0cd

Browse files
committed
[GR-70365] Specialize tuples/list storage if possible when created from Bytecode DSL loop.
PullRequest: graalpython/4265
2 parents c5b8041 + 623adaf commit 8b8e0cd

File tree

4 files changed

+286
-49
lines changed

4 files changed

+286
-49
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/PList.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2025, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2026, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -47,13 +47,11 @@
4747
import com.oracle.truffle.api.dsl.Cached.Exclusive;
4848
import com.oracle.truffle.api.interop.InteropLibrary;
4949
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
50-
import com.oracle.truffle.api.interop.UnsupportedMessageException;
5150
import com.oracle.truffle.api.library.ExportLibrary;
5251
import com.oracle.truffle.api.library.ExportMessage;
5352
import com.oracle.truffle.api.nodes.Node;
5453
import com.oracle.truffle.api.object.Shape;
5554
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
56-
import com.oracle.truffle.api.source.SourceSection;
5755

5856
@SuppressWarnings("truffle-abstract-export")
5957
@ExportLibrary(InteropLibrary.class)
@@ -82,29 +80,6 @@ public ListOrigin getOrigin() {
8280
return origin;
8381
}
8482

85-
@ExportMessage
86-
public SourceSection getSourceLocation(@Exclusive @Cached GilNode gil) throws UnsupportedMessageException {
87-
boolean mustRelease = gil.acquire();
88-
try {
89-
ListOrigin node = getOrigin();
90-
SourceSection result = null;
91-
if (node != null) {
92-
result = node.getSourceSection();
93-
}
94-
if (result == null) {
95-
throw UnsupportedMessageException.create();
96-
}
97-
return result;
98-
} finally {
99-
gil.release(mustRelease);
100-
}
101-
}
102-
103-
@ExportMessage
104-
public boolean hasSourceLocation() {
105-
return getOrigin() != null && getOrigin().getSourceSection() != null;
106-
}
107-
10883
@ExportMessage
10984
public boolean isArrayElementModifiable(long index,
11085
@Exclusive @Cached IndexNodes.NormalizeIndexCustomMessageNode normalize,
@@ -235,7 +210,5 @@ public int updateFrom(int newSizeEstimate) {
235210
}
236211

237212
void reportUpdatedCapacity(ArrayBasedSequenceStorage newStore);
238-
239-
SourceSection getSourceSection();
240213
}
241214
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/SequenceFromStackNode.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,6 @@ protected int getCapacityEstimate() {
238238
return initialCapacity.estimate();
239239
}
240240

241-
@Override
242-
public SourceSection getSourceSection() {
243-
return null;
244-
}
245-
246241
@Override
247242
public void reportUpdatedCapacity(ArrayBasedSequenceStorage newStore) {
248243
if (CompilerDirectives.inInterpreter()) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@
237237
import com.oracle.graal.python.runtime.sequence.PTupleListBase;
238238
import com.oracle.graal.python.runtime.sequence.storage.BoolSequenceStorage;
239239
import com.oracle.graal.python.runtime.sequence.storage.DoubleSequenceStorage;
240-
import com.oracle.graal.python.runtime.sequence.storage.EmptySequenceStorage;
241240
import com.oracle.graal.python.runtime.sequence.storage.IntSequenceStorage;
242241
import com.oracle.graal.python.runtime.sequence.storage.LongSequenceStorage;
243242
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
@@ -1733,19 +1732,11 @@ public static Object perform(VirtualFrame frame,
17331732

17341733
@Operation(storeBytecodeIndex = false)
17351734
public static final class MakeList {
1736-
@Specialization(guards = "elements.length == 0")
1737-
public static PList doEmpty(@Variadic Object[] elements,
1738-
@Bind PBytecodeDSLRootNode rootNode) {
1739-
// Common pattern is to create an empty list and then add items.
1740-
// We need to start from empty storage, so that we can specialize to, say, int storage
1741-
// if only ints are appended to this list
1742-
return PFactory.createList(rootNode.getLanguage(), EmptySequenceStorage.INSTANCE);
1743-
}
1744-
1745-
@Specialization(guards = "elements.length > 0")
1735+
@Specialization
17461736
public static PList perform(@Variadic Object[] elements,
1747-
@Bind PBytecodeDSLRootNode rootNode) {
1748-
return PFactory.createList(rootNode.getLanguage(), elements);
1737+
@Bind PBytecodeDSLRootNode rootNode,
1738+
@Cached SequenceFromArrayNode.ListFromArrayNode listFromArrayNode) {
1739+
return listFromArrayNode.execute(rootNode.getLanguage(), elements);
17491740
}
17501741
}
17511742

@@ -1792,8 +1783,9 @@ public static PFrozenSet doNonEmpty(VirtualFrame frame, @Variadic Object[] eleme
17921783
public static final class MakeTuple {
17931784
@Specialization
17941785
public static Object perform(@Variadic Object[] elements,
1795-
@Bind PBytecodeDSLRootNode rootNode) {
1796-
return PFactory.createTuple(rootNode.getLanguage(), elements);
1786+
@Bind PBytecodeDSLRootNode rootNode,
1787+
@Cached SequenceFromArrayNode.TupleFromArrayNode tupleFromArrayNode) {
1788+
return tupleFromArrayNode.execute(rootNode.getLanguage(), elements);
17971789
}
17981790
}
17991791

@@ -3433,7 +3425,7 @@ public static final class PreResumeYield {
34333425
@Specialization
34343426
public static Object doObject(VirtualFrame frame, LocalAccessor currentGeneratorException, LocalAccessor savedException, Object sendValue,
34353427
@Bind BytecodeNode bytecode) {
3436-
if (savedException != currentGeneratorException) {
3428+
if (!savedException.equals(currentGeneratorException)) {
34373429
// We cannot pass `null` as savedException, so savedException ==
34383430
// currentGeneratorException means "no saveException"
34393431
//

0 commit comments

Comments
 (0)