Skip to content

[client-v2] Do not create a UTC ZoneId for each Date and DateTime value - #3472

Open
utvaxling wants to merge 3 commits into
ClickHouse:mainfrom
utvaxling:perf/c01-utc-zoneid
Open

utvaxling wants to merge 3 commits into
ClickHouse:mainfrom
utvaxling:perf/c01-utc-zoneid

Conversation

@utvaxling

@utvaxling utvaxling commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

client-v2 made a new UTC ZoneId with ZoneId.of("UTC") for each value in six places. This PR uses the existing constant DataTypeConverter.UTC_ZONE_ID of client-v2 in these places. The constant is equal to ZoneId.of("UTC"), so the written bytes and the returned values do not change.

  • SerializerUtils.serializePrimitiveData (4 places): each Date and Date32 value, and each DateTime and DateTime64 value of a column that has no time zone.
  • AbstractBinaryFormatReader (2 places): getInstant on a Date or Date32 column, and the conversion of a LocalDateTime of a Dynamic or Variant column to an Instant.

Why it matters: on JDK 17, ZoneId.of("UTC") makes a ZoneRegion, new ZoneRules, a ConcurrentHashMap and a ZoneOffset[] on each call. That is 152 bytes of garbage for each value.

There is no issue for this change. It is a small change with no change of behavior.

Why DataTypeConverter.UTC_ZONE_ID and not ZoneOffset.UTC

ZoneOffset.UTC is a different kind of zone, and it is slower here for zoned values. In the source of JDK 11 and JDK 17, ZoneOffset.getRules() returns new ZoneRules on each call, and ZonedDateTime.withZoneSameInstant and OffsetDateTime.atZoneSameInstant always ask the zone for its rules. A ZoneRegion keeps its rules.

Allocation and time for each call of the conversion, measured with a small probe on JDK 17.0.21 (one JVM for each cell, after warm-up; ThreadMXBean.getThreadAllocatedBytes):

Conversion ZoneId.of("UTC") for each call (before) Constant ZoneId.of("UTC") (this PR) ZoneOffset.UTC
writeDate, LocalDate 152 B, 19 ns 0 B, 6 ns 0 B, 6 ns
writeDate, ZonedDateTime in zone UTC 152 B, 20 ns 0 B, 7 ns 224 B, 34 ns
writeDate, ZonedDateTime in another zone 248 B, 43 ns 96 B, 30 ns 224 B, 34 ns
writeDate, OffsetDateTime 248 B, 42 ns 96 B, 30 ns 312 B, 38 ns
writeDateTime, LocalDateTime 152 B, 20 ns 0 B, 8 ns 0 B, 7 ns
getInstant of a Date 152 B, 21 ns 0 B, 8 ns 0 B, 8 ns
LocalDateTime to Instant 152 B, 22 ns 0 B, 10 ns 0 B, 10 ns

ClickHouseValues.UTC_ZONE of clickhouse-data has the same value, and the second commit of this PR used it. The third commit uses the constant of client-v2 itself instead, so the two changed classes get no new import from clickhouse-data. DataTypeConverter.UTC_ZONE_ID is a public static field that is not final; this PR does not change the field.

Measurement

Benchmark Serializers.SerializerOutputStreamV2 of the performance module, JMH 1.37, JDK 17.0.21, heap 2 GiB, G1. One operation writes 100,000 rows of this table (the dataset is not in the repository; the dataset of the module has no date column):

ts DateTime('UTC'), ts_ms DateTime64(3, 'UTC'), day Date, event_id UInt64, user_id Int64, code Int32, flag UInt8, bucket UInt16, value Float64, ratio Float32, name String, tag String

Only the Date column takes the changed code path here: 100,000 values for each operation.

Allocation for each operation (1 MB = 2^20 bytes) Before This PR
With -XX:-DoEscapeAnalysis (the same in every fork) 60.27 MB 45.78 MB (-14.49 MB, -24%)
Default JVM, 5 forks, by fork 41.20, 41.20, 41.20, 48.07, 51.88 MB 28.99, 28.99, 32.81, 37.39, 37.39 MB

With the default JVM this benchmark does not give one allocation number: the JIT compiler removes a different part of the allocation in each fork. Each fork of this PR is below each fork of the base. The run with escape analysis off shows what the code asks for: 14.49 MB less, which is 152 bytes for each Date value.

Time: the mean of a fork is 12.0 to 14.9 ms before and 11.4 to 13.5 ms with this PR. This difference is not more than the noise of the benchmark. I make no claim about time.

The third commit (the constant of client-v2) was measured against the second commit (ClickHouseValues.UTC_ZONE) in one session. With escape analysis off, both give 45.78 MB. With the default JVM and 5 forks each, both have 28.99 MB as the lowest level and 37.39 MB as the highest, and the forks on the lowest level take 11.6 ms with both.

The two reader places are not in a benchmark of the module. The probe above shows 152 bytes less for each call.

Tests

mvn -pl client-v2 verify with ClickHouse 26.8: 986 unit tests and 1,896 integration tests pass.

No test was added, because the constant has the same value as before. Existing integration tests cover the changed lines: RowBinaryFormatWriterTest writes and reads back Date, Date32, DateTime, DateTime32 and DateTime64 columns that have no time zone, and the reader data set tests call getInstant on Date columns.

Compatibility

No public signature changes. No written byte and no returned value changes.

Related

#3425 changes the two DateTime lines of SerializerUtils for another purpose. The PR that is merged second will have a small conflict in these two lines. The Date lines and the reader lines do not overlap.

Text for the change record

There is no issue number for a file in history/latest. Proposed text:

  • [client-v2] The RowBinary serializer and getInstant of the binary format readers made a new UTC ZoneId for each Date, Date32, DateTime and DateTime64 value. They now use one constant. On JDK 17 this removes 152 bytes of allocation for each such value.

Checklist

  • A human-readable description of the changes was provided to include in CHANGELOG
  • Tests: no new test; the existing unit and integration tests cover the changed lines (see "Tests")

🤖 Generated with Claude Code


Note

Low Risk
Internal refactor with identical timezone semantics; no API or wire-format changes.

Overview
Replaces repeated ZoneId.of("UTC") calls with the shared DataTypeConverter.UTC_ZONE_ID constant in RowBinary serialization and binary reader paths that handle Date, Date32, DateTime, and DateTime64 (when the column has no timezone).

SerializerUtils.serializePrimitiveData: four call sites for writing Date/Date32 and default-UTC DateTime/DateTime64.

AbstractBinaryFormatReader: getInstant for Date/Date32, and objectToInstant for LocalDateTime in Dynamic/Variant columns. The unused java.time.ZoneId import is removed there.

Serialized bytes and returned values stay the same; on JDK 17 this avoids ~152 bytes of allocation per value that previously came from creating a new ZoneRegion on each ZoneId.of("UTC") call.

Reviewed by Cursor Bugbot for commit 1c5117c. Bugbot is set up for automated code reviews on this repo. Configure here.

utvaxling and others added 2 commits November 30, 2026 10:24
…e values

SerializerUtils.serializePrimitiveData called ZoneId.of("UTC") for each
Date and Date32 value, and for each DateTime and DateTime64 value of a
column that has no time zone. On JDK 17 each call makes a new ZoneRegion
with new ZoneRules, and the ZoneRules make a ConcurrentHashMap and a
ZoneOffset array.

The zone is now a constant of the class. It is the same ZoneId value as
before, so the bytes that the serializer writes do not change.

Measured with the JMH benchmark Serializers.SerializerOutputStreamV2 of
the performance module on JDK 17: 100,000 rows of 12 columns, with one
Date, one DateTime and one DateTime64 column, 5 forks before and 5 forks
after. Allocation for each operation goes from 41.20 MB to 28.99 MB
(-29.6%) in the forks on the lowest allocation level (4 of 5 forks on
each side; the level depends on what escape analysis removes in a fork).
The mean time of these forks is 12.0 ms before and 11.5 ms after, which
is not more than the noise of this benchmark.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…e conversions

The first commit of this change added a private UTC ZoneId constant to
SerializerUtils. This commit uses the constant that already exists,
ClickHouseValues.UTC_ZONE, and removes the new one.

Two more places made ZoneId.of("UTC") for each call, both in the binary
readers: getInstant on a Date or Date32 column, and the conversion of a
LocalDateTime of a Dynamic or Variant column to an Instant. They use the
same constant now.

ClickHouseValues.UTC_ZONE is equal to ZoneId.of("UTC"), so the written
bytes and the returned Instant values do not change.

ZoneOffset.UTC of the JDK is not used here on purpose. When a
ZonedDateTime or an OffsetDateTime is written to a Date column,
java.time asks the zone for its rules, and ZoneOffset.getRules() makes
new ZoneRules for each call on JDK 17 and earlier. A probe of the
conversions on JDK 17 gave, for a ZonedDateTime in zone UTC that is
written to a Date column: 224 bytes for each value with ZoneOffset.UTC,
152 bytes with ZoneId.of("UTC") for each value, and 0 bytes with a
constant ZoneId.of("UTC"). For LocalDate and LocalDateTime values both
constants gave 0 bytes. For the two reader places the probe gave 152
bytes less for each call.

Measured with the JMH benchmark Serializers.SerializerOutputStreamV2 of
the performance module on JDK 17 (100,000 rows of 12 columns, one Date
column). With escape analysis off, where allocation does not depend on
the JIT compiler: 45.78 MB for each operation, the same as with the
first commit, and 14.49 MB less than before the change (60.27 MB). With
the default JVM the lowest allocation level is 28.99 MB, the same as
with the first commit (41.20 MB before the change).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@utvaxling
utvaxling requested review from a team and mzitnik as code owners November 30, 2026 20:37
…onversions

The six places of this change used ClickHouseValues.UTC_ZONE of
clickhouse-data as the UTC zone. They now use
DataTypeConverter.UTC_ZONE_ID, the UTC zone constant of client-v2
itself: four places in SerializerUtils.serializePrimitiveData and two
in AbstractBinaryFormatReader (getInstant on a Date or Date32 column,
and the conversion of a LocalDateTime of a Dynamic or Variant column to
an Instant). The two classes no longer import ClickHouseValues.

Both constants are equal to ZoneId.of("UTC"), so the written bytes and
the returned Instant values do not change.

Measured with the JMH benchmark Serializers.SerializerOutputStreamV2 of
the performance module on JDK 17 (100,000 rows of 12 columns, one Date
column), against the commit before this one in the same session. With
escape analysis off: 45.78 MB for each operation with both commits.
With the default JVM, 5 forks each: the lowest allocation level is
28.99 MB and the highest is 37.39 MB with both commits, and the mean
time of the forks on the lowest level is 11.6 ms with both.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant