Title: Types with `mro()` unconditionally disables type attribute cache, causing performance regression for compatible MRO implementations, such as Zope · Issue #156096 · python/cpython · GitHub
Open Graph Title: Types with `mro()` unconditionally disables type attribute cache, causing performance regression for compatible MRO implementations, such as Zope · Issue #156096 · python/cpython
X Title: Types with `mro()` unconditionally disables type attribute cache, causing performance regression for compatible MRO implementations, such as Zope · Issue #156096 · python/cpython
Description: Bug report Bug description: Since #127773 / #127924, any type whose metaclass defines a custom mro() method has its type attribute cache permanently disabled. This was made initially to fix a problem with metaclasses defining mro() to in...
Open Graph Description: Bug report Bug description: Since #127773 / #127924, any type whose metaclass defines a custom mro() method has its type attribute cache permanently disabled. This was made initially to fix a probl...
X Description: Bug report Bug description: Since #127773 / #127924, any type whose metaclass defines a custom mro() method has its type attribute cache permanently disabled. This was made initially to fix a probl...
Opengraph URL: https://github.com/python/cpython/issues/156096
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Types with `mro()` unconditionally disables type attribute cache, causing performance regression for compatible MRO implementations, such as Zope","articleBody":"# Bug report\n\n### Bug description:\n\n\nSince https://github.com/python/cpython/issues/127773 / https://github.com/python/cpython/pull/127924, any type whose metaclass defines a custom `mro()` method has its type attribute cache permanently disabled. This was made initially to fix a problem with metaclasses defining `mro()` to inject new bases that are not in the class bases.\n\nThis affects [Zope's ExtensionClass](https://github.com/zopefoundation/ExtensionClass), used as base class for most classes the Zope ecosystem. This package has 5000 daily downloads on pypi ( https://pypistats.org/packages/extensionclass ). ExtensionClass defines a custom `mro()`, but only to *reorganizes* bases that are already bases of the class, which is different from the scenario from the issue and it looks like a case where the cache could still be used.\n\nCompared to python3.11, this seems to causes a ~3x performance regression for attribute lookups on all zope classes:\n\n```\n$ uv run --python=3.11 --with Zope python -m timeit -s 'import OFS.Folder; f = OFS.Folder.Folder(\"f\")' 'f.getId'\n2000000 loops, best of 5: 116 nsec per loop\n$ uv run --python=3.14 --with Zope python -m timeit -s 'import OFS.Folder; f = OFS.Folder.Folder(\"f\")' 'f.getId'\n500000 loops, best of 5: 399 nsec per loop\n```\n\nit's hard to tell how much this affect applications, this is just a micro benchmark, but we are also observing that running ERP5 test suites (a complex application based on zope) is slower on python3.13 than it was on python3.11.\n\n## Analysis\n\nIn `type_mro_modified()`, the check `has_custom_mro(type)` unconditionally jumps to `clear:` \n\nhttps://github.com/python/cpython/blob/af49df919dafc3767ae956767dce0482f9cd6d4e/Objects/typeobject.c#L1261-L1262\n\nwhich sets `tp_versions_used = _Py_ATTR_CACHE_UNUSED`, whenever the metaclass overrides `mro()`:\n\nhttps://github.com/python/cpython/blob/af49df919dafc3767ae956767dce0482f9cd6d4e/Objects/typeobject.c#L1279-L1283\n\n\nThis was introduced to fix https://github.com/python/cpython/issues/127773, where a custom `mro()` that injects a class not in `tp_bases` (e.g. `Base`) causes stale cache entries because `PyType_Modified()` only propagates through `tp_subclasses`, not through MRO-only relationships.\n\nAlthough these are not much relevant here, ExtensionClass's `mro` is implemented in C as [`_ExtensionClass.c:569-626`](https://github.com/zopefoundation/ExtensionClass/blob/26e99ca69077016c518ef82bddf66c57475035ed/src/ExtensionClass/_ExtensionClass.c#L569-L626) and also has an equivalent pure-python implementation in [`ExtensionClass.__init__.py:181-200`](https://github.com/zopefoundation/ExtensionClass/blob/26e99ca69077016c518ef82bddf66c57475035ed/src/ExtensionClass/__init__.py#L181-L200).\n\n\n\n## Suggested fix\n\nCheck whether the custom MRO actually contains non-base entries before disabling the cache. This requires an `is_superclass()` helper that walks `tp_bases` (not `tp_mro`) to determine reachability:\n\n```c\n// Return true if `super` is reachable from `sub` through `tp_bases`,\n// as opposed to a type merely injected into the MRO by a custom `mro()`\n// implementation. Only entries reachable through `tp_bases` are covered\n// by the version-tag invalidation that _PyType_Modified_Unlocked()\n// propagates through `tp_subclasses`.\nstatic int\nis_superclass(PyTypeObject *super, PyTypeObject *sub)\n{\n PyObject *bases = lookup_tp_bases(sub);\n if (bases == NULL) {\n return 0;\n }\n Py_ssize_t n = PyTuple_GET_SIZE(bases);\n for (Py_ssize_t i = 0; i \u003c n; i++) {\n PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(bases, i));\n if (base == super || is_superclass(super, base)) {\n return 1;\n }\n }\n return 0;\n}\n```\n\nThen in `type_mro_modified`, replace the unconditional `goto clear` for custom MROs:\n\n```c\nif (!Py_IS_TYPE(type, &PyType_Type) && has_custom_mro(type)) {\n // Only disable cache if the custom mro() injected an entry that\n // is not reachable through tp_bases;\n PyObject *mro = lookup_tp_mro(type);\n if (mro == NULL) {\n goto clear;\n }\n Py_ssize_t mro_size = PyTuple_GET_SIZE(mro);\n for (Py_ssize_t m = 0; m \u003c mro_size; m++) {\n PyTypeObject *entry = _PyType_CAST(PyTuple_GET_ITEM(mro, m));\n if (entry != type && !is_superclass(entry, type)) {\n goto clear;\n }\n }\n}\n```\n\nI'm not at all familiar with this, but it seems to me that this would preserve the correctness of https://github.com/python/cpython/pull/127924 for types that do inject non-base entries into their MRO (because cache would still be disabled for those), while restoring cache performance for compatible custom MRO implementations like Zope's ExtensionClass.\n\nIf this approach makes sense, I already have a draft commit implementing this: https://github.com/perrinjerome/cpython/commit/89af3385eb7e930466dcaef97b2d7b9286eb755e and I would be happy to clean it up and make a pull request.\n\n\n### CPython versions tested on:\n\n3.13\n\n### Operating systems tested on:\n\nLinux","author":{"url":"https://github.com/perrinjerome","@type":"Person","name":"perrinjerome"},"datePublished":"2026-08-20T10:39:56Z","url":"https://github.com/python/cpython/issues/156096"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:56bbc251-67b0-43a1-9449-cd7547ff6e57 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B322:785A9:4CA991B:60FDE69:6B025D10 |
| html-safe-nonce | bc3fb2cf4b501548d39a089871ed296bfcaf1e97ab660b43aa6b0c9ab3f9bc6e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMzIyOjc4NUE5OjRDQTk5MUI6NjBGREU2OTo2QjAyNUQxMCIsInZpc2l0b3JfaWQiOiI3NzMyMDgxMTYwNjQ5NTk2MTc2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5b044d0844231006fa5ce1be18637a7be1cfbc19163ee9d5f05b96a6fecc4565 |
| hovercard-subject-tag | issue:5202478741 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/156096/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9124b02fd5f4ee62d13edfcf07d30b1093fcc5b0e181c2dbc21de588eed0f519/python/cpython/issues/156096 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9124b02fd5f4ee62d13edfcf07d30b1093fcc5b0e181c2dbc21de588eed0f519/python/cpython/issues/156096 |
| og:image:alt | Bug report Bug description: Since #127773 / #127924, any type whose metaclass defines a custom mro() method has its type attribute cache permanently disabled. This was made initially to fix a probl... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | perrinjerome |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width