Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,12 @@ public final class DateTimeFormatter {
*/
private final ZoneId zone;

/**
* Flag indicating whether this formatter only uses ChronoField instances.
* This is used to optimize the storage of parsed field values in the Parsed class.
*/
final boolean onlyChronoField;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add to DateTimePrinterParser the method:

public default boolean onlyChronoFields() {
    return true;
} 

and override in CompositePrinterParser, NumberPrinterParser, TextPrinterParser, DefaultValueParser with obvious implementations you should be able to get rid of this field, same in DateTimeFormatterBuilder. (Or keep the field, but initialize in the constructor from printerParser).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d8742d7

The initial version was similar to what you suggested. In the discussion above, I accepted liach's suggestion and modified it into the current implementation. I prefer the current implementation, and it will be easier to calculate chronoFieldsBitSet in the next step.


//-----------------------------------------------------------------------
/**
* Creates a formatter using the specified pattern.
Expand Down Expand Up @@ -1474,18 +1480,20 @@ public static final TemporalQuery<Boolean> parsedLeapSecond() {
* @param resolverFields the fields to use during resolving, null for all fields
* @param chrono the chronology to use, null for no override
* @param zone the zone to use, null for no override
* @param onlyChronoField flag indicating whether this formatter only uses ChronoField instances
*/
DateTimeFormatter(CompositePrinterParser printerParser,
Locale locale, DecimalStyle decimalStyle,
ResolverStyle resolverStyle, Set<TemporalField> resolverFields,
Chronology chrono, ZoneId zone) {
Chronology chrono, ZoneId zone, boolean onlyChronoField) {
this.printerParser = Objects.requireNonNull(printerParser, "printerParser");
this.resolverFields = resolverFields;
this.locale = Objects.requireNonNull(locale, "locale");
this.decimalStyle = Objects.requireNonNull(decimalStyle, "decimalStyle");
this.resolverStyle = Objects.requireNonNull(resolverStyle, "resolverStyle");
this.chrono = chrono;
this.zone = zone;
this.onlyChronoField = onlyChronoField;
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -1523,7 +1531,7 @@ public DateTimeFormatter withLocale(Locale locale) {
if (this.locale.equals(locale)) {
return this;
}
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone);
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone, onlyChronoField);
}

/**
Expand Down Expand Up @@ -1569,7 +1577,7 @@ public DateTimeFormatter localizedBy(Locale locale) {
Objects.equals(z, zone)) {
return this;
} else {
return new DateTimeFormatter(printerParser, locale, ds, resolverStyle, resolverFields, c, z);
return new DateTimeFormatter(printerParser, locale, ds, resolverStyle, resolverFields, c, z, onlyChronoField);
}
}

Expand All @@ -1595,7 +1603,7 @@ public DateTimeFormatter withDecimalStyle(DecimalStyle decimalStyle) {
if (this.decimalStyle.equals(decimalStyle)) {
return this;
}
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone);
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone, onlyChronoField);
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -1649,7 +1657,7 @@ public DateTimeFormatter withChronology(Chronology chrono) {
if (Objects.equals(this.chrono, chrono)) {
return this;
}
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone);
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone, onlyChronoField);
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -1706,7 +1714,7 @@ public DateTimeFormatter withZone(ZoneId zone) {
if (Objects.equals(this.zone, zone)) {
return this;
}
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone);
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone, onlyChronoField);
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -1748,7 +1756,7 @@ public DateTimeFormatter withResolverStyle(ResolverStyle resolverStyle) {
if (Objects.equals(this.resolverStyle, resolverStyle)) {
return this;
}
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone);
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone, onlyChronoField);
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -1814,7 +1822,7 @@ public DateTimeFormatter withResolverFields(TemporalField... resolverFields) {
if (Objects.equals(this.resolverFields, fields)) {
return this;
}
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, fields, chrono, zone);
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, fields, chrono, zone, onlyChronoField);
}

/**
Expand Down Expand Up @@ -1863,7 +1871,7 @@ public DateTimeFormatter withResolverFields(Set<TemporalField> resolverFields) {
if (resolverFields != null) {
resolverFields = Collections.unmodifiableSet(new HashSet<>(resolverFields));
}
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone);
return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone, onlyChronoField);
}

//-----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ public final class DateTimeFormatterBuilder {
*/
private int valueParserIndex = -1;

/**
* Flag indicating whether this builder only uses ChronoField instances.
* This is used to optimize the storage of parsed field values in the Parsed class.
*/
private boolean onlyChronoField = true;

/**
* Gets the formatting pattern for date and time styles for a locale and chronology.
* The locale and chronology are used to lookup the locale specific format
Expand Down Expand Up @@ -2371,11 +2377,35 @@ private int appendInternal(DateTimePrinterParser pp) {
active.padNextWidth = 0;
active.padNextChar = 0;
}

// Update the onlyChronoField flag if the printer/parser uses non-ChronoField instances
checkField(pp);
active.printerParsers.add(pp);
active.valueParserIndex = -1;
return active.printerParsers.size() - 1;
}

/**
* Update the onlyChronoField flag if the printer/parser uses non-ChronoField instances
* @param pp the printer-parser
*/
private void checkField(DateTimePrinterParser pp) {
TemporalField field;
if (pp instanceof NumberPrinterParser npp) {
field = npp.field;
} else if (pp instanceof TextPrinterParser tpp) {
field = tpp.field;
} else if (pp instanceof DefaultValueParser dvp) {
field = dvp.field;
} else {
return;
}

if (!(field instanceof ChronoField)) {
active.onlyChronoField = false;
}
}

