Skip to content

perf(schema): compute optionally_keyed_by fields once per Schema class - #1049

Merged
jcristau merged 3 commits into
taskcluster:mainfrom
marco-c:schema-cache-keyed-by-fields
Sep 24, 2026
Merged

jcristau merged 3 commits into
taskcluster:mainfrom
marco-c:schema-cache-keyed-by-fields

Conversation

@marco-c

@marco-c marco-c commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Schema.post_init walked every annotation of the class and called get_origin() and get_args() on each of them to find the fields using optionally_keyed_by, every time an instance was created. The answer only depends on the class, so it is now computed once per class and cached.

Creating an instance of a 10-field schema with two keyed-by fields takes 4.9us instead of 8.3us. In Firefox's task graph generation (38,223 tasks, ./mach taskgraph full -j 8), the full task set is generated ~1-2s sooner (roughly 8%).

Schema.__post_init__ walked every annotation of the class and called
get_origin() and get_args() on each of them to find the fields using
optionally_keyed_by, every time an instance was created. The answer only
depends on the class, so it is now computed once per class and cached.

Creating an instance of a 10-field schema with two keyed-by fields takes
4.9us instead of 8.3us. In Firefox's task graph generation (38,223
tasks, `./mach taskgraph full -j 8`), the full task set
is generated ~1-2s sooner (roughly 8%).
@marco-c
marco-c requested a review from a team as a code owner September 23, 2026 13:05
@marco-c
marco-c requested a review from bhearsum September 23, 2026 13:05
Comment thread src/taskgraph/util/schema.py Outdated

@jcristau jcristau left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+wc, thanks

Comment thread src/taskgraph/util/schema.py Outdated
Comment on lines +373 to +374
# Find the fields that use `optionally_keyed_by` once per class, rather
# than every time an instance is validated.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This in the commit message already I don't think it is needed as a comment.

Comment thread test/test_util_schema.py
TestSchema.validate({"field": {"by-foo": {"a": "b"}}})


def test_optionally_keyed_by_per_class():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be useful to have a test with a subclass, to ensure keyed-by is found for fields defined in both?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was actually a bug here, so added test and fixed

@marco-c
marco-c force-pushed the schema-cache-keyed-by-fields branch from 8e32f34 to f11aab5 Compare September 24, 2026 08:59
@jcristau
jcristau merged commit 064375f into taskcluster:main Sep 24, 2026
22 checks passed
@marco-c
marco-c deleted the schema-cache-keyed-by-fields branch September 24, 2026 09:59
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.

2 participants