From 71d391a6c8ac78d5f5f42c0e3600df5014a85821 Mon Sep 17 00:00:00 2001 From: scolebourne Date: Thu, 12 Jun 2014 00:31:27 +0100 Subject: [PATCH] 8046707: Performance of java.time could be better Summary: Optimise performance Reviewed-by: rriggs --- src/share/classes/java/time/Instant.java | 3 ++- .../classes/java/time/OffsetDateTime.java | 9 +++---- .../classes/java/time/ZonedDateTime.java | 15 ++++++++---- .../classes/java/time/format/Parsed.java | 24 ++++++++++--------- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/share/classes/java/time/Instant.java b/src/share/classes/java/time/Instant.java index 7f7bb7ba5..42e1b8df9 100644 --- a/src/share/classes/java/time/Instant.java +++ b/src/share/classes/java/time/Instant.java @@ -1058,7 +1058,8 @@ public final class Instant } // inline TemporalAccessor.super.query(query) as an optimization if (query == TemporalQueries.chronology() || query == TemporalQueries.zoneId() || - query == TemporalQueries.zone() || query == TemporalQueries.offset()) { + query == TemporalQueries.zone() || query == TemporalQueries.offset() || + query == TemporalQueries.localDate() || query == TemporalQueries.localTime()) { return null; } return query.queryFrom(this); diff --git a/src/share/classes/java/time/OffsetDateTime.java b/src/share/classes/java/time/OffsetDateTime.java index cd0eff6e5..4e505fcaa 100644 --- a/src/share/classes/java/time/OffsetDateTime.java +++ b/src/share/classes/java/time/OffsetDateTime.java @@ -357,10 +357,11 @@ public final class OffsetDateTime } try { ZoneOffset offset = ZoneOffset.from(temporal); - try { - LocalDateTime ldt = LocalDateTime.from(temporal); - return OffsetDateTime.of(ldt, offset); - } catch (DateTimeException ignore) { + LocalDate date = temporal.query(TemporalQueries.localDate()); + LocalTime time = temporal.query(TemporalQueries.localTime()); + if (date != null && time != null) { + return OffsetDateTime.of(date, time, offset); + } else { Instant instant = Instant.from(temporal); return OffsetDateTime.ofInstant(instant, offset); } diff --git a/src/share/classes/java/time/ZonedDateTime.java b/src/share/classes/java/time/ZonedDateTime.java index 752756bb7..a23ac821d 100644 --- a/src/share/classes/java/time/ZonedDateTime.java +++ b/src/share/classes/java/time/ZonedDateTime.java @@ -81,6 +81,7 @@ import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAdjuster; import java.time.temporal.TemporalAmount; import java.time.temporal.TemporalField; +import java.time.temporal.TemporalQueries; import java.time.temporal.TemporalQuery; import java.time.temporal.TemporalUnit; import java.time.temporal.UnsupportedTemporalTypeException; @@ -550,14 +551,14 @@ public final class ZonedDateTime } try { ZoneId zone = ZoneId.from(temporal); - try { + if (temporal.isSupported(INSTANT_SECONDS)) { long epochSecond = temporal.getLong(INSTANT_SECONDS); int nanoOfSecond = temporal.get(NANO_OF_SECOND); return create(epochSecond, nanoOfSecond, zone); - - } catch (DateTimeException ex1) { - LocalDateTime ldt = LocalDateTime.from(temporal); - return of(ldt, zone); + } else { + LocalDate date = LocalDate.from(temporal); + LocalTime time = LocalTime.from(temporal); + return of(date, time, zone); } } catch (DateTimeException ex) { throw new DateTimeException("Unable to obtain ZonedDateTime from TemporalAccessor: " + @@ -2037,8 +2038,12 @@ public final class ZonedDateTime * @throws DateTimeException if unable to query (defined by the query) * @throws ArithmeticException if numeric overflow occurs (defined by the query) */ + @SuppressWarnings("unchecked") @Override // override for Javadoc public R query(TemporalQuery query) { + if (query == TemporalQueries.localDate()) { + return (R) toLocalDate(); + } return ChronoZonedDateTime.super.query(query); } diff --git a/src/share/classes/java/time/format/Parsed.java b/src/share/classes/java/time/format/Parsed.java index 5cd88e896..1eb884544 100644 --- a/src/share/classes/java/time/format/Parsed.java +++ b/src/share/classes/java/time/format/Parsed.java @@ -635,18 +635,20 @@ final class Parsed implements TemporalAccessor { for (Iterator> it = fieldValues.entrySet().iterator(); it.hasNext(); ) { Entry entry = it.next(); TemporalField field = entry.getKey(); - long val1; - try { - val1 = target.getLong(field); - } catch (RuntimeException ex) { - continue; - } - long val2 = entry.getValue(); - if (val1 != val2) { - throw new DateTimeException("Conflict found: Field " + field + " " + val1 + - " differs from " + field + " " + val2 + " derived from " + target); + if (target.isSupported(field)) { + long val1; + try { + val1 = target.getLong(field); + } catch (RuntimeException ex) { + continue; + } + long val2 = entry.getValue(); + if (val1 != val2) { + throw new DateTimeException("Conflict found: Field " + field + " " + val1 + + " differs from " + field + " " + val2 + " derived from " + target); + } + it.remove(); } - it.remove(); } } -- GitLab