Skip to content

Commit 6cc522a

Browse files
rustyconoverclaude
andcommitted
Catalog: build each function listing once and encode each item once
A function listing is derived only from the registered classes' static metadata, yet every catalog_schema_contents_functions request rebuilt it: every function in the schema was turned into a FunctionInfo (to keep the requested type's) and the worker Arrow-encoded each kept one again, about 0.8 ms per function. The example worker's 162 table functions cost 134 ms of worker time per listing; DuckDB pays the same for every function-set load. In the test suite these listings were half of the shared launched worker's busy time, and the listing tests queued behind each other on it. CatalogInterface now keeps each (schema, listing type) result, built on first request, so it returns the same FunctionInfo instances every time. FunctionsResponse.from_infos (and the other catalog item responses) store an item's encoding on the instance, the way functools.cached_property does on a frozen dataclass, so an instance is encoded once. Catalogs that build fresh items per call are unaffected. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 96d15b2 commit 6cc522a

2 files changed

Lines changed: 73 additions & 22 deletions

File tree

‎vgi/catalog/catalog_interface.py‎

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,6 +2698,8 @@ class ReadOnlyCatalogInterface(CatalogInterface):
26982698
_function_registry: "dict[tuple[SchemaKey, str], list[type]] | None" = None
26992699
_macro_registry: "dict[tuple[SchemaKey, str], Macro] | None" = None
27002700
_index_registry: "dict[tuple[SchemaKey, str], Index] | None" = None
2701+
# Function listings by (schema, listing type), filled in by ``_function_infos``.
2702+
_function_info_cache: "dict[tuple[SchemaKey, SchemaObjectType], tuple[FunctionInfo, ...]] | None" = None
27012703
# Lazy registry build is one-time but the fixture HTTP server is
27022704
# multi-threaded and shares one catalog instance, so concurrent
27032705
# first-requests can race the build. Serialize it under a lock and flip
@@ -2738,6 +2740,7 @@ def _build_registries_locked(self) -> None:
27382740
self._function_registry = {}
27392741
self._macro_registry = {}
27402742
self._index_registry = {}
2743+
self._function_info_cache = {}
27412744

