datasette/perf/request-response/patches/critic-2-table-rows-dict-zip.diff
Claude d1dbb40362
Add request/response profiling harness and proposed optimisation patches
Tools for profiling Datasette's request/response cycle in-process with
Python 3.15's profiling.sampling, plus the patches proposed by the study
(one per optimisation, and ALL-combined.diff). The patches are not
applied to the code. A report with measurements follows.

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

21 lines
1,011 B
Diff

diff --git a/datasette/views/table.py b/datasette/views/table.py
index 92473bc..1aa5d10 100644
--- a/datasette/views/table.py
+++ b/datasette/views/table.py
@@ -2420,8 +2420,14 @@ async def table_view_data(
# Apply transform_value for columns with assigned types
ct_map = await datasette.get_column_types(database_name, table_name)
transformed_rows = []
- for r in raw_sqlite_rows:
- row_dict = dict(r)
+ row_keys = raw_sqlite_rows[0].keys() if raw_sqlite_rows else []
+ # dict(sqlite3.Row) looks every key up by name (a linear,
+ # case-insensitive scan), so zip with the keys when they are unique
+ if len({k.lower() for k in row_keys}) == len(row_keys):
+ dict_rows = (dict(zip(row_keys, r)) for r in raw_sqlite_rows)
+ else:
+ dict_rows = (dict(r) for r in raw_sqlite_rows)
+ for row_dict in dict_rows:
for col_name, ct in ct_map.items():
if col_name in row_dict:
row_dict[col_name] = await ct.transform_value(