Skip to content

Instantly share code, notes, and snippets.

@saurabh500
Last active August 16, 2026 20:19
Show Gist options
  • Select an option

  • Save saurabh500/114e2f8a5e1379264fae51593e503d9f to your computer and use it in GitHub Desktop.

Select an option

Save saurabh500/114e2f8a5e1379264fae51593e503d9f to your computer and use it in GitHub Desktop.
mssql-rs: making decimal/numeric decode faster on top of PR #311 — six recommendations, measured (5.2x text path, 9.5x binary path)

Speeding up decimal / numeric decode in mssql-rs, on top of PR #311

PR #311 gave RowWriter a value-destination sink so a consumer can have known-length PLP payloads written straight into its own storage. That is a real win for varchar(max) / nvarchar(max) / varbinary(max), but it does nothing for decimal / numeric, which never take the PLP path.

In the PostgreSQL FDW workload that motivated #311, numeric is the one column class where the #299/#311 stack still loses badly to the poc/tds-decode-fdw-usage branch:

workload poc/tds-decode-fdw-usage PR #311
fixed 725.9 ms 662.1 ms
text 1410.1 ms 1282.6 ms
binary 1080.9 ms 801.3 ms
numeric 1510.7 ms 2673.2 ms
mixed (SELECT *) 3952.6 ms 4032.7 ms

1,000,049 rows, 4 numeric columns, medians of a 12-round balanced interleave.

The gap is not a TDS-protocol problem. It is two things: the decoder allocates twice per numeric value, and the only way to get a value out is a freshly allocated String. This note proposes six changes, five of which are backwards compatible, and measures each one.

Everything below is reproducible: numeric_decode.rs reproduces the three decode shapes and five format shapes byte for byte, asserts they all agree, and only then times them.


Status — R1, R2 and R5 are implemented

microsoft/mssql-rs#313 implements R1 + R2 + R5 as layer 6 of stack #287, on top of #311. Measured in-repo, ns/value:

workload before after speedup
decode, FDW columns 34.75 4.19 8.3x
decode, decimal(38,10) 40.09 4.18 9.6x
format, FDW columns (format_into) 83.15 17.05 4.9x
format, decimal(38,10) (format_into) 155.89 88.33 1.8x

Decode landed ~3x better than the ~13 ns this note projected.

Two corrections that implementation forced, both folded into the text below:

  1. The two-limb split formatter in R5 was wrong — it silently truncated above u64::MAX × 10¹⁹. Three limbs are required. See R5.
  2. R1's pub magnitude: u128 field is the wrong representation — a u128 field imposes 16-byte alignment, which propagates into the row-fetch async state machines and grows them past their size budget. Store [u64; 2] privately behind an accessor. See R1.

R4 is subsumed by R1 and is no longer a separate recommendation: once DecimalParts is Copy and carries the magnitude inline, a typed writer entry point is a pure API addition with no representation work behind it. R3 overlaps #303–#307. R6 remains open and is the largest outstanding win.


TL;DR

# Change Breaking? Measured effect
R1 Carry the magnitude inline in DecimalParts (u128), make it Copy yes enables R2
R2 Decode into a [u8; 16] + u128::from_le_bytes; drop both heap allocations no¹ 2.6x faster decode (3.9x on decimal(38,10))
R3 Sync-first read for the magnitude bytes no removes one .await per value
R4 write_decimal_compact default method taking a Copy decimal no subsumed by R1 — see Status
R5 format_into(&mut [u8; 48]) -> &str instead of to_decimal_string() -> String no 5.6x faster formatting (1.6x on decimal(38,10))
R6 Optional base-10000 digit export no 10.4x vs today, and removes the consumer's text parse

¹ R2 is internal. It is fully realised only with R1, but it can be applied on its own — see "If you cannot break the API".

End to end, per decoded value, for the FDW's four numeric columns:

path ns / value vs #311
#311 head: decode + to_decimal_string 124.6 1.00x
POC branch: decode + to_decimal_string 107.6 1.16x
proposed: decode + format_into 23.7 5.25x
proposed: decode + base-10000 digits 11.5 10.84x

Where the time goes today