27422745
def _register_table(schema_key: SchemaKey, table: "Table") -> None:
27432746
key = (schema_key, table.name.lower())
@@ -3320,29 +3323,46 @@ def schema_contents(
33203323
if sn == path_key and macro.macro_type == target_macro_type:
33213324
results.append(macro.to_macro_info(schema_path))
33223325
else:
3323-
# SCALAR_FUNCTION or TABLE_FUNCTION
3324-
for (sn, _), func_classes in self._function_registry.items():
3325-
if sn != path_key:
3326-
continue
3327-
for func_cls in func_classes:
3328-
func_info = self._function_to_info(func_cls, schema_path)
3329-
# Filter by function type
3330-
if type_enum == SchemaObjectType.SCALAR_FUNCTION and func_info.function_type != FunctionType.SCALAR:
3331-
continue
3332-
if type_enum == SchemaObjectType.TABLE_FUNCTION and func_info.function_type not in (
3333-
FunctionType.TABLE,
3334-
FunctionType.TABLE_BUFFERING,
3335-
):
3336-
continue
3337-
if (
3338-
type_enum == SchemaObjectType.AGGREGATE_FUNCTION
3339-
and func_info.function_type != FunctionType.AGGREGATE
3340-
):
3341-
continue
3342-
results.append(func_info)
3326+
# SCALAR_FUNCTION, TABLE_FUNCTION or AGGREGATE_FUNCTION
3327+
results.extend(self._function_infos(path_key, schema_path, type_enum))
33433328

33443329
return results
33453330

3331+
def _function_infos(
3332+
self, path_key: SchemaKey, schema_path: SchemaPath, type_enum: SchemaObjectType
3333+
) -> tuple[FunctionInfo, ...]:
3334+
"""Return the schema's functions of one listing type, built on the first request.
3335+
3336+
A listing depends only on the registered classes' static metadata, so
3337+
every request for it returns the same `FunctionInfo` instances. Rebuilding
3338+
it on each request re-derived every function in the schema to keep the
3339+
requested type's. It also made the worker encode each one again, at
3340+
about 0.8 ms per function, because ``FunctionsResponse.from_infos``
3341+
stores an instance's encoding on the instance.
3342+
The instances are shared between requests, so they must not be mutated.
3343+
"""
3344+
assert self._function_registry is not None
3345+
assert self._function_info_cache is not None
3346+
key = (path_key, type_enum)
3347+
cached = self._function_info_cache.get(key)
3348+
if cached is not None:
3349+
return cached
3350+
wanted = {
3351+
SchemaObjectType.SCALAR_FUNCTION: (FunctionType.SCALAR,),
3352+
SchemaObjectType.TABLE_FUNCTION: (FunctionType.TABLE, FunctionType.TABLE_BUFFERING),
3353+
SchemaObjectType.AGGREGATE_FUNCTION: (FunctionType.AGGREGATE,),
3354+
}.get(type_enum, ())
3355+
infos: list[FunctionInfo] = []
3356+
for (sn, _), func_classes in self._function_registry.items():
3357+
if sn != path_key:
3358+
continue
3359+
for func_cls in func_classes:
3360+
func_info = self._function_to_info(func_cls, schema_path)
3361+
if func_info.function_type in wanted:
3362+
infos.append(func_info)
3363+
# Concurrent first requests may both build it; keep whichever landed first.
3364+
return self._function_info_cache.setdefault(key, tuple(infos))
3365+
33463366
def copy_from_formats(
33473367
self,
33483368
*,

‎vgi/protocol.py‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,37 @@ class TransactionBeginResponse(ArrowSerializableDataclass):
843843
transaction_opaque_data: bytes | None = None
844844

845845

846+
#: Instance attribute where `_item_ipc_bytes` keeps an item's encoding.
847+
_ITEM_IPC_MEMO = "_vgi_item_ipc"
848+
849+
850+
def _item_ipc_bytes(info: Any) -> bytes:
851+
"""Return a catalog item's Arrow IPC encoding, encoded once per instance.
852+
853+
A catalog item is a frozen dataclass, so its encoding cannot change, and
854+
encoding one costs about 0.8 ms (a one-row batch of some 40 columns, plus
855+
its schema). A catalog that returns the same instances on every listing,
856+
as ``CatalogInterface`` does for functions, therefore pays for the encoding
857+
once. The encoding is kept in the instance's ``__dict__``, as
858+
``functools.cached_property`` does on a frozen dataclass. It is not a field,
859+
so it takes no part in equality, ``repr``, ``dataclasses.replace`` or
860+
serialization. An item without a ``__dict__`` is encoded on every call.
861+
862+
Args:
863+
info: The catalog item (an ``ArrowSerializableDataclass``).
864+
865+
Returns:
866+
The item's Arrow IPC stream bytes.
867+
"""
868+
memo = getattr(info, "__dict__", None)
869+
if memo is None:
870+
return info.serialize_to_bytes() # type: ignore[no-any-return]
871+
encoded = memo.get(_ITEM_IPC_MEMO)
872+
if encoded is None:
873+
encoded = memo[_ITEM_IPC_MEMO] = info.serialize_to_bytes()
874+
return encoded # type: ignore[no-any-return]
875+
876+
846877
def _catalog_items_response(item_type: type) -> type:
847878
"""Generate a catalog items response class for the given `ArrowSerializableDataclass` type.
848879
@@ -869,13 +900,13 @@ class _Response(ArrowSerializableDataclass):
869900

870901
@staticmethod
871902
def from_infos(infos: list) -> _Response: # type: ignore[type-arg]
872-
return _Response(items=[info.serialize_to_bytes() for info in infos])
903+
return _Response(items=[_item_ipc_bytes(info) for info in infos])
873904

874905
@staticmethod
875906
def from_optional(info: object | None) -> _Response:
876907
if info is None:
877908
return _Response(items=[])
878-
return _Response(items=[info.serialize_to_bytes()]) # type: ignore[attr-defined]
909+
return _Response(items=[_item_ipc_bytes(info)])
879910

880911
def to_infos(self) -> list: # type: ignore[type-arg]
881912
return [item_type.deserialize_from_bytes(b) for b in self.items] # type: ignore[attr-defined]

0 commit comments

Comments
 (0)