Skip to content

Conversation

@f0ster
Copy link

@f0ster f0ster commented Oct 27, 2025

Replace SHA256 with xxhash for cache key generation

Switched from SHA256 to xxhash64 for cache key hashing. SHA256 is cryptographically secure but overkill for cache keys where we just need fast, collision-resistant hashing.

Performance

  • 2.4x faster cache key generation (0.50µs → 0.21µs)
  • 59% time reduction on every cache operation

Changes

  • dspy/clients/cache.py: Replace sha256 with xxhash.xxh64
  • tests/clients/test_cache.py: Update hash length test, add determinism check
  • tests/benchmarks/test_cache_key_performance.py: Benchmark suite with before/after comparison

Testing

  • ✅ All 12 existing cache tests pass
  • ✅ Cache keys remain deterministic
  • ✅ 64-bit hash space sufficient for realistic workloads
  • ✅ No behavioral changes

xxhash is already a dependency, so this is a pure performance win with zero additional overhead.

Copy link
Collaborator

@chenmoneygithub chenmoneygithub left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

Changing hashing algorithm will invalidate users' local cache though, so we shouldn't touch it unless something is broken. Btw it's true that scale-wise the time cost reduction is significant, but the absolute time cost is very small for cache key computation.

Thank again for the contribution, really appreciate it!

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