Skip to content

Commit ad01279

Browse files
committed
General
1 parent c5d1843 commit ad01279

File tree

3 files changed

+71
-81
lines changed

3 files changed

+71
-81
lines changed

src/main/java/org/truffleruby/core/rope/RopeConstants.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ private static LeafRope[] createPaddedNumbersTable() {
141141
return table;
142142
}
143143

144-
/***
145-
* Zero-padded numbers in the format %02d, between 00 and 99.
146-
*/
144+
/*** Zero-padded numbers in the format %02d, between 00 and 99. */
147145
public static LeafRope paddedNumber(int n) {
148146
return PADDED_NUMBERS[n];
149147
}

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

Lines changed: 60 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -616,96 +616,81 @@ public static boolean formatToRopeBuilderCanBeFast(Token[] compiledPattern) {
616616
@ExplodeLoop
617617
public static ManagedRope formatToRopeBuilderFast(Token[] compiledPattern, ZonedDateTime dt, Object zone,
618618
RubyContext context, RubyLanguage language, Node currentNode, ErrnoErrorNode errnoErrorNode) {
619-
try {
620-
ManagedRope rope = null;
619+
ManagedRope rope = null;
621620

622-
for (Token token : compiledPattern) {
623-
final ManagedRope appendRope;
621+
for (Token token : compiledPattern) {
622+
final ManagedRope appendRope;
624623

625-
switch (token.getFormat()) {
626-
case FORMAT_ENCODING:
627-
case FORMAT_OUTPUT:
628-
continue;
624+
switch (token.getFormat()) {
625+
case FORMAT_ENCODING:
626+
case FORMAT_OUTPUT:
627+
continue;
629628

630-
case FORMAT_STRING:
631-
appendRope = token.getRope();
632-
break;
633-
case FORMAT_DAY:
634-
appendRope = RopeConstants.paddedNumber(dt.getDayOfMonth());
635-
break;
636-
case FORMAT_HOUR:
637-
appendRope = RopeConstants.paddedNumber(dt.getHour());
638-
break;
639-
case FORMAT_MINUTES:
640-
appendRope = RopeConstants.paddedNumber(dt.getMinute());
641-
break;
642-
case FORMAT_MONTH:
643-
appendRope = RopeConstants.paddedNumber(dt.getMonthValue());
644-
break;
645-
case FORMAT_SECONDS:
646-
appendRope = RopeConstants.paddedNumber(dt.getSecond());
647-
break;
629+
case FORMAT_STRING:
630+
appendRope = token.getRope();
631+
break;
632+
case FORMAT_DAY:
633+
appendRope = RopeConstants.paddedNumber(dt.getDayOfMonth());
634+
break;
635+
case FORMAT_HOUR:
636+
appendRope = RopeConstants.paddedNumber(dt.getHour());
637+
break;
638+
case FORMAT_MINUTES:
639+
appendRope = RopeConstants.paddedNumber(dt.getMinute());
640+
break;
641+
case FORMAT_MONTH:
642+
appendRope = RopeConstants.paddedNumber(dt.getMonthValue());
643+
break;
644+
case FORMAT_SECONDS:
645+
appendRope = RopeConstants.paddedNumber(dt.getSecond());
646+
break;
648647

649-
case FORMAT_YEAR_LONG: {
650-
final int value = dt.getYear();
651-
652-
if (value < 1000 || value > 9999) {
653-
CompilerDirectives.transferToInterpreterAndInvalidate();
654-
return formatToRopeBuilder(
655-
compiledPattern,
656-
dt,
657-
zone,
658-
context,
659-
language,
660-
currentNode,
661-
errnoErrorNode).toRope();
662-
}
648+
case FORMAT_YEAR_LONG: {
649+
final int value = dt.getYear();
663650

664-
appendRope = new LazyIntRope(value, UTF8Encoding.INSTANCE, 4);
665-
}
666-
break;
651+
assert value >= 1000;
652+
assert value <= 9999;
667653

668-
case FORMAT_NANOSEC: {
669-
final int nano = dt.getNano();
670-
assert nano >= 0;
671-
assert nano < 1000000000;
654+
appendRope = new LazyIntRope(value, UTF8Encoding.INSTANCE, 4);
655+
}
656+
break;
672657

673-
final LazyIntRope nanoRope = new LazyIntRope(nano);
658+
case FORMAT_NANOSEC: {
659+
final int nano = dt.getNano();
660+
assert nano >= 0;
661+
assert nano < 1000000000;
674662

675-
final int padding = 6 - nanoRope.characterLength();
663+
final LazyIntRope nanoRope = new LazyIntRope(nano);
676664

677-
if (padding == 0) {
678-
appendRope = nanoRope;
679-
} else if (padding < 0) {
680-
appendRope = new SubstringRope(UTF8Encoding.INSTANCE, nanoRope, 0, 6, 6, CodeRange.CR_7BIT);
681-
} else {
682-
appendRope = new ConcatRope(
683-
nanoRope,
684-
RopeConstants.paddingZeros(padding),
685-
UTF8Encoding.INSTANCE,
686-
CodeRange.CR_7BIT);
687-
}
688-
}
689-
break;
665+
final int padding = 6 - nanoRope.characterLength();
690666

691-
default:
692-
CompilerDirectives.transferToInterpreterAndInvalidate();
693-
throw new UnsupportedOperationException();
667+
if (padding == 0) {
668+
appendRope = nanoRope;
669+
} else if (padding < 0) {
670+
appendRope = new SubstringRope(UTF8Encoding.INSTANCE, nanoRope, 0, 6, 6, CodeRange.CR_7BIT);
671+
} else {
672+
appendRope = new ConcatRope(
673+
nanoRope,
674+
RopeConstants.paddingZeros(padding),
675+
UTF8Encoding.INSTANCE,
676+
CodeRange.CR_7BIT);
677+
}
694678
}
679+
break;
695680

696-
if (rope == null) {
697-
rope = appendRope;
698-
} else {
699-
rope = new ConcatRope(rope, appendRope, UTF8Encoding.INSTANCE, CodeRange.CR_UNKNOWN);
700-
}
681+
default:
682+
CompilerDirectives.shouldNotReachHere();
683+
throw new UnsupportedOperationException();
701684
}
702685

703-
return rope;
704-
} catch (IndexOutOfBoundsException ioobe) {
705-
CompilerDirectives.transferToInterpreterAndInvalidate();
706-
return formatToRopeBuilder(compiledPattern, dt, zone, context, language, currentNode, errnoErrorNode)
707-
.toRope();
686+
if (rope == null) {
687+
rope = appendRope;
688+
} else {
689+
rope = new ConcatRope(rope, appendRope, UTF8Encoding.INSTANCE, CodeRange.CR_UNKNOWN);
690+
}
708691
}
692+
693+
return rope;
709694
}
710695

711696
private static int formatWeekOfYear(ZonedDateTime dt, int firstDayOfWeek) {

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.oracle.truffle.api.frame.VirtualFrame;
1818
import com.oracle.truffle.api.library.CachedLibrary;
1919
import com.oracle.truffle.api.object.Shape;
20+
import com.oracle.truffle.api.profiles.ConditionProfile;
2021
import org.jcodings.specific.UTF8Encoding;
2122
import org.truffleruby.RubyLanguage;
2223
import org.truffleruby.builtins.CoreMethod;
@@ -48,7 +49,6 @@
4849
import java.time.ZoneId;
4950
import java.time.ZoneOffset;
5051
import java.time.ZonedDateTime;
51-
import java.util.List;
5252

5353
@CoreModule(value = "Time", isClass = true)
5454
public abstract class TimeNodes {
@@ -423,8 +423,9 @@ protected RubyString timeStrftime(VirtualFrame frame, RubyTime time, Object form
423423
@Cached("libFormat.getRope(format)") Rope cachedFormat,
424424
@Cached(value = "compilePattern(cachedFormat)", dimensions = 1) Token[] pattern,
425425
@Cached RopeNodes.EqualNode equalNode,
426-
@Cached("formatToRopeBuilderCanBeFast(pattern)") boolean canUseFast) {
427-
if (canUseFast) {
426+
@Cached("formatToRopeBuilderCanBeFast(pattern)") boolean canUseFast,
427+
@Cached ConditionProfile yearIsFastProfile) {
428+
if (canUseFast && yearIsFastProfile.profile(yearIsFast(time))) {
428429
return makeStringNode.fromRope(formatTimeFast(time, pattern));
429430
} else {
430431
return makeStringNode.fromBuilderUnsafe(formatTime(time, pattern), CodeRange.CR_UNKNOWN);
@@ -435,6 +436,12 @@ protected boolean formatToRopeBuilderCanBeFast(Token[] pattern) {
435436
return RubyDateFormatter.formatToRopeBuilderCanBeFast(pattern);
436437
}
437438

439+
protected boolean yearIsFast(RubyTime time) {
440+
// See formatToRopeBuilderCanBeFast
441+
final int year = time.dateTime.getYear();
442+
return year >= 1000 && year <= 9999;
443+
}
444+
438445
@Specialization(guards = "libFormat.isRubyString(format)")
439446
protected RubyString timeStrftime(VirtualFrame frame, RubyTime time, Object format,
440447
@CachedLibrary(limit = "2") RubyStringLibrary libFormat) {

0 commit comments

Comments
 (0)