Skip to content

bigint: to_string_radix is O(n²) for non-power-of-two radixes other than 10 #3827

Description

@bobzhang

bigint/bigint_nonjs.mbt:1101-1133: radix 10 and power-of-two radixes have optimized paths, but other radixes (3, 5, 6, 7, ...) extract digits by repeatedly calling grade_school_div(v, base) — each O(n) — once per output digit (~O(n) digits), i.e. O(n²) overall.

Standard fix: divide by base^k per step, where base^k is the largest power fitting in a limb, extracting k digits per full-bigint division — cutting the number of O(n) divisions by a factor of ~k (e.g. k=20 for radix 3 with 32-bit limbs, k=12 for radix 6).

Low urgency (odd radixes are rare) but the fix is mechanical and the surrounding radix-10 code (decimal_mask batching at line ~1019) already demonstrates the pattern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions