Skip to content

Commit c1989a7

Browse files
committed
Bytecode DSL: construct native int storage for list literals if --python.UseNativePrimitiveStorageStrategy=true
1 parent fe9d49e commit c1989a7

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ public boolean isSingleContext() {
395395

396396
@CompilationFinal(dimensions = 1) private volatile Object[] engineOptionsStorage;
397397
@CompilationFinal private volatile OptionValues engineOptions;
398+
@CompilationFinal private boolean useNativePrimitiveStorage;
398399

399400
/** For fast access to the PythonThreadState object by the owning thread. */
400401
private final ContextThreadLocal<PythonThreadState> threadState = locals.createContextThreadLocal(PythonContext.PythonThreadState::new);
@@ -476,6 +477,7 @@ protected PythonContext createContext(Env env) {
476477
} else {
477478
assert areOptionsCompatible(options, PythonOptions.createEngineOptions(env)) : "invalid engine options";
478479
}
480+
this.useNativePrimitiveStorage = getEngineOption(PythonOptions.UseNativePrimitiveStorageStrategy);
479481

480482
return context;
481483
}
@@ -490,6 +492,10 @@ public <T> T getEngineOption(OptionKey<T> key) {
490492
}
491493
}
492494

495+
public boolean useNativePrimitiveStorage() {
496+
return useNativePrimitiveStorage;
497+
}
498+
493499
@Override
494500
protected OptionDescriptors getOptionDescriptors() {
495501
return PythonOptions.DESCRIPTORS;
@@ -1101,6 +1107,7 @@ private Shape createBuiltinShape(PythonBuiltinClassType type, int ordinal) {
11011107
shapeBuilder.shapeFlags(PythonObject.HAS_SLOTS_BUT_NO_DICT_FLAG);
11021108
}
11031109
shape = shapeBuilder.build();
1110+
VarHandle.storeStoreFence();
11041111
builtinTypeInstanceShapes[ordinal] = shape;
11051112
return shape;
11061113
}

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

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3968,10 +3968,6 @@ static boolean isObjectSequenceStorage(SequenceStorage s) {
39683968
@ImportStatic(PInt.class)
39693969
public abstract static class InsertItemArrayBasedStorageNode extends Node {
39703970

3971-
public static SequenceStorage executeUncached(ArrayBasedSequenceStorage storage, int index, Object value) {
3972-
return SequenceStorageNodesFactory.InsertItemArrayBasedStorageNodeGen.getUncached().execute(null, storage, index, value);
3973-
}
3974-
39753971
protected abstract SequenceStorage execute(Node inliningTarget, ArrayBasedSequenceStorage storage, int index, Object value);
39763972

39773973
@Specialization
@@ -4037,6 +4033,40 @@ static SequenceStorage doGeneralization(Node inliningTarget, ArrayBasedSequenceS
40374033
}
40384034
}
40394035

4036+
@GenerateUncached
4037+
@GenerateInline
4038+
public abstract static class InsertItemNativePrimitiveBasedStorageNode extends Node {
4039+
protected abstract SequenceStorage execute(Node inliningTarget, NativePrimitiveSequenceStorage storage, int index, Object value);
4040+
4041+
@Specialization
4042+
static SequenceStorage doIntStorage(Node inliningTarget, NativeIntSequenceStorage storage, int index, int value,
4043+
@Cached EnsureCapacityNode ensureCapacity) {
4044+
int length = storage.length();
4045+
var context = PythonContext.get(inliningTarget);
4046+
var unsafe = context.getUnsafe();
4047+
long itemSize = storage.getItemSize();
4048+
ensureCapacity.execute(inliningTarget, storage, length + 1);
4049+
// shifting tail to the right by one slot
4050+
long startAddr = storage.getValueBufferAddr() + (index * itemSize);
4051+
long endAddr = startAddr + itemSize;
4052+
long sizeInBytes = (length - index) * itemSize;
4053+
unsafe.copyMemory(startAddr, endAddr, sizeInBytes);
4054+
4055+
storage.setIntItemNormalized(index, value);
4056+
storage.incLength();
4057+
return storage;
4058+
}
4059+
4060+
@Fallback
4061+
static SequenceStorage doGeneralization(Node inliningTarget, NativePrimitiveSequenceStorage storage, int idx, Object value,
4062+
@Cached GetInternalObjectArrayNode getInternalObjectArrayNode) {
4063+
Object[] values = getInternalObjectArrayNode.execute(inliningTarget, storage);
4064+
ObjectSequenceStorage newStorage = new ObjectSequenceStorage(values);
4065+
newStorage.insertItem(idx, value);
4066+
return newStorage;
4067+
}
4068+
}
4069+
40404070
@GenerateUncached
40414071
@GenerateInline
40424072
@GenerateCached(false)
@@ -4062,24 +4092,10 @@ static SequenceStorage doArrayBasedStorage(Node inliningTarget, ArrayBasedSequen
40624092

40634093
}
40644094

4065-
// TODO introduce something similar to InsertItemArrayBasedStorageNode
40664095
@Specialization
4067-
static SequenceStorage doNativeInt(Node inliningTarget, NativeIntSequenceStorage storage, int index, int value,
4068-
@Exclusive @Cached EnsureCapacityNode ensureCapacity) {
4069-
int length = storage.length();
4070-
var context = PythonContext.get(inliningTarget);
4071-
var unsafe = context.getUnsafe();
4072-
long itemSize = storage.getItemSize();
4073-
ensureCapacity.execute(inliningTarget, storage, length + 1);
4074-
// shifting tail to the right by one slot
4075-
long startAddr = storage.getValueBufferAddr() + (index * itemSize);
4076-
long endAddr = startAddr + itemSize;
4077-
long sizeInBytes = (length - index) * itemSize;
4078-
unsafe.copyMemory(startAddr, endAddr, sizeInBytes);
4079-
4080-
storage.setIntItemNormalized(index, value);
4081-
storage.incLength();
4082-
return storage;
4096+
static SequenceStorage doNativeStorage(Node inliningTarget, NativeIntSequenceStorage storage, int index, Object value,
4097+
@Cached InsertItemNativePrimitiveBasedStorageNode insertNativeStorageNode) {
4098+
return insertNativeStorageNode.execute(inliningTarget, storage, index, value);
40834099
}
40844100

40854101
@Specialization

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,8 +1682,18 @@ public static final class MakeConstantIntList {
16821682
@Specialization
16831683
public static PList perform(int[] array,
16841684
@Bind PBytecodeDSLRootNode rootNode) {
1685-
SequenceStorage storage = new IntSequenceStorage(PythonUtils.arrayCopyOf(array, array.length));
1686-
return PFactory.createList(rootNode.getLanguage(), storage);
1685+
PythonLanguage language = rootNode.getLanguage();
1686+
if (!language.useNativePrimitiveStorage()) {
1687+
return PFactory.createList(language, new IntSequenceStorage(PythonUtils.arrayCopyOf(array, array.length)));
1688+
} else {
1689+
return createNativeList(array, rootNode, language);
1690+
}
1691+
}
1692+
1693+
@InliningCutoff
1694+
private static PList createNativeList(int[] array, PBytecodeDSLRootNode rootNode, PythonLanguage language) {
1695+
SequenceStorage storage = PythonContext.get(rootNode).nativeBufferContext.toNativeIntStorage(array);
1696+
return PFactory.createList(language, storage);
16871697
}
16881698
}
16891699

0 commit comments

Comments
 (0)