All line references are against 890ef66 (head of PR #311).

1. Two heap allocations per value

GenericDecoder::read_decimal_data — mssql-tds/src/datatypes/decoder.rs:742-790:

validate_alloc_size(number_of_int_parts * 4, "read_decimal int_parts")?;
let mut magnitude = vec![0u8; number_of_int_parts * 4];      // alloc #1, zeroed
reader.read_bytes(&mut magnitude[..magnitude_len]).await?;   // copy #1

let int_parts = magnitude
    .chunks_exact(4)
    .map(|chunk| {
        let mut word = [0u8; 4];
        word.copy_from_slice(chunk);
        i32::from_le_bytes(word)
    })
    .collect();                                              // alloc #2, copy #2

Ok(Some(DecimalParts { is_positive, scale, precision, int_parts }))

The payload here is at most 16 bytes. MAX_DECIMAL_INT_PARTS is 4 (decoder.rs:158), and it is checked immediately above. So the code pays a malloc, a memset, a second malloc, two copies, and later two frees, to move up to sixteen bytes that are already sitting in the packet buffer.

DecimalParts itself (decoder.rs:2062-2071) is what forces this:

pub struct DecimalParts {
    pub is_positive: bool,
    pub scale: u8,
    pub precision: u8,
    pub int_parts: Vec<i32>,   // <- the whole problem
}

The POC branch's sync row path (io/token_stream.rs:569-590) halves this — it drops the staging buffer and pushes straight from the borrowed packet buffer — but it still does Vec::with_capacity((length - 1) / 4), so one malloc/free per numeric value survives on every branch measured so far, including the POC.

Note also that 890ef66 has no DecodeOp and no sync row path, so at #311 the async read_decimal_data above is the only decimal path.

2. validate_alloc_size on a provably-bounded size

number_of_int_parts <= MAX_DECIMAL_INT_PARTS is checked on the line above, so the size argument is <= 16 by construction. The call is a branch and a function call per value that can never fire.

3. An .await for bytes that are almost always already buffered

The length and sign bytes use read_sync_first!, but the magnitude does not — it goes through reader.read_bytes(..).await. TdsPacketReader has try_read_byte / try_read_uint32 / try_read_int64 and so on (io/packet_reader.rs:15-69) but no try_read for a byte run.

4. Getting a value out costs one to three more allocations

to_decimal_string — decoder.rs:2222-2247:

let value_str = magnitude.to_string();                    // alloc
let result = format!("{}.{}", &value_str[..split_pos], &value_str[split_pos..]);  // alloc + copy
if self.is_positive { result } else { format!("-{}", result) }                   // alloc + copy

This is the only way to get full precision out of DecimalParts. to_f64 loses precision; magnitude() is public but returns the unscaled integer, so a caller still has to place the decimal point itself. Every text-input consumer — PostgreSQL numeric_in, ODBC SQL_C_CHAR, Python Decimal(str), JSON serialisation — lands here.

At 81 ns/value it is the single most expensive thing in the numeric path, more than twice the cost of the decode it follows.


The recommendations

R1 — Carry the magnitude inline; make DecimalParts Copy

A valid decimal/numeric has precision ≤ 38, so the magnitude is < 10³⁸ < 2¹²⁷ and always fits in a u128. The existing MAX_DECIMAL_INT_PARTS = 4 already encodes exactly this.

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DecimalParts {
    pub is_positive: bool,
    pub scale: u8,
    pub precision: u8,
    magnitude: u128,
}

magnitude() (decoder.rs:2267) becomes a field read instead of a rposition scan plus a fold. magnitude_big() (decoder.rs:2288) and its BigUint allocation become dead for anything the decoder produces — read_decimal_data already rejects more than four words, so the None arm of magnitude() is unreachable on the decode path today. Keep BigDecimal for from_string, which is the encode/bulk-copy direction and genuinely needs it.

Store [u64; 2], not u128. The sketch above reads well but costs more than it looks. A u128 field gives DecimalParts 16-byte alignment, which propagates through ColumnValues into the generated row-fetch async state machines; in #313 that grew next_row_cursor from 4248 to 4400 bytes and tripped the crate's future-size guard. Keeping the storage private and 8-aligned avoids the whole problem, and one shift-plus-or on read is free relative to the decode it replaces:

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DecimalParts {
    pub is_positive: bool,
    pub scale: u8,
    pub precision: u8,
    /// Little-endian halves. Deliberately not `u128`: that would force
    /// 16-byte alignment on every type that embeds this one.
    magnitude: [u64; 2],
}

impl DecimalParts {
    #[inline]
    pub fn magnitude(&self) -> u128 {
        (self.magnitude[1] as u128) << 64 | self.magnitude[0] as u128
    }
}

That lands DecimalParts at 24 bytes, align 8 — 8 bytes smaller than the Vec-based struct it replaces — and leaves the row-fetch futures byte-for-byte unchanged. Expose word(i) / word_count() / from_words(..) alongside it for the wire encoders and the JS FFI, which want the little-endian 32-bit view.

This is the one breaking change, because int_parts is a public field. Options:

  • Preferred while the crate is 0.1.0: change it, and keep a pub fn int_parts(&self) -> ArrayVec<i32, 4> (or ([i32; 4], usize)) accessor for anyone who was reading the words.

  • Compatible: leave DecimalParts alone, add a new CompactDecimal and route the fast path through R4. See "If you cannot break the API".

  • Preferred while the crate is 0.1.0: change it, and keep a pub fn int_parts(&self) -> ArrayVec<i32, 4> (or ([i32; 4], usize)) accessor for anyone who was reading the words.

  • Compatible: leave DecimalParts alone, add a new CompactDecimal and route the fast path through R4. See "If you cannot break the API".

R2 — Decode into a stack array; delete both allocations

let length = read_sync_first!(reader, try_read_byte, read_byte);
if length == 0 { return Ok(None); }
let is_positive = read_sync_first!(reader, try_read_byte, read_byte) == 1;

let mut word = [0u8; 16];
reader.read_bytes(&mut word[..(length - 1) as usize]).await?;

Ok(Some(DecimalParts {
    is_positive,
    scale,
    precision,
    magnitude: u128::from_le_bytes(word),
}))

Because TDS is little-endian and the array is zero-initialised, u128::from_le_bytes is the reassembly. The chunks_exact / map / collect disappears. The zero-initialised array also preserves the existing zero-padding semantics for a short read for free, which is what the comment at decoder.rs:758-761 is protecting — so div_ceil can stay if the second call site needs it (see "What this does not address").

validate_alloc_size goes away with the allocation.

Measured, 16,384 values of decimal(15,2), decimal(15,2), decimal(18,3), decimal(19,4):

decode shape ns / value speedup
#311 head (vec! + chunks_exact.collect) 34.6 1.00x
POC sync row (Vec::with_capacity + push) 24.8 1.40x
proposed ([u8; 16] + from_le_bytes) 13.3 2.61x

On decimal(38,10) (17-byte wire, four words) the gap widens, because the per-word loop is longer and the allocation is bigger:

decode shape ns / value speedup
#311 head 39.9 1.00x
POC sync row 24.8 1.61x
proposed 10.2 3.89x

The proposed decode gets faster on wide values than narrow ones (10.2 vs 13.3 ns), because the work is now a fixed-size 16-byte copy regardless of precision.

R3 — Give the reader a sync-first byte-run read

read_sync_first! already exists and is used for the length and sign. Add the missing primitive so the magnitude can use it too:

fn try_read_bytes(&mut self, buf: &mut [u8]) -> bool { .. }
// or, better for this call site:
fn try_read_array<const N: usize>(&mut self) -> Option<[u8; N]> { .. }

A borrowed variant — try_read_slice(len) -> Option<&[u8]> — would also let the sign and magnitude be taken in one borrow, which is the same trick the POC branch's sync row path uses and the same spirit as #311's borrowed destinations.

Without any new primitive there is still a cheap win available: decimal(p ≤ 9) is one try_read_uint32 and decimal(p ≤ 19) is one try_read_int64, both of which already exist. That covers essentially every real schema.

(Not separately quantified here — this micro-benchmark reads from an in-memory buffer, so it cannot show the .await cost. It is included because it is the same class of change and free to make alongside R2.)

R4 — A Copy writer entry point, in the shape #311 established

Subsumed by R1. Once DecimalParts is itself Copy and carries the magnitude inline — as shipped in #313 — this becomes a pure API addition with no representation work behind it, and there is no longer a reason to introduce a separate CompactDecimal. The section is kept because it remains the right answer under "If you cannot break the API", where DecimalParts must stay as it is.

#311 added opt-in RowWriter methods with defaults so no existing consumer breaks. Do the same for decimals:

/// A decoded `decimal`/`numeric`. Never allocates.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CompactDecimal {
    pub is_positive: bool,
    pub precision: u8,
    pub scale: u8,
    pub magnitude: u128,
}

pub trait RowWriter {
    // ...
    /// Receives a decimal without allocating. The default forwards to
    /// [`RowWriter::write_decimal`], so existing implementations are unaffected.
    fn write_decimal_compact(&mut self, col: usize, val: CompactDecimal) {
        self.write_decimal(col, val.into());
    }
    fn write_numeric_compact(&mut self, col: usize, val: CompactDecimal) {
        self.write_numeric(col, val.into());
    }
}

Consumers that override pay zero allocations. Consumers that do not pay exactly what they pay today, because the From<CompactDecimal> for DecimalParts conversion rebuilds the Vec. This is precisely the #311 contract — opt in for the fast path, default for compatibility — and it means R4 can ship without R1.

R5 — Format without allocating

Replace the to_string() + format! splice with a single reverse pass into a caller-supplied buffer. 38 digits + sign + point + slack fits in 48 bytes:

impl DecimalParts {
    /// Renders the value into `buf` and returns the populated prefix.
    /// Never allocates.
    pub fn format_into<'a>(&self, buf: &'a mut [u8; 48]) -> &'a str { .. }
}

// keep the old entry point as a thin wrapper
pub fn to_decimal_string(&self) -> String {
    self.format_into(&mut [0u8; 48]).to_owned()
}

One caveat worth stating, because I got it wrong first. The obvious implementation — buf[pos] = b'0' + (mag % 10) as u8; mag /= 10; on the u128 — is slower than to_string() on wide values, because every u128 / 10 is a __udivti3 libcall while u128::to_string() uses a chunked algorithm.

The fix is one u128 division per limb. Split by 10¹⁹ into u64 limbs and generate every digit with u64 arithmetic, which the compiler turns into a multiply-shift.

Use three limbs, not two. An earlier revision of this gist showed a two-limb split, and it was wrong:

// WRONG — silently truncates
let (hi, lo) = ((magnitude / P19) as u64, (magnitude % P19) as u64);

magnitude / 10¹⁹ exceeds u64::MAX as soon as magnitude > u64::MAX × 10¹⁹ ≈ 1.845e38, and the as u64 cast then discards the overflow with no diagnostic. A u128 reaches 39 digits and u128::MAX ≈ 3.403e38, so the top of the range is unrepresentable: u128::MAX rendered as 155814926183842947303374607431768211455 instead of 340282366920938463463374607431768211455.

