Skip to content

Commit de15e38

Browse files
committed
Loop profiling for all/any related methods
Signed-off-by: Octave Larose <[email protected]>
1 parent 5dd75dc commit de15e38

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,13 @@
241241
import com.oracle.truffle.api.library.CachedLibrary;
242242
import com.oracle.truffle.api.nodes.ExplodeLoop;
243243
import com.oracle.truffle.api.nodes.ExplodeLoop.LoopExplosionKind;
244+
import com.oracle.truffle.api.nodes.LoopNode;
244245
import com.oracle.truffle.api.nodes.Node;
245246
import com.oracle.truffle.api.nodes.RootNode;
246247
import com.oracle.truffle.api.nodes.UnexpectedResultException;
247248
import com.oracle.truffle.api.profiles.BranchProfile;
248249
import com.oracle.truffle.api.profiles.ConditionProfile;
250+
import com.oracle.truffle.api.profiles.LoopConditionProfile;
249251
import com.oracle.truffle.api.source.Source;
250252
import com.oracle.truffle.api.utilities.TriState;
251253

@@ -306,20 +308,24 @@ enum NodeType {
306308

307309
@Child private PyObjectIsTrueNode isTrueNode = PyObjectIsTrueNode.create();
308310

311+
final private LoopConditionProfile loopConditionProfile = LoopConditionProfile.create();
312+
309313
abstract boolean execute(Frame frame, Object storageObj, NodeType nodeType);
310314

311315
@Specialization
312316
boolean doBoolSequence(VirtualFrame frame,
313317
BoolSequenceStorage sequenceStorage,
314318
NodeType nodeType) {
315319
boolean[] internalArray = sequenceStorage.getInternalBoolArray();
320+
int i = 0;
316321

317-
for (int i = 0; i < sequenceStorage.length(); i++) {
322+
while (loopConditionProfile.profile(i < sequenceStorage.length())) {
318323
if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) {
319324
return false;
320325
} else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) {
321326
return true;
322327
}
328+
i++;
323329
}
324330

325331
return nodeType == NodeType.ALL;
@@ -330,8 +336,9 @@ boolean doIntSequence(VirtualFrame frame,
330336
IntSequenceStorage sequenceStorage,
331337
NodeType nodeType) {
332338
int[] internalArray = sequenceStorage.getInternalIntArray();
339+
int i = 0;
333340

334-
for (int i = 0; i < sequenceStorage.length(); i++) {
341+
while (loopConditionProfile.profile(i < sequenceStorage.length())) {
335342
if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) {
336343
return false;
337344
} else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) {
@@ -348,7 +355,10 @@ boolean doGenericSequence(VirtualFrame frame,
348355
NodeType nodeType,
349356
@Cached SequenceStorageNodes.LenNode lenNode) {
350357
Object[] internalArray = sequenceStorage.getInternalArray();
351-
for (int i = 0; i < lenNode.execute(sequenceStorage); i++) {
358+
int i = 0;
359+
int seqLength = lenNode.execute(sequenceStorage);
360+
361+
while (loopConditionProfile.profile(i < seqLength)) {
352362
if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) {
353363
return false;
354364
} else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) {
@@ -365,6 +375,7 @@ protected boolean doHashStorage(VirtualFrame frame,
365375
NodeType nodeType,
366376
@CachedLibrary("hashingStorage") HashingStorageLibrary hlib) {
367377
for (Object key : hlib.keys(hashingStorage)) {
378+
LoopNode.reportLoopCount(this, 1);
368379
if (nodeType == NodeType.ALL) {
369380
if (!isTrueNode.execute(frame, key)) {
370381
return false;
@@ -407,7 +418,7 @@ static boolean doHashColl(VirtualFrame frame,
407418
}
408419

409420
@Specialization
410-
static boolean doObject(VirtualFrame frame,
421+
boolean doObject(VirtualFrame frame,
411422
Object object,
412423
@Cached PyObjectGetIter getIter,
413424
@Cached GetNextNode nextNode,
@@ -416,6 +427,7 @@ static boolean doObject(VirtualFrame frame,
416427
Object iterator = getIter.execute(frame, object);
417428
while (true) {
418429
try {
430+
LoopNode.reportLoopCount(this, 1);
419431
Object next = nextNode.execute(frame, iterator);
420432
if (!isTrueNode.execute(frame, next)) {
421433
return false;
@@ -452,14 +464,14 @@ static boolean doTuple(VirtualFrame frame,
452464

453465
@Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1")
454466
static boolean doHashColl(VirtualFrame frame,
455-
PHashingCollection object,
467+
PHashingCollection object,
456468
@SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode,
457469
@Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) {
458470
return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ANY);
459471
}
460472

461473
@Specialization
462-
static boolean doObject(VirtualFrame frame,
474+
boolean doObject(VirtualFrame frame,
463475
Object object,
464476
@Cached PyObjectGetIter getIter,
465477
@Cached GetNextNode nextNode,
@@ -468,6 +480,7 @@ static boolean doObject(VirtualFrame frame,
468480
Object iterator = getIter.execute(frame, object);
469481
while (true) {
470482
try {
483+
LoopNode.reportLoopCount(this, 1);
471484
Object next = nextNode.execute(frame, iterator);
472485
if (isTrueNode.execute(frame, next)) {
473486
return true;

0 commit comments

Comments
 (0)