Skip to content

perf(python): cache the main-thread check per thread - #468

Open
vuanhphung wants to merge 3 commits into
perf/rd-gil-handoffsfrom
perf/rd-main-thread-cache
Open

vuanhphung wants to merge 3 commits into
perf/rd-gil-handoffsfrom
perf/rd-main-thread-cache

Conversation

@vuanhphung

Copy link
Copy Markdown
Collaborator

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's threading module on every call: main_thread(), get_ident(), ident, then get_native_id() / native_id. That is four or five Python calls and a gettid() 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 running SELECT 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. So on_main_thread now caches it per thread, tagged with a fork generation that a pthread_atfork child handler increments (registered once at module import; the handler only bumps an atomic). A cached answer from before a fork is recomputed with the same threading check 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:

without with
Live 1x800, avg QPS (3 pairs) 7238 / 7221 / 7526 7250 / 7354 / 7749
Live 1x800, client CPU per query (ms) 0.441 / 0.439 / 0.437 0.430 / 0.424 / 0.428
Mock 1x800, avg QPS (median of 4) 7917 8774
Mock 1x800, client CPU per query (ms, median) 0.354 0.335

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.

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>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread pyo3/src/gil.rs Resolved
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>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread pyo3/src/gil.rs Resolved
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>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No issues identified by the review bot.

This branch was previously deployed

1 inactive deployment
azure-prod — f9a3914b Deployed Nov 14, 2026 by vuanhphung via followup #3124
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant