8046707: Performance of java.time could be better

Optimise performance

Reviewed-by: rriggs
This commit is contained in:
Stephen Colebourne 2014-06-12 00:31:27 +01:00
parent 7d5e52ba49
commit e5ca02132f
4 changed files with 30 additions and 21 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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;
@ -551,14 +552,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: " +
@ -2039,8 +2040,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);
}

View File

@ -568,18 +568,20 @@ 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();
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();
}
}