Skip to content

Commit 0510937

Browse files
committed
feat: Removed increaseCapacityExactWithCopy and move ensureCapacity to leaf classes
1 parent 9fe3a5d commit 0510937

File tree

11 files changed

+126
-42
lines changed

11 files changed

+126
-42
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/nodes/storage/GetItemSliceNodeTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
141
package com.oracle.graal.python.test.nodes.storage;
242

343
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetItemSliceNode;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/json/JSONScannerBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2020, 2023, Oracle and/or its affiliates.
1+
/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2

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

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3004,11 +3004,12 @@ public abstract static class CreateEmptyNode extends SequenceStorageBaseNode {
30043004

30053005
@Specialization
30063006
static BasicSequenceStorage doIt(Node inliningTarget, SequenceStorage s, int cap, int len,
3007+
@Cached EnsureCapacityNode ensureCapacityNode,
30073008
@Cached GetElementType getElementType,
30083009
@Cached CreateEmptyForTypeNode createEmptyForTypeNode) {
30093010
BasicSequenceStorage ss = createEmptyForTypeNode.execute(inliningTarget, getElementType.execute(inliningTarget, s), cap);
30103011
if (len != -1) {
3011-
ss.ensureCapacity(len);
3012+
ensureCapacityNode.execute(inliningTarget, ss, len);
30123013
ss.setNewLength(len);
30133014
}
30143015
return ss;
@@ -3064,16 +3065,34 @@ static void doEmpty(EmptySequenceStorage s, @SuppressWarnings("unused") int cap)
30643065
// do nothing
30653066
}
30663067

3067-
@Specialization(limit = "MAX_BASIC_STORAGES", guards = "s.getClass() == cachedClass")
3068-
static void doManaged(Node inliningTarget, BasicSequenceStorage s, int cap,
3069-
@Cached PRaiseNode.Lazy raiseNode,
3070-
@Cached("s.getClass()") Class<? extends BasicSequenceStorage> cachedClass) {
3071-
try {
3072-
BasicSequenceStorage profiled = cachedClass.cast(s);
3073-
profiled.ensureCapacity(cap);
3074-
} catch (OutOfMemoryError | ArithmeticException e) {
3075-
throw raiseNode.get(inliningTarget).raise(MemoryError);
3076-
}
3068+
@Specialization
3069+
static void doInt(IntSequenceStorage storage, int cap) {
3070+
storage.ensureCapacity(cap);
3071+
}
3072+
3073+
@Specialization
3074+
static void doLong(LongSequenceStorage storage, int cap) {
3075+
storage.ensureCapacity(cap);
3076+
}
3077+
3078+
@Specialization
3079+
static void doDouble(DoubleSequenceStorage storage, int cap) {
3080+
storage.ensureCapacity(cap);
3081+
}
3082+
3083+
@Specialization
3084+
static void doByte(ByteSequenceStorage storage, int cap) {
3085+
storage.ensureCapacity(cap);
3086+
}
3087+
3088+
@Specialization
3089+
static void doObject(ObjectSequenceStorage storage, int cap) {
3090+
storage.ensureCapacity(cap);
3091+
}
3092+
3093+
@Specialization
3094+
static void doBool(BoolSequenceStorage storage, int cap) {
3095+
storage.ensureCapacity(cap);
30773096
}
30783097

30793098
@Specialization
@@ -3083,6 +3102,11 @@ static void doNative(NativeSequenceStorage s, int cap,
30833102
helper.execute(s, cap);
30843103
}
30853104

3105+
@Specialization
3106+
static void doMro(MroSequenceStorage storage, int cap) {
3107+
throw CompilerDirectives.shouldNotReachHere();
3108+
}
3109+
30863110
@GenerateInline(false)
30873111
@GenerateUncached
30883112
abstract static class EnsureCapacityNativeNode extends Node {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/BasicSequenceStorage.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,10 @@ public final void incLength() {
5151
/**
5252
* The capacity we should allocate for a given length.
5353
*/
54-
private static int capacityFor(int length) throws ArithmeticException {
54+
protected static int capacityFor(int length) throws ArithmeticException {
5555
return Math.max(16, Math.multiplyExact(length, 2));
5656
}
5757

58-
/**
59-
* Ensure that the current capacity is big enough. If not, we increase capacity to the next
60-
* designated size (not necessarily the requested one).
61-
*/
62-
public final void ensureCapacity(int newCapacity) throws ArithmeticException {
63-
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, newCapacity > capacity)) {
64-
increaseCapacityExactWithCopy(capacityFor(newCapacity));
65-
}
66-
}
67-
68-
protected abstract void increaseCapacityExactWithCopy(int newCapacity);
69-
7058
public void minimizeCapacity() {
7159
capacity = length;
7260
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/BoolSequenceStorage.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626
package com.oracle.graal.python.runtime.sequence.storage;
2727

28+
import com.oracle.truffle.api.CompilerDirectives;
29+
2830
import java.util.Arrays;
2931

3032
public final class BoolSequenceStorage extends ArrayBasedSequenceStorage {
@@ -51,12 +53,17 @@ public BoolSequenceStorage(int capacity) {
5153
this.length = 0;
5254
}
5355

54-
@Override
55-
protected void increaseCapacityExactWithCopy(int newCapacity) {
56+
private void increaseCapacityExactWithCopy(int newCapacity) {
5657
values = Arrays.copyOf(values, newCapacity);
5758
capacity = values.length;
5859
}
5960

61+
public void ensureCapacity(int newCapacity) throws ArithmeticException {
62+
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, newCapacity > capacity)) {
63+
increaseCapacityExactWithCopy(capacityFor(newCapacity));
64+
}
65+
}
66+
6067
@Override
6168
public BoolSequenceStorage createEmpty(int newLength) {
6269
return new BoolSequenceStorage(newLength);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/ByteSequenceStorage.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.oracle.graal.python.nodes.PRaiseNode;
3737
import com.oracle.graal.python.util.PythonUtils;
3838
import com.oracle.truffle.api.ArrayUtils;
39+
import com.oracle.truffle.api.CompilerDirectives;
3940
import com.oracle.truffle.api.library.ExportLibrary;
4041
import com.oracle.truffle.api.library.ExportMessage;
4142

@@ -60,12 +61,17 @@ public ByteSequenceStorage(int capacity) {
6061
this.length = 0;
6162
}
6263

63-
@Override
64-
protected void increaseCapacityExactWithCopy(int newCapacity) {
64+
private void increaseCapacityExactWithCopy(int newCapacity) {
6565
values = Arrays.copyOf(values, newCapacity);
6666
capacity = values.length;
6767
}
6868

69+
public void ensureCapacity(int newCapacity) throws ArithmeticException {
70+
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, newCapacity > capacity)) {
71+
increaseCapacityExactWithCopy(capacityFor(newCapacity));
72+
}
73+
}
74+
6975
@Override
7076
public ByteSequenceStorage createEmpty(int newCapacity) {
7177
return new ByteSequenceStorage(newCapacity);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/DoubleSequenceStorage.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626
package com.oracle.graal.python.runtime.sequence.storage;
2727

28+
import com.oracle.truffle.api.CompilerDirectives;
29+
2830
import java.util.Arrays;
2931

3032
public final class DoubleSequenceStorage extends ArrayBasedSequenceStorage {
@@ -53,12 +55,17 @@ public DoubleSequenceStorage(int capacity) {
5355
this.length = 0;
5456
}
5557

56-
@Override
57-
protected void increaseCapacityExactWithCopy(int newCapacity) {
58+
private void increaseCapacityExactWithCopy(int newCapacity) {
5859
values = Arrays.copyOf(values, newCapacity);
5960
capacity = values.length;
6061
}
6162

63+
public void ensureCapacity(int newCapacity) throws ArithmeticException {
64+
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, newCapacity > capacity)) {
65+
increaseCapacityExactWithCopy(capacityFor(newCapacity));
66+
}
67+
}
68+
6269
@Override
6370
public ArrayBasedSequenceStorage createEmpty(int newCapacity) {
6471
return new DoubleSequenceStorage(newCapacity);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/IntSequenceStorage.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626
package com.oracle.graal.python.runtime.sequence.storage;
2727

28+
import com.oracle.truffle.api.CompilerDirectives;
29+
2830
import java.util.Arrays;
2931

3032
public final class IntSequenceStorage extends ArrayBasedSequenceStorage {
@@ -53,12 +55,17 @@ public IntSequenceStorage(int capacity) {
5355
this.length = 0;
5456
}
5557

56-
@Override
57-
protected void increaseCapacityExactWithCopy(int newCapacity) {
58+
private void increaseCapacityExactWithCopy(int newCapacity) {
5859
values = Arrays.copyOf(values, newCapacity);
5960
capacity = values.length;
6061
}
6162

63+
public void ensureCapacity(int newCapacity) throws ArithmeticException {
64+
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, newCapacity > capacity)) {
65+
increaseCapacityExactWithCopy(capacityFor(newCapacity));
66+
}
67+
}
68+
6269
@Override
6370
public IntSequenceStorage createEmpty(int newCapacity) {
6471
return new IntSequenceStorage(newCapacity);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/LongSequenceStorage.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Arrays;
3030

3131
import com.oracle.graal.python.builtins.objects.ints.PInt;
32+
import com.oracle.truffle.api.CompilerDirectives;
3233

3334
public final class LongSequenceStorage extends ArrayBasedSequenceStorage {
3435

@@ -56,12 +57,17 @@ public LongSequenceStorage(int capacity) {
5657
this.length = 0;
5758
}
5859

59-
@Override
60-
protected void increaseCapacityExactWithCopy(int newCapacity) {
60+
private void increaseCapacityExactWithCopy(int newCapacity) {
6161
values = Arrays.copyOf(values, newCapacity);
6262
capacity = values.length;
6363
}
6464

65+
public void ensureCapacity(int newCapacity) throws ArithmeticException {
66+
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, newCapacity > capacity)) {
67+
increaseCapacityExactWithCopy(capacityFor(newCapacity));
68+
}
69+
}
70+
6571
@Override
6672
public LongSequenceStorage createEmpty(int newCapacity) {
6773
return new LongSequenceStorage(newCapacity);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/MroSequenceStorage.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,6 @@ public PythonAbstractClass[] getInternalClassArray() {
132132
return values;
133133
}
134134

135-
@SuppressWarnings("unused")
136-
@Override
137-
public void increaseCapacityExactWithCopy(int newCapacity) {
138-
throw CompilerDirectives.shouldNotReachHere();
139-
}
140-
141135
@Override
142136
public Object getIndicativeValue() {
143137
return null;

0 commit comments

Comments
 (0)