A legal decimal(38, s) tops out at 10³⁸ − 1 ≈ 9.99e37 and never reaches the cutoff, so this is unreachable through a conforming wire value — but DecimalParts accepts an arbitrary u128, and 16 bytes of 0xFF off the wire will reach it. Three limbs cover the full domain:

const P19: u128 = 10_000_000_000_000_000_000;
let mut lo  = (magnitude % P19) as u64;             // digits 0..18
let mut mid = ((magnitude / P19) % P19) as u64;     // digits 19..37
let mut hi  = (magnitude / (P19 * P19)) as u64;     // digits 38+
// emit 19 digits from `lo`, then 19 from `mid`, then the rest from `hi`,
// inserting '.' after `scale` digits. The limbs are positional, so a limb
// that empties early still yields zeros until its 19 slots are spent.

Credit for catching this: it surfaced during the implementation of R1/R2/R5 in microsoft/mssql-rs#313, and only because the implementer's oracle was the independent u128::to_string() + splice rather than a reuse of the same limb decomposition. An oracle that shares arithmetic with the kernel it validates would have been wrong in exactly the same way and the corpus would have passed. benches/numeric_decode.rs now carries a check_adversarial() corpus — u128::MAX, every 10ᵏ and 10ᵏ−1 for k ≤ 38, the exact two-limb cutoff and its neighbours, and 20 000 deterministic random magnitudes across 9 scales and both signs, 361 656 cases — which fails against the two-limb version and passes against this one. The original workload-shaped corpus could not have caught it, because every magnitude it generates is a legal decimal(38, s).

Measured, the FDW's four columns:

formatter ns / value vs today
to_decimal_string (today) 81.4 1.00x
naive reverse loop 21.1 3.87x
u64-only fast path (p ≤ 19) 12.0 6.79x
split-by-10¹⁹ (handles all widths) 14.6 5.59x

and on decimal(38,10) with a magnitude just under 10³⁸:

formatter ns / value vs today
to_decimal_string (today) 159.3 1.00x
naive reverse loop 219.5 0.73x — slower
u64-only fast path 222.1 0.72x — slower
split-by-10¹⁹ 97.8 1.63x

The third limb costs nothing measurable: it is one extra u128 division at setup, and the digit loop's limb selection is a comparison against a counter that was already being maintained.

Ship the split version as the single implementation: it is 5.6x on narrow values and still 1.6x on the widest ones. The u64-only variant is quicker on narrow input but degenerates to the naive loop above 19 digits, so it is not worth the branch.

Buffer bound. 48 bytes is exactly tight, not generous: the worst case is a negative u128::MAX at scale = 45, which writes 46 digits + '.' + '-' = 48. Within the legal scale ≤ 38 domain the worst case is 41. If you take a scale from the wire without validating it, bound it before formatting.

R6 — Optional: export base-10000 digits directly

Any consumer that stores decimals in a base-10⁴ representation — PostgreSQL's numeric is exactly this: int16 digits, a weight, a sign and a display scale — currently has to go u128 → decimal string → parser → base-10⁴. Both ends of that round trip are pure overhead.

impl DecimalParts {
    /// Base-10000 digits, most significant first. Returns `(ndigits, weight)`.
    pub fn to_base10000(&self, out: &mut [i16; 12]) -> (usize, i32) { .. }
}
export ns / value (FDW cols) ns / value (38,10)
to_decimal_string (today) 81.4 159.3
to_base10000 7.8 106.9

That is 10.4x on the FDW columns, and it deletes the consumer's numeric_in parse on top, which this benchmark does not even count. On a full 38-digit value the margin narrows to 1.5x, because carry propagation across eleven base-10⁴ digits is real work.

⚠️ Implementation trap — do not pre-scale the magnitude.

To align the fractional part onto a base-10⁴ boundary you need to multiply by 10^pad where pad = (4 - scale % 4) % 4. The obvious place to do it is on the u128 before converting:

let mut mag = self.magnitude * 10u128.pow(pad as u32);   // WRONG

For decimal(38, s) with s % 4 != 0, the magnitude reaches 10³⁸ − 1 and 10³⁸ × 100 ≈ 10⁴⁰ does not fit in a u128 (max ≈ 3.40 × 10³⁸). In a release build this wraps silently and yields a completely wrong number:

magnitude          : 99999999999999999999999999999999999998
magnitude * 100    : 131811359292784559562136384478721867576   <- wrapped
checked_mul        : None

Convert to base-10⁴ first, then scale the digit array with carry propagation. The largest intermediate is then 9999 * 1000 + carry, which fits in a u32. The FDW POC's numeric_binary_input gets this right; my first draft of this gist did not.

This one is arguably out of scope for a TDS crate, since base-10⁴ is a PostgreSQL detail. If so, R1/R4 alone are enough — once the magnitude is reachable as a u128, a consumer can do this conversion itself in ~8 ns. The important thing is that today it cannot, because the only lossless accessor returns a String.


End to end

Decode plus "make it usable", per value, over the FDW's four numeric columns:

path ns / value vs #311 head
#311 head: read_decimal_data + to_decimal_string 124.6 1.00x
POC branch: sync-row decode + to_decimal_string 107.6 1.16x
R2 + R5: inline decode + format_into 23.7 5.25x
R2 + R6: inline decode + base-10000 11.5 10.84x

Note how little the POC branch's decode work buys once formatting is included — 1.16x. The POC's real numeric win in the FDW came from the consumer using PostgreSQL's binary numeric receive instead of numeric_in, i.e. it routed around to_decimal_string rather than making it cheaper. R5 and R6 make that win available to every consumer instead of only to one that can bypass the crate.

What this is worth in the FDW measurement. The numeric workload is 1,000,049 rows × 4 columns = 4,000,196 values, and measured 2673 ms at #311. Straight arithmetic on the per-value figures above:

  • in-process decode + format at #311: ~498 ms of that 2673 ms
  • with R2 + R5: ~95 ms, so ~403 ms saved, roughly 15%
  • with R2 + R6: ~46 ms, ~452 ms saved, plus the numeric_in parse that disappears with it

This is extrapolation from a micro-benchmark, not an end-to-end measurement — the rest of the 2673 ms is network, tuple forming and executor overhead that none of this touches. Treat ~15% as the honest floor for the text path and rather more for the binary one.

It is also not enough to beat the POC on its own. The POC's numeric advantage is ~1162 ms, and only about a third of that is reachable from inside mssql-tds. The rest is PostgreSQL's numeric_in on the consumer side, which R5 cannot touch — you need R6 and a consumer that switches to binary receive. R5 is still the change worth shipping, because it benefits every text consumer of the crate; it just is not the whole story for this particular FDW.


If you cannot break the API

R1 is the only breaking change. Without it you still get most of the benefit:

  1. R5 (format_into) is purely additive and is the largest single win — 5.6x on formatting.
  2. R6 (to_base10000) is purely additive.
  3. R4 (write_decimal_compact with a defaulted forward) is purely additive, and lets the decoder run R2's allocation-free path internally, materialising a DecimalParts only for consumers that did not opt in.
  4. R2 can be applied halfway on its own: read into [u8; 16] on the stack, then build the Vec<i32> from it. That kills one of the two allocations and the memset without touching any public type.

The combination R2(partial) + R4 + R5 is backwards compatible and captures most of the 5.25x.


Also worth upstreaming

The POC branch's DecodeOp plan — resolving each column's type, precision and scale once per result set rather than re-matching metadata.type_info.type_info_variant on every value — is orthogonal to everything above and applies to every type, not just decimals. At #311 head, read_decimal (decoder.rs:721-740) re-destructures the metadata enum per value. That is already prototyped on 9df51f0; it should not be left on a POC branch.


Running this

Gists are flat, so recreate the two-file layout:

mkdir -p tds-numeric-bench/benches && cd tds-numeric-bench
# Cargo.toml at the root, numeric_decode.rs into benches/
cargo bench

