Skip to content

unicode: make _PyUnicode_FromId thread-safe in --disable-gil builds #111971

Description

@colesbury

Feature or enhancement

The _PyUnicode_FromId(_Py_Identifier *id) function gets a Python str object (i.e., a PyUnicodeObject) from a static C string. Subsequent calls to _PyUnicode_FromId() return the same object. The initialization is not currently thread-safe without the GIL.

Mostly for my own reference, here is the implementation from nogil-3.12: colesbury/nogil-3.12@6540bf3e6a. We will want to do things a bit differently in CPython 3.13.

Linked PRs

Activity

  1. changed the title [-]Make `_PyUnicode_FromId` thread-safe in `--disable-gil` builds[/-] [+]unicode: make `_PyUnicode_FromId` thread-safe in `--disable-gil` builds[/+] on Nov 10, 2023
  2. corona10 commented on Dec 20, 2023

    @corona10
    Member

    @colesbury Can I take a look at this issue too?

  3. colesbury commented on Dec 20, 2023

    @colesbury
    ContributorAuthor

    @corona10, sure. I think this requires some investigation to figure out what the right thing to do. From a quick glance, we've gotten rid of _Py_IDENTIFIER() in CPython, which is great! Since we only are keeping it to support "naught PyPI modules" we might be able to get away with just some simple locking.

  4. self-assigned this
    on Dec 20, 2023
  5. corona10 commented on Dec 26, 2023

    @corona10
    Member

    @colesbury
    You are right.
    Internally, we are not using _Py_IDENTIFIER() anymore.

    Since we only are keeping it to support "naught PyPI modules" we might be able to get away with just some simple locking.

    For sync our implementation strategy:
    So you mean that adding a mutex or lock to _Py_IDENTIFIER struct and then use the new lock from _PyUnicode_FromId side? (Allow for slight performance loss in naught PyPI modules.)

  6. added 2 commits that reference this issue on Dec 26, 2023
  7. corona10 commented on Dec 26, 2023

    @corona10
    Member

    Now we can close this issue

  8. added a commit that references this issue on Jan 22, 2024
  9. added a commit that references this issue on Feb 11, 2024
  10. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions