Skip to content

Commit 92f5dee

Browse files
committed
mmap builtins
1 parent cd08f0f commit 92f5dee

File tree

1 file changed

+16
-21
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/mmap

1 file changed

+16
-21
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/mmap/MMapBuiltins.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@
7575
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
7676
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
7777
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum;
78+
import com.oracle.graal.python.builtins.objects.function.PArguments;
7879
import com.oracle.graal.python.builtins.objects.ints.PInt;
7980
import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
8081
import com.oracle.graal.python.builtins.objects.mmap.MMapBuiltinsFactory.InternalLenNodeGen;
82+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
8183
import com.oracle.graal.python.builtins.objects.slice.PSlice;
8284
import com.oracle.graal.python.builtins.objects.slice.PSlice.SliceInfo;
8385
import com.oracle.graal.python.nodes.PNodeWithContext;
@@ -91,7 +93,6 @@
9193
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
9294
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
9395
import com.oracle.graal.python.nodes.util.CastToByteNode;
94-
import com.oracle.graal.python.nodes.util.CastToIndexNode;
9596
import com.oracle.graal.python.nodes.util.ChannelNodes;
9697
import com.oracle.graal.python.nodes.util.ChannelNodes.ReadByteFromChannelNode;
9798
import com.oracle.graal.python.nodes.util.ChannelNodes.ReadFromChannelNode;
@@ -103,12 +104,14 @@
103104
import com.oracle.truffle.api.CompilerDirectives;
104105
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
105106
import com.oracle.truffle.api.dsl.Cached;
107+
import com.oracle.truffle.api.dsl.Cached.Shared;
106108
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
107109
import com.oracle.truffle.api.dsl.GenerateUncached;
108110
import com.oracle.truffle.api.dsl.NodeFactory;
109111
import com.oracle.truffle.api.dsl.Specialization;
110112
import com.oracle.truffle.api.dsl.TypeSystemReference;
111113
import com.oracle.truffle.api.frame.VirtualFrame;
114+
import com.oracle.truffle.api.library.CachedLibrary;
112115
import com.oracle.truffle.api.profiles.BranchProfile;
113116
import com.oracle.truffle.api.profiles.ConditionProfile;
114117

@@ -489,12 +492,12 @@ PBytes readUnlimited(PMMap self, @SuppressWarnings("unused") PNone n,
489492
return factory().createBytes(res);
490493
}
491494

492-
@Specialization(guards = "!isNoValue(n)")
495+
@Specialization(guards = "!isNoValue(n)", limit = "getCallSiteInlineCacheMaxDepth()")
493496
PBytes read(VirtualFrame frame, PMMap self, Object n,
494497
@Cached("create()") ReadFromChannelNode readChannelNode,
495-
@Cached("create()") CastToIndexNode castToIndexNode,
498+
@CachedLibrary("n") PythonObjectLibrary lib,
496499
@Cached("createBinaryProfile()") ConditionProfile negativeProfile) {
497-
int nread = castToIndexNode.execute(frame, n);
500+
int nread = lib.asIndexWithState(n, PArguments.getThreadState(frame));
498501
if (negativeProfile.profile(nread < 0)) {
499502
return readUnlimited(self, PNone.NO_VALUE, readChannelNode);
500503
}
@@ -569,17 +572,17 @@ int writeMemoryview(VirtualFrame frame, PMMap self, PMemoryView memoryView,
569572
@GenerateNodeFactory
570573
@TypeSystemReference(PythonArithmeticTypes.class)
571574
abstract static class SeekNode extends PythonBuiltinNode implements MMapBaseNode {
572-
@Child private CastToIndexNode castToLongNode;
573-
574-
private final BranchProfile errorProfile = BranchProfile.create();
575-
576575
@Specialization(guards = "isNoValue(how)")
577-
Object seek(VirtualFrame frame, PMMap self, long dist, @SuppressWarnings("unused") PNone how) {
578-
return seek(frame, self, dist, 0);
576+
Object seek(VirtualFrame frame, PMMap self, long dist, @SuppressWarnings("unused") PNone how,
577+
@Shared("errorProfile") @Cached BranchProfile errorProfile,
578+
@Shared("library") @CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib) {
579+
return seek(frame, self, dist, 0, errorProfile, lib);
579580
}
580581

581-
@Specialization
582-
Object seek(VirtualFrame frame, PMMap self, long dist, Object how) {
582+
@Specialization(guards = "!isNoValue(how)")
583+
Object seek(VirtualFrame frame, PMMap self, long dist, Object how,
584+
@Shared("errorProfile") @Cached BranchProfile errorProfile,
585+
@Shared("library") @CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib) {
583586
try {
584587
SeekableByteChannel channel = self.getChannel();
585588
long size;
@@ -589,7 +592,7 @@ Object seek(VirtualFrame frame, PMMap self, long dist, Object how) {
589592
size = self.getLength();
590593
}
591594
long where;
592-
int ihow = castToInt(frame, how);
595+
int ihow = lib.asIndexWithState(how, PArguments.getThreadState(frame));
593596
switch (ihow) {
594597
case 0: /* relative to start */
595598
where = dist;
@@ -615,14 +618,6 @@ Object seek(VirtualFrame frame, PMMap self, long dist, Object how) {
615618
throw raiseOSError(frame, OSErrorEnum.EIO, e);
616619
}
617620
}
618-
619-
private int castToInt(VirtualFrame frame, Object val) {
620-
if (castToLongNode == null) {
621-
CompilerDirectives.transferToInterpreterAndInvalidate();
622-
castToLongNode = insert(CastToIndexNode.create(PythonBuiltinClassType.TypeError, (obj) -> 0));
623-
}
624-
return castToLongNode.execute(frame, val);
625-
}
626621
}
627622

628623
@Builtin(name = "find", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 4)

0 commit comments

Comments
 (0)