Skip to content

Commit 0458880

Browse files
rustyconoverclaude
andcommitted
perf: cache the per-class FunctionArguments parse plan
_parse_arguments ran get_type_hints(args_class, include_extras=True) on every HTTP stream turn to rediscover which fields carry Arg descriptors. Nothing about that can change -- it is a pure function of the class's annotations -- and once the state token was split it was the largest single item left in a turn: 58ms per 400 turns, essentially the whole cost of _parse_arguments. Hoisted into a cached per-class plan, following the precedent _extract_argument_specs_cached already set for the same reason. The plan also precomputes _accepts_none() and the varargs union check, which were being redone per field per turn. Unbounded functools.cache is the right size here: both make_dataclass sites for FunctionArguments live in __init_subclass__, so keys are created once per class definition and are already retained by the function registry for the process's lifetime. Measured: rehydrate() 104us -> 19us, _parse_arguments ~87us -> 2.2us, producer turns 2,564 -> 3,154/s single-threaded and 1,194 -> 1,308/s at 8 threads. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 9fd97d0 commit 0458880

1 file changed

Lines changed: 140 additions & 49 deletions

File tree

‎vgi/table_function.py‎

Lines changed: 140 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
from collections.abc import Mapping
1414
from dataclasses import dataclass, is_dataclass
1515
from enum import Enum, auto
16+
from functools import cache
1617
from typing import (
1718
TYPE_CHECKING,
1819
Annotated,
1920
Any,
2021
ClassVar,
22+
NamedTuple,
2123
TypeVar,
2224
final,
2325
get_args,
@@ -82,6 +84,99 @@
8284
]
8385

8486

87+
class _ArgFieldPlan(NamedTuple):
88+
"""One ``FunctionArguments`` field, resolved once per class.
89+
90+
Everything here is derived purely from the field's ``Annotated`` type
91+
hint, so it is fixed for the life of the class — see
92+
:func:`_arg_field_plans` for why that matters.
93+
94+
Attributes:
95+
name: The dataclass field name.
96+
arg: The field's `Arg` descriptor, or ``None`` for a ``TableInput``
97+
sentinel field (which carries no wire value).
98+
is_table_input: True for a ``TableInput`` sentinel field.
99+
accepts_none: Whether the declared type admits ``None`` — i.e.
100+
whether SQL NULL is legal for this argument.
101+
varargs_is_union: Whether a varargs field's declared Arrow type is a
102+
union, which selects the tagged-scalar decode path.
103+
104+
"""
105+
106+
name: str
107+
arg: Arg[Any] | None
108+
is_table_input: bool
109+
accepts_none: bool
110+
varargs_is_union: bool
111+
112+
113+
@cache
114+
def _arg_field_plans(args_class: type) -> tuple[_ArgFieldPlan, ...]:
115+
"""Resolve a ``FunctionArguments`` class into its per-field parse plan.
116+
117+
``_parse_arguments`` runs on every HTTP stream turn — the state token
118+
carries no transient state, so a rehydrated worker rebuilds the typed
119+
arguments from scratch each time. Deriving the plan meant a
120+
``typing.get_type_hints(include_extras=True)`` per turn, which measured
121+
at 58 ms per 400 turns: essentially the entire cost of
122+
``_parse_arguments``, and the largest single item left in a turn once
123+
the state token was split.
124+
125+
None of it can change: the plan is a function of the class's
126+
annotations. So it is memoized on the class, exactly as
127+
``_extract_argument_specs_cached`` memoizes argument specs for the same
128+
reason.
129+
130+
Unbounded (``functools.cache``) is the right size — the keys are
131+
``FunctionArguments`` classes, which the worker's function registry
132+
already holds for the process's lifetime, so this adds no retention that
133+
did not already exist. It is also thread safe, which matters because
134+
workers serve requests on a pool.
135+
136+
Fields whose annotation carries no `Arg` are omitted, matching the
137+
original behaviour: they are left to the dataclass's own default.
138+
139+
Args:
140+
args_class: The ``FunctionArguments`` dataclass type.
141+
142+
Returns:
143+
The per-field plans, in declaration order.
144+
145+
"""
146+
plans: list[_ArgFieldPlan] = []
147+
for attr_name, hint in get_type_hints(args_class, include_extras=True).items():
148+
if get_origin(hint) is not Annotated:
149+
continue
150+
type_args = get_args(hint)
151+
base_type = type_args[0]
152+
if base_type is TableInput:
153+
plans.append(
154+
_ArgFieldPlan(
155+
name=attr_name,
156+
arg=None,
157+
is_table_input=True,
158+
accepts_none=False,
159+
varargs_is_union=False,
160+
)
161+
)
162+
continue
163+
for meta in type_args[1:]:
164+
if isinstance(meta, Arg):
165+
plans.append(
166+
_ArgFieldPlan(
167+
name=attr_name,
168+
arg=meta,
169+
is_table_input=False,
170+
accepts_none=_accepts_none(base_type),
171+
varargs_is_union=(
172+
meta.varargs and meta.arrow_type is not None and pa.types.is_union(meta.arrow_type)
173+
),
174+
)
175+
)
176+
break
177+
return tuple(plans)
178+
179+
85180
@dataclass(frozen=True, slots=True)
86181
class TableCardinality(ArrowSerializableDataclass):
87182
"""Cardinality hints for query optimization.
@@ -782,59 +877,55 @@ def _parse_arguments(args_class: type[TArgs], arguments: Arguments, *, blended:
782877
An instance of ``args_class`` populated from ``arguments``.
783878
784879
"""
785-
hints = get_type_hints(args_class, include_extras=True)
786880
kwargs: dict[str, Any] = {}
787881

788-
for attr_name, hint in hints.items():
789-
if get_origin(hint) is not Annotated:
882+
# Widened to a bare ``type`` for typeshed's benefit: ``functools.cache``
883+
# types every argument as ``Hashable``, which ``type[TArgs]`` does not
884+
# statically satisfy even though every class object is hashable.
885+
plan_key: type = args_class
886+
for plan in _arg_field_plans(plan_key):
887+
if plan.is_table_input:
888+
kwargs[plan.name] = TableInput()
790889
continue
791-
# Check if this is a TableInput parameter (sentinel, no real data)
792-
base_type = get_args(hint)[0]
793-
if base_type is TableInput:
794-
kwargs[attr_name] = TableInput()
890+
meta = plan.arg
891+
assert meta is not None # only a TableInput field has no Arg
892+
# Blended: skip positional/varargs Args — they are the input
893+
# columns (read from batch), absent from the wire args. Set the
894+
# field to None so construction succeeds; the worker reads the
895+
# value from ``batch``, never from ``params.args.<name>``.
896+
if blended and isinstance(meta.position, int):
897+
kwargs[plan.name] = None
795898
continue
796-
for meta in get_args(hint)[1:]:
797-
if isinstance(meta, Arg):
798-
# Blended: skip positional/varargs Args — they are the input
799-
# columns (read from batch), absent from the wire args. Set the
800-
# field to None so construction succeeds; the worker reads the
801-
# value from ``batch``, never from ``params.args.<name>``.
802-
if blended and isinstance(meta.position, int):
803-
kwargs[attr_name] = None
804-
break
805-
if meta.varargs:
806-
# Varargs: collect remaining positional args as raw pa.Scalar
807-
# objects (e.g. constant_columns reads .type / pa.repeat off
808-
# them). Union-typed varargs are the exception: decode each
809-
# scalar to a TaggedUnion so the active member discriminator
810-
# is preserved — matching how non-vararg union args resolve
811-
# via Arguments.get()/_scalar_to_py(). Keyed on the declared
812-
# arrow_type so the raw-scalar contract is untouched otherwise.
813-
assert isinstance(meta.position, int)
814-
varargs_scalars = arguments.positional[meta.position :]
815-
if meta.arrow_type is not None and pa.types.is_union(meta.arrow_type):
816-
kwargs[attr_name] = tuple(
817-
_scalar_to_py(s) if s is not None else None for s in varargs_scalars
818-
)
819-
else:
820-
kwargs[attr_name] = tuple(varargs_scalars)
821-
else:
822-
value = arguments.get(meta.position, default=meta.default)
823-
# Reject SQL NULL for non-Optional Args. Without this,
824-
# None silently propagated through validation and
825-
# crashed deep in the user's process()/update() with
826-
# an opaque Python ``TypeError`` (e.g. ``'<=' not
827-
# supported between instances of NoneType and int``)
828-
# that surfaced in the C++ extension as a worker
829-
# exception with no hint at the cause.
830-
if value is None and not _accepts_none(base_type):
831-
raise meta._reject_none()
832-
# Run Arg constraint validation (ge/le/gt/lt/choices/pattern).
833-
# Skip for None — accepted via Optional[T].
834-
if value is not None:
835-
meta._validate(value)
836-
kwargs[attr_name] = value
837-
break
899+
if meta.varargs:
900+
# Varargs: collect remaining positional args as raw pa.Scalar
901+
# objects (e.g. constant_columns reads .type / pa.repeat off
902+
# them). Union-typed varargs are the exception: decode each
903+
# scalar to a TaggedUnion so the active member discriminator
904+
# is preserved — matching how non-vararg union args resolve
905+
# via Arguments.get()/_scalar_to_py(). Keyed on the declared
906+
# arrow_type so the raw-scalar contract is untouched otherwise.
907+
assert isinstance(meta.position, int)
908+
varargs_scalars = arguments.positional[meta.position :]
909+
if plan.varargs_is_union:
910+
kwargs[plan.name] = tuple(_scalar_to_py(s) if s is not None else None for s in varargs_scalars)
911+
else:
912+
kwargs[plan.name] = tuple(varargs_scalars)
913+
else:
914+
value = arguments.get(meta.position, default=meta.default)
915+
# Reject SQL NULL for non-Optional Args. Without this,
916+
# None silently propagated through validation and
917+
# crashed deep in the user's process()/update() with
918+
# an opaque Python ``TypeError`` (e.g. ``'<=' not
919+
# supported between instances of NoneType and int``)
920+
# that surfaced in the C++ extension as a worker
921+
# exception with no hint at the cause.
922+
if value is None and not plan.accepts_none:
923+
raise meta._reject_none()
924+
# Run Arg constraint validation (ge/le/gt/lt/choices/pattern).
925+
# Skip for None — accepted via Optional[T].
926+
if value is not None:
927+
meta._validate(value)
928+
kwargs[plan.name] = value
838929

839930
return args_class(**kwargs)
840931

0 commit comments

Comments
 (0)