numeric_decode.rs asserts that all three decode shapes and all five format shapes produce identical results before it times any of them, on both workloads, so the numbers are comparable by construction. The base-10⁴ encoding is validated by rendering it back to a decimal string rather than by reconstructing the magnitude — reconstruction would hit the very u128 overflow the encoder exists to avoid, so it cannot serve as its own oracle.

The workload mirrors the FDW regression table:

CAST((n % 10000000) * 0.01   AS decimal(15,2)) AS n1,
CAST((n % 1000000)  * -0.01  AS decimal(15,2)) AS n2,   -- negative: exercises the extra format!
CAST((n % 100000)   * 0.001  AS decimal(18,3)) AS n3,
CAST((n % 10000)    * 0.0001 AS decimal(19,4)) AS n4

plus a decimal(38,10) workload whose magnitude sits just under 10³⁸ — the true maximum for precision 38 — to exercise the four-word path and the overflow trap described under R6.

Environment for the numbers above: AMD EPYC 7763, Ubuntu 24.04 (WSL2, kernel 6.6.87.2), rustc 1.93.1, opt-level=3, lto="fat", codegen-units=1. Criterion medians; run-to-run variation was under 2%. Raw output in results-linux.txt.

What this does not address

  • NULL handling and the length == 0 path — unchanged, already cheap.
  • The sql_variant caller — read_decimal_data has a second call site at decoder.rs:2356, where the length comes from the variant header rather than from a DecimalN field. If that path can present lengths outside {5,9,13,17}, keep the length-tolerant div_ceil width calculation there. The [u8; 16] rewrite is compatible either way: the array is zero-initialised, so a short read leaves the high bytes zero exactly as the staging Vec did.
  • money / smallmoney — separate types with their own decode, not touched here.
  • from_string / bulk copy — the encode direction still wants BigDecimal. None of the above changes it.