//-----------------------------------------------------------------------
/**
* Completes this builder by creating the {@code DateTimeFormatter}
Expand Down Expand Up @@ -2443,7 +2473,7 @@ private DateTimeFormatter toFormatter(Locale locale, ResolverStyle resolverStyle
}
CompositePrinterParser pp = new CompositePrinterParser(printerParsers, false);
return new DateTimeFormatter(pp, locale, DecimalStyle.STANDARD,
resolverStyle, null, chrono, null);
resolverStyle, null, chrono, null, onlyChronoField);
}

//-----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -120,7 +120,7 @@ final class DateTimeParseContext {
DateTimeParseContext(DateTimeFormatter formatter) {
super();
this.formatter = formatter;
parsed.add(new Parsed());
parsed.add(new Parsed(formatter.onlyChronoField));
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/java.base/share/classes/java/time/format/Parsed.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025, Alibaba Group Holding Limited. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -98,6 +99,7 @@
import java.time.temporal.TemporalQueries;
import java.time.temporal.TemporalQuery;
import java.time.temporal.UnsupportedTemporalTypeException;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -128,7 +130,7 @@ final class Parsed implements TemporalAccessor {
/**
* The parsed fields.
*/
final Map<TemporalField, Long> fieldValues = new HashMap<>();
final Map<TemporalField, Long> fieldValues;
/**
* The parsed zone.
*/
Expand Down Expand Up @@ -169,15 +171,20 @@ final class Parsed implements TemporalAccessor {
/**
* Creates an instance.
*/
Parsed() {
@SuppressWarnings("unchecked")
Parsed(boolean onlyChronoField) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you know that only ChronoFields are used then imho the loop over the entries of fieldValues in method resolveFields can be skipped (line 290ff).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, but that should be a separate PR

// Create the EnumMap with raw types and cast it appropriately
// This is safe because ChronoField implements TemporalField
fieldValues = onlyChronoField ? (Map<TemporalField, Long>) (Map) new EnumMap<>(ChronoField.class)
: new HashMap<>();
}

/**
* Creates a copy.
*/
Parsed copy() {
// only copy fields used in parsing stage
Parsed cloned = new Parsed();
Parsed cloned = new Parsed(fieldValues instanceof EnumMap);
cloned.fieldValues.putAll(this.fieldValues);
cloned.zone = this.zone;
cloned.zoneNameType = this.zoneNameType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright (c) 2025, Alibaba Group Holding Limited. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.java.time.format;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import java.util.Locale;
import java.util.Random;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
@Fork(3)
@State(Scope.Thread)
public class DateTimeFormatterParse {
static final DateTimeFormatter formatterLocalTime = DateTimeFormatter.ofPattern("HH:mm:ss");
static final DateTimeFormatter formatterLocalTimeWithNano = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
static final DateTimeFormatter formatterLocalDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
static final DateTimeFormatter formatterLocalDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
static final DateTimeFormatter formatterLocalDateTimeWithNano = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
static final DateTimeFormatter formatterOffsetDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ");
static final DateTimeFormatter formatterZonedDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ'['VV']'");

static final String STR_LOCALTIME = "21:11:48";
static final String STR_LOCALTIME_WITH_NANO = "21:11:48.456";
static final String STR_LOCALDATE = "2024-08-01";
static final String STR_LOCALDATETIME = "2024-08-01T21:11:48";
static final String STR_LOCALDATETIME_WITH_NANO = "2024-08-01T21:11:48.456";
static final String STR_INSTANT = "2024-08-12T03:25:54.980339Z";
static final String STR_OFFSETDATETIME = "2024-08-12T11:50:46.731509+08:00";
static final String STR_ZONEDDATETIME = "2024-08-12T11:50:46.731509+08:00[Asia/Shanghai]";

@Benchmark
public LocalTime parseLocalTime() {
return LocalTime.parse(STR_LOCALTIME, formatterLocalTime);
}

@Benchmark
public LocalTime parseLocalTimeWithNano() {
return LocalTime.parse(STR_LOCALTIME_WITH_NANO, formatterLocalTimeWithNano);
}

@Benchmark
public LocalDate parseLocalDate() {
return LocalDate.parse(STR_LOCALDATE, formatterLocalDate);
}

@Benchmark
public LocalDateTime parseLocalDateTime() {
return LocalDateTime.parse(STR_LOCALDATETIME, formatterLocalDateTime);
}

@Benchmark
public LocalDateTime parseLocalDateTimeWithNano() {
return LocalDateTime.parse(STR_LOCALDATETIME_WITH_NANO, formatterLocalDateTimeWithNano);
}

@Benchmark
public OffsetDateTime parseOffsetDateTime() {
return OffsetDateTime.parse(STR_OFFSETDATETIME, formatterOffsetDateTime);
}

@Benchmark
public ZonedDateTime parseZonedDateTime() {
return ZonedDateTime.parse(STR_ZONEDDATETIME, formatterZonedDateTime);
}

@Benchmark
public Instant parseInstant() {
return Instant.parse(STR_INSTANT);
}
}