提交 71d391a6 编写于 作者: S scolebourne

8046707: Performance of java.time could be better

Summary: Optimise performance
Reviewed-by: rriggs
上级 b44cf83b
...@@ -1058,7 +1058,8 @@ public final class Instant ...@@ -1058,7 +1058,8 @@ public final class Instant
} }
// inline TemporalAccessor.super.query(query) as an optimization // inline TemporalAccessor.super.query(query) as an optimization
if (query == TemporalQueries.chronology() || query == TemporalQueries.zoneId() || 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 null;
} }
return query.queryFrom(this); return query.queryFrom(this);
......
...@@ -357,10 +357,11 @@ public final class OffsetDateTime ...@@ -357,10 +357,11 @@ public final class OffsetDateTime
} }
try { try {
ZoneOffset offset = ZoneOffset.from(temporal); ZoneOffset offset = ZoneOffset.from(temporal);
try { LocalDate date = temporal.query(TemporalQueries.localDate());
LocalDateTime ldt = LocalDateTime.from(temporal); LocalTime time = temporal.query(TemporalQueries.localTime());
return OffsetDateTime.of(ldt, offset); if (date != null && time != null) {
} catch (DateTimeException ignore) { return OffsetDateTime.of(date, time, offset);
} else {
Instant instant = Instant.from(temporal); Instant instant = Instant.from(temporal);
return OffsetDateTime.ofInstant(instant, offset); return OffsetDateTime.ofInstant(instant, offset);
} }
......
...@@ -81,6 +81,7 @@ import java.time.temporal.TemporalAccessor; ...@@ -81,6 +81,7 @@ import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjuster; import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAmount; import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalField; import java.time.temporal.TemporalField;
import java.time.temporal.TemporalQueries;
import java.time.temporal.TemporalQuery; import java.time.temporal.TemporalQuery;
import java.time.temporal.TemporalUnit; import java.time.temporal.TemporalUnit;
import java.time.temporal.UnsupportedTemporalTypeException; import java.time.temporal.UnsupportedTemporalTypeException;
...@@ -550,14 +551,14 @@ public final class ZonedDateTime ...@@ -550,14 +551,14 @@ public final class ZonedDateTime
} }
try { try {
ZoneId zone = ZoneId.from(temporal); ZoneId zone = ZoneId.from(temporal);
try { if (temporal.isSupported(INSTANT_SECONDS)) {
long epochSecond = temporal.getLong(INSTANT_SECONDS); long epochSecond = temporal.getLong(INSTANT_SECONDS);
int nanoOfSecond = temporal.get(NANO_OF_SECOND); int nanoOfSecond = temporal.get(NANO_OF_SECOND);
return create(epochSecond, nanoOfSecond, zone); return create(epochSecond, nanoOfSecond, zone);
} else {
} catch (DateTimeException ex1) { LocalDate date = LocalDate.from(temporal);
LocalDateTime ldt = LocalDateTime.from(temporal); LocalTime time = LocalTime.from(temporal);
return of(ldt, zone); return of(date, time, zone);
} }
} catch (DateTimeException ex) { } catch (DateTimeException ex) {
throw new DateTimeException("Unable to obtain ZonedDateTime from TemporalAccessor: " + throw new DateTimeException("Unable to obtain ZonedDateTime from TemporalAccessor: " +
...@@ -2037,8 +2038,12 @@ public final class ZonedDateTime ...@@ -2037,8 +2038,12 @@ public final class ZonedDateTime
* @throws DateTimeException if unable to query (defined by the query) * @throws DateTimeException if unable to query (defined by the query)
* @throws ArithmeticException if numeric overflow occurs (defined by the query) * @throws ArithmeticException if numeric overflow occurs (defined by the query)
*/ */
@SuppressWarnings("unchecked")
@Override // override for Javadoc @Override // override for Javadoc
public <R> R query(TemporalQuery<R> query) { public <R> R query(TemporalQuery<R> query) {
if (query == TemporalQueries.localDate()) {
return (R) toLocalDate();
}
return ChronoZonedDateTime.super.query(query); return ChronoZonedDateTime.super.query(query);
} }
......
...@@ -635,18 +635,20 @@ final class Parsed implements TemporalAccessor { ...@@ -635,18 +635,20 @@ final class Parsed implements TemporalAccessor {
for (Iterator<Entry<TemporalField, Long>> it = fieldValues.entrySet().iterator(); it.hasNext(); ) { for (Iterator<Entry<TemporalField, Long>> it = fieldValues.entrySet().iterator(); it.hasNext(); ) {
Entry<TemporalField, Long> entry = it.next(); Entry<TemporalField, Long> entry = it.next();
TemporalField field = entry.getKey(); TemporalField field = entry.getKey();
long val1; if (target.isSupported(field)) {
try { long val1;
val1 = target.getLong(field); try {
} catch (RuntimeException ex) { val1 = target.getLong(field);
continue; } catch (RuntimeException ex) {
} continue;
long val2 = entry.getValue(); }
if (val1 != val2) { long val2 = entry.getValue();
throw new DateTimeException("Conflict found: Field " + field + " " + val1 + if (val1 != val2) {
" differs from " + field + " " + val2 + " derived from " + target); throw new DateTimeException("Conflict found: Field " + field + " " + val1 +
" differs from " + field + " " + val2 + " derived from " + target);
}
it.remove();
} }
it.remove();
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册