feat: Use LazyLock for static schema caching in AvroSchema impl#90
feat: Use LazyLock for static schema caching in AvroSchema impl#90lerouxrgd merged 3 commits intolerouxrgd:masterfrom
Conversation
Changed the generated `get_schema()` implementation to cache parsed schemas using `std::sync::OnceLock` instead of parsing on every call. Performance improvement (measured with criterion on a MacBook Pro M4): - Without cache: ~11.77 µs per call - With cache: ~701 ns per call - Speedup: ~16.8x faster The cache uses lazy initialization with lock-free reads after the first access, providing significant performance gains for applications that frequently call get_schema() with minimal memory overhead. Changes: - src/templates.rs: Updated template to use std::sync::OnceLock - tests/schemas/*: Updated expected test outputs - Added benchmark to measure performance impact
|
I did not expect that this would have so much of an impact because of the I would suggest using a Also, you should use |
LazyLock provides cleaner syntax and is more idiomatic for lazy initialization patterns. The initialization closure is passed directly to LazyLock::new(), eliminating the need for get_or_init() calls. Performance remains equivalent (~15x faster than no caching), with benchmark showing cached schema access at ~687ns vs ~10.8µs uncached. - src/templates.rs: Updated template to use ::std::sync::LazyLock - benches/schema_cache.rs: Updated benchmark implementation - tests/schemas/*.rs: Regenerated with new template
thanks for the hint. Valid point - I updated the PR.
Updated. |
|
Thanks for this good PR too ! After fixing formatting it should be good to merge, let's keep the criterion benchmark :) |
|
Thanks ! |
|
Thanks for merging and the crates.io release. I just noticed that a additional fn that returns |
Changed the generated
get_schema()implementation to cache parsed schemas usingstd::sync::OnceLockinstead of parsing on every call.Performance improvement (measured with criterion on a MacBook Pro M4):
The cache uses lazy initialization with lock-free reads after the first access, providing significant performance gains for applications that frequently call get_schema() with minimal memory overhead.
Changes:
The PR includes a
criterionbenchmark. I'm happy to remove that before merging but kept for now as reference.