[package]
name = "tds-numeric-bench"
version = "0.1.0"
edition = "2021"
[dependencies]
byteorder = "1"
[dev-dependencies]
criterion = "0.5"
[[bench]]
name = "numeric_decode"
harness = false
[profile.release]
opt-level = 3
lto = "fat"
codegen-units = 1
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! Micro-benchmark for the `decimal`/`numeric` decode path in `mssql-rs`.
//!
//! It reproduces, byte for byte, three shapes of the same work:
//!
//! * `head` - `GenericDecoder::read_decimal_data` as it stands at the head of
//! PR #311 (`890ef66`): a zeroed staging `Vec<u8>`, then
//! `chunks_exact(4).map(..).collect::<Vec<i32>>()`.
//! * `poc` - the sync row path from the `poc/tds-decode-fdw-usage` branch
//! (`9df51f0`, `io/token_stream.rs`): one `Vec::with_capacity`
//! filled by a push loop reading from the borrowed packet buffer.
//! * `inline` - the proposal: a 16-byte stack array and `u128::from_le_bytes`,
//! with the magnitude carried inline in a `Copy` struct.
//!
//! and three shapes of the formatting that a text-input consumer (PostgreSQL
//! `numeric_in`, ODBC `SQL_C_CHAR`, Python `Decimal(str)`) needs afterwards:
//!
//! * `to_decimal_string` - as it stands today: `u128::to_string()` plus one or
//! two `format!` splices.
//! * `format_into` - the proposal: one reverse pass into a caller-supplied
//! `[u8; 48]`, no allocation.
//! * `format_into_fast` - the same, taking a `u64` path when the magnitude fits,
//! so digit generation avoids the `__udivti3` libcall.
//!
//! plus the binary alternative that skips text entirely:
//!
//! * `to_base10000` - convert straight to PostgreSQL's `numeric` wire digits.
//!
//! Run with:
//!
//! ```text
//! cargo bench
//! ```
use byteorder::{ByteOrder, LittleEndian};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
// ---------------------------------------------------------------------------
// Shared types
// ---------------------------------------------------------------------------
const MAX_DECIMAL_INT_PARTS: usize = 4;
/// `DecimalParts` as it exists today: the magnitude lives in a heap `Vec<i32>`.
#[derive(Clone, Debug, PartialEq)]
pub struct DecimalPartsVec {
pub is_positive: bool,
pub scale: u8,
pub precision: u8,
pub int_parts: Vec<i32>,
}
/// The proposed replacement: magnitude inline, struct is `Copy`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DecimalInline {
pub is_positive: bool,
pub scale: u8,
pub precision: u8,
pub magnitude: u128,
}
impl DecimalPartsVec {
/// `DecimalParts::magnitude` as it stands today.
fn magnitude(&self) -> Option<u128> {
let significant = self
.int_parts
.iter()
.rposition(|&part| part != 0)
.map_or(0, |i| i + 1);
if significant > MAX_DECIMAL_INT_PARTS {
return None;
}
Some(
self.int_parts[..significant]
.iter()
.enumerate()
.fold(0u128, |acc, (i, &part)| {
acc | ((part as u32 as u128) << (i * 32))
}),
)
}
/// `DecimalParts::to_decimal_string` as it stands today.
fn to_decimal_string(&self) -> String {
let value_str = match self.magnitude() {
Some(magnitude) => magnitude.to_string(),
None => unreachable!("decoded values always fit in four words"),
};
let result = if self.scale == 0 {
value_str
} else {
let scale_pos = self.scale as usize;
if value_str.len() <= scale_pos {
format!("0.{}{}", "0".repeat(scale_pos - value_str.len()), value_str)
} else {
let split_pos = value_str.len() - scale_pos;
format!("{}.{}", &value_str[..split_pos], &value_str[split_pos..])
}
};
if self.is_positive {
result
} else {
format!("-{}", result)
}
}
}
// ---------------------------------------------------------------------------
// Decode variants
// ---------------------------------------------------------------------------
/// Stand-in for `validate_alloc_size`, which the current path calls per value.
#[inline]
fn validate_alloc_size(size: usize, _what: &str) -> Result<(), ()> {
if size > 1 << 30 {
return Err(());
}
Ok(())
}
/// PR #311 head: `read_decimal_data`. Two heap allocations, two copies, one memset.
#[inline]
fn decode_head(buf: &[u8], pos: &mut usize, precision: u8, scale: u8) -> Option<DecimalPartsVec> {
let length = buf[*pos];
*pos += 1;
if length == 0 {
return None;
}
let sign = buf[*pos];
*pos += 1;
let is_positive = sign == 1;
let magnitude_len = (length - 1) as usize;
let number_of_int_parts = magnitude_len.div_ceil(4);
assert!(number_of_int_parts <= MAX_DECIMAL_INT_PARTS);
validate_alloc_size(number_of_int_parts * 4, "read_decimal int_parts").unwrap();
// allocation #1, zeroed
let mut magnitude = vec![0u8; number_of_int_parts * 4];
magnitude[..magnitude_len].copy_from_slice(&buf[*pos..*pos + magnitude_len]);
*pos += magnitude_len;
// allocation #2
let int_parts = magnitude
.chunks_exact(4)
.map(|chunk| {
let mut word = [0u8; 4];
word.copy_from_slice(chunk);
i32::from_le_bytes(word)
})
.collect();
Some(DecimalPartsVec {
is_positive,
scale,
precision,
int_parts,
})
}
/// POC branch sync row path. One heap allocation, one copy.
#[inline]
fn decode_poc(buf: &[u8], pos: &mut usize, precision: u8, scale: u8) -> Option<DecimalPartsVec> {
let length = buf[*pos] as usize;
*pos += 1;
if !matches!(length, 0 | 5 | 9 | 13 | 17) {
panic!("invalid decimal length");
}
if length == 0 {
return None;
}
let is_positive = buf[*pos] == 1;
*pos += 1;
let mut int_parts = Vec::with_capacity((length - 1) / 4);
for _ in 0..(length - 1) / 4 {
int_parts.push(LittleEndian::read_i32(&buf[*pos..]));
*pos += 4;
}
Some(DecimalPartsVec {
is_positive,
scale,
precision,
int_parts,
})
}
/// Proposal: stack array plus `u128::from_le_bytes`. No heap, no per-word loop.
#[inline]
fn decode_inline(buf: &[u8], pos: &mut usize, precision: u8, scale: u8) -> Option<DecimalInline> {
let length = buf[*pos] as usize;
*pos += 1;
if length == 0 {
return None;
}
// 5 / 9 / 13 / 17 are the only lengths TDS emits for decimal and numeric.
if !matches!(length, 5 | 9 | 13 | 17) {
panic!("invalid decimal length");
}
let is_positive = buf[*pos] == 1;
*pos += 1;
let magnitude_len = length - 1;
let mut word = [0u8; 16];
word[..magnitude_len].copy_from_slice(&buf[*pos..*pos + magnitude_len]);
*pos += magnitude_len;
Some(DecimalInline {
is_positive,
scale,
precision,
magnitude: u128::from_le_bytes(word),
})
}
// ---------------------------------------------------------------------------
// Format variants
// ---------------------------------------------------------------------------
/// Proposal: single reverse pass into a caller-supplied buffer. No allocation.
#[inline]
fn format_into<'a>(d: &DecimalInline, buf: &'a mut [u8; 48]) -> &'a str {
let mut pos = buf.len();
let mut mag = d.magnitude;
if d.scale > 0 {
for _ in 0..d.scale {
pos -= 1;
buf[pos] = b'0' + (mag % 10) as u8;
mag /= 10;
}
pos -= 1;
buf[pos] = b'.';
}
loop {
pos -= 1;
buf[pos] = b'0' + (mag % 10) as u8;
mag /= 10;
if mag == 0 {
break;
}
}
if !d.is_positive {
pos -= 1;
buf[pos] = b'-';
}
// SAFETY-free: every byte written above is ASCII.
std::str::from_utf8(&buf[pos..]).unwrap()
}
/// Same, but digit generation drops to `u64` when the magnitude fits, which it
/// does for every `decimal(p, s)` with `p <= 19`. `u128 / 10` is a compiler-rt
/// call; `u64 / 10` is a multiply-shift the compiler emits inline.
#[inline]
fn format_into_fast<'a>(d: &DecimalInline, buf: &'a mut [u8; 48]) -> &'a str {
let mut pos = buf.len();
if let Ok(mut mag) = u64::try_from(d.magnitude) {
if d.scale > 0 {
for _ in 0..d.scale {
pos -= 1;
buf[pos] = b'0' + (mag % 10) as u8;
mag /= 10;
}
pos -= 1;
buf[pos] = b'.';
}
loop {
pos -= 1;
buf[pos] = b'0' + (mag % 10) as u8;
mag /= 10;
if mag == 0 {
break;
}
}
} else {
let mut mag = d.magnitude;
if d.scale > 0 {
for _ in 0..d.scale {
pos -= 1;
buf[pos] = b'0' + (mag % 10) as u8;
mag /= 10;
}
pos -= 1;
buf[pos] = b'.';
}
loop {
pos -= 1;
buf[pos] = b'0' + (mag % 10) as u8;
mag /= 10;
if mag == 0 {
break;
}
}
}
if !d.is_positive {
pos -= 1;
buf[pos] = b'-';
}
std::str::from_utf8(&buf[pos..]).unwrap()
}
/// The fix for wide values: one `u128` division by 10^19 splits the magnitude
/// into two `u64` halves, after which every digit is generated with `u64`
/// arithmetic. `u128 % 10` in a loop is a `__udivti3` call per digit, which is
/// why the plain reverse loop above loses to `u128::to_string` at 38 digits.
#[inline]
fn format_into_split<'a>(d: &DecimalInline, buf: &'a mut [u8; 48]) -> &'a str {
const P19: u128 = 10_000_000_000_000_000_000;
let scale = d.scale as usize;
// Three limbs, not two. A u128 reaches 39 digits, so two 19-digit limbs
// cannot cover it: `(magnitude / 10^19) as u64` truncates as soon as the
// quotient exceeds u64::MAX, i.e. magnitude > u64::MAX * 10^19 ~= 1.845e38,
// while u128::MAX is ~3.403e38. A legal decimal(38, s) tops out at 10^38 - 1
// ~= 9.99e37 and never reaches that, but DecimalParts can carry any u128
// (16 bytes of 0xFF off the wire), and the truncation is silent.
let mut lo = (d.magnitude % P19) as u64;
let mut mid = ((d.magnitude / P19) % P19) as u64;
let mut hi = (d.magnitude / (P19 * P19)) as u64;
let mut pos = buf.len();
let mut emitted = 0usize;
let mut taken = 0usize;
loop {
if scale > 0 && emitted == scale {
pos -= 1;
buf[pos] = b'.';
}
// The limbs are positional, so a limb that empties early still has to
// yield zeros until its 19 digit slots are spent.
let digit = if taken < 19 {
let d = (lo % 10) as u8;
lo /= 10;
d
} else if taken < 38 {
let d = (mid % 10) as u8;
mid /= 10;
d
} else {
let d = (hi % 10) as u8;
hi /= 10;
d
};
taken += 1;
pos -= 1;
buf[pos] = b'0' + digit;
emitted += 1;
if lo == 0 && mid == 0 && hi == 0 && emitted > scale {
break;
}
}
if !d.is_positive {
pos -= 1;
buf[pos] = b'-';
}
std::str::from_utf8(&buf[pos..]).unwrap()
}
// ---------------------------------------------------------------------------
// Binary export: PostgreSQL `numeric` base-10000 digits
// ---------------------------------------------------------------------------
/// PostgreSQL stores `numeric` as base-10000 `int16` digits. Producing them
/// directly skips the decimal string and the server-side `numeric_in` parse.
///
/// Returns `(ndigits, weight)`; digits are most-significant first in `out`.
#[inline]
fn to_base10000(d: &DecimalInline, out: &mut [i16; 12]) -> (usize, i32) {
let mut mag = d.magnitude;
let mut tmp = [0i16; 12];
let mut n = 0usize;
while mag > 0 {
tmp[n] = (mag % 10_000) as i16;
mag /= 10_000;
n += 1;
}
if n == 0 {
return (0, 0);
}
// Align the fractional part onto a base-10000 boundary.
//
// This scales the *digit array* with carry propagation rather than
// multiplying the u128 magnitude by 10^pad up front. That ordering is not
// cosmetic: for `decimal(38, s)` with `s % 4 != 0`, the magnitude can reach
// 10^38 - 1, and 10^38 * 100 does not fit in a u128 (max ~3.40e38). Doing
// the multiply on the magnitude wraps silently in release builds and
// corrupts the value. Post-conversion the largest per-digit intermediate is
// 9999 * 1000 + carry, which fits in a u32.
let pad = (4 - (d.scale % 4)) % 4;
if pad != 0 {
let multiplier = 10u32.pow(pad as u32);
let mut carry = 0u32;
for digit in &mut tmp[..n] {
let product = *digit as u32 * multiplier + carry;
*digit = (product % 10_000) as i16;
carry = product / 10_000;
}
if carry != 0 {
tmp[n] = carry as i16;
n += 1;
}
}
let frac_digits = ((d.scale as usize + pad as usize) / 4) as i32;
// tmp is least-significant first. Strip the low-order zero digits the way
// PostgreSQL does.
let mut lo = 0usize;
while lo < n && tmp[lo] == 0 {
lo += 1;
}
let weight = (n as i32 - 1) - frac_digits;
let ndigits = n - lo;
for i in 0..ndigits {
out[i] = tmp[n - 1 - i];
}
(ndigits, weight)
}
/// Renders base-10000 digits back to a decimal string.
///
/// Used only by [`check`], to validate the encoding without reconstructing the
/// magnitude — reconstruction would hit the same u128 overflow the encoder has
/// to avoid, so it cannot be the oracle.
fn base10000_to_string(
digits: &[i16],
ndigits: usize,
weight: i32,
is_positive: bool,
scale: u8,
) -> String {
let mut s = String::new();
if !is_positive && ndigits != 0 {
s.push('-');
}
if weight < 0 {
s.push('0');
} else {
for i in 0..=weight {
let d = if (i as usize) < ndigits {
digits[i as usize]
} else {
0
};
if i == 0 {
s.push_str(&d.to_string());
} else {
s.push_str(&format!("{d:04}"));
}
}
}
if scale > 0 {
s.push('.');
let mut frac = String::new();
for j in 1..=(scale as usize).div_ceil(4) {
let idx = weight + j as i32;
let d = if idx >= 0 && (idx as usize) < ndigits {
digits[idx as usize]
} else {
0
};
frac.push_str(&format!("{d:04}"));
}
frac.truncate(scale as usize);
s.push_str(&frac);
}
s
}
// ---------------------------------------------------------------------------
// Workload
// ---------------------------------------------------------------------------
/// One decoded column: TDS wire bytes plus its declared precision and scale.
struct Column {
precision: u8,
scale: u8,
}
fn encode(buf: &mut Vec<u8>, magnitude: u128, is_positive: bool, precision: u8) {
let width = match precision {
1..=9 => 4usize,
10..=19 => 8,
20..=28 => 12,
_ => 16,
};
buf.push((width + 1) as u8);
buf.push(if is_positive { 1 } else { 0 });
let le = magnitude.to_le_bytes();
buf.extend_from_slice(&le[..width]);
}
/// The four `numeric` columns of the FDW regression workload:
///
/// ```sql
/// CAST((n % 10000000) * 0.01 AS decimal(15,2)) AS n1,
/// CAST((n % 1000000) * -0.01 AS decimal(15,2)) AS n2,
/// CAST((n % 100000) * 0.001 AS decimal(18,3)) AS n3,
/// CAST((n % 10000) * 0.0001 AS decimal(19,4)) AS n4
/// ```
fn workload_fdw(rows: u128) -> (Vec<u8>, Vec<Column>) {
let mut buf = Vec::new();
let mut cols = Vec::new();
for n in 1..=rows {
encode(&mut buf, n % 10_000_000, true, 15);
encode(&mut buf, n % 1_000_000, false, 15);
encode(&mut buf, n % 100_000, true, 18);
encode(&mut buf, n % 10_000, true, 19);
}
for _ in 0..rows {
cols.push(Column {
precision: 15,
scale: 2,
});
cols.push(Column {
precision: 15,
scale: 2,
});
cols.push(Column {
precision: 18,
scale: 3,
});
cols.push(Column {
precision: 19,
scale: 4,
});
}
(buf, cols)
}
/// A wide workload that exercises the four-word path: `decimal(38,10)`.
///
/// The magnitude sits just under 10^38, the true maximum for precision 38, and
/// the scale is deliberately not a multiple of 4. That combination is what
/// catches a base-10000 encoder that pre-multiplies the magnitude by 10^pad:
/// 10^38 * 100 overflows u128 and wraps silently in release builds.
fn workload_wide(rows: u128) -> (Vec<u8>, Vec<Column>) {
let mut buf = Vec::new();
let mut cols = Vec::new();
for n in 1..=rows {
let mag = 99_999_999_999_999_999_999_999_999_999_999_999_999u128 - n;
encode(&mut buf, mag, n % 2 == 0, 38);
cols.push(Column {
precision: 38,
scale: 10,
});
}
(buf, cols)
}
// ---------------------------------------------------------------------------
// Correctness: every variant must agree before any of them is timed.
// ---------------------------------------------------------------------------
fn check(buf: &[u8], cols: &[Column]) {
let (mut a, mut b, mut c) = (0usize, 0usize, 0usize);
for col in cols {
let head = decode_head(buf, &mut a, col.precision, col.scale).unwrap();
let poc = decode_poc(buf, &mut b, col.precision, col.scale).unwrap();
let inl = decode_inline(buf, &mut c, col.precision, col.scale).unwrap();
assert_eq!(head, poc, "head and poc disagree");
assert_eq!(
head.magnitude().unwrap(),
inl.magnitude,
"inline magnitude disagrees"
);
let expected = head.to_decimal_string();
let mut sbuf = [0u8; 48];
assert_eq!(expected, format_into(&inl, &mut sbuf), "format_into");
let mut fbuf = [0u8; 48];
assert_eq!(
expected,
format_into_fast(&inl, &mut fbuf),
"format_into_fast"
);
let mut pbuf = [0u8; 48];
assert_eq!(
expected,
format_into_split(&inl, &mut pbuf),
"format_into_split"
);
// Validate the base-10000 encoding by rendering it back to a decimal
// string. An arithmetic round trip would need `magnitude * 10^pad`,
// which is exactly the overflow the encoder exists to avoid, so it
// cannot serve as the oracle here.
let mut digits = [0i16; 12];
let (ndigits, weight) = to_base10000(&inl, &mut digits);
assert_eq!(
expected,
base10000_to_string(&digits, ndigits, weight, inl.is_positive, inl.scale),
"base10000 encoding"
);
}
assert_eq!(a, buf.len());
assert_eq!(b, buf.len());
assert_eq!(c, buf.len());
}
// ---------------------------------------------------------------------------
// Adversarial corpus: the workload-shaped corpus above cannot catch a limb
// decomposition bug, because a legal decimal(38, s) tops out at 10^38 - 1 and
// never reaches the point where a two-limb split truncates. An earlier revision
// of `format_into_split` computed `(magnitude / 10^19) as u64`, which silently
// truncates once the quotient exceeds u64::MAX -- that is magnitude greater
// than u64::MAX * 10^19 ~= 1.845e38, against a u128::MAX of ~3.403e38 -- and
// rendered u128::MAX as 155814926183842947303374607431768211455. Every kernel
// here accepts an arbitrary u128, so the corpus has to cover one.
// ---------------------------------------------------------------------------
/// Renders via `u128::to_string()` and an index splice. This shares no
/// arithmetic with any kernel under test, which is the only reason the
/// truncation above was observable: an oracle that reused the same limb
/// decomposition would have been wrong in exactly the same way.
fn oracle(d: &DecimalInline) -> String {
let digits = d.magnitude.to_string();
let scale = d.scale as usize;
let body = if scale == 0 {
digits
} else {
let padded = if digits.len() <= scale {
format!("{}{}", "0".repeat(scale - digits.len() + 1), digits)
} else {
digits
};
let split = padded.len() - scale;
format!("{}.{}", &padded[..split], &padded[split..])
};
if d.is_positive {
body
} else {
format!("-{body}")
}
}
fn check_adversarial() {
const P19: u128 = 10_000_000_000_000_000_000;
let mut mags: Vec<u128> = vec![
0,
1,
u128::MAX,
u64::MAX as u128,
u64::MAX as u128 + 1,
// The exact two-limb cutoff and its immediate neighbours.
u64::MAX as u128 * P19,
u64::MAX as u128 * P19 + P19 - 1,
u64::MAX as u128 * P19 + P19,
// Limb boundaries.
P19 - 1,
P19,
P19 + 1,
P19 * P19 - 1,
P19 * P19,
P19 * P19 + 1,
];
for k in 0..=38u32 {
mags.push(10u128.pow(k));
mags.push(10u128.pow(k) - 1);
}
// Deterministic xorshift, so a failure is reproducible without a dep.
let mut state: u128 = 0x2545_F491_4F6C_DD1D;
for _ in 0..20_000 {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
mags.push(state);
}
let mut n = 0usize;
for &magnitude in &mags {
for scale in [0u8, 1, 2, 6, 10, 19, 20, 37, 38] {
for is_positive in [true, false] {
let d = DecimalInline {
is_positive,
scale,
precision: 38,
magnitude,
};
let expected = oracle(&d);
let mut b1 = [0u8; 48];
assert_eq!(expected, format_into(&d, &mut b1), "format_into {d:?}");
let mut b2 = [0u8; 48];
assert_eq!(
expected,
format_into_fast(&d, &mut b2),
"format_into_fast {d:?}"
);
let mut b3 = [0u8; 48];
assert_eq!(
expected,
format_into_split(&d, &mut b3),
"format_into_split {d:?}"
);
n += 1;
}
}
}
eprintln!(
"adversarial corpus: {n} cases across {} magnitudes",
mags.len()
);
}
// ---------------------------------------------------------------------------
// Benchmarks
// ---------------------------------------------------------------------------
const ROWS: u128 = 4096;
fn bench_decode(c: &mut Criterion) {
for (name, (buf, cols)) in [("fdw", workload_fdw(ROWS)), ("wide", workload_wide(ROWS))] {
check(&buf, &cols);
check_adversarial();
let mut group = c.benchmark_group(format!("decode/{name}"));
group.throughput(Throughput::Elements(cols.len() as u64));
group.bench_function(BenchmarkId::new("head", "pr311"), |b| {
b.iter(|| {
let mut pos = 0usize;
for col in &cols {
black_box(decode_head(
black_box(&buf),
&mut pos,
col.precision,
col.scale,
));
}
})
});
group.bench_function(BenchmarkId::new("poc", "sync_row"), |b| {
b.iter(|| {
let mut pos = 0usize;
for col in &cols {
black_box(decode_poc(
black_box(&buf),
&mut pos,
col.precision,
col.scale,
));
}
})
});
group.bench_function(BenchmarkId::new("inline", "proposed"), |b| {
b.iter(|| {
let mut pos = 0usize;
for col in &cols {
black_box(decode_inline(
black_box(&buf),
&mut pos,
col.precision,
col.scale,
));
}
})
});
group.finish();
}
}
fn bench_format(c: &mut Criterion) {
for (name, (buf, cols)) in [("fdw", workload_fdw(ROWS)), ("wide", workload_wide(ROWS))] {
// Pre-decode so the group measures formatting only.
let mut vecs = Vec::new();
let mut inls = Vec::new();
let mut pos = 0usize;
let mut pos2 = 0usize;
for col in &cols {
vecs.push(decode_head(&buf, &mut pos, col.precision, col.scale).unwrap());
inls.push(decode_inline(&buf, &mut pos2, col.precision, col.scale).unwrap());
}
let mut group = c.benchmark_group(format!("format/{name}"));
group.throughput(Throughput::Elements(cols.len() as u64));
group.bench_function(BenchmarkId::new("to_decimal_string", "current"), |b| {
b.iter(|| {
for d in &vecs {
black_box(black_box(d).to_decimal_string());
}
})
});
group.bench_function(BenchmarkId::new("format_into", "proposed"), |b| {
b.iter(|| {
let mut sbuf = [0u8; 48];
for d in &inls {
black_box(format_into(black_box(d), &mut sbuf));
}
})
});
group.bench_function(BenchmarkId::new("format_into_fast", "proposed_u64"), |b| {
b.iter(|| {
let mut sbuf = [0u8; 48];
for d in &inls {
black_box(format_into_fast(black_box(d), &mut sbuf));
}
})
});
group.bench_function(
BenchmarkId::new("format_into_split", "proposed_split"),
|b| {
b.iter(|| {
let mut sbuf = [0u8; 48];
for d in &inls {
black_box(format_into_split(black_box(d), &mut sbuf));
}
})
},
);
group.bench_function(BenchmarkId::new("to_base10000", "pg_binary"), |b| {
b.iter(|| {
let mut digits = [0i16; 12];
for d in &inls {
black_box(to_base10000(black_box(d), &mut digits));
}
})
});
group.finish();
}
}
/// What a consumer actually pays per value, end to end.
fn bench_end_to_end(c: &mut Criterion) {
let (buf, cols) = workload_fdw(ROWS);
check(&buf, &cols);
let mut group = c.benchmark_group("end_to_end/fdw");
group.throughput(Throughput::Elements(cols.len() as u64));
group.bench_function("pr311_decode_plus_text", |b| {
b.iter(|| {
let mut pos = 0usize;
for col in &cols {
let d = decode_head(black_box(&buf), &mut pos, col.precision, col.scale).unwrap();
black_box(d.to_decimal_string());
}
})
});
group.bench_function("poc_decode_plus_text", |b| {
b.iter(|| {
let mut pos = 0usize;
for col in &cols {
let d = decode_poc(black_box(&buf), &mut pos, col.precision, col.scale).unwrap();
black_box(d.to_decimal_string());
}
})
});
group.bench_function("proposed_decode_plus_text", |b| {
b.iter(|| {
let mut pos = 0usize;
let mut sbuf = [0u8; 48];
for col in &cols {
let d = decode_inline(black_box(&buf), &mut pos, col.precision, col.scale).unwrap();
black_box(format_into_split(&d, &mut sbuf));
}
})
});
group.bench_function("proposed_decode_plus_pg_binary", |b| {
b.iter(|| {
let mut pos = 0usize;
let mut digits = [0i16; 12];
for col in &cols {
let d = decode_inline(black_box(&buf), &mut pos, col.precision, col.scale).unwrap();
black_box(to_base10000(&d, &mut digits));
}
})
});
group.finish();
}
criterion_group!(benches, bench_decode, bench_format, bench_end_to_end);
criterion_main!(benches);
Compiling proc-macro2 v1.0.107
Compiling quote v1.0.47
Compiling unicode-ident v1.0.24
Compiling crossbeam-utils v0.8.22
Compiling serde_core v1.0.229
Compiling crossbeam-epoch v0.9.20
Compiling zerocopy v0.8.56
Compiling crossbeam-deque v0.8.7
Compiling zmij v1.0.23
Compiling serde v1.0.229
Compiling autocfg v1.5.1
Compiling serde_json v1.0.151
Compiling cfg-if v1.0.4
Compiling either v1.17.0
Compiling rayon-core v1.13.0
Compiling libc v0.2.189
Compiling ciborium-io v0.2.2
Compiling regex-syntax v0.8.11
Compiling clap_lex v1.1.0
Compiling itoa v1.0.18
Compiling anstyle v1.0.14
Compiling memchr v2.8.3
Compiling plotters-backend v0.3.7
Compiling same-file v1.0.6
Compiling cast v0.3.0
Compiling once_cell v1.21.4
Compiling byteorder v1.5.0
Compiling oorandom v11.1.5
Compiling anes v0.1.6
Compiling walkdir v2.5.0
Compiling clap_builder v4.6.6
Compiling itertools v0.10.5
Compiling plotters-svg v0.3.7
Compiling tds-numeric-bench v0.1.0 (/home/saurabh/.copilot/session-state/d3fe1c57-00e0-4a99-bf66-2fc5285ca885/files/numeric-gist)
Compiling num-traits v0.2.19
Compiling syn v2.0.119
Compiling syn v3.0.3
Compiling regex-automata v0.4.18
Compiling criterion-plot v0.5.0
Compiling plotters v0.3.7
Compiling rayon v1.12.0
Compiling is-terminal v0.4.17
Compiling clap v4.6.6
Compiling serde_derive v1.0.229
Compiling regex v1.13.1
Compiling zerocopy-derive v0.8.56
Compiling tinytemplate v1.2.1
Compiling half v2.7.1
Compiling ciborium-ll v0.2.2
Compiling ciborium v0.2.2
Compiling criterion v0.5.1
Finished `bench` profile [optimized] target(s) in 46.38s
Running unittests src/lib.rs (target/release/deps/tds_numeric_bench-bc4e4e54b5bc3875)
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running benches/numeric_decode.rs (target/release/deps/numeric_decode-434e6ee35777a4fc)
decode/fdw/head/pr311 time: [566.50 µs 567.00 µs 567.56 µs]
thrpt: [28.867 Melem/s 28.896 Melem/s 28.921 Melem/s]
Found 17 outliers among 100 measurements (17.00%)
3 (3.00%) low severe
5 (5.00%) low mild
3 (3.00%) high mild
6 (6.00%) high severe
decode/fdw/poc/sync_row time: [405.70 µs 406.07 µs 406.58 µs]
thrpt: [40.297 Melem/s 40.348 Melem/s 40.384 Melem/s]
Found 5 outliers among 100 measurements (5.00%)
1 (1.00%) low severe
1 (1.00%) low mild
3 (3.00%) high severe
decode/fdw/inline/proposed
time: [217.34 µs 217.48 µs 217.63 µs]
thrpt: [75.285 Melem/s 75.336 Melem/s 75.384 Melem/s]
Found 10 outliers among 100 measurements (10.00%)
1 (1.00%) low severe
1 (1.00%) low mild
6 (6.00%) high mild
2 (2.00%) high severe
decode/wide/head/pr311 time: [162.76 µs 163.00 µs 163.27 µs]
thrpt: [25.087 Melem/s 25.129 Melem/s 25.165 Melem/s]
Found 16 outliers among 100 measurements (16.00%)
2 (2.00%) low severe
4 (4.00%) low mild
7 (7.00%) high mild
3 (3.00%) high severe
decode/wide/poc/sync_row
time: [101.72 µs 101.87 µs 102.04 µs]
thrpt: [40.143 Melem/s 40.208 Melem/s 40.267 Melem/s]
Found 13 outliers among 100 measurements (13.00%)
1 (1.00%) low severe
1 (1.00%) low mild
5 (5.00%) high mild
6 (6.00%) high severe
decode/wide/inline/proposed
time: [41.952 µs 41.977 µs 42.005 µs]
thrpt: [97.511 Melem/s 97.577 Melem/s 97.636 Melem/s]
Found 12 outliers among 100 measurements (12.00%)
2 (2.00%) low severe
1 (1.00%) low mild
7 (7.00%) high mild
2 (2.00%) high severe
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 6.8s, enable flat sampling, or reduce sample count to 60.
format/fdw/to_decimal_string/current
time: [1.3306 ms 1.3318 ms 1.3330 ms]
thrpt: [12.291 Melem/s 12.302 Melem/s 12.313 Melem/s]
Found 4 outliers among 100 measurements (4.00%)
4 (4.00%) high mild
format/fdw/format_into/proposed
time: [344.80 µs 345.02 µs 345.29 µs]
thrpt: [47.450 Melem/s 47.487 Melem/s 47.518 Melem/s]
Found 9 outliers among 100 measurements (9.00%)
1 (1.00%) low severe
3 (3.00%) low mild
3 (3.00%) high mild
2 (2.00%) high severe
format/fdw/format_into_fast/proposed_u64
time: [196.50 µs 196.74 µs 197.08 µs]
thrpt: [83.133 Melem/s 83.276 Melem/s 83.377 Melem/s]
Found 7 outliers among 100 measurements (7.00%)
1 (1.00%) low mild
2 (2.00%) high mild
4 (4.00%) high severe
format/fdw/format_into_split/proposed_split
time: [237.80 µs 238.15 µs 238.48 µs]
thrpt: [68.700 Melem/s 68.796 Melem/s 68.899 Melem/s]
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) low mild
format/fdw/to_base10000/pg_binary
time: [128.28 µs 128.52 µs 128.95 µs]
thrpt: [127.06 Melem/s 127.48 Melem/s 127.72 Melem/s]
Found 11 outliers among 100 measurements (11.00%)
4 (4.00%) low severe
1 (1.00%) low mild
4 (4.00%) high mild
2 (2.00%) high severe
format/wide/to_decimal_string/current
time: [651.71 µs 653.96 µs 658.50 µs]
thrpt: [6.2202 Melem/s 6.2634 Melem/s 6.2850 Melem/s]
Found 8 outliers among 100 measurements (8.00%)
5 (5.00%) high mild
3 (3.00%) high severe
format/wide/format_into/proposed
time: [899.13 µs 899.57 µs 900.11 µs]
thrpt: [4.5506 Melem/s 4.5533 Melem/s 4.5555 Melem/s]
Found 9 outliers among 100 measurements (9.00%)
1 (1.00%) low severe
1 (1.00%) low mild
1 (1.00%) high mild
6 (6.00%) high severe
format/wide/format_into_fast/proposed_u64
time: [909.79 µs 910.47 µs 911.31 µs]
thrpt: [4.4946 Melem/s 4.4988 Melem/s 4.5021 Melem/s]
Found 10 outliers among 100 measurements (10.00%)
2 (2.00%) low severe
1 (1.00%) low mild
4 (4.00%) high mild
3 (3.00%) high severe
format/wide/format_into_split/proposed_split
time: [400.48 µs 400.78 µs 401.08 µs]
thrpt: [10.213 Melem/s 10.220 Melem/s 10.228 Melem/s]
Found 5 outliers among 100 measurements (5.00%)
1 (1.00%) low mild
3 (3.00%) high mild
1 (1.00%) high severe
format/wide/to_base10000/pg_binary
time: [437.85 µs 438.12 µs 438.39 µs]
thrpt: [9.3433 Melem/s 9.3491 Melem/s 9.3548 Melem/s]
Found 3 outliers among 100 measurements (3.00%)
1 (1.00%) low mild
1 (1.00%) high mild
1 (1.00%) high severe
end_to_end/fdw/pr311_decode_plus_text
time: [2.0406 ms 2.0430 ms 2.0457 ms]
thrpt: [8.0090 Melem/s 8.0195 Melem/s 8.0292 Melem/s]
Found 7 outliers among 100 measurements (7.00%)
5 (5.00%) high mild
2 (2.00%) high severe
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 8.9s, enable flat sampling, or reduce sample count to 50.
end_to_end/fdw/poc_decode_plus_text
time: [1.7629 ms 1.7684 ms 1.7760 ms]
thrpt: [9.2255 Melem/s 9.2646 Melem/s 9.2936 Melem/s]
Found 10 outliers among 100 measurements (10.00%)
1 (1.00%) low severe
5 (5.00%) high mild
4 (4.00%) high severe
end_to_end/fdw/proposed_decode_plus_text
time: [388.48 µs 388.72 µs 389.02 µs]
thrpt: [42.116 Melem/s 42.148 Melem/s 42.175 Melem/s]
Found 10 outliers among 100 measurements (10.00%)
1 (1.00%) low severe
2 (2.00%) low mild
1 (1.00%) high mild
6 (6.00%) high severe
end_to_end/fdw/proposed_decode_plus_pg_binary
time: [186.66 µs 187.26 µs 187.87 µs]
thrpt: [87.210 Melem/s 87.494 Melem/s 87.774 Melem/s]
Found 14 outliers among 100 measurements (14.00%)
7 (7.00%) low severe
6 (6.00%) low mild
1 (1.00%) high mild
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment