Skip to content

Commit af6dcff

Browse files
authored
[opt](paimon) Reduce JNI read object allocations (apache#66244)
Problem Summary: The Paimon JNI read path created a new PaimonColumnValue for every array element, map key/value, and struct field on every row. Timestamp conversions also resolved the session time zone per value and built redundant intermediate temporal objects. On wide nested data or large scan batches, these short-lived objects increase allocation rate and GC pressure. For example, consider two consecutive ARRAY<INT> rows, [10, 20] and [30, 40]. Previously, reading the second row allocated two new wrappers even though the first row's wrappers had already been consumed. This PR keeps two position-specific wrappers and resets their record, index, Doris type, Paimon type, and time zone before reading the second row. This is safe because VectorColumn consumes the unpacked list synchronously before the scanner advances to another value. The same invariant is applied separately to array elements, map keys, map values, and struct fields. Caches are lazy, grow only when needed, and only the current collection size or projected struct indexes are emitted, so shrinking collections cannot expose stale entries. Struct wrappers are cached by the original field index, preserving sparse/non-contiguous projection behavior fixed by apache#66223. For temporal values, the session time-zone string is now parsed once into a ZoneId. A Paimon TIMESTAMP_LTZ epoch instant is converted directly into that zone instead of constructing UTC and target-zone intermediates. TIMESTAMPTZ uses Paimon's equivalent local representation directly instead of converting through Instant and UTC. What changed? Lazily reuse nested PaimonColumnValue wrappers across rows for arrays, maps, structs, and recursively nested values. Reset all row-dependent state before a cached wrapper is reused. Cache the parsed session ZoneId instead of resolving it for every value. Remove redundant temporal conversion objects while preserving negative-epoch, nanosecond, and DST behavior. Add unit coverage for wrapper identity reuse, growing/shrinking arrays, maps, sparse struct projections, nested arrays, DST, negative epoch, and nanosecond precision. Local allocation and latency microbenchmark Environment: Linux x86_64, Oracle JDK 17.0.16, Paimon 1.3.1. The benchmark ran latest master and this commit in separate worktrees on the same machine. Each case used 100,000 warmup iterations and 5 measured trials; the table reports medians. Collection cases ran 300,000 operations per trial, timestamp cases ran 1,000,000. Allocation was measured with ThreadMXBean; GC count/time was measured with GarbageCollectorMXBean. A full GC was requested before each measured trial and excluded from the reported GC delta. The collection output lists were preallocated and cleared between operations to isolate allocation inside the Paimon value conversion path. Therefore these numbers are a focused Java microbenchmark, not an end-to-end query throughput claim. Benchmark data shape and values: ARRAY<INT>[16]: one GenericRow containing one GenericArray with [0, 1, ..., 15]. MAP<INT,BIGINT>[16]: one GenericRow containing one insertion-ordered GenericMap with {0: 0, 1: 1, ..., 15: 15}. STRUCT<INT,STRING,BIGINT>: one binary row containing (10, "x", 100); all three fields are projected. TIMESTAMP_LTZ(9): one fixed instant, 2024-03-10T10:30:00.123456789Z, read in session zone America/Los_Angeles. This date exercises the US DST transition day. TIMESTAMPTZ(9): one fixed pre-epoch value, 1969-12-31T23:59:59.999999999Z, read in UTC. Every benchmark operation rereads the same Paimon row/value. This intentionally removes file I/O, row construction, and table scan variability, and isolates the allocation/latency of PaimonColumnValue.unpack*() or timestamp conversion. The core collection loop is equivalent to: values.clear(); // preallocated outside the measured loop columnValue.unpackArray(values); consume(values.get(0), values.get(15)); On master, each unpackArray call above creates 16 wrapper objects. With this PR, the warmup creates the 16 position-specific wrappers once and measured iterations only reset them. Map and struct use the same loop shape with separate key/value or projected-field output lists. Case master alloc (B/op) PR alloc (B/op) Allocation reduction master time (ns/op) PR time (ns/op) Time reduction Median GC collections/trial ARRAY<INT>[16] 512.03 0.03 99.99% 368.38 141.61 61.6% 2 → 0 MAP<INT,BIGINT>[16] 1232.03 208.03 83.1% 668.39 508.57 23.9% 2 → 1 STRUCT<INT,STRING,BIGINT> 128.03 32.03 75.0% 63.74 63.73 ~0% 0 → 0 TIMESTAMP_LTZ(9) 384.01 64.01 83.3% 156.25 57.88 63.0% 2 → 1 TIMESTAMPTZ(9) 200.01 48.01 76.0% 44.45 21.65 51.3% 1 → 0 The remaining map allocation comes primarily from Paimon's GenericMap.keyArray() / valueArray() materialization, which is unchanged by this PR. Struct latency is effectively unchanged while allocation drops by 75%.
1 parent a73cdc0 commit af6dcff

3 files changed

Lines changed: 354 additions & 23 deletions

File tree

‎fe/be-java-extensions/paimon-scanner/src/main/java/org/apache/doris/paimon/PaimonColumnValue.java‎

Lines changed: 111 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,47 @@
3838
import java.time.LocalDate;
3939
import java.time.LocalDateTime;
4040
import java.time.ZoneId;
41+
import java.util.ArrayList;
42+
import java.util.Collections;
43+
import java.util.HashMap;
4144
import java.util.List;
45+
import java.util.Map;
4246

4347
public class PaimonColumnValue implements ColumnValue {
4448
private static final Logger LOG = LoggerFactory.getLogger(PaimonColumnValue.class);
49+
private static final Map<String, String> DORIS_TIME_ZONE_ALIASES;
50+
51+
static {
52+
Map<String, String> aliases = new HashMap<>(ZoneId.SHORT_IDS);
53+
// The scanner cannot depend on FE's TimeUtils, so keep its accepted aliases and CST
54+
// interpretation identical at this JNI boundary.
55+
aliases.put("CST", "Asia/Shanghai");
56+
aliases.put("PRC", "Asia/Shanghai");
57+
aliases.put("UTC", "UTC");
58+
aliases.put("GMT", "UTC");
59+
DORIS_TIME_ZONE_ALIASES = Collections.unmodifiableMap(aliases);
60+
}
61+
4562
private int idx;
4663
private DataGetters record;
4764
private ColumnType dorisType;
4865
private DataType dataType;
49-
private String timeZone;
66+
private ZoneId timeZone;
67+
// Keep these caches lazy so scalar columns do not pay for complex-type reuse bookkeeping.
68+
private List<PaimonColumnValue> arrayValues;
69+
private List<PaimonColumnValue> mapKeys;
70+
private List<PaimonColumnValue> mapValues;
71+
private List<PaimonColumnValue> structValues;
5072

5173
public PaimonColumnValue() {
5274
}
5375

5476
public PaimonColumnValue(DataGetters record, int idx, ColumnType columnType, DataType dataType, String timeZone) {
77+
this(record, idx, columnType, dataType, resolveTimeZone(timeZone));
78+
}
79+
80+
private PaimonColumnValue(
81+
DataGetters record, int idx, ColumnType columnType, DataType dataType, ZoneId timeZone) {
5582
this.idx = idx;
5683
this.record = record;
5784
this.dorisType = columnType;
@@ -70,7 +97,7 @@ public void setOffsetRow(InternalRow record) {
7097
}
7198

7299
public void setTimeZone(String timeZone) {
73-
this.timeZone = timeZone;
100+
this.timeZone = resolveTimeZone(timeZone);
74101
}
75102

76103
@Override
@@ -142,8 +169,8 @@ public LocalDate getDate() {
142169
public LocalDateTime getDateTime() {
143170
Timestamp ts = record.getTimestamp(idx, dorisType.getPrecision());
144171
if (dataType instanceof LocalZonedTimestampType) {
145-
return ts.toLocalDateTime().atZone(ZoneId.of("UTC"))
146-
.withZoneSameInstant(ZoneId.of(timeZone)).toLocalDateTime();
172+
// Paimon stores TIMESTAMP_LTZ as an epoch instant, so convert it directly in the cached session zone.
173+
return LocalDateTime.ofInstant(ts.toInstant(), timeZone);
147174
} else {
148175
return ts.toLocalDateTime();
149176
}
@@ -152,15 +179,18 @@ public LocalDateTime getDateTime() {
152179
@Override
153180
public LocalDateTime getTimeStampTz() {
154181
Timestamp ts = record.getTimestamp(idx, dorisType.getPrecision());
155-
LocalDateTime v = ts.toInstant()
156-
.atZone(ZoneId.of("UTC"))
157-
.toLocalDateTime();
158-
return v;
182+
// Timestamp's local representation is identical to converting its epoch instant in UTC.
183+
return ts.toLocalDateTime();
159184
}
160185

161186
@Override
162187
public boolean isNull() {
163-
return record.isNullAt(idx);
188+
boolean isNull = record.isNullAt(idx);
189+
if (isNull) {
190+
// A null complex value has no live descendants; release wrappers retained by its prior row.
191+
clearChildCaches();
192+
}
193+
return isNull;
164194
}
165195

166196
@Override
@@ -171,38 +201,99 @@ public byte[] getBytes() {
171201
@Override
172202
public void unpackArray(List<ColumnValue> values) {
173203
InternalArray recordArray = record.getArray(idx);
204+
if (arrayValues == null) {
205+
arrayValues = new ArrayList<>();
206+
}
207+
ColumnType elementDorisType = dorisType.getChildTypes().get(0);
208+
DataType elementPaimonType = ((ArrayType) dataType).getElementType();
174209
for (int i = 0; i < recordArray.size(); i++) {
175-
PaimonColumnValue arrayColumnValue = new PaimonColumnValue((DataGetters) recordArray, i,
176-
dorisType.getChildTypes().get(0), ((ArrayType) dataType).getElementType(), timeZone);
177-
values.add(arrayColumnValue);
210+
values.add(reuseColumnValue(arrayValues, i, (DataGetters) recordArray, i,
211+
elementDorisType, elementPaimonType));
178212
}
213+
trimCache(arrayValues, recordArray.size());
179214
}
180215

181216
@Override
182217
public void unpackMap(List<ColumnValue> keys, List<ColumnValue> values) {
183218
InternalMap map = record.getMap(idx);
219+
if (mapKeys == null) {
220+
mapKeys = new ArrayList<>();
221+
mapValues = new ArrayList<>();
222+
}
184223
InternalArray key = map.keyArray();
224+
ColumnType keyDorisType = dorisType.getChildTypes().get(0);
225+
DataType keyPaimonType = ((MapType) dataType).getKeyType();
185226
for (int i = 0; i < key.size(); i++) {
186-
PaimonColumnValue keyColumnValue = new PaimonColumnValue((DataGetters) key, i,
187-
dorisType.getChildTypes().get(0), ((MapType) dataType).getKeyType(), timeZone);
188-
keys.add(keyColumnValue);
227+
keys.add(reuseColumnValue(mapKeys, i, (DataGetters) key, i,
228+
keyDorisType, keyPaimonType));
189229
}
230+
trimCache(mapKeys, key.size());
190231
InternalArray value = map.valueArray();
232+
ColumnType valueDorisType = dorisType.getChildTypes().get(1);
233+
DataType valuePaimonType = ((MapType) dataType).getValueType();
191234
for (int i = 0; i < value.size(); i++) {
192-
PaimonColumnValue valueColumnValue = new PaimonColumnValue((DataGetters) value, i,
193-
dorisType.getChildTypes().get(1), ((MapType) dataType).getValueType(), timeZone);
194-
values.add(valueColumnValue);
235+
values.add(reuseColumnValue(mapValues, i, (DataGetters) value, i,
236+
valueDorisType, valuePaimonType));
195237
}
238+
trimCache(mapValues, value.size());
196239
}
197240

198241
@Override
199242
public void unpackStruct(List<Integer> structFieldIndex, List<ColumnValue> values) {
200243
RowType rowType = (RowType) dataType;
201244
// Projection entries are original child indexes, so the binary row must keep the full RowType arity.
202245
InternalRow row = record.getRow(idx, rowType.getFieldCount());
246+
if (structValues == null) {
247+
structValues = new ArrayList<>();
248+
}
203249
for (int i : structFieldIndex) {
204-
values.add(new PaimonColumnValue(row, i, dorisType.getChildTypes().get(i),
205-
rowType.getFields().get(i).type(), timeZone));
250+
values.add(reuseColumnValue(structValues, i, row, i, dorisType.getChildTypes().get(i),
251+
rowType.getFields().get(i).type()));
252+
}
253+
}
254+
255+
private PaimonColumnValue reuseColumnValue(
256+
List<PaimonColumnValue> cache, int cacheIndex, DataGetters childRecord, int childIndex,
257+
ColumnType childDorisType, DataType childPaimonType) {
258+
while (cache.size() <= cacheIndex) {
259+
cache.add(null);
260+
}
261+
PaimonColumnValue value = cache.get(cacheIndex);
262+
if (value == null) {
263+
value = new PaimonColumnValue(
264+
childRecord, childIndex, childDorisType, childPaimonType, timeZone);
265+
cache.set(cacheIndex, value);
266+
} else {
267+
// VectorColumn consumes unpacked values synchronously, before this parent advances to another value.
268+
value.reset(childRecord, childIndex, childDorisType, childPaimonType, timeZone);
206269
}
270+
return value;
271+
}
272+
273+
private void reset(
274+
DataGetters record, int idx, ColumnType dorisType, DataType dataType, ZoneId timeZone) {
275+
this.record = record;
276+
this.idx = idx;
277+
this.dorisType = dorisType;
278+
this.dataType = dataType;
279+
this.timeZone = timeZone;
280+
}
281+
282+
private static ZoneId resolveTimeZone(String timeZone) {
283+
return ZoneId.of(timeZone, DORIS_TIME_ZONE_ALIASES);
284+
}
285+
286+
private static void trimCache(List<PaimonColumnValue> cache, int liveSize) {
287+
if (cache.size() > liveSize) {
288+
// Retain only wrappers addressable by the current container, not its historical maximum.
289+
cache.subList(liveSize, cache.size()).clear();
290+
}
291+
}
292+
293+
private void clearChildCaches() {
294+
arrayValues = null;
295+
mapKeys = null;
296+
mapValues = null;
297+
structValues = null;
207298
}
208299
}

0 commit comments

Comments
 (0)