Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import com.oracle.graal.python.PythonLanguage;
Expand Down Expand Up @@ -124,6 +121,7 @@
import com.oracle.graal.python.runtime.object.PFactory;
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
import com.oracle.graal.python.util.ArrayBuilder;
import com.oracle.graal.python.util.OverflowException;
import com.oracle.graal.python.util.PythonUtils;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
Expand All @@ -142,6 +140,7 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.nodes.LoopNode;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
Expand Down Expand Up @@ -1353,7 +1352,7 @@ static byte[] replaceWithEmptySub(byte[] bytes, int len, byte[] replacementBytes
@TruffleBoundary(allowInlining = true)
static byte[] replace(byte[] bytes, int len, byte[] sub, byte[] replacementBytes, int count) {
int i, j, pos, maxcount = count, subLen = sub.length, repLen = replacementBytes.length;
List<byte[]> list = new ArrayList<>();
ArrayBuilder<byte[]> list = new ArrayBuilder<>();

int resultLen = 0;
i = 0;
Expand All @@ -1378,9 +1377,8 @@ static byte[] replace(byte[] bytes, int len, byte[] sub, byte[] replacementBytes

i = 0;
byte[] result = new byte[resultLen];
Iterator<byte[]> it = iterator(list);
while (hasNext(it)) {
byte[] b = next(it);
for (int k = 0; k < list.size(); k++) {
byte[] b = list.get(k);
PythonUtils.arraycopy(b, 0, result, i, b.length);
i += b.length;
}
Expand Down Expand Up @@ -1537,11 +1535,11 @@ abstract static class AbstractSplitNode extends PythonTernaryClinicBuiltinNode {

protected static final byte[] WHITESPACE = new byte[]{' '};

protected abstract List<byte[]> splitWhitespace(byte[] bytes, int size, int maxsplit);
protected abstract ArrayBuilder<byte[]> splitWhitespace(byte[] bytes, int size, int maxsplit);

protected abstract List<byte[]> splitSingle(byte[] bytes, int size, byte sep, int maxsplit);
protected abstract ArrayBuilder<byte[]> splitSingle(byte[] bytes, int size, byte sep, int maxsplit);

protected abstract List<byte[]> splitDelimiter(byte[] bytes, int size, byte[] sep, int maxsplit);
protected abstract ArrayBuilder<byte[]> splitDelimiter(byte[] bytes, int size, byte[] sep, int maxsplit);

protected static boolean isEmptySep(byte[] sep) {
return sep.length == 0;
Expand Down Expand Up @@ -1605,13 +1603,13 @@ static PList error(Object bytes, byte[] sep, int maxsplit,
throw PRaiseNode.raiseStatic(inliningTarget, PythonErrorType.ValueError, ErrorMessages.EMPTY_SEPARATOR);
}

private static PList getBytesResult(List<byte[]> bytes, ListNodes.AppendNode appendNode, Object self, Node inliningTarget, BytesNodes.CreateBytesNode createBytesNode,
PythonLanguage language) {
private static PList getBytesResult(ArrayBuilder<byte[]> bytes, ListNodes.AppendNode appendNode, Object self, Node inliningTarget,
BytesNodes.CreateBytesNode createBytesNode, PythonLanguage language) {
PList result = PFactory.createList(language);
Iterator<byte[]> it = iterator(bytes);
while (hasNext(it)) {
appendNode.execute(result, createBytesNode.execute(inliningTarget, self, next(it)));
for (int i = 0; i < bytes.size(); i++) {
appendNode.execute(result, createBytesNode.execute(inliningTarget, self, bytes.get(i)));
}
LoopNode.reportLoopCount(inliningTarget, bytes.size());
return result;
}
}
Expand All @@ -1633,9 +1631,9 @@ protected ArgumentClinicProvider getArgumentClinic() {

@Override
@TruffleBoundary
protected List<byte[]> splitWhitespace(byte[] bytes, int len, int maxsplit) {
protected ArrayBuilder<byte[]> splitWhitespace(byte[] bytes, int len, int maxsplit) {
int i, j, maxcount = maxsplit;
List<byte[]> list = new ArrayList<>();
ArrayBuilder<byte[]> list = new ArrayBuilder<>();

i = 0;
while (maxcount-- > 0) {
Expand Down Expand Up @@ -1667,9 +1665,9 @@ protected List<byte[]> splitWhitespace(byte[] bytes, int len, int maxsplit) {
}

@Override
protected List<byte[]> splitSingle(byte[] bytes, int len, byte sep, int maxsplit) {
protected ArrayBuilder<byte[]> splitSingle(byte[] bytes, int len, byte sep, int maxsplit) {
int i, j, maxcount = maxsplit;
List<byte[]> list = new ArrayList<>();
ArrayBuilder<byte[]> list = new ArrayBuilder<>();

i = j = 0;
while ((j < len) && (maxcount-- > 0)) {
Expand All @@ -1689,9 +1687,9 @@ protected List<byte[]> splitSingle(byte[] bytes, int len, byte sep, int maxsplit
}

@Override
protected List<byte[]> splitDelimiter(byte[] bytes, int len, byte[] sep, int maxsplit) {
protected ArrayBuilder<byte[]> splitDelimiter(byte[] bytes, int len, byte[] sep, int maxsplit) {
int i, j, pos, maxcount = maxsplit, sepLen = sep.length;
List<byte[]> list = new ArrayList<>();
ArrayBuilder<byte[]> list = new ArrayBuilder<>();

i = 0;
while (maxcount-- > 0) {
Expand Down Expand Up @@ -1725,15 +1723,10 @@ protected ArgumentClinicProvider getArgumentClinic() {
return BytesCommonBuiltinsClinicProviders.RSplitNodeClinicProviderGen.INSTANCE;
}

@TruffleBoundary
private static void reverseList(ArrayList<byte[]> list) {
Collections.reverse(list);
}

@Override
protected List<byte[]> splitWhitespace(byte[] bytes, int len, int maxsplit) {
protected ArrayBuilder<byte[]> splitWhitespace(byte[] bytes, int len, int maxsplit) {
int i, j, maxcount = maxsplit;
ArrayList<byte[]> list = new ArrayList<>();
ArrayBuilder<byte[]> list = new ArrayBuilder<>();

i = len - 1;
while (maxcount-- > 0) {
Expand Down Expand Up @@ -1761,14 +1754,14 @@ protected List<byte[]> splitWhitespace(byte[] bytes, int len, int maxsplit) {
list.add(copyOfRange(bytes, 0, i + 1));
}
}
reverseList(list);
list.reverse();
return list;
}

@Override
protected List<byte[]> splitSingle(byte[] bytes, int len, byte sep, int maxsplit) {
protected ArrayBuilder<byte[]> splitSingle(byte[] bytes, int len, byte sep, int maxsplit) {
int i, j, maxcount = maxsplit;
ArrayList<byte[]> list = new ArrayList<>();
ArrayBuilder<byte[]> list = new ArrayBuilder<>();

i = j = len - 1;
while ((i >= 0) && (maxcount-- > 0)) {
Expand All @@ -1783,14 +1776,14 @@ protected List<byte[]> splitSingle(byte[] bytes, int len, byte sep, int maxsplit
if (j >= -1) {
list.add(copyOfRange(bytes, 0, j + 1));
}
reverseList(list);
list.reverse();
return list;
}

@Override
protected List<byte[]> splitDelimiter(byte[] bytes, int len, byte[] sep, int maxsplit) {
protected ArrayBuilder<byte[]> splitDelimiter(byte[] bytes, int len, byte[] sep, int maxsplit) {
int j, pos, maxcount = maxsplit, sepLen = sep.length;
ArrayList<byte[]> list = new ArrayList<>();
ArrayBuilder<byte[]> list = new ArrayBuilder<>();

if (sepLen == 1) {
return splitSingle(bytes, len, sep[0], maxcount);
Expand All @@ -1806,9 +1799,8 @@ protected List<byte[]> splitDelimiter(byte[] bytes, int len, byte[] sep, int max
j = pos;
}
list.add(copyOfRange(bytes, 0, j));
reverseList(list);
list.reverse();
return list;

}
}

Expand Down Expand Up @@ -2281,21 +2273,6 @@ static byte[] copyOfRange(byte[] bytes, int from, int to) {
return PythonUtils.arrayCopyOfRange(bytes, from, to);
}

@TruffleBoundary(allowInlining = true)
static Iterator<byte[]> iterator(List<byte[]> bytes) {
return bytes.iterator();
}

@TruffleBoundary(allowInlining = true)
static byte[] next(Iterator<byte[]> it) {
return it.next();
}

@TruffleBoundary(allowInlining = true)
static boolean hasNext(Iterator<byte[]> it) {
return it.hasNext();
}

@Builtin(name = J___GETNEWARGS__, minNumOfPositionalArgs = 1)
@GenerateNodeFactory
public abstract static class GetNewargsNode extends PythonUnaryBuiltinNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
*/
package com.oracle.graal.python.builtins.objects.frame;

import java.util.ArrayList;
import java.util.List;

import com.oracle.graal.python.PythonLanguage;
Expand Down Expand Up @@ -82,6 +81,7 @@
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
import com.oracle.graal.python.runtime.object.PFactory;
import com.oracle.graal.python.util.ArrayBuilder;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.bytecode.BytecodeFrame;
import com.oracle.truffle.api.dsl.Bind;
Expand All @@ -90,6 +90,7 @@
import com.oracle.truffle.api.dsl.NodeFactory;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.LoopNode;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.strings.TruffleString;

Expand Down Expand Up @@ -190,7 +191,7 @@ private static Object[] snapshotEntries(PFrameLocalsProxy self, PythonLanguage l
PDict snapshot = snapshot(self, language, inliningTarget, setItem);
HashingStorage storage = snapshot.getDictStorage();
HashingStorageIterator iterator = getIterator.execute(inliningTarget, storage);
List<Object> result = new ArrayList<>();
ArrayBuilder<Object> result = new ArrayBuilder<>();
while (iteratorNext.execute(inliningTarget, storage, iterator)) {
Object key = iteratorKey.execute(inliningTarget, storage, iterator);
Object value = iteratorValue.execute(inliningTarget, storage, iterator);
Expand All @@ -200,7 +201,8 @@ private static Object[] snapshotEntries(PFrameLocalsProxy self, PythonLanguage l
case ITEMS -> PFactory.createTuple(language, new Object[]{key, value});
});
}
return result.toArray();
LoopNode.reportLoopCount(inliningTarget, result.size());
return result.toObjectArray();
}

@Slot(value = SlotKind.mp_subscript, isComplex = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
import static com.oracle.truffle.api.CompilerDirectives.shouldNotReachHere;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.graalvm.shadowed.com.ibm.icu.lang.UCharacter;
import org.graalvm.shadowed.com.ibm.icu.lang.UCharacterCategory;
import org.graalvm.shadowed.com.ibm.icu.lang.UProperty;

import com.oracle.graal.python.util.ArrayBuilder;
import com.oracle.graal.python.util.PythonUtils;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
Expand All @@ -60,6 +59,7 @@
import com.oracle.truffle.api.dsl.GenerateInline;
import com.oracle.truffle.api.dsl.GenerateUncached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.LoopNode;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.strings.TruffleString;
import com.oracle.truffle.api.strings.TruffleStringBuilder;
Expand Down Expand Up @@ -343,24 +343,20 @@ public static TruffleString[] split(TruffleString s, TruffleString sep, TruffleS
if (selfLen == sepLen && eqNode.execute(s, sep, TS_ENCODING)) {
return PythonUtils.EMPTY_TRUFFLESTRING_ARRAY;
}
List<TruffleString> l = new ArrayList<>();
ArrayBuilder<TruffleString> l = new ArrayBuilder<>();
while (lastEnd < selfLen) {
int nextIndex = indexOfStringNode.execute(s, sep, lastEnd, selfLen, TS_ENCODING);
if (nextIndex < 0) {
break;
}
add(l, substringNode.execute(s, lastEnd, nextIndex - lastEnd, TS_ENCODING, false));
l.add(substringNode.execute(s, lastEnd, nextIndex - lastEnd, TS_ENCODING, false));
lastEnd = nextIndex + sepLen;
}
add(l, substringNode.execute(s, lastEnd, selfLen - lastEnd, TS_ENCODING, false));
LoopNode.reportLoopCount(substringNode, l.size());
l.add(substringNode.execute(s, lastEnd, selfLen - lastEnd, TS_ENCODING, false));
return l.toArray(new TruffleString[l.size()]);
}

@TruffleBoundary
private static void add(List<TruffleString> l, TruffleString s) {
l.add(s);
}

@TruffleBoundary
public static TruffleString cat(TruffleString arg1, TruffleString arg2) {
return arg1.concatUncached(arg2, TS_ENCODING, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ public T peek() {
return (T) data[size - 1];
}

public void reverse() {
for (int left = 0, right = size - 1; left < right; left++, right--) {
Object item = data[left];
data[left] = data[right];
data[right] = item;
}
}

public boolean containsIdentical(Object o) {
for (int i = 0; i < size; i++) {
if (data[i] == o) {
Expand Down
Loading