Skip to content

feat: Use LazyLock for static schema caching in AvroSchema impl#90

Merged
lerouxrgd merged 3 commits intolerouxrgd:masterfrom
flxo:static-schema
Nov 29, 2025
Merged

feat: Use LazyLock for static schema caching in AvroSchema impl#90
lerouxrgd merged 3 commits intolerouxrgd:masterfrom
flxo:static-schema

Conversation

@flxo
Copy link
Copy Markdown
Contributor

@flxo flxo commented Nov 27, 2025

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

The PR includes a criterion benchmark. I'm happy to remove that before merging but kept for now as reference.

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
@Kriskras99
Copy link
Copy Markdown
Contributor

Kriskras99 commented Nov 28, 2025

I did not expect that this would have so much of an impact because of the .clone(), good that you tried it out!

I would suggest using a LazyLock instead of a OnceLock, as a OnceLock is designed for use with different init functions while LazyLock is designed for use with a "static" init function.

Also, you should use ::std instead of std in case a user overrides the std name in their file.

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
@flxo
Copy link
Copy Markdown
Contributor Author

flxo commented Nov 28, 2025

I would suggest using a LazyLock instead of a OnceLock, as a OnceLock is designed for use with different init functions while LazyLock is designed for use with a "static" init function.

thanks for the hint. Valid point - I updated the PR.

Also, you should use ::std instead of std in case a user overrides the std name in their file.

Updated.

@flxo flxo changed the title feat: Use OnceLock for static schema caching in AvroSchema impl feat: Use LazyLock for static schema caching in AvroSchema impl Nov 28, 2025
@lerouxrgd
Copy link
Copy Markdown
Owner

Thanks for this good PR too ! After fixing formatting it should be good to merge, let's keep the criterion benchmark :)

@lerouxrgd lerouxrgd merged commit d44b237 into lerouxrgd:master Nov 29, 2025
2 checks passed
@lerouxrgd
Copy link
Copy Markdown
Owner

Thanks !

@flxo flxo deleted the static-schema branch December 1, 2025 07:51
@flxo
Copy link
Copy Markdown
Contributor Author

flxo commented Dec 1, 2025

Thanks for merging and the crates.io release. I just noticed that a additional fn that returns &Schema could be beneficial. This would be in addition to the AvroSchema impl. For using apache-avro there's not an owned version of Schema needed and a ref is enough. This would avoid the clone.

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.

3 participants