Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cache borrowed references, cleared in func_dealloc
  • Loading branch information
gvanrossum committed Aug 16, 2023
commit 60967905dce381e377eb81d6ea7f587bb40990af
8 changes: 4 additions & 4 deletions Include/internal/pycore_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ extern PyObject* _PyFunction_Vectorcall(
#define FUNC_VERSION_CACHE_SIZE (1<<12) /* Must be a power of 2 */
struct _py_func_state {
uint32_t next_version;
// Function objects whose func_version % FUNC_VERSION_CACHE_SIZE
// once equaled the index in the table. The references are owned:
// Call _PyFunction_ClearByVersionCache() to clear.
// Borrowed references to function objects whose
// func_version % FUNC_VERSION_CACHE_SIZE
// once was equal to the index in the table.
// They are cleared when the function is deallocated.
PyFunctionObject *func_version_cache[FUNC_VERSION_CACHE_SIZE];
};

Expand All @@ -30,7 +31,6 @@ extern PyFunctionObject* _PyFunction_FromConstructor(PyFrameConstructor *constr)
extern uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func);
extern void _PyFunction_SetVersion(PyFunctionObject *func, uint32_t version);
PyFunctionObject *_PyFunction_LookupByVersion(uint32_t version);
void _PyFunction_ClearByVersionCache(PyInterpreterState *interp);

extern PyObject *_Py_set_function_type_params(
PyThreadState* unused, PyObject *func, PyObject *type_params);
Expand Down
33 changes: 15 additions & 18 deletions Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,35 +271,23 @@ _PyFunction_SetVersion(PyFunctionObject *func, uint32_t version)
func->func_version = version;
if (version != 0) {
PyInterpreterState *interp = _PyInterpreterState_GET();
PyFunctionObject **slot =
interp->func_state.func_version_cache
+ (version % FUNC_VERSION_CACHE_SIZE);
Py_XSETREF(*slot, (PyFunctionObject *)Py_NewRef(func));
interp->func_state.func_version_cache[
version % FUNC_VERSION_CACHE_SIZE] = func;
Comment on lines +274 to +275

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be slightly more readable with something like this:

Suggested change
interp->func_state.func_version_cache[
version % FUNC_VERSION_CACHE_SIZE] = func;
uint32_t idx = version % FUNC_VERSION_CACHE_SIZE;
interp->func_state.func_version_cache[idx] = func;

Ditto for the function below.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry. It's probably not worth it to clean this up now that I landed it.

}
}

PyFunctionObject *
_PyFunction_LookupByVersion(uint32_t version)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
PyFunctionObject **slot =
interp->func_state.func_version_cache
+ (version % FUNC_VERSION_CACHE_SIZE);
if (*slot != NULL && (*slot)->func_version == version) {
return (PyFunctionObject *)Py_NewRef(*slot);
PyFunctionObject *func = interp->func_state.func_version_cache[
version % FUNC_VERSION_CACHE_SIZE];
if (func != NULL && func->func_version == version) {
return (PyFunctionObject *)Py_NewRef(func);
}
return NULL;
}

void
_PyFunction_ClearByVersionCache(PyInterpreterState *interp)
{
for (int i = 0; i < FUNC_VERSION_CACHE_SIZE; i++) {
PyFunctionObject **slot = interp->func_state.func_version_cache + i;
Py_CLEAR(*slot);
}
}

uint32_t
_PyFunction_GetVersionForCurrentState(PyFunctionObject *func)
{
Expand Down Expand Up @@ -929,6 +917,15 @@ func_dealloc(PyFunctionObject *op)
if (op->func_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *) op);
}
if (op->func_version != 0) {
PyInterpreterState *interp = _PyInterpreterState_GET();
PyFunctionObject **slot =
interp->func_state.func_version_cache
+ (op->func_version % FUNC_VERSION_CACHE_SIZE);
if (*slot == op) {
*slot = NULL;
}
}
(void)func_clear(op);
// These aren't cleared by func_clear().
Py_DECREF(op->func_code);
Expand Down
4 changes: 4 additions & 0 deletions Python/abstract_interp_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1767,11 +1767,6 @@ Py_FinalizeEx(void)
// XXX assert(_Py_IsMainInterpreter(tstate->interp));
// XXX assert(_Py_IsMainThread());

// The function version cache keeps functions alive, so clear it.
// It's used for optimizations, which we don't need in this stage.
tstate->interp->func_state.next_version = 0; // No more new versions
_PyFunction_ClearByVersionCache(tstate->interp);

// Block some operations.
tstate->interp->finalizing = 1;

Expand Down
3 changes: 0 additions & 3 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2079,9 +2079,6 @@ sys__clear_type_cache_impl(PyObject *module)
/*[clinic end generated code: output=20e48ca54a6f6971 input=127f3e04a8d9b555]*/
{
PyType_ClearCache();
// Also clear the function-by-version cache
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyFunction_ClearByVersionCache(interp);
Py_RETURN_NONE;
}

Expand Down