Skip to content

[feature] Hash shared nested models once in cache keys and data tokens (changes all cache keys) - #302

Merged
timkpaine merged 3 commits into
mainfrom
feature/tokenize-nested-digest
Nov 30, 2026
Merged

timkpaine merged 3 commits into
mainfrom
feature/tokenize-nested-digest

Conversation

@ptomecek

@ptomecek ptomecek commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Description

cache_key() tokenized model_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.model generates. 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, but normalize_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:

  • A normalize_token handler registered for a model class now applies inside cache keys.
  • A ccflow model class that sets polymorphic_serialization=False is still digested on its own, although its parent would serialize it by the field's declared type.
  • A Python model_dump() override on the keyed model is not used; the compiled serializer is.

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactor / code cleanup
  • CI / build configuration
  • Other (describe below)

Performance: faster cache keys and data tokens for models with shared nested models.

Checklist

  • Linting passes (make lint): passed.
  • Tests pass (make test): 1636 passed, 3 skipped, 7 failed. The 7 failures are the lerna tests in ccflow/tests/test_config.py, which fail the same way on main in this environment because lerna is 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).
  • New tests added for new functionality
  • Documentation updated (if applicable)
  • Changelog / version bump (if applicable)

@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.94872% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.66%. Comparing base (424a17f) to head (c63fa2e).

Files with missing lines Patch % Lines
ccflow/tests/utils/test_tokenize.py 97.03% 4 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ptomecek ptomecek changed the title [feature] Hash shared nested models once in cache keys and data tokens [feature] Hash shared nested models once in cache keys and data tokens (changes all cache keys) Nov 29, 2026
@ptomecek
ptomecek force-pushed the feature/tokenize-nested-digest branch from f3be065 to 4af2a9f Compare November 30, 2026 13:34
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
ptomecek force-pushed the feature/tokenize-nested-digest branch from 4af2a9f to 8ed4384 Compare November 30, 2026 14:17
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

    1 files  ± 0      1 suites  ±0   2m 46s ⏱️ +21s
1 650 tests +15  1 648 ✅ +18  2 💤  - 3  0 ❌ ±0 
1 737 runs  +15  1 735 ✅ +18  2 💤  - 3  0 ❌ ±0 

Results for commit c63fa2e. ± Comparison against base commit 424a17f.

This pull request skips 1 and un-skips 4 tests.
ccflow.tests.enums.test_pydantic ‑ test_json_schema_no_csp
ccflow.tests.enums.test_pydantic ‑ test_json_schema_csp
ccflow.tests.examples.test_calculator.TestCalculatorCli ‑ test_cli
ccflow.tests.examples.test_etl.TestEtl ‑ test_cli
ccflow.tests.utils.test_tokenize.TestAdversarialFixes ‑ test_function_docstring_still_stripped

♻️ 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
ptomecek force-pushed the feature/tokenize-nested-digest branch from 8ed4384 to 9164c92 Compare November 30, 2026 14:30
@ptomecek
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>
@timkpaine
timkpaine merged commit f43013d into main Nov 30, 2026
20 checks passed
@timkpaine
timkpaine deleted the feature/tokenize-nested-digest branch November 30, 2026 19:13
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