Skip to content

Speed up f-string float formatting #2195

Description

@scoder

There is dedicated code for formatting integer literals in f-strings, which avoids converting C integers to Python integer objects just for formatting them as strings. It is used by the FormattedValueNode in ExprNodes.py.

The same should be done for C float/double values, as Python provides PyOS_double_to_string(). See CPython's own float formatting in unicodeobject.c and formatter_unicode.c.

  • implement relevant tests in tests/run/fstring.pyx and/or check if tests/run/test_fstring.pyx (copied from CPython's test suite) covers this sufficiently
  • extract the relevant parts of CPython's formatting implementation (potentially limiting it to easy to support formats)
  • create a new C utility function in TypeConversion.c that formats double values
  • implement CFloatType.can_coerce_to_pystring() in PyrexTypes.py to make it return True when supported
  • implement CFloatType.convert_to_pystring() to call the formatting C function.

This can be done incrementally for selected formatting modifiers, e.g. a format like f"{x: .2f}" should be fairly easy to support and covers a large part of the usages.

Hint: this ticket requires good C skills.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions