Optimize 128-bit decimal formatting

Open
#5,006 3 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 9 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Active
Tech stack
cpp
Domain
performance

Research direction

Start by locating count_digits(native_uint128) and do_format_decimal, then trace how the 64-bit formatter returns the start of its output. Check the existing tests and benchmarks for 128-bit decimal formatting before changing the division path. Done means the proposed operations are removed or replaced without changing formatting results, and benchmarks confirm the performance impact.

Written by the indexing model from the issue text.

Description

Hi..

current 128-bit decimal formatting path uses division and modulo by 10^19 to reduce values into 64-bit chunks........ and GCC emits __udivti3 and __udivmodti4 calls for these operations.

During this i found two probably redundant operations - please correct me if i'm wrong or missed a point to review here :

  • count_digits(native_uint128) performs a second 128-bit division when the result is already known to have 39 digits.
  • do_format_decimal counts the remaining digits again, although the 64-bit formatter already returns the start of the formatted output.

so can we remove these redundant operations and replacing division by 10^19 with an exact three-multiply reciprocal implementation .... 10^19 = 2^19 * 5^19.

anyway in my testing GCC (12.2.0)benchmarks show a 14.6% improvement in fmt::format_to for uniformly distributed 128-bit values and 24.8% for the upper 39-digit range.

Dominant language
C++
Stars
25.9k
Forks
3.1k
Avg merge
9d 5h
Merged PRs (30d)
13

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from fmtlib/fmt

All issues in fmtlib/fmt

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.