Repository navigation
Avoid allocating a digits array when writing decimal as numeric - #6729
Open
tamasprincz97-dot wants to merge 1 commit into
Open
tamasprincz97-dot wants to merge 1 commit into
tamasprincz97-dot wants to merge 1 commit into
Conversation
DecimalNumericConverter.Write passed a stackalloc'd destination of MaxDecimalNumericDigits (8) shorts to PgNumeric.Builder. The builder reserves (3 * 10 + 3) / 4 + 1 = 9 digits for the 3 uints of a decimal, so the destination was always too small and the builder allocated a short[9] (48 B) for every non-null value written, including byte, short, int, long, float and double values mapped to numeric. Size the destination with a new DecimalDestinationLength constant, which is derived from the same bound the builder uses (asserted in debug builds). Fixes npgsql#6728
This branch has not been deployed
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.
Avoid allocating a digits array when writing decimal as numeric
Fixes #6728
DecimalNumericConverter<T>.Writestackalloc'dMaxDecimalNumericDigits(8) shorts. For a decimal's 3 uints,PgNumeric.Builder.Createreserves (3 * 10 + 3) / 4 + 1 = 9 digits, and falls back tonew short[9]when the destination is smaller. So every non-null value written through this converter allocated 48 B, and the stack buffer was never used. That coversdecimal, plus thebyte/short/int/long/float/doubletonumericmappings.Changes:
PgNumeric.Builder: the bound expression moves intoGetDestinationLength(bitsLength), whichCreatenow uses. A newinternal const int DecimalDestinationLength(9) uses the same expression, andAssertInvariantschecks in debug builds that the two agree.DecimalNumericConverter<T>.WritestackallocsDecimalDestinationLength.MaxDecimalNumericDigitsstays 8. That is the real maximum for a decimal, andToDecimalandDecimalBasedMaxByteCountdepend on it. A simplerMaxDecimalNumericDigits + 1at the call site, or a tighter bound for decimals inside the builder, would also work. I preferred not to change the shared conversion code.Tests (
NumericTests):ReadWriteCaseswith 29 significant digits at scales 28, 27, 26, 5, 4, 3 and 1, positive and negative, so the existing read/write round-trip tests cover values that use all 8 numeric digits, with and without the scale-remainder digit.Builder_from_decimal_uses_destination([IssueLink]to Writing a decimal as numeric allocates a short[9] per value #6728) buildsPgNumeric.Builderfrom aDecimalDestinationLengthbuffer. It checks that the digits land in that buffer (no array allocated) and that the value round-trips exactly. With the buffer set to 8, 68 of the 69 cases fail; only the value 0 passes, as it writes no digits. It tests the builder, not the converter's call site; the benchmark below covers that.TypeHandlers.Numeric.Write(existing benchmark,MemoryDiagnoser), ShortRun, .NET 10.0.14, 4-core Xeon 2.1 GHz VM:All 15 benchmark values go from 48 B to 0 B. Mean times were 58-109 ns before and 49-99 ns after, but the ShortRun error bars overlap, so this PR claims only the allocation change.
Types.NumericTestsandTypes.NumericTypeTestspass on net10.0 in Debug and Release (372/372) against PostgreSQL 17.For 10.0.x: the same change applies to
hotfix/10.0.4, where the method is stillWriteCore.git apply -3merges the source files but leaves a conflict inNumericTests.cs.Unrelated to #6509 (read-side
ToDecimalbug).