8046707: Performance of java.time could be better
authorscolebourne
Thu, 12 Jun 2014 00:31:27 +0100
changeset 24985 61ec15d123c3
parent 24984 c95ced1327af
child 24986 81e3c2e485d4
8046707: Performance of java.time could be better Summary: Optimise performance Reviewed-by: rriggs
jdk/src/share/classes/java/time/Instant.java
jdk/src/share/classes/java/time/OffsetDateTime.java
jdk/src/share/classes/java/time/ZonedDateTime.java
jdk/src/share/classes/java/time/format/Parsed.java
--- a/jdk/src/share/classes/java/time/Instant.java	Wed Jun 18 15:05:29 2014 +0100
+++ b/jdk/src/share/classes/java/time/Instant.java	Thu Jun 12 00:31:27 2014 +0100
@@ -1058,7 +1058,8 @@
         }
         // 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);
--- a/jdk/src/share/classes/java/time/OffsetDateTime.java	Wed Jun 18 15:05:29 2014 +0100
+++ b/jdk/src/share/classes/java/time/OffsetDateTime.java	Thu Jun 12 00:31:27 2014 +0100
@@ -357,10 +357,11 @@
         }
         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);
             }
--- a/jdk/src/share/classes/java/time/ZonedDateTime.java	Wed Jun 18 15:05:29 2014 +0100
+++ b/jdk/src/share/classes/java/time/ZonedDateTime.java	Thu Jun 12 00:31:27 2014 +0100
@@ -81,6 +81,7 @@
 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;
@@ -551,14 +552,14 @@
         }
         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: " +
@@ -2039,8 +2040,12 @@
      * @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> R query(TemporalQuery<R> query) {
+        if (query == TemporalQueries.localDate()) {
+            return (R) toLocalDate();
+        }
         return ChronoZonedDateTime.super.query(query);
     }
 
--- a/jdk/src/share/classes/java/time/format/Parsed.java	Wed Jun 18 15:05:29 2014 +0100
+++ b/jdk/src/share/classes/java/time/format/Parsed.java	Thu Jun 12 00:31:27 2014 +0100
@@ -568,18 +568,20 @@
         for (Iterator<Entry<TemporalField, Long>> it = fieldValues.entrySet().iterator(); it.hasNext(); ) {
             Entry<TemporalField, Long> entry = it.next();
             TemporalField field = entry.getKey();
-            long val1;
-            try {
-                val1 = target.getLong(field);
-            } catch (RuntimeException ex) {
-                continue;
+            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();
             }
-            long val2 = entry.getValue();
-            if (val1 != val2) {
-                throw new DateTimeException("Conflict found: Field " + field + " " + val1 +
-                        " differs from " + field + " " + val2 + " derived from " + target);
-            }
-            it.remove();
         }
     }