Skip to content

Commit 232ef50

Browse files
committed
Use profileAndReportLoopCount() instead of reportLoopCount() and LoopConditionProfile#profile and check with CheckStyle
1 parent 21ee6ff commit 232ef50

File tree

6 files changed

+33
-24
lines changed

6 files changed

+33
-24
lines changed

src/main/.checkstyle_checks.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@
234234
<property name="format" value='Collections\.newSetFromMap\(new ConcurrentHashMap'/>
235235
<property name="message" value="Use ConcurrentHashMap.newKeySet() instead."/>
236236
</module>
237+
<module name="RegexpSinglelineJava">
238+
<property name="format" value='LoopNode\.reportLoopCount\('/>
239+
<property name="message" value="Use profileAndReportLoopCount() instead."/>
240+
</module>
241+
<module name="RegexpSinglelineJava">
242+
<property name="format" value='\.profileCounted\('/>
243+
<property name="message" value="Use profileAndReportLoopCount() instead."/>
244+
</module>
237245
<module name="IllegalType">
238246
<!-- Use PrintStream instead of PrintWriter, PrintWriter does not consistently flush, even when writing \n.-->
239247
<property name="illegalClassNames" value="TruffleObject,DynamicObject,PrintWriter"/>

src/main/java/org/truffleruby/core/encoding/EncodingLeftCharHeadNode.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1313
import com.oracle.truffle.api.dsl.Cached;
1414
import com.oracle.truffle.api.dsl.Specialization;
15-
import com.oracle.truffle.api.nodes.LoopNode;
1615
import com.oracle.truffle.api.profiles.LoopConditionProfile;
1716
import org.truffleruby.language.RubyBaseNode;
1817

