GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Populate the initial per-interpreter interned_strings dict with runtime-global singleton strings · Issue #103571 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
Populate the initial per-interpreter interned_strings dict with runtime-global singleton strings#103571
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement

Description

@Christopher-Chianelli
Christopher-Chianelli
opened on Apr 16, 2023
Issue body actions

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

  • gh-103571: Add runtime-global strings to the initial per-interpreter interned_dict #103572

Activity

  1. Christopher-Chianelli
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Apr 16, 2023
  2. terryjreedy
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    performancePerformance or resource usagePerformance or resource usage
    on Apr 16, 2023
  3. terryjreedy commented on Apr 16, 2023

    @terryjreedy
    terryjreedy
    on Apr 16, 2023
    Member
    More actions

    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.

  4. kumaraditya303 commented on Apr 16, 2023

    @kumaraditya303
    kumaraditya303
    on Apr 16, 2023
    Contributor
    More actions

    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.

    Reacted by Christopher Chianelli
  5. kumaraditya303
    closed this as not plannedon Apr 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.