Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8276220

Reduce excessive allocations in DateTimeFormatter

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 18
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b23

      Description

      When printing number fields and and fractional fields, the DateTimeFormatters produced by java.time.format.DateTimeFormatterBuilder can produce a lot of allocations.

      - Some are intrinsic to how java.time operates: Instants are converted to ZonedLocalDateTime before printing, along with a DateTimePrintContext. So at least 5 objects are allocated
      - Then each number field is turned into a String before appending it to the buffer.
      - For fractional fields, for example the sub-second part in a pattern like "HH:mm:ss.SSS", we always use a rather generic method involving BigDecimal.

      In this enhancement I will deal with the second two inefficiencies. Integral numbers can be emitted directly to the buffer, and for NANO_OF_SECOND (the single most common "fraction") we can get exactly the same result with only int arithmetic and avoid the BigDecimal dance altogether. For some of the patterns tested this realizes a 80-85% reduction in allocation while speeding up the formatting by 1.7x.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              redestad Claes Redestad
              Reporter:
              redestad Claes Redestad
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: