Skip to content

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Oct 7, 2025

  • Move Python/formatter_unicode.c to Objects/unicode_formatter.c.
  • Move Objects/stringlib/localeutil.h content into unicode_formatter.c. Remove localeutil.h.
  • Move _PyUnicode_InsertThousandsGrouping() to unicode_formatter.c and mark the function as static.
  • Rename unicode_fill() to _PyUnicode_Fill() and export it in pycore_unicodeobject.h.

* Move Python/formatter_unicode.c to Objects/unicode_formatter.c.
* Move Objects/stringlib/localeutil.h content into
  unicode_formatter.c. Remove localeutil.h.
* Move _PyUnicode_InsertThousandsGrouping() to unicode_formatter.c
  and mark the function as static.
* Rename unicode_fill() to _PyUnicode_Fill() and export it in
  pycore_unicodeobject.h.
@vstinner
Copy link
Member Author

vstinner commented Oct 7, 2025

@serhiy-storchaka: Is it what you asked for?

@vstinner vstinner marked this pull request as ready for review October 7, 2025 23:21
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, lets go.

Please check that the formatting code that uses _PyUnicode_InsertThousandsGrouping() does not struggle due to non-inlined _PyUnicode_Fill().

Comment on lines 424 to 426
void
_PyUnicode_Fill(int kind, void *data, Py_UCS4 value,
Py_ssize_t start, Py_ssize_t length)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that it will be inlined? Maybe keep static inline unicode_fill() and add a wrapper _PyUnicode_Fill()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe keep static inline unicode_fill() and add a wrapper _PyUnicode_Fill()?

Ok, I implemented _PyUnicode_Fill() as a static inline function. So the compiler can decide to inline it or not.

@vstinner
Copy link
Member Author

vstinner commented Oct 8, 2025

Please check that the formatting code that uses _PyUnicode_InsertThousandsGrouping() does not struggle due to non-inlined _PyUnicode_Fill().

I checked the Python 3.14 executable on Fedora which is built with PGO+LTO. I looked at _PyUnicode_InsertThousandsGrouping() x86-64 assembly code in gdb. I found:

...
   0x00007ffff7abe483 <+339>:	call   0x7ffff7abe700 <unicode_fill.lto_priv.0>
...
   0x00007ffff7abe665 <+821>:	call   0x7ffff7abe700 <unicode_fill.lto_priv.0>
...

So in the Fedora package at least, unicode_fill() is not inlined in _PyUnicode_InsertThousandsGrouping(). Inlining is not always a good idea, it can be bad for the L1-instruction cache and puts more pressure on register allocation. I trust the compiler to decide if it's a good idea to inline or not.

Move MAX_UNICODE to pycore_unicodeobject.h as _Py_MAX_UNICODE.
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I did not suggest to make it inline. I only asked if this makes noticeable difference. Locale specific formatting is complex, so I think it would not, even if it was inlined previously, but we should check.

Anyway, LGTM. 👍

@vstinner vstinner merged commit 3d3f126 into python:main Oct 8, 2025
43 checks passed
@vstinner vstinner deleted the unicode_formatter branch October 8, 2025 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants