提交 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
}
// 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);
......
......@@ -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);
}
......
......@@ -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> R query(TemporalQuery<R> query) {
if (query == TemporalQueries.localDate()) {
return (R) toLocalDate();
}
return ChronoZonedDateTime.super.query(query);
}
......
......@@ -635,6 +635,7 @@ final class Parsed implements TemporalAccessor {
for (Iterator<Entry<TemporalField, Long>> it = fieldValues.entrySet().iterator(); it.hasNext(); ) {
Entry<TemporalField, Long> entry = it.next();
TemporalField field = entry.getKey();
if (target.isSupported(field)) {
long val1;
try {
val1 = target.getLong(field);
......@@ -649,6 +650,7 @@ final class Parsed implements TemporalAccessor {
it.remove();
}
}
}
//-----------------------------------------------------------------------
@Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册