Skip to content

Commit 2279839

Browse files
committed
Implement 'SequenceStorageNodes.AppendNode'.
1 parent d32ef81 commit 2279839

File tree

2 files changed

+81
-56
lines changed

2 files changed

+81
-56
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.PCallBinaryCapiFunction;
5959
import com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols;
60+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.AppendNodeGen;
6061
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.CastToByteNodeGen;
6162
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.CmpNodeGen;
6263
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.ConcatNodeGen;
@@ -1998,7 +1999,7 @@ public static GeneralizationNode create(String invalidItemErrorMessage) {
19981999
// Implements list generalization rules; previously in 'SequenceStroage.generalizeFor'.
19992000
public abstract static class ListGeneralizationNode extends GeneralizationNode {
20002001

2001-
private static final int DEFAULT_CAPACITY = 16;
2002+
private static final int DEFAULT_CAPACITY = 8;
20022003

20032004
@Specialization
20042005
SequenceStorage doObject(@SuppressWarnings("unused") ObjectSequenceStorage s, @SuppressWarnings("unused") Object indicationValue) {
@@ -2050,6 +2051,11 @@ SequenceStorage doEmptyPTuple(@SuppressWarnings("unused") EmptySequenceStorage s
20502051
return new TupleSequenceStorage();
20512052
}
20522053

2054+
@Specialization
2055+
SequenceStorage doEmptyObject(@SuppressWarnings("unused") EmptySequenceStorage s, @SuppressWarnings("unused") Object val) {
2056+
return new ObjectSequenceStorage(DEFAULT_CAPACITY);
2057+
}
2058+
20532059
@Specialization
20542060
IntSequenceStorage doByteInteger(@SuppressWarnings("unused") ByteSequenceStorage s, @SuppressWarnings("unused") int val) {
20552061
int[] copied = new int[s.length()];
@@ -2099,4 +2105,67 @@ public static ListGeneralizationNode create() {
20992105

21002106
}
21012107

2108+
public abstract static class AppendNode extends SequenceStorageBaseNode {
2109+
2110+
@Child private GeneralizationNode genNode;
2111+
2112+
private final Supplier<GeneralizationNode> genNodeProvider;
2113+
2114+
public AppendNode(Supplier<GeneralizationNode> genNodeProvider) {
2115+
this.genNodeProvider = genNodeProvider;
2116+
}
2117+
2118+
public abstract SequenceStorage execute(SequenceStorage s, Object val);
2119+
2120+
@Specialization
2121+
SequenceStorage doEmpty(EmptySequenceStorage s, Object val,
2122+
@Cached("createRecursive()") AppendNode recursive) {
2123+
SequenceStorage newStorage = generalizeStore(s, val);
2124+
return recursive.execute(newStorage, val);
2125+
}
2126+
2127+
@Specialization(limit = "9", guards = "s.getClass() == cachedClass")
2128+
SequenceStorage doManaged(BasicSequenceStorage s, Object val,
2129+
@Cached("s.getClass()") Class<? extends BasicSequenceStorage> cachedClass,
2130+
@Cached("create()") SetItemScalarNode setItemNode) {
2131+
BasicSequenceStorage profiled = cachedClass.cast(s);
2132+
int len = profiled.length();
2133+
profiled.ensureCapacity(len + 1);
2134+
try {
2135+
setItemNode.execute(profiled, len, val);
2136+
profiled.setNewLength(len + 1);
2137+
return profiled;
2138+
} catch (SequenceStoreException e) {
2139+
SequenceStorage generalized = generalizeStore(profiled, e.getIndicationValue());
2140+
generalized.ensureCapacity(len + 1);
2141+
try {
2142+
setItemNode.execute(generalized, len, val);
2143+
generalized.setNewLength(len + 1);
2144+
return generalized;
2145+
} catch (SequenceStoreException e1) {
2146+
CompilerDirectives.transferToInterpreter();
2147+
throw new IllegalStateException();
2148+
}
2149+
}
2150+
}
2151+
2152+
// TODO native sequence storage
2153+
2154+
protected AppendNode createRecursive() {
2155+
return AppendNodeGen.create(genNodeProvider);
2156+
}
2157+
2158+
private SequenceStorage generalizeStore(SequenceStorage storage, Object value) {
2159+
if (genNode == null) {
2160+
CompilerDirectives.transferToInterpreterAndInvalidate();
2161+
genNode = insert(genNodeProvider.get());
2162+
}
2163+
return genNode.execute(storage, value);
2164+
}
2165+
2166+
public static AppendNode create(Supplier<GeneralizationNode> genNodeProvider) {
2167+
return AppendNodeGen.create(genNodeProvider);
2168+
}
2169+
2170+
}
21022171
}

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

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,10 @@
9393
import com.oracle.graal.python.runtime.sequence.storage.DoubleSequenceStorage;
9494
import com.oracle.graal.python.runtime.sequence.storage.EmptySequenceStorage;
9595
import com.oracle.graal.python.runtime.sequence.storage.IntSequenceStorage;
96-
import com.oracle.graal.python.runtime.sequence.storage.ListSequenceStorage;
9796
import com.oracle.graal.python.runtime.sequence.storage.LongSequenceStorage;
9897
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
9998
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
10099
import com.oracle.graal.python.runtime.sequence.storage.SequenceStoreException;
101-
import com.oracle.graal.python.runtime.sequence.storage.TupleSequenceStorage;
102100
import com.oracle.truffle.api.CompilerDirectives;
103101
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
104102
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -360,62 +358,20 @@ protected Object doGeneric(Object self, Object objectIdx, Object value) {
360358
@GenerateNodeFactory
361359
public abstract static class ListAppendNode extends PythonBinaryBuiltinNode {
362360

363-
@Specialization(guards = "isEmptyStorage(list)")
364-
public PNone appendEmpty(PList list, Object arg) {
365-
list.append(arg);
366-
return PNone.NONE;
367-
}
368-
369-
@Specialization(guards = "isIntStorage(list)")
370-
public PNone appendInt(PList list, int arg) {
371-
IntSequenceStorage store = (IntSequenceStorage) list.getSequenceStorage();
372-
store.appendInt(arg);
373-
return PNone.NONE;
374-
}
375-
376-
@Specialization(guards = "isLongStorage(list)")
377-
public PNone appendLong(PList list, long arg) {
378-
LongSequenceStorage store = (LongSequenceStorage) list.getSequenceStorage();
379-
store.appendLong(arg);
380-
return PNone.NONE;
381-
}
382-
383-
@Specialization(guards = "isDoubleStorage(list)")
384-
public PNone appendDouble(PList list, double arg) {
385-
DoubleSequenceStorage store = (DoubleSequenceStorage) list.getSequenceStorage();
386-
store.appendDouble(arg);
387-
return PNone.NONE;
388-
}
389-
390-
@Specialization(guards = "isListStorage(list)")
391-
public PNone appendList(PList list, PList arg) {
392-
ListSequenceStorage store = (ListSequenceStorage) list.getSequenceStorage();
393-
store.appendList(arg);
394-
return PNone.NONE;
395-
}
396-
397-
@Specialization(guards = "isTupleStorage(list)")
398-
public PNone appendTuple(PList list, PTuple arg) {
399-
TupleSequenceStorage store = (TupleSequenceStorage) list.getSequenceStorage();
400-
store.appendPTuple(arg);
401-
return PNone.NONE;
402-
}
403-
404-
@Specialization(guards = {"!isKnownStorage(list)"}, rewriteOn = {SequenceStoreException.class})
405-
public PNone appendObject(PList list, Object arg) throws SequenceStoreException {
406-
list.getSequenceStorage().append(arg);
407-
return PNone.NONE;
408-
}
409-
410-
@Specialization()
411-
public PNone appendObjectGeneric(PList list, Object arg) {
412-
list.append(arg);
361+
@Specialization
362+
public PNone appendObjectGeneric(PList list, Object arg,
363+
@Cached("createAppend()") SequenceStorageNodes.AppendNode appendNode,
364+
@Cached("create()") BranchProfile updateStoreProfile) {
365+
SequenceStorage newStore = appendNode.execute(list.getSequenceStorage(), arg);
366+
if (list.getSequenceStorage() != newStore) {
367+
updateStoreProfile.enter();
368+
list.setSequenceStorage(newStore);
369+
}
413370
return PNone.NONE;
414371
}
415372

416-
protected boolean isKnownStorage(PList list) {
417-
return PGuards.isEmptyStorage(list) || PGuards.isIntStorage(list) || PGuards.isLongStorage(list) || PGuards.isDoubleStorage(list) || PGuards.isListStorage(list) ||
418-
PGuards.isTupleStorage(list);
373+
protected static SequenceStorageNodes.AppendNode createAppend() {
374+
return SequenceStorageNodes.AppendNode.create(() -> ListGeneralizationNode.create());
419375
}
420376
}
421377

0 commit comments

Comments
 (0)