Skip to content

Speed up f-string float formatting #2195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
scoder opened this issue Mar 31, 2018 · 2 comments
Open

Speed up f-string float formatting #2195

scoder opened this issue Mar 31, 2018 · 2 comments

Comments

@scoder
Copy link
Contributor

scoder commented Mar 31, 2018

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.

@AMMAR-62
Copy link

Hello is someone working on this issue??

@scoder
Copy link
Contributor Author

scoder commented Oct 12, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants