perf(core_helpers): fast-path dicts and lists in the checked JSON helpers

as_str_mapping now tries the concrete dict check before the Mapping ABC
check, and iter_items returns lists before the runtime-checkable Protocol
check. Both return the same object as before, and roughly halve the
overhead CodSpeed flagged on the A2A text-extraction benchmarks.
This commit is contained in:
mateo-berri 2026-11-08 00:58:16 +00:00
parent 5815c092f0
commit 463e3ccdf7

View file

@ -102,7 +102,7 @@ _LAX_BOOL: Final = TypeAdapter(bool)
def as_str_mapping(value: object) -> Mapping[str, object] | None:
return value if isinstance(value, Mapping) else None
return value if isinstance(value, (dict, Mapping)) else None
def as_object_list(value: object) -> Sequence[object] | None:
@ -174,6 +174,9 @@ def keyword_arguments(value: object, target: type[object]) -> KeywordArguments:
def iter_items(value: object) -> Iterable[object]:
listed: Final = as_object_list(value)
if listed is not None:
return listed
if isinstance(value, _SupportsIter):
return value
if isinstance(value, _SupportsGetItem):