Repository navigation
perf(python): skip GIL hand-offs that wait for nothing #458
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dc4759b
perf(python): skip GIL hand-offs that wait for nothing
vuanhphung f53c150
perf(python): release the GIL for batches that need column rewrites
vuanhphung a0e8ca9
perf(python): take the GIL-held fast path only for readers that opt in
vuanhphung 07db0b1
test(reader): pin which readers report a cheap try_next_batch
vuanhphung a9d5ab9
fix(python): recognise the main thread by either thread id
vuanhphung 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
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
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 |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright (c) 2026 Databricks, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Helpers that avoid needless GIL hand-offs. | ||
| //! | ||
| //! Each `py.detach` releases the GIL and re-acquires it on return. With | ||
| //! hundreds of Python threads sharing one connection pool, every | ||
| //! re-acquire waits behind the other threads, so a call that needs no | ||
| //! I/O is cheaper done with the GIL held. | ||
|
|
||
| use pyo3::prelude::*; | ||
| use pyo3::sync::PyOnceLock; | ||
| use pyo3::types::PyModule; | ||
| use std::future::Future; | ||
| use std::task::{Context, Poll, Waker}; | ||
| use tokio::runtime::Runtime; | ||
|
|
||
| static THREADING: PyOnceLock<Py<PyModule>> = PyOnceLock::new(); | ||
|
|
||
| /// Whether the calling thread is Python's main thread, the only one that | ||
| /// runs signal handlers. Errs towards `true` (a wrong `true` only keeps | ||
| /// signal checks on). | ||
| /// | ||
| /// Either id may match: `threading` refreshes the main thread's `ident` | ||
| /// after `fork()` but not its `native_id`, while greenlet libraries | ||
| /// (gevent, eventlet) patch `get_ident` but not the OS-level `native_id`. | ||
| pub(crate) fn on_main_thread(py: Python<'_>) -> bool { | ||
| let check = || -> PyResult<bool> { | ||
| let threading = THREADING | ||
| .get_or_try_init(py, || py.import("threading").map(Bound::unbind))? | ||
| .bind(py); | ||
| let main = threading.call_method0("main_thread")?; | ||
| if threading | ||
| .call_method0("get_ident")? | ||
| .eq(main.getattr("ident")?)? | ||
| { | ||
| return Ok(true); | ||
| } | ||
| threading | ||
| .call_method0("get_native_id")? | ||
| .eq(main.getattr("native_id")?) | ||
| }; | ||
| check().unwrap_or(true) | ||
| } | ||
|
|
||
| /// Drive `fut` to completion, polling it once with the GIL held first and | ||
| /// releasing the GIL only if it isn't ready. | ||
| /// | ||
| /// Only for futures whose first poll never blocks the thread: no network | ||
| /// I/O and no token refresh before the first `.await` that can pend. | ||
| pub(crate) fn run_ready_first<F>(py: Python<'_>, runtime: &Runtime, fut: F) -> F::Output | ||
| where | ||
| F: Future + Send, | ||
| F::Output: Send, | ||
| { | ||
| let mut fut = std::pin::pin!(fut); | ||
| let first = { | ||
| let _context = runtime.enter(); | ||
| fut.as_mut().poll(&mut Context::from_waker(Waker::noop())) | ||
| }; | ||
| match first { | ||
| Poll::Ready(output) => output, | ||
| Poll::Pending => py.detach(|| runtime.block_on(fut)), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::run_ready_first; | ||
| use pyo3::prelude::*; | ||
| use std::time::Duration; | ||
|
|
||
| fn runtime() -> tokio::runtime::Runtime { | ||
| tokio::runtime::Builder::new_multi_thread() | ||
| .worker_threads(1) | ||
| .enable_all() | ||
| .build() | ||
| .expect("build runtime") | ||
| } | ||
|
|
||
| #[test] | ||
| fn ready_future_completes_on_first_poll() { | ||
| Python::initialize(); | ||
| let runtime = runtime(); | ||
| let out = Python::attach(|py| run_ready_first(py, &runtime, async { 7 })); | ||
| assert_eq!(out, 7); | ||
| } | ||
|
|
||
| #[test] | ||
| fn pending_future_finishes_on_the_runtime() { | ||
| Python::initialize(); | ||
| let runtime = runtime(); | ||
| let (tx, rx) = tokio::sync::oneshot::channel(); | ||
| runtime.spawn(async move { | ||
| tokio::time::sleep(Duration::from_millis(20)).await; | ||
| let _ = tx.send(7); | ||
| }); | ||
| // The first poll registers a no-op waker; block_on must re-poll. | ||
| let out = Python::attach(|py| run_ready_first(py, &runtime, async { rx.await.ok() })); | ||
| assert_eq!(out, Some(7)); | ||
| } | ||
| } |
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
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.