This commit introduces a RAII-based mechanism to manage the
lifecycle of background threads, resolving an issue where temporary files
could be left behind if the program exits before caching is complete.
It also improves the performance of cache validation checks.
Changes:
- Hierarchical Cache Validation for Performance:
Cache validation is now performed in tiers for better performance. It first
attempts a near-instant check using a hash of file metadata. Only if
this fails does it fall back to the slower, full-file SHA256 hash
verification. This reduces subsequent startup times from milliseconds
to microseconds while maintaining correctness.
- Safe Background Thread Joining:
When a legacy dictionary is loaded via `from_zstd`, a background thread
is spawned for caching. Previously, this could cause the main thread to
block unexpectedly or leave temporary files if the program exited
quickly.
This is now resolved by implementing a RAII pattern:
- The `Dictionary::Owned` variant now holds an `Arc<JoinHandle>` to the
background thread.
- The `Drop` implementation for `Dictionary` joins the thread
only when it holds the last reference to the handle (`Arc::try_unwrap`).
- This ensures the caching process completes safely without blocking the
user's application flow unnecessarily, providing a "weak" guarantee that
balances safety and performance.
- Polished Error Handling in `Drop`:
If the background thread panics, the error is now reported using the
`log::error!` macro.