Skip to content

Commit 67678de

Browse files
committed
Decref objects deleted from native storage
1 parent eae9d4d commit 67678de

File tree

1 file changed

+50
-17
lines changed

1 file changed

+50
-17
lines changed

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

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.oracle.graal.python.builtins.modules.SysModuleBuiltins;
5050
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
5151
import com.oracle.graal.python.builtins.objects.bytes.PBytesLike;
52+
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes;
5253
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.PCallCapiFunction;
5354
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.NativeToPythonNode;
5455
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.PythonToNativeNewRefNode;
@@ -2935,13 +2936,47 @@ public abstract static class SetLenNode extends Node {
29352936
public abstract void execute(Node inliningTarget, SequenceStorage s, int len);
29362937

29372938
@Specialization(limit = "MAX_SEQUENCE_STORAGES", guards = "s.getClass() == cachedClass")
2938-
static void doSpecial(SequenceStorage s, int len,
2939+
static void doSpecial(BasicSequenceStorage s, int len,
29392940
@Cached("s.getClass()") Class<? extends SequenceStorage> cachedClass) {
29402941
cachedClass.cast(s).setNewLength(len);
29412942
}
29422943

29432944
@Specialization(replaces = "doSpecial")
2944-
static void doGeneric(SequenceStorage s, int len) {
2945+
static void doGeneric(BasicSequenceStorage s, int len) {
2946+
s.setNewLength(len);
2947+
}
2948+
2949+
@Specialization
2950+
static void doNative(NativeSequenceStorage s, int len,
2951+
@Cached(inline = false) SetNativeLenNode setLen) {
2952+
setLen.execute(s, len);
2953+
}
2954+
}
2955+
2956+
@GenerateUncached
2957+
@GenerateInline(false) // Uncommon node
2958+
public abstract static class SetNativeLenNode extends Node {
2959+
2960+
public abstract void execute(NativeSequenceStorage s, int len);
2961+
2962+
@Specialization(guards = "len < s.length()")
2963+
@InliningCutoff
2964+
static void doShrink(NativeSequenceStorage s, int len,
2965+
@Bind("this") Node inliningTarget,
2966+
@Cached CStructAccess.ReadPointerNode readNode,
2967+
@Cached CExtNodes.DecRefPointerNode decRefPointerNode) {
2968+
if (len < s.length()) {
2969+
// When shrinking, we need to decref the items that are now past the end
2970+
for (int i = len; i < s.length(); i++) {
2971+
Object elementPointer = readNode.readArrayElement(s.getPtr(), i);
2972+
decRefPointerNode.execute(inliningTarget, elementPointer);
2973+
}
2974+
}
2975+
s.setNewLength(len);
2976+
}
2977+
2978+
@Fallback
2979+
static void doEnlarge(NativeSequenceStorage s, int len) {
29452980
s.setNewLength(len);
29462981
}
29472982
}
@@ -3025,29 +3060,27 @@ public abstract static class DeleteItemNode extends SequenceStorageBaseNode {
30253060

30263061
public abstract void execute(Node node, SequenceStorage s, int idx);
30273062

3028-
@Specialization(limit = "MAX_SEQUENCE_STORAGES", guards = {"s.getClass() == cachedClass", "isLastItem(s, cachedClass, idx)"})
3029-
static void doLastItem(SequenceStorage s, @SuppressWarnings("unused") int idx,
3030-
@Cached("s.getClass()") Class<? extends SequenceStorage> cachedClass) {
3031-
SequenceStorage profiled = cachedClass.cast(s);
3032-
profiled.setNewLength(profiled.length() - 1);
3063+
@Specialization(guards = "isLastItem(s, idx)")
3064+
static void doLastItem(Node inliningTarget, SequenceStorage s, @SuppressWarnings("unused") int idx,
3065+
@Shared @Cached SetLenNode setLenNode) {
3066+
setLenNode.execute(inliningTarget, s, s.length() - 1);
30333067
}
30343068

3035-
@Specialization(limit = "MAX_SEQUENCE_STORAGES", guards = "s.getClass() == cachedClass")
3036-
static void doGeneric(Node inliningTarget, SequenceStorage s, @SuppressWarnings("unused") int idx,
3037-
@Cached(inline = false) GetItemScalarNode getItemNode,
3069+
@Specialization
3070+
static void doGeneric(Node inliningTarget, SequenceStorage s, int idx,
3071+
@Cached GetItemScalarNode getItemNode,
30383072
@Cached SetItemScalarNode setItemNode,
3039-
@Cached("s.getClass()") Class<? extends SequenceStorage> cachedClass) {
3040-
SequenceStorage profiled = cachedClass.cast(s);
3041-
int len = profiled.length();
3073+
@Shared @Cached SetLenNode setLenNode) {
3074+
int len = s.length();
30423075

30433076
for (int i = idx; i < len - 1; i++) {
3044-
setItemNode.execute(inliningTarget, profiled, i, getItemNode.execute(inliningTarget, profiled, i + 1));
3077+
setItemNode.execute(inliningTarget, s, i, getItemNode.execute(inliningTarget, s, i + 1));
30453078
}
3046-
profiled.setNewLength(len - 1);
3079+
setLenNode.execute(inliningTarget, s, len - 1);
30473080
}
30483081

3049-
protected static boolean isLastItem(SequenceStorage s, Class<? extends SequenceStorage> cachedClass, int idx) {
3050-
return idx == cachedClass.cast(s).length() - 1;
3082+
protected static boolean isLastItem(SequenceStorage s, int idx) {
3083+
return idx == s.length() - 1;
30513084
}
30523085
}
30533086

0 commit comments

Comments
 (0)