Skip to content

Commit bee0a92

Browse files
committed
Always use the fast Time formatter when the format allows it
* So it is much easier to assert the correctness of it.
1 parent 8eb8704 commit bee0a92

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ public static RopeBuilder formatToRopeBuilder(Token[] compiledPattern, ZonedDate
566566
return toAppendTo;
567567
}
568568

569+
@TruffleBoundary
569570
public static boolean formatToRopeBuilderCanBeFast(Token[] compiledPattern) {
570571
for (int i = 0, compiledPatternLength = compiledPattern.length; i < compiledPatternLength; i++) {
571572
Token token = compiledPattern[i];

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.oracle.truffle.api.dsl.Cached;
1515
import com.oracle.truffle.api.dsl.ImportStatic;
1616
import com.oracle.truffle.api.dsl.Specialization;
17-
import com.oracle.truffle.api.frame.VirtualFrame;
1817
import com.oracle.truffle.api.library.CachedLibrary;
1918
import com.oracle.truffle.api.object.Shape;
2019
import com.oracle.truffle.api.profiles.ConditionProfile;
@@ -417,7 +416,7 @@ public abstract static class TimeStrftimePrimitiveNode extends PrimitiveArrayArg
417416
@Specialization(
418417
guards = { "equalNode.execute(libFormat.getRope(format), cachedFormat)" },
419418
limit = "getLanguage().options.TIME_FORMAT_CACHE")
420-
protected RubyString timeStrftime(VirtualFrame frame, RubyTime time, Object format,
419+
protected RubyString timeStrftime(RubyTime time, Object format,
421420
@CachedLibrary(limit = "2") RubyStringLibrary libFormat,
422421
@Cached("libFormat.getRope(format)") Rope cachedFormat,
423422
@Cached(value = "compilePattern(cachedFormat)", dimensions = 1) Token[] pattern,
@@ -437,6 +436,24 @@ protected RubyString timeStrftime(VirtualFrame frame, RubyTime time, Object form
437436
}
438437
}
439438

439+
@TruffleBoundary
440+
@Specialization(guards = "libFormat.isRubyString(format)")
441+
protected RubyString timeStrftime(RubyTime time, Object format,
442+
@CachedLibrary(limit = "2") RubyStringLibrary libFormat,
443+
@Cached RopeNodes.ConcatNode concatNode,
444+
@Cached RopeNodes.SubstringNode substringNode) {
445+
final Token[] pattern = compilePattern(libFormat.getRope(format));
446+
if (formatToRopeBuilderCanBeFast(pattern) && yearIsFast(time)) {
447+
return makeStringNode.fromRope(RubyDateFormatter.formatToRopeBuilderFast(
448+
pattern,
449+
time.dateTime,
450+
concatNode,
451+
substringNode));
452+
} else {
453+
return makeStringNode.fromBuilderUnsafe(formatTime(time, pattern), CodeRange.CR_UNKNOWN);
454+
}
455+
}
456+
440457
protected boolean formatToRopeBuilderCanBeFast(Token[] pattern) {
441458
return RubyDateFormatter.formatToRopeBuilderCanBeFast(pattern);
442459
}
@@ -447,15 +464,6 @@ protected boolean yearIsFast(RubyTime time) {
447464
return year >= 1000 && year <= 9999;
448465
}
449466

450-
@Specialization(guards = "libFormat.isRubyString(format)")
451-
protected RubyString timeStrftime(VirtualFrame frame, RubyTime time, Object format,
452-
@CachedLibrary(limit = "2") RubyStringLibrary libFormat) {
453-
final Token[] pattern = compilePattern(libFormat.getRope(format));
454-
return makeStringNode.fromBuilderUnsafe(
455-
formatTime(time, pattern),
456-
CodeRange.CR_UNKNOWN);
457-
}
458-
459467
protected Token[] compilePattern(Rope format) {
460468
return RubyDateFormatter.compilePattern(format, false, getContext(), this);
461469
}

0 commit comments

Comments
 (0)