You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Avoid resolving column serializers for every row in RowBinaryFormatWriter #3492
clientModule: the client (client-v2; with legacy: the v1 client)data-typesArea: type names, value encoding and decoding, row mapping, data formatsperformance
RowBinaryFormatWriter performs serializer resolution for each column during row commit. This means the serializer for a column may be resolved repeatedly for every row, even though the writer’s schema does not change.
For R rows and C columns, this introduces approximately R × C serializer-resolution operations in the serialization hot path.
Recommendation: Keep open — maintainer decision
This issue splits points 3 and 4 of #3289 into their own issue, and #3289 still has no answer to the contributor's offer of a PR. Decide whether to accept a PR for this part and make it a sub-issue of #3289, or close it as a duplicate of #3289.
Category: Performance
Classification: impact: Performance · area: Data types & formats (RowBinary)
Status on main: Reproduced on main — the per-cell calls of commitRow take about 50 ns for each row of 20 Int64 columns (2.5 ns per cell), 25–30 % of the writer's time. They allocate nothing.
Breaking change: No — can be done without a break. The class is marked "Experimental API".
Duplicates / related: No others
Analysis: For each cell, commitRow reads the column and its default (client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryFormatWriter.java:91-96), writeValuePreamble reads the nullability, type, and default again (RowBinaryFormatSerializer.java:195-196), and serializeData and serializePrimitiveData select the write with two switches on the type (internal/SerializerUtils.java:68, :656). The schema does not change during the life of the writer, so each row gets the same results.
Reproduction: complete program and output
importcom.clickhouse.client.api.Client;
importcom.clickhouse.client.api.data_formats.RowBinaryFormatSerializer;
importcom.clickhouse.client.api.data_formats.RowBinaryFormatWriter;
importcom.clickhouse.client.api.data_formats.internal.SerializerUtils;
importcom.clickhouse.client.api.metadata.TableSchema;
importcom.clickhouse.data.ClickHouseColumn;
importcom.clickhouse.data.ClickHouseFormat;
importcom.clickhouse.data.format.BinaryStreamUtils;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.lang.management.ManagementFactory;
importjava.util.List;
publicclassRepro {
staticfinalStringURL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
staticfinalStringPASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
staticfinalintROWS = 1_000_000;
staticfinalintCOLS = 20;
// Values boxed in advance, so that no case measures the boxing of the setters (#3289).staticfinalObject[] VALUES = newObject[1024];
/** Discards the bytes, so that only the serialization is measured. */staticfinalclassNullOutextendsOutputStream {
longn;
@Overridepublicvoidwrite(intb) { n++; }
@Overridepublicvoidwrite(byte[] b, intoff, intlen) { n += len; }
}
interfaceBody { voidrun(OutputStreamout) throwsIOException; }
publicstaticvoidmain(String[] args) throwsException {
for (inti = 0; i < VALUES.length; i++) VALUES[i] = Long.valueOf(1_000_000L + i);
TableSchemaschema;
try (Clientclient = newClient.Builder().addEndpoint(URL)
.setUsername("default").setPassword(PASSWORD).build()) {
StringBuilderddl = newStringBuilder("CREATE TABLE triage_3492 (");
for (intc = 0; c < COLS; c++) ddl.append(c == 0 ? "" : ", ").append("c").append(c).append(" Int64");
client.execute("DROP TABLE IF EXISTS triage_3492").get().close();
client.execute(ddl.append(") ENGINE = Memory").toString()).get().close();
schema = client.getTableSchema("triage_3492");
client.execute("DROP TABLE triage_3492").get().close();
}
finalTableSchemats = schema;
finalList<ClickHouseColumn> cols = ts.getColumns();
// 1. The writer: setValue stages the row, commitRow resolves the serializer of each cell.Bodywriter = out -> {
RowBinaryFormatWriterw = newRowBinaryFormatWriter(out, ts, ClickHouseFormat.RowBinary);
for (intr = 0; r < ROWS; r++) {
for (intc = 1; c <= COLS; c++) w.setValue(c, VALUES[(r + c) & 1023]);
w.commitRow();
}
};
// 2. The calls that commitRow makes for each cell (RowBinaryFormatWriter.java:91-99), with no staging.BodyperCell = out -> {
for (intr = 0; r < ROWS; r++) {
for (inti = 0; i < COLS; i++) {
Objectv = VALUES[(r + i + 1) & 1023];
ClickHouseColumncolumn = cols.get(i);
if (column.hasDefault() && column.getDefaultValue() != ClickHouseColumn.DefaultValue.DEFAULT) continue;
if (RowBinaryFormatSerializer.writeValuePreamble(out, false, column, v)) {
SerializerUtils.serializeData(out, v, column);
}
}
}
};
// 3. Contrast: the same writes with the serializer known in advance (all columns are Int64).Bodyknown = out -> {
for (intr = 0; r < ROWS; r++)
for (inti = 0; i < COLS; i++) BinaryStreamUtils.writeInt64(out, (Long) VALUES[(r + i + 1) & 1023]);
};
Body[] bodies = {writer, perCell, known};
String[] labels = {"RowBinaryFormatWriter setValue + commitRow",
"commitRow per-cell calls, no staging",
"BinaryStreamUtils.writeInt64, serializer known"};
for (inti = 0; i < 3; i++) for (Bodyb : bodies) measure(null, b); // warm-up (JIT)for (inti = 0; i < bodies.length; i++) measure(labels[i], bodies[i]);
}
staticvoidmeasure(Stringlabel, Bodybody) throwsIOException {
com.sun.management.ThreadMXBeanmx = (com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean();
longtid = Thread.currentThread().getId();
NullOutout = newNullOut();
longa0 = mx.getThreadAllocatedBytes(tid);
longt0 = System.nanoTime();
body.run(out);
longt1 = System.nanoTime();
longa1 = mx.getThreadAllocatedBytes(tid);
if (label != null) {
System.out.printf("%s: %d rows x %d Int64, %d bytes, %.1f ns/row, %.1f B allocated/row%n",
label, ROWS, COLS, out.n, (t1 - t0) / (double) ROWS, (a1 - a0) / (double) ROWS);
}
}
}
Output on main (27d63e941), ClickHouse 26.11.7.10, OpenJDK 17.0.21.1 (in three runs, the writer case gave 170–200 ns/row, the other cases changed by less than 1 %):
RowBinaryFormatWriter setValue + commitRow: 1000000 rows x 20 Int64, 160000000 bytes, 170.5 ns/row, 480.0 B allocated/row
commitRow per-cell calls, no staging: 1000000 rows x 20 Int64, 160000000 bytes, 134.4 ns/row, 480.0 B allocated/row
BinaryStreamUtils.writeInt64, serializer known: 1000000 rows x 20 Int64, 160000000 bytes, 84.4 ns/row, 480.0 B allocated/row
Automated triage by Polyglot AI. A maintainer makes the final decision.
clientModule: the client (client-v2; with legacy: the v1 client)data-typesArea: type names, value encoding and decoding, row mapping, data formatsperformance
Activity
polyglotAI-bot commentedon Dec 2, 2026
Recommendation: Keep open — maintainer decision
This issue splits points 3 and 4 of #3289 into their own issue, and #3289 still has no answer to the contributor's offer of a PR. Decide whether to accept a PR for this part and make it a sub-issue of #3289, or close it as a duplicate of #3289.
main: Reproduced on main — the per-cell calls ofcommitRowtake about 50 ns for each row of 20Int64columns (2.5 ns per cell), 25–30 % of the writer's time. They allocate nothing.Analysis: For each cell,
commitRowreads the column and its default (client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryFormatWriter.java:91-96),writeValuePreamblereads the nullability, type, and default again (RowBinaryFormatSerializer.java:195-196), andserializeDataandserializePrimitiveDataselect the write with two switches on the type (internal/SerializerUtils.java:68,:656). The schema does not change during the life of the writer, so each row gets the same results.Reproduction: complete program and output
Output on
main(27d63e941), ClickHouse 26.11.7.10, OpenJDK 17.0.21.1 (in three runs, the writer case gave 170–200 ns/row, the other cases changed by less than 1 %):Automated triage by Polyglot AI. A maintainer makes the final decision.
Harry262530 commentedon Dec 2, 2026
We will be raising a merge request in a while for this issue