Perf bigdecimals - #740
Merged
Merged
Conversation
Refactored BigDecimal and ParquetPlainEncoder to use stack-allocated Span<byte> buffers and a new WriteBytes method, reducing heap allocations and improving performance. Updated handling of sign extension, endianness, and SchemaElement.TypeLength.
Owner
|
Looks interesting. Out of interest, did you run performance benchmarks to see what we gain? |
Contributor
Author
Benchmark ResultsHere's the key takeaway from the results:
Key findings
Files created/modified• New: BigDecimalBenchmarks.cs — 4 benchmarks × 6 parameter combos = 24 runs |
aloneguid
self-requested a review
May 14, 2026 15:00
aloneguid
approved these changes
May 14, 2026
aloneguid
left a comment
Owner
There was a problem hiding this comment.
Looks good, thank you very much!
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
Refactors
BigDecimalencoding to use stack-allocatedSpan<byte>buffers and a newWriteBytesmethod,eliminating intermediate
byte[]allocations andArray.Copycalls in the hot encoding path.Also adds comprehensive test coverage for
BigDecimalvalues.Changes
Parquet/Data/BigDecimal.csWriteBytes(Span<byte>)method that writes the unscaled value directly into a caller-providedbuffer using
BigInteger.TryWriteBytes, avoiding the heap allocation fromBigInteger.ToByteArray().GetBytes()implementation (which usedToByteArray()+Array.Copy+Enumerable.Reverse)with a thin wrapper over
WriteBytes.Span.Fill(0xFF)/Span.Fill(0)instead of a manual loop.Span.Reverse()instead ofEnumerable.Reverse().ToArray().Parquet/Encodings/ParquetPlainEncoder.csEncode(ReadOnlySpan<BigDecimal>, Stream, SchemaElement)now uses astackalloc byte[16]bufferand calls
WriteBytesinstead ofGetBytes(), removing per-element heap allocations.Encode(ReadOnlySpan<decimal>, Stream, SchemaElement)forFIXED_LEN_BYTE_ARRAYcase updated similarly.SchemaElement.TypeLengthis set fromWriteBytesreturn value.Parquet.Test/DataTypes/BigDecimalTest.cs[Theory]with[InlineData]:GetBytes_roundtrip_preserves_value— 40 cases: encode → reverse → reconstructBigInteger,covering zero, small/medium/large, positive/negative, no scale/with scale/high scale, all precision tiers (1–38).
WriteBytes_returns_correct_size_for_precision— 10 cases: validates buffer size for each precision tier.WriteBytes_zero_produces_all_zero_bytes— 3 cases: zero encoding at different precisions.WriteBytes_negative_value_has_sign_extension— 3 cases: leading byte has sign bit set.WriteBytes_positive_value_has_no_sign_bit— 3 cases: leading byte has no sign bit.WriteBytes_buffer_too_small_throws— error handling.ToString_formats_correctly— 11 cases: string formatting with ±, zeros, scale/no-scale.Parquet.Test/Types/EndToEndTypeTest.cs[Theory]:Type_bigdecimal_write_read_roundtrip— full parquet file write → read roundtrip covering thesame value matrix (zero, positive/negative, small/large, all precision tiers, high scale).
Motivation
The original
GetBytes()path performed 3 heap allocations per value during encoding(
ToByteArray(),new byte[]result,Reverse().ToArray()). For columns with millions of decimalvalues this creates significant GC pressure. The new path uses a single
stackallocbuffer per batch,writing directly via
TryWriteBytesand reversing in-place.Test Results
All 161 related tests pass on both
.NET 8and.NET 10:BigDecimalTestEndToEndTypeTest