perf: eliminate double JSON serialization in cache key hot path by ppraneth · Pull Request #7345 · tensorzero/tensorzero · GitHub
Skip to content

perf: eliminate double JSON serialization in cache key hot path#7345

Open
ppraneth wants to merge 5 commits intotensorzero:mainfrom
ppraneth:perf1
Open

perf: eliminate double JSON serialization in cache key hot path#7345
ppraneth wants to merge 5 commits intotensorzero:mainfrom
ppraneth:perf1

Conversation

@ppraneth
Copy link
Copy Markdown

Previously, ModelProviderRequest::get_cache_key() serialized the request twice: once via serde_json::to_value to produce a serde_json::Value, then again via .to_string() to get a JSON string for hashing. Between the two steps it also had to allocate the intermediate Value, find and remove the inference_id key from it, and then allocate the final String buffer.

This PR replaces that with a single streaming serialization pass directly into the blake3::Hasher (which implements std::io::Write), using serde_json::to_writer. To exclude inference_id from the hash without an intermediate allocation, it is now marked #[serde(skip)] on ModelInferenceRequest.


What changed

  • tensorzero-inference-types

    • Added #[serde(skip)] to the inference_id field on ModelInferenceRequest. The field is only serialized for cache key purposes and is intentionally excluded from the hash, so skipping it during serialization is correct and safe.
  • tensorzero-core/src/cache.rs

    • Replaced the three-step to_valueremoveto_string sequence with:
      serde_json::to_writer(&mut hasher, request)
  • Added a Criterion benchmark: benches/cache_key.rs for regression tracking.


Benchmark results (release mode, same machine)

Version Time
Before 11.4 µs
After 5.0 µs
Change ~57% faster (≈2.3× speedup)

✅ All 88 cache unit tests pass.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 22, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@ppraneth
Copy link
Copy Markdown
Author

I have read the Contributor License Agreement (CLA) and hereby sign the CLA.

github-actions Bot added a commit that referenced this pull request Apr 22, 2026
@ppraneth
Copy link
Copy Markdown
Author

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