New GitHub issue #119663 from markshannon:<br>

<hr>

<pre>
`PyType_GetModuleByDef` does a lot of work for what was a single memory read prior to sub-interpreters.

For example, https://github.com/python/cpython/issues/114682.

We should provide a more efficient way to get to the module state from the module def.

AFAICT, the function to get the module state is a pure function of the interpreter and the module def.
Since the module def already has a `m_index` field that is described as "The module's index into its interpreter's modules_by_index cache.", then a `Py_GetModuleFromDef()` function could be as simple as:

```C
inline PyObject *
Py_GetModuleFromDef(PyModuleDef *def)
{
     return PyInterpreterState_Get()->modules_table[def->m_base.m_index];
}
```

</pre>

<hr>

<a href="https://github.com/python/cpython/issues/119663">View on GitHub</a>
<p>Labels: </p>
<p>Assignee: </p>