Skip to content

Commit 5337682

Browse files
committed
[GR-47834] Always split String#<< and profile to avoid unnecessary compile() calls
PullRequest: truffleruby/4189
2 parents e50119f + 17306fc commit 5337682

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/main/java/org/truffleruby/core/regexp/TruffleRegexpNodes.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.oracle.truffle.api.CompilerDirectives;
2525
import com.oracle.truffle.api.TruffleSafepoint;
2626
import com.oracle.truffle.api.dsl.Bind;
27+
import com.oracle.truffle.api.dsl.Cached.Exclusive;
2728
import com.oracle.truffle.api.dsl.Fallback;
2829
import com.oracle.truffle.api.dsl.GenerateCached;
2930
import com.oracle.truffle.api.dsl.GenerateInline;
@@ -86,6 +87,7 @@
8687
import org.truffleruby.language.library.RubyStringLibrary;
8788
import org.truffleruby.language.objects.AllocationTracing;
8889
import org.truffleruby.parser.RubyDeferredWarnings;
90+
import org.truffleruby.utils.RunTwiceBranchProfile;
8991

9092
import static com.oracle.truffle.api.strings.TruffleString.CodeRange.ASCII;
9193
import static com.oracle.truffle.api.strings.TruffleString.CodeRange.BROKEN;
@@ -372,41 +374,49 @@ public abstract static class TRegexCompileNode extends RubyBaseNode {
372374
@Child DispatchNode warnOnFallbackNode;
373375

374376
@Specialization(guards = "encoding == US_ASCII")
375-
Object usASCII(RubyRegexp regexp, boolean atStart, RubyEncoding encoding) {
377+
Object usASCII(RubyRegexp regexp, boolean atStart, RubyEncoding encoding,
378+
@Cached @Exclusive RunTwiceBranchProfile compileProfile) {
376379
final Object tregex = regexp.tregexCache.getUSASCIIRegex(atStart);
377380
if (tregex != null) {
378381
return tregex;
379382
} else {
383+
compileProfile.enter();
380384
return regexp.tregexCache.compile(getContext(), regexp, atStart, encoding, this);
381385
}
382386
}
383387

384388
@Specialization(guards = "encoding == ISO_8859_1")
385-
Object latin1(RubyRegexp regexp, boolean atStart, RubyEncoding encoding) {
389+
Object latin1(RubyRegexp regexp, boolean atStart, RubyEncoding encoding,
390+
@Cached @Exclusive RunTwiceBranchProfile compileProfile) {
386391
final Object tregex = regexp.tregexCache.getLatin1Regex(atStart);
387392
if (tregex != null) {
388393
return tregex;
389394
} else {
395+
compileProfile.enter();
390396
return regexp.tregexCache.compile(getContext(), regexp, atStart, encoding, this);
391397
}
392398
}
393399

394400
@Specialization(guards = "encoding == UTF_8")
395-
Object utf8(RubyRegexp regexp, boolean atStart, RubyEncoding encoding) {
401+
Object utf8(RubyRegexp regexp, boolean atStart, RubyEncoding encoding,
402+
@Cached @Exclusive RunTwiceBranchProfile compileProfile) {
396403
final Object tregex = regexp.tregexCache.getUTF8Regex(atStart);
397404
if (tregex != null) {
398405
return tregex;
399406
} else {
407+
compileProfile.enter();
400408
return regexp.tregexCache.compile(getContext(), regexp, atStart, encoding, this);
401409
}
402410
}
403411

404412
@Specialization(guards = "encoding == BINARY")
405-
Object binary(RubyRegexp regexp, boolean atStart, RubyEncoding encoding) {
413+
Object binary(RubyRegexp regexp, boolean atStart, RubyEncoding encoding,
414+
@Cached @Exclusive RunTwiceBranchProfile compileProfile) {
406415
final Object tregex = regexp.tregexCache.getBinaryRegex(atStart);
407416
if (tregex != null) {
408417
return tregex;
409418
} else {
419+
compileProfile.enter();
410420
return regexp.tregexCache.compile(getContext(), regexp, atStart, encoding, this);
411421
}
412422
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
import org.jcodings.specific.ASCIIEncoding;
102102
import org.truffleruby.Layouts;
103103
import org.truffleruby.annotations.CoreMethod;
104+
import org.truffleruby.annotations.Split;
104105
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
105106
import org.truffleruby.annotations.CoreModule;
106107
import org.truffleruby.annotations.Primitive;
@@ -417,7 +418,7 @@ RubyString dupAsStringInstance(Object string,
417418
}
418419
}
419420

420-
@CoreMethod(names = "<<", required = 1, raiseIfNotMutableSelf = true)
421+
@CoreMethod(names = "<<", required = 1, raiseIfNotMutableSelf = true, split = Split.ALWAYS)
421422
@ImportStatic(StringGuards.class)
422423
public abstract static class StringConcatOneNode extends CoreMethodArrayArgumentsNode {
423424

src/main/java/org/truffleruby/utils/RunTwiceBranchProfile.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.oracle.truffle.api.CompilerDirectives;
1313
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
14+
import com.oracle.truffle.api.dsl.NeverDefault;
1415
import com.oracle.truffle.api.nodes.NodeCloneable;
1516

1617
public final class RunTwiceBranchProfile extends NodeCloneable {
@@ -29,6 +30,11 @@ public ExecuteCounter next() {
2930
}
3031
}
3132

33+
@NeverDefault
34+
public static RunTwiceBranchProfile create() {
35+
return new RunTwiceBranchProfile();
36+
}
37+
3238
@CompilationFinal private ExecuteCounter executeCounter = ExecuteCounter.NEVER;
3339

3440
public void enter() {

0 commit comments

Comments
 (0)