Repository navigation
[feature] Hash shared nested models once in cache keys and data tokens (changes all cache keys) - #302
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #302 +/- ##
==========================================
+ Coverage 93.63% 93.66% +0.03%
==========================================
Files 205 205
Lines 23158 23341 +183
Branches 1519 1533 +14
==========================================
+ Hits 21683 21862 +179
- Misses 1181 1185 +4
Partials 294 294 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ptomecek
force-pushed
the
feature/tokenize-nested-digest
branch
from
November 30, 2026 13:34
f3be065 to
4af2a9f
Compare
cache_key() tokenized model_dump(), which copies every shared nested model, so a config graph that references the same models many times produced a dump of several MB and took seconds to key. cache_key() now digests a model's dump with each field that holds only ccflow models replaced by those models' digests, computed once per key. This applies on pydantic>=2.13, where ccflow models serialize as their runtime type; other fields, and older pydantic versions, use the dump as before. compute_data_token() also hashes each nested model once per call, by digest. Every cache key and every data token of a model changes once. Signed-off-by: Pascal Tomecek <40371786+ptomecek@users.noreply.github.com>
ptomecek
force-pushed
the
feature/tokenize-nested-digest
branch
from
November 30, 2026 14:17
4af2a9f to
8ed4384
Compare
Contributor
Test Results 1 files ± 0 1 suites ±0 2m 46s ⏱️ +21s Results for commit c63fa2e. ± Comparison against base commit 424a17f. This pull request skips 1 and un-skips 4 tests.♻️ This comment has been updated with latest results. |
…pers in evaluators Split fields marked SkipValidation, and fields whose exclude_if is false for their current value, so shared dependencies of @Flow.model nodes are serialized once per key. Move the cache-key digest helpers next to cache_key(), test that fields with a field serializer or an Annotated serializer stay in the dump, and describe how cache keys use the compiled serializer. Signed-off-by: Pascal Tomecek <40371786+ptomecek@users.noreply.github.com>
ptomecek
force-pushed
the
feature/tokenize-nested-digest
branch
from
November 30, 2026 14:30
8ed4384 to
9164c92
Compare
ptomecek
marked this pull request as ready for review
November 30, 2026 16:01
A field was replaced by its nested models' digests unless a serializer was visible on its annotation. A field_serializer("*"), a TypeAliasType wrapping an Annotated serializer, or a TypedDict member serializer went undetected, so two models differing only in what that serializer exposed got the same cache key.
Now a field is replaced only when its annotation is built from ccflow model classes, Any, None and builtin scalars inside lists, tuples, dicts and unions, and no field_serializer("*") is defined.
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
cache_key()tokenizedmodel_dump(), which copies every shared nested model. A config graph that references the same models many times (for example through registry references) produced a dump of several MB, and keying one model took seconds.compute_data_token(model)also normalized a shared model again at every reference.cache_key()digests a model's compiled dump with each field that holds only ccflow models replaced by those models' digests, computed once per key. A field is replaced only when its annotation is plain (model classes, lists, tuples, dicts, unions,SerializeAsAny,InstanceOf,SkipValidation), it has no serializer, and it is not excluded for its current value. This covers the dependency fields that@Flow.modelgenerates. Every other field stays in the dump. This applies on pydantic>=2.13, where ccflow models serialize as their runtime type, so a child's own dump is the one its parent would contain. Older pydantic versions use the full dump as before.compute_data_token()represents a nested model by a digest and normalizes each model object once per call. Models are still hashed by their field values, butnormalize_token()now returns("pydantic", type_path, digest)for a model instead of its inline fields.On a config graph with heavily shared models (a 7 MB dump, most of it repeated), one cache key went from 4.4 s to 0.05 s, and
compute_data_token()from 1.4 s to 0.03 s. Keys are stable across processes.Every cache key, and every data token of a model, changes once with this PR, including on pydantic<2.13, where the dump is not split. A cache that stores keys across processes misses once after upgrading; old and new keys cannot collide.
Models without shared nested models pay a small per-model cost for the extra digests: tens of microseconds per key for small models, a few milliseconds for a key over about 1,100 distinct models.
Known differences from the
model_dump()-based key, left unhandled because they are rare:normalize_tokenhandler registered for a model class now applies inside cache keys.polymorphic_serialization=Falseis still digested on its own, although its parent would serialize it by the field's declared type.model_dump()override on the keyed model is not used; the compiled serializer is.Type of Change
Performance: faster cache keys and data tokens for models with shared nested models.
Checklist
make lint): passed.make test): 1636 passed, 3 skipped, 7 failed. The 7 failures are thelernatests inccflow/tests/test_config.py, which fail the same way onmainin this environment becauselernais not installed. The targeted suites (python -m pytest ccflow/tests/utils/test_tokenize.py ccflow/tests/evaluators) also pass on pydantic 2.11.10 (two expected skips).