Skip to content

Commit be9da6c

Browse files
committed
[GR-21867] Clean up few performance warnings
PullRequest: graalpython/1209
2 parents 9bb52f0 + bf15454 commit be9da6c

File tree

14 files changed

+280
-271
lines changed

14 files changed

+280
-271
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MarshalModuleBuiltins.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
4747
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
4848
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
49-
import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
5049
import com.oracle.graal.python.builtins.objects.code.CodeNodes;
5150
import com.oracle.graal.python.builtins.objects.code.CodeNodes.CreateCodeNode;
5251
import com.oracle.graal.python.builtins.objects.code.PCode;
@@ -75,7 +74,6 @@
7574
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7675
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
7776
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
78-
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
7977
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
8078
import com.oracle.graal.python.runtime.PythonContext;
8179
import com.oracle.truffle.api.Assumption;
@@ -228,7 +226,6 @@ abstract static class MarshallerNode extends PNodeWithState {
228226

229227
public abstract void execute(VirtualFrame frame, Object x, int version, DataOutputStream buffer);
230228

231-
@Child private CastToJavaStringNode castStrNode;
232229
@Child private MarshallerNode recursiveNode;
233230
private int depth = 0;
234231
@Child private IsBuiltinClassProfile isBuiltinProfile;
@@ -373,7 +370,14 @@ void handleInternedString(InternedString v, int version, DataOutputStream buffer
373370
}
374371

375372
@Specialization
376-
void handleBytesLike(VirtualFrame frame, PIBytesLike v, int version, DataOutputStream buffer,
373+
void handleBytesLike(VirtualFrame frame, PBytes v, int version, DataOutputStream buffer,
374+
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
375+
writeByte(TYPE_BYTESLIKE, version, buffer);
376+
writeBytes(toBytesNode.execute(frame, v), version, buffer);
377+
}
378+
379+
@Specialization
380+
void handleBytesLike(VirtualFrame frame, PByteArray v, int version, DataOutputStream buffer,
377381
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
378382
writeByte(TYPE_BYTESLIKE, version, buffer);
379383
writeBytes(toBytesNode.execute(frame, v), version, buffer);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PosixModuleBuiltins.java

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
9696
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
9797
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
98-
import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
9998
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
10099
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.LenNode;
101100
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
@@ -135,6 +134,7 @@
135134
import com.oracle.graal.python.runtime.exception.PythonExitException;
136135
import com.oracle.graal.python.runtime.sequence.PSequence;
137136
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
137+
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
138138
import com.oracle.graal.python.util.FileDeleteShutdownHook;
139139
import com.oracle.truffle.api.CompilerDirectives;
140140
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
@@ -427,7 +427,7 @@ long getPid() throws Exception {
427427

428428
@Specialization
429429
@TruffleBoundary
430-
long getPidFallback() {
430+
static long getPidFallback() {
431431
String info = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
432432
return Long.parseLong(info.split("@")[0]);
433433
}
@@ -437,12 +437,12 @@ long getPidFallback() {
437437
@GenerateNodeFactory
438438
public abstract static class GetUidNode extends PythonBuiltinNode {
439439
@Specialization
440-
int getPid() {
440+
static int getPid() {
441441
return getSystemUid();
442442
}
443443

444444
@TruffleBoundary
445-
int getSystemUid() {
445+
static int getSystemUid() {
446446
String osName = System.getProperty("os.name");
447447
if (osName.contains("Linux")) {
448448
return (int) new com.sun.security.auth.module.UnixSystem().getUid();
@@ -488,31 +488,36 @@ Object fstat(VirtualFrame frame, int fd,
488488
return statNode.executeWith(frame, resources.getFilePath(fd), PNone.NO_VALUE);
489489
} else {
490490
fstatForNonFile.enter();
491-
Channel fileChannel = resources.getFileChannel(fd, channelClassProfile);
492-
int mode = 0;
493-
if (fileChannel instanceof ReadableByteChannel) {
494-
mode |= 0444;
495-
}
496-
if (fileChannel instanceof WritableByteChannel) {
497-
mode |= 0222;
498-
}
499-
return factory().createTuple(new Object[]{
500-
mode,
501-
0, // ino
502-
0, // dev
503-
0, // nlink
504-
0,
505-
0,
506-
0,
507-
0,
508-
0,
509-
0,
510-
});
491+
return fstatWithoutPath(resources, fd, channelClassProfile);
511492
}
512493
}
513494

495+
@TruffleBoundary(allowInlining = true)
496+
private PTuple fstatWithoutPath(PosixResources resources, int fd, ValueProfile channelClassProfile) {
497+
Channel fileChannel = resources.getFileChannel(fd, channelClassProfile);
498+
int mode = 0;
499+
if (fileChannel instanceof ReadableByteChannel) {
500+
mode |= 0444;
501+
}
502+
if (fileChannel instanceof WritableByteChannel) {
503+
mode |= 0222;
504+
}
505+
return factory().createTuple(new Object[]{
506+
mode,
507+
0, // ino
508+
0, // dev
509+
0, // nlink
510+
0,
511+
0,
512+
0,
513+
0,
514+
0,
515+
0,
516+
});
517+
}
518+
514519
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
515-
Object fstatPInt(VirtualFrame frame, Object fd,
520+
static Object fstatPInt(VirtualFrame frame, Object fd,
516521
@CachedLibrary("fd") PythonObjectLibrary lib,
517522
@Cached("create()") FstatNode recursive) {
518523
return recursive.executeWith(frame, lib.asSizeWithState(fd, PArguments.getThreadState(frame)));
@@ -527,7 +532,7 @@ protected static FstatNode create() {
527532
@GenerateNodeFactory
528533
public abstract static class SetInheritableNode extends PythonFileNode {
529534
@Specialization(guards = {"fd >= 0", "fd <= 2"})
530-
Object setInheritableStd(@SuppressWarnings("unused") int fd, @SuppressWarnings("unused") Object inheritable) {
535+
static Object setInheritableStd(@SuppressWarnings("unused") int fd, @SuppressWarnings("unused") Object inheritable) {
531536
// TODO: investigate if for the stdout/in/err this flag can be set
532537
return PNone.NONE;
533538
}
@@ -571,7 +576,7 @@ Object doStatDefault(VirtualFrame frame, Object path, @SuppressWarnings("unused"
571576
}
572577

573578
@TruffleBoundary
574-
long fileTimeToSeconds(FileTime t) {
579+
static long fileTimeToSeconds(FileTime t) {
575580
return t.to(TimeUnit.SECONDS);
576581
}
577582

@@ -1142,26 +1147,34 @@ public abstract static class WriteNode extends PythonFileNode {
11421147

11431148
public abstract Object executeWith(VirtualFrame frame, Object fd, Object data);
11441149

1150+
@TruffleBoundary(allowInlining = true, transferToInterpreterOnException = false)
1151+
private static Object writableOp(byte[] data, Object channel) throws IOException {
1152+
if (channel instanceof WritableByteChannel) {
1153+
return doWriteOp(data, channel);
1154+
}
1155+
return null;
1156+
}
1157+
11451158
@Specialization
11461159
Object write(VirtualFrame frame, int fd, byte[] data,
11471160
@Cached("createClassProfile()") ValueProfile channelClassProfile) {
11481161
Channel channel = getResources().getFileChannel(fd, channelClassProfile);
1149-
if (channel instanceof WritableByteChannel) {
1150-
try {
1151-
return doWriteOp(data, (WritableByteChannel) channel);
1152-
} catch (Exception e) {
1153-
gotException.enter();
1154-
throw raiseOSError(frame, e);
1162+
try {
1163+
Object ret = writableOp(data, channel);
1164+
if (ret != null) {
1165+
return ret;
11551166
}
1156-
} else {
1157-
notWritable.enter();
1158-
throw raiseOSError(frame, OSErrorEnum.EBADF);
1167+
} catch (Exception e) {
1168+
gotException.enter();
1169+
throw raiseOSError(frame, e);
11591170
}
1171+
notWritable.enter();
1172+
throw raiseOSError(frame, OSErrorEnum.EBADF);
11601173
}
11611174

11621175
@TruffleBoundary(allowInlining = true, transferToInterpreterOnException = false)
1163-
private static int doWriteOp(byte[] data, WritableByteChannel channel) throws IOException {
1164-
return channel.write(ByteBuffer.wrap(data));
1176+
private static int doWriteOp(byte[] data, Object channel) throws IOException {
1177+
return ((WritableByteChannel) channel).write(ByteBuffer.wrap(data));
11651178
}
11661179

11671180
@Specialization
@@ -1178,28 +1191,28 @@ private static byte[] stringToBytes(String data) {
11781191
@Specialization
11791192
Object write(VirtualFrame frame, int fd, PBytes data,
11801193
@Cached("createClassProfile()") ValueProfile channelClassProfile) {
1181-
return write(frame, fd, getByteArray(data), channelClassProfile);
1194+
return write(frame, fd, getByteArray(data.getSequenceStorage()), channelClassProfile);
11821195
}
11831196

11841197
@Specialization
11851198
Object write(VirtualFrame frame, int fd, PByteArray data,
11861199
@Cached("createClassProfile()") ValueProfile channelClassProfile) {
1187-
return write(frame, fd, getByteArray(data), channelClassProfile);
1200+
return write(frame, fd, getByteArray(data.getSequenceStorage()), channelClassProfile);
11881201
}
11891202

11901203
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
1191-
Object writePInt(VirtualFrame frame, Object fd, Object data,
1204+
static Object writePInt(VirtualFrame frame, Object fd, Object data,
11921205
@CachedLibrary("fd") PythonObjectLibrary lib,
11931206
@Cached("create()") WriteNode recursive) {
11941207
return recursive.executeWith(frame, lib.asSizeWithState(fd, PArguments.getThreadState(frame)), data);
11951208
}
11961209

1197-
private byte[] getByteArray(PIBytesLike pByteArray) {
1210+
private byte[] getByteArray(SequenceStorage storage) {
11981211
if (toByteArrayNode == null) {
11991212
CompilerDirectives.transferToInterpreterAndInvalidate();
12001213
toByteArrayNode = insert(ToByteArrayNodeGen.create());
12011214
}
1202-
return toByteArrayNode.execute(pByteArray.getSequenceStorage());
1215+
return toByteArrayNode.execute(storage);
12031216
}
12041217

12051218
public static WriteNode create() {
@@ -1262,7 +1275,7 @@ boolean isATTY(long fd) {
12621275
}
12631276

12641277
@Fallback
1265-
boolean isATTY(@SuppressWarnings("unused") Object fd) {
1278+
static boolean isATTY(@SuppressWarnings("unused") Object fd) {
12661279
return false;
12671280
}
12681281
}
@@ -1773,7 +1786,7 @@ boolean access(String path, int mode, Object dirFd, boolean effectiveIds, boolea
17731786
@GenerateNodeFactory
17741787
abstract static class CpuCountNode extends PythonBuiltinNode {
17751788
@Specialization
1776-
int getCpuCount() {
1789+
static int getCpuCount() {
17771790
return Runtime.getRuntime().availableProcessors();
17781791
}
17791792
}
@@ -1893,7 +1906,7 @@ Object getTerminalSize(VirtualFrame frame, Object fd) {
18931906
return recursiveNode.execute(frame, value);
18941907
}
18951908

1896-
protected GetTerminalSizeNode create() {
1909+
protected static GetTerminalSizeNode create() {
18971910
return PosixModuleBuiltinsFactory.GetTerminalSizeNodeFactory.create();
18981911
}
18991912
}
@@ -1927,7 +1940,7 @@ public abstract static class StrErrorNode extends PythonBuiltinNode {
19271940

19281941
@Specialization
19291942
@TruffleBoundary
1930-
String getStrError(int errno) {
1943+
static String getStrError(int errno) {
19311944
if (STR_ERROR_MAP.isEmpty()) {
19321945
for (OSErrorEnum error : OSErrorEnum.values()) {
19331946
STR_ERROR_MAP.put(error.getNumber(), error.getMessage());
@@ -1945,7 +1958,7 @@ String getStrError(int errno) {
19451958
@GenerateNodeFactory
19461959
abstract static class CtermId extends PythonBuiltinNode {
19471960
@Specialization
1948-
String ctermid() {
1961+
static String ctermid() {
19491962
return "/dev/tty";
19501963
}
19511964
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PosixSubprocessModuleBuiltins.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,25 +221,29 @@ private synchronized int forkExec(PList args, @SuppressWarnings("unused") PList
221221

222222
return resources.registerChild(process);
223223
} catch (IOException e) {
224-
Channel err = null;
225224
if (errpipe_write != -1) {
226225
// write exec error information here. Data format: "exception name:hex
227226
// errno:description"
228-
err = resources.getFileChannel(errpipe_write);
229-
if (!(err instanceof WritableByteChannel)) {
230-
throw raise(PythonBuiltinClassType.OSError, ErrorMessages.ERROR_WRITING_FORKEXEC);
231-
} else {
232-
ErrorAndMessagePair pair = OSErrorEnum.fromException(e);
233-
try {
234-
((WritableByteChannel) err).write(ByteBuffer.wrap(("OSError:" + Long.toHexString(pair.oserror.getNumber()) + ":" + pair.message).getBytes()));
235-
} catch (IOException e1) {
236-
}
237-
}
227+
handleIOError(errpipe_write, resources, e);
238228
}
239229
return -1;
240230
}
241231
}
242232

233+
@TruffleBoundary(allowInlining = true)
234+
private void handleIOError(int errpipe_write, PosixResources resources, IOException e) {
235+
Channel err = resources.getFileChannel(errpipe_write);
236+
if (!(err instanceof WritableByteChannel)) {
237+
throw raise(PythonBuiltinClassType.OSError, ErrorMessages.ERROR_WRITING_FORKEXEC);
238+
} else {
239+
ErrorAndMessagePair pair = OSErrorEnum.fromException(e);
240+
try {
241+
((WritableByteChannel) err).write(ByteBuffer.wrap(("OSError:" + Long.toHexString(pair.oserror.getNumber()) + ":" + pair.message).getBytes()));
242+
} catch (IOException e1) {
243+
}
244+
}
245+
}
246+
243247
@Specialization(replaces = "forkExec")
244248
int forkExecDefault(VirtualFrame frame, Object args, Object executable_list, Object close_fds,
245249
Object fdsToKeep, Object cwd, Object env,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SREModuleBuiltins.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,23 +219,23 @@ Object call(VirtualFrame frame, Object callable, Object arg1, Object arg2,
219219
typeError.enter();
220220
throw raise(TypeError, "%s", e);
221221
} catch (RuntimeException e) {
222-
if (e instanceof TruffleException) {
223-
potentialSyntaxError.enter(); // this guards the TruffleBoundary invoke
224-
if (isSyntaxError(e)) {
225-
syntaxError.enter();
226-
throw raise(ValueError, "%s", e);
227-
}
228-
}
229-
// just re-throw
230-
throw e;
222+
return handleError(e, syntaxError, potentialSyntaxError);
231223
} finally {
232224
IndirectCallContext.exit(frame, context, state);
233225
}
234226
}
235227

236228
@TruffleBoundary
237-
private static boolean isSyntaxError(RuntimeException e) {
238-
return ((TruffleException) e).isSyntaxError();
229+
private Object handleError(RuntimeException e, BranchProfile syntaxError, BranchProfile potentialSyntaxError) {
230+
if (e instanceof TruffleException) {
231+
potentialSyntaxError.enter(); // this guards the TruffleBoundary invoke
232+
if (((TruffleException) e).isSyntaxError()) {
233+
syntaxError.enter();
234+
throw raise(ValueError, "%s", e);
235+
}
236+
}
237+
// just re-throw
238+
throw e;
239239
}
240240
}
241241

0 commit comments

Comments
 (0)