@@ -40,11 +39,11 @@ protected int leftAdjustCharHeadUtf8(RubyEncoding enc, byte[] bytes, int p, int
4039
}
4140
int i = s;
4241
try {
43-
while (loopProfile.profile(!utf8IsLead(bytes[i] & 0xff) && i > p)) {
42+
while (loopProfile.inject(!utf8IsLead(bytes[i] & 0xff) && i > p)) {
4443
i--;
4544
}
4645
} finally {
47-
LoopNode.reportLoopCount(this, s - i);
46+
profileAndReportLoopCount(loopProfile, s - i);
4847
}
4948
return i;
5049

src/main/java/org/truffleruby/core/format/control/RepeatLoopNode.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
*/
1010
package org.truffleruby.core.format.control;
1111

12+
import com.oracle.truffle.api.profiles.LoopConditionProfile;
1213
import org.truffleruby.core.format.FormatNode;
1314

1415
import com.oracle.truffle.api.frame.VirtualFrame;
15-
import com.oracle.truffle.api.nodes.LoopNode;
1616

1717
public class RepeatLoopNode extends FormatNode {
1818

1919
private final int count;
2020

2121
@Child private FormatNode child;
22+
private final LoopConditionProfile loopProfile = LoopConditionProfile.create();
2223

2324
public RepeatLoopNode(int count, FormatNode child) {
2425
this.count = count;
@@ -27,12 +28,13 @@ public RepeatLoopNode(int count, FormatNode child) {
2728

2829
@Override
2930
public Object execute(VirtualFrame frame) {
31+
int i = 0;
3032
try {
31-
for (int i = 0; i < count; i++) {
33+
for (; loopProfile.inject(i < count); i++) {
3234
child.execute(frame);
3335
}
3436
} finally {
35-
LoopNode.reportLoopCount(this, count);
37+
profileAndReportLoopCount(loopProfile, i);
3638
}
3739

3840
return null;

src/main/java/org/truffleruby/core/format/control/SequenceNode.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import com.oracle.truffle.api.frame.VirtualFrame;
1515
import com.oracle.truffle.api.nodes.ExplodeLoop;
16-
import com.oracle.truffle.api.nodes.LoopNode;
1716

1817
public class SequenceNode extends FormatNode {
1918

@@ -26,12 +25,8 @@ public SequenceNode(FormatNode[] children) {
2625
@ExplodeLoop
2726
@Override
2827
public Object execute(VirtualFrame frame) {
29-
try {
30-
for (FormatNode child : children) {
31-
child.execute(frame);
32-
}
33-
} finally {
34-
LoopNode.reportLoopCount(this, children.length);
28+
for (FormatNode child : children) {
29+
child.execute(frame);
3530
}
3631

3732
return null;

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
import com.oracle.truffle.api.dsl.Bind;
7979
import com.oracle.truffle.api.dsl.Cached.Exclusive;
8080
import com.oracle.truffle.api.dsl.Cached.Shared;
81-
import com.oracle.truffle.api.nodes.LoopNode;
8281
import com.oracle.truffle.api.profiles.LoopConditionProfile;
8382
import org.graalvm.collections.Pair;
8483
import org.jcodings.Config;
@@ -4549,8 +4548,7 @@ protected RubyNode coercePatternToRope(RubyNode pattern) {
45494548
protected Object singleByteOptimizable(Rope stringRope, Rope patternRope, int offset,
45504549
@Cached @Shared("stringBytesNode") BytesNode stringBytesNode,
45514550
@Cached @Shared("patternBytesNode") BytesNode patternBytesNode,
4552-
@Cached LoopConditionProfile loopProfile,
4553-
@Cached("createCountingProfile()") ConditionProfile matchProfile) {
4551+
@Cached LoopConditionProfile loopProfile) {
45544552

45554553
assert offset >= 0;
45564554
assert offset + patternRope.byteLength() <= stringRope
@@ -4565,13 +4563,13 @@ protected Object singleByteOptimizable(Rope stringRope, Rope patternRope, int of
45654563
final byte[] patternBytes = patternBytesNode.execute(patternRope);
45664564

45674565
try {
4568-
for (; loopProfile.profile(p < l); p++) {
4569-
if (matchProfile.profile(ArrayUtils.regionEquals(stringBytes, p, patternBytes, 0, pe))) {
4566+
for (; loopProfile.inject(p < l); p++) {
4567+
if (ArrayUtils.regionEquals(stringBytes, p, patternBytes, 0, pe)) {
45704568
return p;
45714569
}
45724570
}
45734571
} finally {
4574-
LoopNode.reportLoopCount(this, p - offset);
4572+
profileAndReportLoopCount(loopProfile, p - offset);
45754573
}
45764574

45774575
return nil;
@@ -4658,8 +4656,7 @@ protected Object patternTooLarge(Rope stringRope, Rope patternRope, int offset)
46584656
protected Object singleByteOptimizable(Rope stringRope, Rope patternRope, int offset,
46594657
@Cached @Shared("stringBytesNode") BytesNode stringBytesNode,
46604658
@Cached @Shared("patternBytesNode") BytesNode patternBytesNode,
4661-
@Cached LoopConditionProfile loopProfile,
4662-
@Cached("createCountingProfile()") ConditionProfile matchProfile) {
4659+
@Cached LoopConditionProfile loopProfile) {
46634660

46644661
assert offset >= 0;
46654662
int p = offset;
@@ -4671,13 +4668,13 @@ protected Object singleByteOptimizable(Rope stringRope, Rope patternRope, int of
46714668
final byte[] patternBytes = patternBytesNode.execute(patternRope);
46724669

46734670
try {
4674-
for (; loopProfile.profile(p < l); p++) {
4675-
if (matchProfile.profile(ArrayUtils.regionEquals(stringBytes, p, patternBytes, 0, pe))) {
4671+
for (; loopProfile.inject(p < l); p++) {
4672+
if (ArrayUtils.regionEquals(stringBytes, p, patternBytes, 0, pe)) {
46764673
return p;
46774674
}
46784675
}
46794676
} finally {
4680-
LoopNode.reportLoopCount(this, p - offset);
4677+
profileAndReportLoopCount(loopProfile, p - offset);
46814678
}
46824679

46834680
return nil;

src/main/java/org/truffleruby/language/RubyBaseNode.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,31 @@ public static Object nullToNil(Object value) {
4545
/** Variants for {@link com.oracle.truffle.api.library.Library}. The node argument should typically be
4646
* {@code node.getNode()} with {@code @CachedLibrary("this") ArrayStoreLibrary node} */
4747
public static void profileAndReportLoopCount(Node node, LoopConditionProfile loopProfile, int count) {
48+
// Checkstyle: stop
4849
loopProfile.profileCounted(count);
4950
LoopNode.reportLoopCount(node, count);
51+
// Checkstyle: resume
5052
}
5153

5254
public void profileAndReportLoopCount(LoopConditionProfile loopProfile, int count) {
55+
// Checkstyle: stop
5356
loopProfile.profileCounted(count);
5457
LoopNode.reportLoopCount(this, count);
58+
// Checkstyle: resume
5559
}
5660

5761
public void profileAndReportLoopCount(LoopConditionProfile loopProfile, long count) {
62+
// Checkstyle: stop
5863
loopProfile.profileCounted(count);
5964
reportLongLoopCount(count);
65+
// Checkstyle: resume
6066
}
6167

6268
protected void reportLongLoopCount(long count) {
6369
assert count >= 0L;
70+
// Checkstyle: stop
6471
LoopNode.reportLoopCount(this, count > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) count);
72+
// Checkstyle: resume
6573
}
6674

6775
protected Node getNode() {

0 commit comments

Comments
 (0)