Repository navigation
perf(python): cache the main-thread check per thread - #468
vuanhphung wants to merge 3 commits into
Conversation
Statement.execute() asked Python's threading module whether it runs on the main thread on every call: four Python calls plus a gettid() with the GIL held (about 1-1.5 us per call, and 2% of the GIL-held samples in a live profile of 200 threads running SELECT 1). The answer only changes for a thread at fork(), so it is now cached per thread and recomputed after a fork, which a pthread_atfork child handler records. Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
There was a problem hiding this comment.
Verdict: 1 Low
Looks good: one low-severity concern about a possibly flaky test. The fork-generation cache is sound. The pthread_atfork child handler only touches an atomic. In the child, only the forking thread survives, and it held the GIL across os.fork, so no stale cache write can race the bump. Errors are still not cached, and the gevent/native_id semantics are unchanged because the check is already effectively per OS thread.
Review feedback: both tests bump the shared counter, so the forked child now checks that it increased rather than that it rose by exactly one; the parent's assertion that could never fail is dropped. Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
There was a problem hiding this comment.
Verdict: 1 Low
Looks good overall, with one low-severity robustness concern. Caching per thread, tagged with a fork generation that a pthread_atfork child handler bumps, is sound. The handler is async-signal-safe, errors are not cached, and the tests cover the cache, its recomputation after a generation change, and that a forked child sees the bump. The concern: if handler registration fails, nothing falls back to the uncached check.
Review feedback: if pthread_atfork failed to register, a cached answer would outlive a fork, so a thread that became the child's main thread could skip signal checks. The cache is now used only after the handler registered (always on non-Unix platforms); otherwise every call asks threading, as before. Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
Stacked on #458 (base
perf/rd-gil-handoffs); review only the top commit.#458 makes
Statement.execute()skip the 100 ms signal-check ticks on threads other than the main thread, and decides "main thread?" by asking Python'sthreadingmodule on every call:main_thread(),get_ident(),ident, thenget_native_id()/native_id. That is four or five Python calls and agettid()with the GIL held on every query, about 1-1.5 µs, and 2% of the GIL-held samples in a live profile of 200 threads runningSELECT 1. With hundreds of threads sharing one GIL, that time comes out of throughput.The answer only changes for a thread at
fork(), where the forking thread becomes the child's main thread. Soon_main_threadnow caches it per thread, tagged with a fork generation that apthread_atforkchild handler increments (registered once at module import; the handler only bumps an atomic). A cached answer from before a fork is recomputed with the samethreadingcheck as before, so the gevent / post-fork behaviour #458 handles is unchanged. Errors are not cached, and still count as "main thread".Rust tests cover the cache, its recomputation after a fork generation change, and that a forked child sees the new generation.
SELECT 1, 1 process, interleaved, kernel = main + the factory PRs including #458 and #467 (GIL-held fill), with and without this commit; connector databricks/databricks-sql-python#1061:Live: staging X-Large Real-Time warehouse, 800 sessions, 60 s, 8 client cores. Mock: local SEA mock with 20 ms server time, 20 s, client on 4 cores. The live QPS gain is small (+0.2%, +1.8%, +3.0%) because the live run is close to its round-trip floor. CPU per query drops 2-3% in every live pair and 5% on the mock. One live 1x1600 pair was inconclusive (avg 6992 vs 6937, steady 7208 vs 7435; the first run took 42 s to open its sessions).
This pull request and its description were written by Isaac.