You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use functools.lru_cache iso. _sqlite.Cache in sqlite3 module #87028
$ ./python.exe
>>> import sqlite3
>>> con = sqlite3.connect(":memory:")
>>> con.execute("select * from sqlite_master")
>>> con.execute("select * from sqlite_master")
>>> c = con.cache()
>>> c.cache_info()
CacheInfo(hits=1, misses=1, maxsize=128, currsize=1)
The test suite runs approx. 10-20 ms slower with this change. Using _functools._lru_cache_wrapper iso. functools.lru_cache almost removes this performance regression.
$ python3.10 -m timeit -s 'import sqlite3; con = sqlite3.connect(":memory:"); query="select * from sqlite_master"''con.execute(query); con.execute(query)'
100000 loops, best of 5: 2.95 usec per loop
$ ./python.exe -m timeit -s 'import sqlite3; con = sqlite3.connect(":memory:"); query="select * from sqlite_master"''con.execute(query); con.execute(query)'
100000 loops, best of 5: 2.68 usec per loop
I can throw up the PoC branch as a draft PR after #68323 is merged. We can just close the PR if this is uninteresting or something we want to postpone.
I don't see any reason to merge #68323 if we are going to remove cache.[ch] in this issue.
Yes, I've thought about that myself. A small argument pro merging #68323 would be that if we for some reason decide to revert this change, then cache.[ch] would still be prepared for module state (which is needed for multi-phase init).
I need some help debugging the Windows issues. There are a handful of tests that fail because the sqlite3 is clinging on to objects or file descriptors. AFAICT, a GC bug. For example, test_open_uri fails with:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: [...]
Add heap type GC (visit type, GC tracking) to all pysqlite types
Maybe it's best to reopen #68323 and continue the work with multi-phase init? That is finalise bpo-40956, add heap type GC (https://bugs.python.org/issue42866#msg384675), establish module state, implement multi-phase init, and then revisit this issue.
explicitly close connections _and_ call GC in problematic tests
The first point might not be needed for this particular fix.
The last point is a workaround, not a solution. Or is it ok to call gc.collect() in the test suite?
New changeset f461a7f by Erlend Egeberg Aasland in branch 'main': bpo-42862: Use functools.lru_cache iso. _sqlite.Cache in sqlite3 module (GH-24203) f461a7f
Activity
erlend-aasland commentedon Jan 8, 2021
Pro: less code, less complexity, improved maintainability
Con: minor performance hit
PoC here: https://github.com/erlend-aasland/cpython/commits/sqlite-cache
The test suite runs approx. 10-20 ms slower with this change. Using _functools._lru_cache_wrapper iso. functools.lru_cache almost removes this performance regression.
Berker, is it worth pursuing?
erlend-aasland commentedon Jan 8, 2021
Short diffstat: 8 files changed, 85 insertions(+), 406 deletions(-)
erlend-aasland commentedon Jan 8, 2021
rhettinger commentedon Jan 12, 2021
+1 This seems reasonable to me.
erlend-aasland commentedon Jan 12, 2021
I can throw up the PoC branch as a draft PR after #68323 is merged. We can just close the PR if this is uninteresting or something we want to postpone.
berkerpeksag commentedon Jan 12, 2021
I don't see any reason to merge #68323 if we are going to remove cache.[ch] in this issue.
I was -0 before but since Raymond gave his +1, you can count me as +1 too.
erlend-aasland commentedon Jan 12, 2021
Yes, I've thought about that myself. A small argument pro merging #68323 would be that if we for some reason decide to revert this change, then cache.[ch] would still be prepared for module state (which is needed for multi-phase init).
berkerpeksag commentedon Jan 12, 2021
We can always reopen #68323 and merge it even if we revert this one for some reason :)
erlend-aasland commentedon Jan 12, 2021
True that :) I'll close #68323 for now and open a PR for this later today.
1 remaining item
erlend-aasland commentedon Jan 12, 2021
All right, thanks.
erlend-aasland commentedon Jan 18, 2021
I need some help debugging the Windows issues. There are a handful of tests that fail because the sqlite3 is clinging on to objects or file descriptors. AFAICT, a GC bug. For example, test_open_uri fails with:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: [...]
I've tried the following, with no success:
Maybe it's best to reopen #68323 and continue the work with multi-phase init? That is finalise bpo-40956, add heap type GC (https://bugs.python.org/issue42866#msg384675), establish module state, implement multi-phase init, and then revisit this issue.
erlend-aasland commentedon Jan 20, 2021
Tried applying bpo-42972 to sqlite and functools, but the error persists.
erlend-aasland commentedon Jan 21, 2021
This works:
The first point might not be needed for this particular fix.
The last point is a workaround, not a solution. Or is it ok to call gc.collect() in the test suite?
erlend-aasland commentedon Jan 21, 2021
Seems like it's ok:
$ grep -r gc.collect Lib/test | wc -l 366berkerpeksag commentedon May 19, 2021
It is but in this case I'd say it's a bit weird that we need to call it. Unfortunately, I don't have much time to investigate it at the moment.
pablogsal commentedon Jun 3, 2021
New changeset f461a7f by Erlend Egeberg Aasland in branch 'main':
bpo-42862: Use functools.lru_cache iso. _sqlite.Cache in sqlite3 module (GH-24203)
f461a7f
corona10 commentedon Jun 23, 2021
New changeset 34356a0 by Erlend Egeberg Aasland in branch 'main':
bpo-42862: Strip stale sqlite3 cache ignores from c-analyzer (GH-26876)
34356a0