Skip to content

perf(helpers): 4.7x faster is_typed_dict, 14% faster TypedDict schemas - #385

Open
dexhunter wants to merge 1 commit into
Fatal1ty:masterfrom
dexhunter:perf/faster-is-typed-dict
Open

dexhunter wants to merge 1 commit into
Fatal1ty:masterfrom
dexhunter:perf/faster-is-typed-dict

Conversation

@dexhunter

@dexhunter dexhunter commented Nov 7, 2026 •

Copy link
Copy Markdown

TL;DR

  • Speeds up is_typed_dict in mashumaro/core/meta/helpers.py, which JSON schema building for TypedDicts calls very often.
  • Replaces two contextlib.suppress blocks with getattr(..., None) and one membership test on type(typ).
  • Inherited PEP 728 TypedDict schema benchmark: 96.7 ms -> 82.8 ms, 14% faster.
  • Tested with the full test suite: 32815 passed, 7 skipped.

Refs #384

What & Why

Before, each call entered a suppress(AttributeError) context manager once per module. It also called type(typ) again in each pass of the loop. The context manager costs more than the type check itself. Schema building calls this function many times per TypedDict.

Now the function reads _TypedDictMeta from typing_extensions and typing with getattr and a None default. Then it checks type(typ) against both values in one tuple. A missing attribute gives None, and no type is None, so the result stays False. The function still returns a real bool.

Changes

  • is_typed_dict: use getattr defaults and one in check instead of a loop with suppress.

Results

Python 3.12.13, typing_extensions 4.16.0, Linux x86_64, best of 7 runs on an isolated core:

Situation Before After Change
is_typed_dict on a typing_extensions TypedDict 549 ns 117 ns 4.7x faster
is_typed_dict on a typing.TypedDict 302 ns 121 ns 2.5x faster
is_typed_dict on int (not a TypedDict) 536 ns 127 ns 4.2x faster
build_json_schema on an inherited PEP 728 TypedDict 75.8 µs 64.4 µs 15% faster
Schema hierarchy from test_jsonschema_for_inherited_pep_728_typed_dict 96.7 ms 82.8 ms 14% faster
from_dict on a plain dataclass (does not call it) 488 ns 489 ns no change

Why this is safe and easy to maintain

The change touches one function and makes it shorter. It keeps the same result for every input, also when a module has no _TypedDictMeta. The JSON schema, data type, dialect and meta tests cover it, and all of them pass.

Validation

  • Full test suite: pytest tests -> 32815 passed, 7 skipped.
  • Benchmark tests: pytest tests/test_jsonschema/test_jsonschema_generation.py tests/test_data_types.py tests/test_dialect.py tests/test_meta.py -> 32212 passed, 3 skipped.
  • Schema hierarchy timing: median of 3 runs, each run times the old and new code in turn.
  • Short reproduction: python -m timeit -s "from typing_extensions import TypedDict; from mashumaro.core.meta.helpers import is_typed_dict as f; T = TypedDict('T', {'x': int})" "f(T)"

AI assistance

This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.

Weco share link: https://dashboard.weco.ai/share/Ymrt0jrzsPyTgP7VzYqS-B5U_xXZLzb4

is_typed_dict entered a contextlib.suppress block for each of typing and
typing_extensions on every call. JSON schema building for TypedDicts
calls it many times, so the context managers cost about 15% of the
inherited PEP 728 TypedDict schema benchmark.

Read both _TypedDictMeta attributes with getattr and a None default and
check type(typ) against them in one membership test. The result is the
same, also when a module has no _TypedDictMeta.

Co-Authored-By: Aiden <aiden@weco.ai>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant