Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Populate the initial per-interpreter interned_strings dict with runtime-global singleton strings #103571

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

Closed
Christopher-Chianelli opened this issue Apr 16, 2023 · 2 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage type-feature A feature request or enhancement

Comments

@Christopher-Chianelli
Copy link
Contributor

Christopher-Chianelli commented Apr 16, 2023

Feature or enhancement

Re-use the runtime-global singleton strings inside the interned_strings dict to reduce duplication of
singleton strings (improving performance where such strings are used).

Pitch

bpo-46430 (#30683) caused an interesting side effect; the code
x = 'a'; x[0] is x no longer returned True. This in turn
is because there are two different cached versions of 'a':

  • One that was cached when code in frozen modules was compiled
    (and is stored in the interned_dict)
  • One that is stored as a runtime-global object that is used
    during function calls (and is stored in _Py_SINGLETON(strings))

However, some characters do not have this behaviour (for example,
'g', 'u', and 'z'). I suspect it because these characters are not
used in co_consts of frozen modules.

The interned_dict is per interpreter, and is initialized by
init_interned_dict(PyInterpreterState *). Currently, it is
initialized to an empty dict, which allows code in frozen modules
to use their (different and per interpreter) singleton strings
instead of the runtime-global one.

Using the synthetic test case:

def test():
    total = 0
    for ch in 'abc':
        if ch in {'a', 'c'}:
            total += 1
    return total

timeit.timeit(`test()`, globals=globals())

I get a ~5.43% improvement when the interned_strings dict reuses the runtime-global singleton strings.

Previous discussion

Linked PRs

@Christopher-Chianelli Christopher-Chianelli added the type-feature A feature request or enhancement label Apr 16, 2023
@terryjreedy terryjreedy added interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels Apr 16, 2023
@terryjreedy
Copy link
Member

x = 'a'; x[0] is x is True in 3.10, False in 3.11. Neither is correct as the reference declares caching-dependent behaviors like this to be implementation details.

@kumaraditya303
Copy link
Contributor

It is an implementation detail, it will soon change as that dict will be made immortal and be global instead of per-interpreter so it will be automatically be resolved, closing as it is not actionable for now.

@kumaraditya303 kumaraditya303 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants