Repository navigation
perf(arrow/array): pre-alloc bulk appends - #699
Conversation
Enhance BinaryBuilder and StringBuilder AppendValues methods to pre-calculate total data size before appending, enabling single allocation strategy that eliminates reallocation overhead. Changes: - Pre-calculate total data size in AppendValues() methods - Add Capacity() method to bufBuilder interface for introspection - Eliminates multiple power-of-2 buffer resizes during bulk operations Performance improvements: - 100% allocation elimination (0 allocs/op in bulk operations) - 45% faster for 100-element batches - 39% faster for 1,000-element batches - 65% memory reduction Benefits: - Fully backward compatible (no API changes) - Transparent optimization (existing code benefits automatically) - Reduces GC pressure in data ingestion pipelines - Zero memory leaks (validated with CheckedAllocator)
Add comprehensive tests to validate bulk append optimizations: - TestBinaryBuilder_AppendValues_PreAlloc - TestStringBuilder_AppendValues_PreAlloc - TestBinaryBuilder_ReserveData_PreAlloc - TestStringBuilder_ReserveData_PreAlloc - TestInt64Builder_AppendValues_Bulk - TestBufferBuilder_Capacity All tests use memory.NewCheckedAllocator() to detect memory leaks. Tests verify correctness of optimized append operations and validate that Capacity() method reports accurate buffer sizes.
Add comprehensive benchmark suite demonstrating performance improvements: - BenchmarkBuilder_AppendOne_Int64 (baseline single append) - BenchmarkBuilder_AppendBulk_Int64 (optimized bulk append) - BenchmarkBuilder_PreReserved_Int64 (with manual Reserve) - BenchmarkStringBuilder_VarLength_* (small/medium/large strings) - BenchmarkStringBuilder_WithReserveData (pre-allocation benefits) - BenchmarkBinaryBuilder_LargeData (large binary data handling) - BenchmarkBuilder_Batch* (10/100/1000/10000 element batches) Benchmark results show: - 0 allocs/op across all batch sizes (100% elimination) - 45% faster for 100-element batches - 39% faster for 1,000-element batches - 65% memory reduction
| for i := 0; i < b.N; i++ { | ||
| b.StopTimer() | ||
| builder := array.NewInt64Builder(mem) | ||
| builder.Reserve(1000) |
There was a problem hiding this comment.
I actually tried a similar pre-allocation approach and ran e2e production benchmarks for Parquet read. Interestingly, there was no measurable change in throughput. After digging into it, I realized the Parquet reader's hot path goes through single Append(val) calls in ReadValuesDense, so AppendValues / AppendStringValues never gets hit during Parquet reads.
Looking at the actual callers of BinaryBuilder.AppendValues in the codebase, it seems like the main ones are scalar/parse.go and arrjson/arrjson.go. Both are relatively low-volume paths. So the benchmark scenario (bulk-appending a large pre-built slice) may not have many real-world counterparts in the current codebase, though external users building Arrow arrays from in-memory slices could definitely benefit.
Not a blocker at all - just something worth calling out so expectations around the performance impact are well-calibrated. It might also be worth trimming the benchmark a bit to focus on the paths that directly exercise the change.
There was a problem hiding this comment.
You're completely correct, and that's something I plan on working on in the future for adding more optimizations into the Parquet side of things. The primary beneficiary of this is definitely going to be external users building Arrow arrays from in-memory slices, parsing json, etc.
Rationale for this change
Array builders (
BinaryBuilder,StringBuilder) don't pre-calculate required buffer capacity for variable-length bulk append operations, resulting in multiple reallocations duringAppendValues()calls.Currently, the binary-type builders will reserve capacity for the offsets buffer, but does not reserve capacity for the total data size (the values buffer), as a result reallocations can get triggered often during appending individual values. For example, if you append 1000 strings of ~100 bytes each, you get ~17 reallocations.
Performance impact:
What changes are included in this PR?
Enhanced
BinaryBuilder.AppendValues()andAppendStringValues()ReserveData(totalDataSize)to allocate exact required capacityAre these changes tested?
Yes, new tests and benchmarks are added in
arrow/array/builder_prealloc_test.goandarrow/array/builder_prealloc_bench_test.go. The tests cover binary, string and numeric builders, the benchmarks cover single vs bulk, pre-reserved vs dynamic, variable-length data comparisons using various batch sizes.Are there any user-facing changes?
Only the performance benefits, no code changes are necessary to pickup the benefits from using
AppendValuesorAppendStringValues.1. String Builder - 100 Elements
Test: Bulk append of 100 strings (~50 bytes each)
BEFORE
AFTER
2. String Builder - 1000 Elements
Test: Bulk append of 1,000 strings (~50 bytes each)
BEFORE
AFTER
Benchmark results demonstrate significant improvements: