Repository navigation
Conversation
…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>
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
client-v2made a new UTCZoneIdwithZoneId.of("UTC")for each value in six places. This PR uses the existing constantDataTypeConverter.UTC_ZONE_IDofclient-v2in these places. The constant is equal toZoneId.of("UTC"), so the written bytes and the returned values do not change.SerializerUtils.serializePrimitiveData(4 places): eachDateandDate32value, and eachDateTimeandDateTime64value of a column that has no time zone.AbstractBinaryFormatReader(2 places):getInstanton aDateorDate32column, and the conversion of aLocalDateTimeof aDynamicorVariantcolumn to anInstant.Why it matters: on JDK 17,
ZoneId.of("UTC")makes aZoneRegion, newZoneRules, aConcurrentHashMapand aZoneOffset[]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_IDand notZoneOffset.UTCZoneOffset.UTCis 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 newZoneRuleson each call, andZonedDateTime.withZoneSameInstantandOffsetDateTime.atZoneSameInstantalways ask the zone for its rules. AZoneRegionkeeps 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):ZoneId.of("UTC")for each call (before)ZoneId.of("UTC")(this PR)ZoneOffset.UTCwriteDate,LocalDatewriteDate,ZonedDateTimein zone UTCwriteDate,ZonedDateTimein another zonewriteDate,OffsetDateTimewriteDateTime,LocalDateTimegetInstantof aDateLocalDateTimetoInstantClickHouseValues.UTC_ZONEofclickhouse-datahas the same value, and the second commit of this PR used it. The third commit uses the constant ofclient-v2itself instead, so the two changed classes get no new import fromclickhouse-data.DataTypeConverter.UTC_ZONE_IDis apublic staticfield that is notfinal; this PR does not change the field.Measurement
Benchmark
Serializers.SerializerOutputStreamV2of theperformancemodule, 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 StringOnly the
Datecolumn takes the changed code path here: 100,000 values for each operation.-XX:-DoEscapeAnalysis(the same in every fork)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
Datevalue.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 verifywith 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:
RowBinaryFormatWriterTestwrites and reads backDate,Date32,DateTime,DateTime32andDateTime64columns that have no time zone, and the reader data set tests callgetInstantonDatecolumns.Compatibility
No public signature changes. No written byte and no returned value changes.
Related
#3425 changes the two
DateTimelines ofSerializerUtilsfor another purpose. The PR that is merged second will have a small conflict in these two lines. TheDatelines 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:getInstantof the binary format readers made a new UTCZoneIdfor eachDate,Date32,DateTimeandDateTime64value. They now use one constant. On JDK 17 this removes 152 bytes of allocation for each such value.Checklist
🤖 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 sharedDataTypeConverter.UTC_ZONE_IDconstant in RowBinary serialization and binary reader paths that handleDate,Date32,DateTime, andDateTime64(when the column has no timezone).SerializerUtils.serializePrimitiveData: four call sites for writingDate/Date32and default-UTCDateTime/DateTime64.AbstractBinaryFormatReader:getInstantforDate/Date32, andobjectToInstantforLocalDateTimeinDynamic/Variantcolumns. The unusedjava.time.ZoneIdimport 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
ZoneRegionon eachZoneId.of("UTC")call.Reviewed by Cursor Bugbot for commit 1c5117c. Bugbot is set up for automated code reviews on this repo. Configure here.