datasette/perf/request-response/patches/extras-parallel-only-without-gil.diff
Claude 07068144f9
Measure table-page extras on free-threaded Python 3.15
Runs the table pages on 3.15.0b4t with parallel and sequential extras,
1-8 SQL threads, and 1 or 8 requests in flight. On the GIL build parallel
extras are slower because the SQL worker threads contend for the GIL on
every fetched row. On the free-threaded build that penalty disappears and
throughput under load is 2.2-3.1x higher. Adds a patch that resolves
extras in parallel only when the GIL is disabled, the stress and gather
microbenchmark tools, and the raw results.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F8mEgRS8swZfYqwqrVMeo5
2026-10-13 19:40:24 +00:00

36 lines
1.4 KiB
Diff

diff --git a/datasette/extras.py b/datasette/extras.py
index fb8c2e0..0e2fbe5 100644
--- a/datasette/extras.py
+++ b/datasette/extras.py
@@ -1,4 +1,5 @@
import re
+import sys
from dataclasses import dataclass
from enum import Enum
from typing import ClassVar
@@ -8,6 +9,16 @@ from asyncinject import Registry
from datasette.utils.asgi import BadRequest
+def _resolve_extras_in_parallel():
+ # With the GIL, resolving extras concurrently puts several queries on the
+ # SQL thread pool at once, and the workers then contend for the GIL on
+ # every fetched row - measured at 2-5x the CPU of running them one at a
+ # time. On a free-threaded build concurrent queries run in parallel.
+ # Checked when a registry is built, since importing an extension module
+ # without free-threading support can turn the GIL back on.
+ return not getattr(sys, "_is_gil_enabled", lambda: True)()
+
+
def extra_names_from_request(request):
extra_bits = request.args.getlist("_extra")
extras = set()
@@ -96,7 +107,7 @@ class ExtraRegistry:
def _registry_for_scope(self, scope):
registry = self._scope_registries.get(scope)
if registry is None:
- registry = Registry()
+ registry = Registry(parallel=_resolve_extras_in_parallel())
for cls in self.classes_for_scope(scope):
registry.register(cls().resolve, name=cls.key())
self._scope_registries[scope] = registry