pg.ddx.io psycopg/psycopg GitHub issues and pull requests
help / color / mirror / Atom feed[psycopg/psycopg] PR #1469: perf: don't wait on the event loop when the socket is writable
11+ messages / 2 participants
[nested] [flat]
* [psycopg/psycopg] PR #1469: perf: don't wait on the event loop when the socket is writable
@ 2026-10-30 07:14 "Alexkane (@Alexkane)" <noreply+Alexkane@github.com>
0 siblings, 0 replies; 11+ messages in thread
From: Alexkane (@Alexkane) @ 2026-10-30 07:14 UTC (permalink / raw)
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
Fix #1468.
When the generator asks to write, poll the fd with zero timeout before registering with the loop, and feed the generator at once if it's ready. Waits for reading only are unchanged.
Localhost: `executemany()` 50k rows 1.61 → 0.37 s (sync: 0.39), pipelined `execute()` 34 → 9.5 µs, `copy()` one `write()` per row 0.60 → 0.27 s. Round trips, copy-out, fetch-all and idle CPU unchanged.
Added `copy-in` and `pipeline` workloads to `waittest.py` (copy-in 1252 → 510 ms, pipeline 71 → 18 ms) and `test_wait_w_no_loop`.
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [psycopg/psycopg] PR #1469: perf: don't wait on the event loop when the socket is writable
@ 2026-11-01 23:05 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: dvarrazzo (@dvarrazzo) @ 2026-11-01 23:05 UTC (permalink / raw)
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
(on psycopg/psycopg/waiting.py:158)
This is not the way we have organised the code in this module. you can see that we have declared functions with different implementations and different names, and eventually we have defined the alias `wait` as the best implementation available on the platform (e.g. `wait = wait_poll`). Whatever you do here, I expect to follow the same pattern.
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [psycopg/psycopg] PR #1469: perf: don't wait on the event loop when the socket is writable
@ 2026-11-01 23:06 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: dvarrazzo (@dvarrazzo) @ 2026-11-01 23:06 UTC (permalink / raw)
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
(on psycopg/psycopg/waiting.py:260)
I believe this comment can be much more compact and convey the same message.
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [psycopg/psycopg] PR #1469: perf: don't wait on the event loop when the socket is writable
@ 2026-11-01 23:12 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: dvarrazzo (@dvarrazzo) @ 2026-11-01 23:12 UTC (permalink / raw)
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
(on psycopg/psycopg/waiting.py:265)
I have run a test and this pretty much becomes a busy loop which, in a long `executemany()`, never yields control, blocking other coroutines, stop canceling, etc.
Therefore let's find a middle ground. I have tested that yielding every 100 null iterations is basically free and keeps the system responsive. So we may add:
```suggestion
# Don't starve the loop if the socket is always ready.
nskip += 1
if nskip >= 100:
nskip = 0
await asyncio.sleep(0)
continue
```
(of course with a `nskip = 0` somewhere upstream and maybe a module level `MAX_SKIPPED_WAITS = 100`...)
Can you test a bit with it?
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [psycopg/psycopg] PR #1469: perf: don't wait on the event loop when the socket is writable
@ 2026-11-06 02:48 ` "Alexkane (@Alexkane)" <noreply+Alexkane@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: Alexkane (@Alexkane) @ 2026-11-06 02:48 UTC (permalink / raw)
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
(on psycopg/psycopg/waiting.py)
Done: `_ready_poll()` / `_ready_select()`, with `_ready` chosen at the bottom next to `wait`.
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [psycopg/psycopg] PR #1469: perf: don't wait on the event loop when the socket is writable
@ 2026-11-06 02:48 ` "Alexkane (@Alexkane)" <noreply+Alexkane@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: Alexkane (@Alexkane) @ 2026-11-06 02:48 UTC (permalink / raw)
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
(on psycopg/psycopg/waiting.py)
Shortened to three lines.
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [psycopg/psycopg] PR #1469: perf: don't wait on the event loop when the socket is writable
@ 2026-11-06 02:48 ` "Alexkane (@Alexkane)" <noreply+Alexkane@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: Alexkane (@Alexkane) @ 2026-11-06 02:48 UTC (permalink / raw)
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
(on psycopg/psycopg/waiting.py)
Good catch, thanks. Added `MAX_SKIPPED_WAITS = 100` and the `sleep(0)` as you suggested. Measured here with a ticker task alongside an `executemany()` of 50k rows: the ticker's longest wait goes from 339 ms to 1 ms, the executemany from 0.34 to 0.38 s (1.65 s before the PR), and a `cancel()` now lands in ~270 ms, most of it the pipeline draining on exit (1.4 s before the PR). Note the counter is per `wait_async()` call, so a `copy()` doing one write per call is not covered by it; that is the same as Linux before the PR, where those writes didn't block either. Happy to make it a module-level counter if you'd rather cover that too.
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [psycopg/psycopg] PR #1469: perf: don't wait on the event loop when the socket is writable
@ 2026-11-07 15:18 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: dvarrazzo (@dvarrazzo) @ 2026-11-07 15:18 UTC (permalink / raw)
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
Hello @Alexkane, thank you for the PR!
I've rebased it on master and added a few commits on top of yours:
- `wait_async()` now yields to the loop when 5ms passed since the loop last got control, instead of after 100 skipped waits. A per-call count never yields with many short calls (`copy.write()`, pipeline mode), and a count across calls not with calls far apart (`copy.write_row()`).
- Added `ready_c()` in the C extension, chosen together with `wait_c()` (new `ReadyFunc` protocol in `psycopg.abc`).
Master now has tools to measure this kind of change (#1483, see the README): `tests/scripts/benchmark.py` (the former `waittest.py`, with your workloads) and `bench_compare.py`. The full comparison against master is in [this run](https://github.com/dvarrazzo/psycopg/actions/runs/42335242456); the summary, on GitHub runners (median):
| | Linux | macOS | Windows |
|---|--:|--:|--:|
| async pipeline | -64% | -67% | -62% |
| async executemany | -65% | -72% | -62% |
| loop max stall, COPY | 207 -> 5 ms | 4 -> 5 ms | 194 -> 5 ms |
No regressions found, sync unchanged.
I'm going to merge it now, as other work depends on it: sorry for the hurry. If you see anything to improve, or want to work on other performance improvements, follow-up PRs are very welcome!
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [psycopg/psycopg] PR #1469: perf: don't wait on the event loop when the socket is writable
@ 2026-11-07 17:36 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: dvarrazzo (@dvarrazzo) @ 2026-11-07 17:36 UTC (permalink / raw)
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
@dvarrazzo merged this (commit 4603fb98c85c).
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [psycopg/psycopg] PR #1469: perf: don't wait on the event loop when the socket is writable
@ 2026-11-07 17:36 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: dvarrazzo (@dvarrazzo) @ 2026-11-07 17:36 UTC (permalink / raw)
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
@dvarrazzo closed this.
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [psycopg/psycopg] PR #1469: perf: don't wait on the event loop when the socket is writable
@ 2026-11-16 04:18 ` "Alexkane (@Alexkane)" <noreply+Alexkane@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: Alexkane (@Alexkane) @ 2026-11-16 04:18 UTC (permalink / raw)
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
Thanks for picking this up and taking it further, Daniele. The time-based yield is much better than my counter. Glad I could put some spare tokens to use on such a great project.
^ permalink raw [nested|flat] 11+ messages in thread
end of thread, other threads:[~2026-11-16 04:18 UTC | newest]
Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-30 07:14 [psycopg/psycopg] PR #1469: perf: don't wait on the event loop when the socket is writable "Alexkane (@Alexkane)" <noreply+Alexkane@github.com>
2026-11-01 23:05 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
2026-11-01 23:06 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
2026-11-01 23:12 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
2026-11-06 02:48 ` "Alexkane (@Alexkane)" <noreply+Alexkane@github.com>
2026-11-06 02:48 ` "Alexkane (@Alexkane)" <noreply+Alexkane@github.com>
2026-11-06 02:48 ` "Alexkane (@Alexkane)" <noreply+Alexkane@github.com>
2026-11-07 15:18 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
2026-11-07 17:36 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
2026-11-07 17:36 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
2026-11-16 04:18 ` "Alexkane (@Alexkane)" <noreply+Alexkane@github.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox