Skip to content

Possible shortcut for format_exact_opt in Grisu3 #110129

Closed
@mazong1123

Description

@mazong1123

In the .NET implementation of Grisu3, there's a shortcut to jump out earlier when the fractionals or integrals cannot meet the determination of requested digits. This significantly improved the performance of converting floating number to string as it falls back even without starting trying the Grisu3 algorithm. This was originally added in this PR, and shipped along with all later .NET versions so it's proved working.

Below is the code snippet from current .NET repo:

                // We deviate from the original algorithm here and do some early checks to determine if we can satisfy requestedDigits.
                // If we determine that we can't, we exit early and avoid most of the heavy lifting that the algorithm otherwise does.
                //
                // When fractionals is zero, we can easily determine if integrals can satisfy requested digits:
                //      If requestedDigits >= 11, integrals is not able to exhaust the count by itself since 10^(11 -1) > uint.MaxValue >= integrals.
                //      If integrals < 10^(requestedDigits - 1), integrals cannot exhaust the count.
                //      Otherwise, integrals might be able to exhaust the count and we need to execute the rest of the code.
                if ((fractionals == 0) && ((requestedDigits >= 11) || (integrals < SmallPowersOfTen[requestedDigits - 1])))
                {
                    Debug.Assert(buffer[0] == '\0');
                    length = 0;
                    kappa = 0;
                    return false;
                }

I'm wondering do we consider apply the same shortcut just after vint and vfrac extracted at line 489 of grisu.rs?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-floating-pointArea: Floating point numbers and arithmeticC-enhancementCategory: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions