Skip to content

Commit c184f87

Browse files
committed
Use nodes for strftime
1 parent ad01279 commit c184f87

File tree

2 files changed

+20
-33
lines changed

2 files changed

+20
-33
lines changed

src/main/java/org/truffleruby/core/time/RubyDateFormatter.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,13 @@
6565
import org.truffleruby.RubyLanguage;
6666
import org.truffleruby.core.exception.ErrnoErrorNode;
6767
import org.truffleruby.core.rope.CodeRange;
68-
import org.truffleruby.core.rope.ConcatRope;
6968
import org.truffleruby.core.rope.LazyIntRope;
7069
import org.truffleruby.core.rope.LeafRope;
71-
import org.truffleruby.core.rope.ManagedRope;
7270
import org.truffleruby.core.rope.Rope;
7371
import org.truffleruby.core.rope.RopeBuilder;
7472
import org.truffleruby.core.rope.RopeConstants;
73+
import org.truffleruby.core.rope.RopeNodes;
7574
import org.truffleruby.core.rope.RopeOperations;
76-
import org.truffleruby.core.rope.SubstringRope;
7775
import org.truffleruby.core.string.RubyString;
7876
import org.truffleruby.core.string.StringOperations;
7977
import org.truffleruby.language.backtrace.Backtrace;
@@ -614,12 +612,12 @@ public static boolean formatToRopeBuilderCanBeFast(Token[] compiledPattern) {
614612
}
615613

616614
@ExplodeLoop
617-
public static ManagedRope formatToRopeBuilderFast(Token[] compiledPattern, ZonedDateTime dt, Object zone,
618-
RubyContext context, RubyLanguage language, Node currentNode, ErrnoErrorNode errnoErrorNode) {
619-
ManagedRope rope = null;
615+
public static Rope formatToRopeBuilderFast(Token[] compiledPattern, ZonedDateTime dt,
616+
RopeNodes.ConcatNode concatNode, RopeNodes.SubstringNode substringNode) {
617+
Rope rope = null;
620618

621619
for (Token token : compiledPattern) {
622-
final ManagedRope appendRope;
620+
final Rope appendRope;
623621

624622
switch (token.getFormat()) {
625623
case FORMAT_ENCODING:
@@ -657,36 +655,30 @@ public static ManagedRope formatToRopeBuilderFast(Token[] compiledPattern, Zoned
657655

658656
case FORMAT_NANOSEC: {
659657
final int nano = dt.getNano();
660-
assert nano >= 0;
661-
assert nano < 1000000000;
662-
663658
final LazyIntRope nanoRope = new LazyIntRope(nano);
664659

665-
final int padding = 6 - nanoRope.characterLength();
660+
final int length = 6;
661+
final int padding = length - nanoRope.characterLength();
666662

667663
if (padding == 0) {
668664
appendRope = nanoRope;
669665
} else if (padding < 0) {
670-
appendRope = new SubstringRope(UTF8Encoding.INSTANCE, nanoRope, 0, 6, 6, CodeRange.CR_7BIT);
666+
appendRope = substringNode.executeSubstring(nanoRope, 0, length);
671667
} else {
672-
appendRope = new ConcatRope(
673-
nanoRope,
674-
RopeConstants.paddingZeros(padding),
675-
UTF8Encoding.INSTANCE,
676-
CodeRange.CR_7BIT);
668+
appendRope = concatNode
669+
.executeConcat(nanoRope, RopeConstants.paddingZeros(padding), UTF8Encoding.INSTANCE);
677670
}
678671
}
679672
break;
680673

681674
default:
682-
CompilerDirectives.shouldNotReachHere();
683-
throw new UnsupportedOperationException();
675+
throw CompilerDirectives.shouldNotReachHere();
684676
}
685677

686678
if (rope == null) {
687679
rope = appendRope;
688680
} else {
689-
rope = new ConcatRope(rope, appendRope, UTF8Encoding.INSTANCE, CodeRange.CR_UNKNOWN);
681+
rope = concatNode.executeConcat(rope, appendRope, UTF8Encoding.INSTANCE);
690682
}
691683
}
692684

src/main/java/org/truffleruby/core/time/TimeNodes.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.truffleruby.core.exception.ErrnoErrorNode;
2929
import org.truffleruby.core.klass.RubyClass;
3030
import org.truffleruby.core.rope.CodeRange;
31-
import org.truffleruby.core.rope.ManagedRope;
3231
import org.truffleruby.core.rope.Rope;
3332
import org.truffleruby.core.rope.RopeBuilder;
3433
import org.truffleruby.core.rope.RopeNodes;
@@ -424,9 +423,15 @@ protected RubyString timeStrftime(VirtualFrame frame, RubyTime time, Object form
424423
@Cached(value = "compilePattern(cachedFormat)", dimensions = 1) Token[] pattern,
425424
@Cached RopeNodes.EqualNode equalNode,
426425
@Cached("formatToRopeBuilderCanBeFast(pattern)") boolean canUseFast,
427-
@Cached ConditionProfile yearIsFastProfile) {
426+
@Cached ConditionProfile yearIsFastProfile,
427+
@Cached RopeNodes.ConcatNode concatNode,
428+
@Cached RopeNodes.SubstringNode substringNode) {
428429
if (canUseFast && yearIsFastProfile.profile(yearIsFast(time))) {
429-
return makeStringNode.fromRope(formatTimeFast(time, pattern));
430+
return makeStringNode.fromRope(RubyDateFormatter.formatToRopeBuilderFast(
431+
pattern,
432+
time.dateTime,
433+
concatNode,
434+
substringNode));
430435
} else {
431436
return makeStringNode.fromBuilderUnsafe(formatTime(time, pattern), CodeRange.CR_UNKNOWN);
432437
}
@@ -456,16 +461,6 @@ protected Token[] compilePattern(Rope format) {
456461
}
457462

458463
// Optimised for the default Logger::Formatter time format: "%Y-%m-%dT%H:%M:%S.%6N "
459-
private ManagedRope formatTimeFast(RubyTime time, Token[] pattern) {
460-
return RubyDateFormatter.formatToRopeBuilderFast(
461-
pattern,
462-
time.dateTime,
463-
time.zone,
464-
getContext(),
465-
getLanguage(),
466-
this,
467-
errnoErrorNode);
468-
}
469464

470465
private RopeBuilder formatTime(RubyTime time, Token[] pattern) {
471466
return RubyDateFormatter.formatToRopeBuilder(

0 commit comments

Comments
 (0)