-
-
Notifications
You must be signed in to change notification settings - Fork 34k
gh-144424: Clean up dead weakrefs to sqlite3 Blob objects #144435
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
Open
takluyver
wants to merge
1
commit into
python:main
Choose a base branch
from
takluyver:sqlite-cleanup-blob-weakrefs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+43
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |
| #include "pycore_pyerrors.h" // _PyErr_ChainExceptions1() | ||
| #include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing() | ||
| #include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8NoNUL | ||
| #include "pycore_weakref.h" | ||
|
|
||
| #include <stdbool.h> | ||
|
|
||
|
|
@@ -143,6 +144,7 @@ class _sqlite3.Connection "pysqlite_Connection *" "clinic_state()->ConnectionTyp | |
| [clinic start generated code]*/ | ||
| /*[clinic end generated code: output=da39a3ee5e6b4b0d input=67369db2faf80891]*/ | ||
|
|
||
| static int _pysqlite_drop_unused_blob_references(pysqlite_Connection* self); | ||
| static void incref_callback_context(callback_context *ctx); | ||
| static void decref_callback_context(callback_context *ctx); | ||
| static void set_callback_context(callback_context **ctx_pp, | ||
|
|
@@ -300,6 +302,7 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database, | |
| self->thread_ident = PyThread_get_thread_ident(); | ||
| self->statement_cache = statement_cache; | ||
| self->blobs = blobs; | ||
| self->created_blobs = 0; | ||
| self->row_factory = Py_NewRef(Py_None); | ||
| self->text_factory = Py_NewRef(&PyUnicode_Type); | ||
| self->trace_ctx = NULL; | ||
|
|
@@ -624,6 +627,10 @@ blobopen_impl(pysqlite_Connection *self, const char *table, const char *col, | |
| goto error; | ||
| } | ||
|
|
||
| if (_pysqlite_drop_unused_blob_references(self) < 0) { | ||
| goto error; | ||
| } | ||
|
|
||
| return (PyObject *)obj; | ||
|
|
||
| error: | ||
|
|
@@ -1049,6 +1056,38 @@ final_callback(sqlite3_context *context) | |
| PyGILState_Release(threadstate); | ||
| } | ||
|
|
||
| static int | ||
| _pysqlite_drop_unused_blob_references(pysqlite_Connection* self) | ||
| { | ||
| /* we only need to do this once in a while */ | ||
| if (self->created_blobs++ < 200) { | ||
| return 0; | ||
| } | ||
|
|
||
| self->created_blobs = 0; | ||
|
|
||
| PyObject* new_list = PyList_New(0); | ||
| if (!new_list) { | ||
| return -1; | ||
| } | ||
|
|
||
| assert(PyList_CheckExact(self->blobs)); | ||
| Py_ssize_t imax = PyList_GET_SIZE(self->blobs); | ||
| for (Py_ssize_t i = 0; i < imax; i++) { | ||
| PyObject* weakref = PyList_GET_ITEM(self->blobs, i); | ||
| if (_PyWeakref_IsDead(weakref)) { | ||
| continue; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation. |
||
| } | ||
| if (PyList_Append(new_list, weakref) != 0) { | ||
| Py_DECREF(new_list); | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| Py_SETREF(self->blobs, new_list); | ||
| return 0; | ||
| } | ||
|
|
||
| /* Allocate a UDF/callback context structure. In order to ensure that the state | ||
| * pointer always outlives the callback context, we make sure it owns a | ||
| * reference to the module itself. create_callback_context() is always called | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we open new blobs without closing them, we will spend quadratic time on making copy of the list. We can avoid this if make the threshold proportional to the size of the list.
(the coefficient can be different).