Skip to main content

decode_packed_decimal_to_string_with_scratch

Function decode_packed_decimal_to_string_with_scratch 

Source
pub fn decode_packed_decimal_to_string_with_scratch(
    data: &[u8],
    digits: u16,
    scale: i16,
    signed: bool,
    scratch: &mut ScratchBuffers,
) -> Result<String>
Expand description

Decode a packed decimal (COMP-3) directly to a String, bypassing the intermediate SmallDecimal allocation.

This is a critical performance optimization for COMP-3 JSON conversion. By decoding nibbles and formatting the result in a single pass using the caller-owned scratch buffer, it avoids the SmallDecimal -> String allocation overhead that caused 94-96% throughput regression in COMP-3 processing benchmarks.

§Arguments

  • data - Raw byte data containing the packed decimal (BCD with trailing sign nibble)
  • digits - Number of decimal digits in the field (1-18)
  • scale - Number of implied decimal places (can be negative for scaling)
  • signed - Whether the field is signed (true) or unsigned (false)
  • scratch - Reusable scratch buffers; the string_buffer is consumed via std::mem::take and returned as the result string.

§Returns

The decoded value formatted as a string (e.g. "123", "-45.67", "0").

§Errors

  • CBKD401_COMP3_INVALID_NIBBLE - if any data nibble is > 9 or the sign nibble is invalid

§Performance

Includes a fast path for single-digit packed decimals (1 byte) and falls back to decode_packed_decimal_with_scratch plus SmallDecimal::format_to_scratch_buffer for larger values.

§See Also