Repository navigation
[client-v2] getString does not copy a String value through a StringBuilder - #3473
Merged
chernser merged 2 commits intoNov 30, 2026
Merged
Conversation
…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>
chernser
approved these changes
Nov 30, 2026
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
getStringof the binary format readers sends the value of aStringcolumn throughDataTypeConverter.stringToString. The method made aStringBuilder, copied the text into it and made a newStringfrom 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
stringToStringso that it finds the text first and makes aStringBuilder, with the correct size, only when it adds the quotes for an array column.Stringvalue is returned as it is.StringValue(the client reads strings as raw bytes) gives theStringthat theStringValuekeeps.CharSequence, abyte[]and other objects are converted one time, without theStringBuilder.There is no issue for this change. It is a small change with no change of results.
Measurement
Benchmark
QueryClient.queryV2WithTypesof theperformancemodule, 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: 10Float64, 10Stringand 10Int64columns). It reads each value withreadValueand with its typed getter, so it callsgetString1,000,000 times for each operation.QueryClient.queryV2WithTypesEach 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
Stringvalue 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
Stringvalues only. For the other value types, allocation and time for each call ofstringToString, 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):String,StringcolumnStringValue,StringcolumnStringBuilder,Stringcolumnbyte[],StringcolumnString, array column (quoted form)Behavior that is the same on purpose
byte[]value is decoded with the default charset of the JVM, as before.StringValuedecodes 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.NullPointerException, as before. The check of the column is still the first statement of the method.Tests
testStringToStringis added to the existingDataTypeConverterTest: 9 rows forString,CharSequence,byte[]and another object, on aStringcolumn and on an array column.StringValuehas no constructor that this test can call. The existingStringValueTestsreadStringValuecolumns throughgetString.mvn -pl client-v2 verifywith 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 aStringvalue,getStringnow returns the same object thatreadValuereturns, and for aStringValueit returns theStringthat theStringValuekeeps. Before, it returned a copy. AStringis 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:getStringon aStringcolumn of a binary format reader copied the value through aStringBuilderfor 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 10Stringcolumns throughgetStringallocates 83.9 MB less.Checklist
getStringonStringcolumns (see "Tests")🤖 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.stringToStringno longer always builds aStringBuilder. It first normalizes the input (String,StringValue,CharSequence,byte[], etc.) into aString, returns that value as-is for non-array columns, and only allocates a sizedStringBuilderwhen wrapping with quotes forArray(String)elements.This cuts redundant copies on the hot path used by binary readers'
getString(viaconvertToString), with benchmarked lower allocation and latency; output strings stay equal, though plainStringinputs may now be returned by reference instead of a duplicate.A parametrized
testStringToStringtest 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.