Skip to content

[client-v2] getString does not copy a String value through a StringBuilder - #3473

Merged
chernser merged 2 commits into
ClickHouse:mainfrom
utvaxling:perf/c02-getstring-fast-path
Nov 30, 2026
Merged

chernser merged 2 commits into
ClickHouse:mainfrom
utvaxling:perf/c02-getstring-fast-path

Conversation

@utvaxling

@utvaxling utvaxling commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

getString of the binary format readers sends the value of a String column through DataTypeConverter.stringToString. The method made a StringBuilder, copied the text into it and made a new String from it: three allocations and two copies for each call. For a column that is not an array the result is equal to the text that the method started with.

This PR changes stringToString so that it finds the text first and makes a StringBuilder, with the correct size, only when it adds the quotes for an array column.

  • A String value is returned as it is.
  • A StringValue (the client reads strings as raw bytes) gives the String that the StringValue keeps.
  • A CharSequence, a byte[] and other objects are converted one time, without the StringBuilder.
  • The quoted form for an array column is the same text as before.

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

Measurement

Benchmark QueryClient.queryV2WithTypes of the performance module, JMH 1.37, JDK 17.0.21, heap 2 GiB, G1, ClickHouse 26.8. One operation reads 100,000 rows of the dataset of the module (performance/sample_dataset.sql: 10 Float64, 10 String and 10 Int64 columns). It reads each value with readValue and with its typed getter, so it calls getString 1,000,000 times for each operation.

QueryClient.queryV2WithTypes Before This PR Difference
Allocation for each operation (1 MB = 2^20 bytes) 237.34 MB 153.42 MB -83.92 MB (-35.4%)
Mean time, first pair of runs 117.7 ms 109.4 ms -7.0%
Mean time, second pair of runs 119.1 ms 108.7 ms -8.8%

Each run has 2 forks. The allocation is the same in every fork. The noise of the time of this benchmark is about 5% on the machine that I used. The first pair measured the first commit and the second pair measured the final code; for a String value the two commits do the same work. QueryClient.queryV2, which calls no getter, has the same allocation before and after (107.64 MB), and its time changes by less than the noise.

The benchmark reads String values only. For the other value types, allocation and time for each call of stringToString, measured with a small probe on JDK 17.0.21 (one JVM for each cell, after warm-up, ThreadMXBean.getThreadAllocatedBytes; the numbers include the value where the call makes it; text of 24 characters):

Value, column Before This PR
String, String column 152 B, 21 ns 0 B, 3 ns
StringValue, String column 296 B, 39 ns 120 B, 17 ns
StringBuilder, String column 232 B, 29 ns 96 B, 13 ns
byte[], String column 216 B, 46 ns 64 B, 29 ns
String, array column (quoted form) 160 B, 23 ns 48 B, 8 ns

Behavior that is the same on purpose

  • A byte[] value is decoded with the default charset of the JVM, as before. StringValue decodes with UTF-8, so UTF-8 is probably correct here also. But that changes the result on a JVM whose default charset is not UTF-8, so it is not part of this PR.
  • A null column gives a NullPointerException, as before. The check of the column is still the first statement of the method.

Tests

  • A parametrized unit test testStringToString is added to the existing DataTypeConverterTest: 9 rows for String, CharSequence, byte[] and another object, on a String column and on an array column.
  • StringValue has no constructor that this test can call. The existing StringValueTests read StringValue columns through getString.
  • mvn -pl client-v2 verify with ClickHouse 26.8: 995 unit tests and 1,896 integration tests pass.

Compatibility

No public signature changes. The returned text is equal for every value type.

One difference that code can see only with ==: for a String value, getString now returns the same object that readValue returns, and for a StringValue it returns the String that the StringValue keeps. Before, it returned a copy. A String is immutable, so the difference has no other effect.

Text for the change record

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

  • [client-v2] getString on a String column of a binary format reader copied the value through a StringBuilder for each call. It now returns the value without the copy, also when the client reads strings as raw bytes. A read of 100,000 rows with 10 String columns through getString allocates 83.9 MB less.

Checklist

  • Tests: a unit test is added; the existing integration tests cover getString on String columns (see "Tests")
  • A human-readable description of the changes was provided to include in CHANGELOG

🤖 Generated with Claude Code


Note

Low Risk
Internal formatting-only change with equivalent string output; only observable difference is possible reference identity for returned String instances.

Overview
DataTypeConverter.stringToString no longer always builds a StringBuilder. It first normalizes the input (String, StringValue, CharSequence, byte[], etc.) into a String, returns that value as-is for non-array columns, and only allocates a sized StringBuilder when wrapping with quotes for Array(String) elements.

This cuts redundant copies on the hot path used by binary readers' getString (via convertToString), with benchmarked lower allocation and latency; output strings stay equal, though plain String inputs may now be returned by reference instead of a duplicate.

A parametrized testStringToString test locks in scalar vs array quoting behavior across input types.

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

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

The typed getter getString of the binary format readers sends the value
of a String column through DataTypeConverter.stringToString. For a value
that already is a String, the method made a StringBuilder, copied the
text into it and made a new String from it: three allocations and two
copies for each call, for a result that is equal to the input.

The method now returns the value when it is a String and the column is
not an array. The quotes for an array column and all other value types
(StringValue, CharSequence, byte[]) go through the same code as before.

Measured with the JMH benchmark QueryClient.queryV2WithTypes of the
performance module on JDK 17: one query of 100,000 rows of 30 columns,
each value read with readValue and with its typed getter, 2 forks before
and 2 forks after. Allocation for each operation goes from 237.3 MB to
153.4 MB (-35.4%). The mean time goes from 117.7 ms to 109.4 ms (-7.0%);
this is from one pair of runs, and the noise of this benchmark is 5%.
QueryClient.queryV2, which does not call getString, does not change.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ly for the quoted form

The first commit of this change returned a String value as it is. All
other values (StringValue, CharSequence, byte[], other objects) still
went through a StringBuilder, also when the column is not an array and
no quotes are added.

The method now finds the text first and makes a StringBuilder, with the
correct size, only when it adds the quotes for an array column. For a
StringValue it returns the String that the StringValue keeps.

The results do not change: a byte[] value is decoded with the default
charset as before, and a null column gives a NullPointerException as
before.

A probe of the method on JDK 17 gave, for each call (the value included
where the call makes it): StringValue 248 bytes and 32 ns before, 120
bytes and 17 ns after; StringBuilder 232 bytes before, 96 bytes after;
byte[] 216 bytes before, 64 bytes after; String on an array column 160
bytes before, 48 bytes after; String on a String column 0 bytes and 3 ns
before and after.

The JMH benchmark QueryClient.queryV2WithTypes of the performance module
reads String values only, so it does not change against the first
commit: 153.4 MB for each operation, against 237.3 MB before the change
(-35.4%). The mean time is 108.7 ms against 119.1 ms before the change
(-8.8%); the run of the first commit gave -7.0%.

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 21:11
@chernser
chernser merged commit 39b4783 into ClickHouse:main Nov 30, 2026
31 of 32 checks passed
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.

2 participants