Why Formatting Large Numbers Efficiently Matters
In database engines and numerical computing, converting large numeric types to their string representations is a frequent operation — whether for output, logging, or serialization. For types like HUGEINT (128-bit integers), UHUGEINT (unsigned 128-bit integers), and DECIMAL(38) (high-precision decimals), this conversion can be surprisingly expensive. Inefficient formatting can introduce significant latency, especially when dealing with bulk data or large-scale analytical queries.
This article explores a recent optimization in DuckDB that dramatically speeds up formatting these large numeric types by rethinking how division is used during string conversion.
The Problem: Costly 128-bit Division per Digit
Before this change, DuckDB’s formatting logic for HUGEINT, UHUGEINT, and DECIMAL(38) values converted numbers to strings by extracting digits one at a time. The core of this process involved a 128-bit division for every single digit extracted from the number.
Why is this a problem? 128-bit division is a relatively slow operation on most CPUs compared to 64-bit division. Doing it once per digit — and potentially dozens of times for large numbers — creates a performance bottleneck. For example, a 38-digit decimal number might require 38 separate 128-bit divisions. This naive approach led to noticeable delays when converting large values to strings, impacting query performance.
How the Formatting Works: Digit Extraction via Division
Converting an integer to a decimal string typically involves repeatedly dividing the number by the base (10) and collecting remainders as digits. For 64-bit integers, this is straightforward and fast. But for 128-bit integers, division is more complex because hardware support is limited or nonexistent, so software routines perform multi-word division.
In the original implementation, each digit extraction performed a full 128-bit division by 10, which is expensive. This per-digit division approach scales linearly with the number of digits.
The Optimization: Batch Processing 19 Digits at a Time
The key insight of the PR was to reduce the number of division operations by processing multiple digits in a batch instead of one at a time. Specifically, the updated code extracts up to 19 digits at once by performing a single 128-bit by 64-bit division.
Why 19 digits? Because 10^19 fits within a 64-bit unsigned integer, allowing the algorithm to divide the large 128-bit number by 10^19 in one step. This yields a quotient and a remainder, where the remainder contains the last 19 digits of the number.
This method drastically cuts down the number of divisions needed. Instead of one division per digit, it requires roughly one division per 19 digits. For a 38-digit number, that reduces divisions from 38 to just 2.
Technical Details of the Implementation
The updated formatting function:\n
- Defines a constant divisor as 10^19 (a 64-bit integer).
- Performs a single 128-bit by 64-bit division to get the quotient and remainder.
- Converts the remainder (which fits in 64 bits) to a string of up to 19 digits.
- Repeats the process with the quotient until the quotient is zero.
This approach leverages the fact that dividing a 128-bit integer by a 64-bit integer is much cheaper than performing 128-bit by 128-bit division repeatedly.
An illustrative example: if the number is N, then
while N > 0:
quotient, remainder = divmod_128_by_64(N, 10^19)
output_digits.append(format_64bit_number_as_string(remainder))
N = quotient
The output digits are then combined in reverse order to form the final string.
Impact and Takeaway
This optimization leads to a significant speedup in formatting HUGEINT, UHUGEINT, and DECIMAL(38) values as strings. By batching digit extraction and minimizing expensive division operations, the code reduces CPU cycles and improves overall query responsiveness.
The broader lesson is that when working with large numeric types, the granularity of division operations matters. Naive per-digit division is straightforward but inefficient, while batching digits using wider division steps can yield substantial performance gains.
This approach can be applied in other systems handling big integers or high-precision decimals, especially where string conversion performance is critical.
Credits
This article is based on the optimization introduced in the DuckDB repository pull request titled "Speed up formatting HUGEINT, UHUGEINT and DECIMAL(38) as strings."
Read the full breakdown on nossletter.tech.
Originally published on Nossletter.
Top comments (0)