tisonkun opened a new issue, #366:
URL: https://github.com/apache/datasketches-rust/issues/366

   ## Summary
   
   In `datasketches/src/tdigest/sketch.rs`, all statistical query methods on 
`TDigestMut` (`cdf`, `pmf`, `rank`, `quantile`, `quantiles`) and 
`TDigestMut::serialize` currently take `&mut self`. Every other mutable sketch 
in the crate (`KllSketch`, `ReqSketch`, `HllSketch`, `CpcSketch`, 
`CountMinSketch`, `FrequentItemsSketch`, `ThetaSketch`, `TupleSketch`) as well 
as the immutable `TDigest` type accept `&self` for queries and serialization.
   
   ## Why `&mut self` Is Currently Required
   
   `TDigestMut::view(&mut self)` and `TDigestMut::serialize(&mut self)` 
currently call private `self.compress()`. When `self.buffer.unmerged_len() > 
0`, `compress()` drains `TDigestBuffer`'s unmerged tail, sorts centroids, 
compacts into the compressed prefix, updates `self.compressed_weight`, and 
flips `self.reverse_merge`. Requiring `&mut self` prevents querying or 
serializing a `TDigestMut` behind a shared reference (`&TDigestMut`, 
`Arc<TDigestMut>`, etc.) without first cloning or calling `freeze()`.
   
   ## Concrete Implementation Options & Tradeoffs
   
   ### Option A: Interior-mutable compressed snapshot (`OnceLock`) invalidated 
on mutation + explicit `compress(&mut self)`
   
   - Store a `OnceLock<...>` cache of compressed centroids on `TDigestMut`, 
cleared on `&mut self` mutators (`update`, `merge`) via `take()` / `get_mut()`, 
following the same pattern already used by `hll::Array8` in 
`datasketches/src/hll/array8.rs`.
   - When `self.buffer.unmerged_len() == 0` (empty, freshly deserialized 
compressed image, or already compacted), `&self` queries and `serialize(&self)` 
read `self.buffer.compressed_centroids()` directly without allocating or 
populating `OnceLock`.
   - When `self.buffer.unmerged_len() > 0`, `OnceLock::get_or_init` computes 
and caches the compressed snapshot once, so back-to-back `&self` reads 
(`quantile`, `rank`, `cdf`, `pmf`, `serialize`) amortize compression work and 
remain thread-safe (`TDigestMut: Send + Sync`).
   - Expose `pub fn compress(&mut self)` explicitly so callers can compact the 
unmerged tail into the primary buffer on demand before cloning or releasing 
tail capacity.
   - **Tradeoffs**: Amortizes repeated `&self` queries on dirty digests without 
changing `&mut self` update/merge speed; adds one `OnceLock` field and 
temporary memory for the cached centroid slice when queried with pending 
unmerged values until the next mutation or `compress()`. `Clone` behavior 
(`OnceLock<T: Clone>: Clone` clones cached state, or a custom `Clone` starts 
uncached) and repeated `serialize(&self)` vs alternating `reverse_merge` 
semantics need explicit specification: serializing via a non-mutating view 
keeps `self.reverse_merge` unchanged until an actual `&mut self` compression 
occurs, making repeated `serialize(&self)` calls deterministic.
   
   ### Option B: Non-mutating scratch compression on `&self` + explicit 
`compress(&mut self)`
   
   - When `self.buffer.unmerged_len() == 0`, read 
`self.buffer.compressed_centroids()` directly with zero allocation.
   - When `self.buffer.unmerged_len() > 0`, build a temporary compressed 
centroid vector in a local scratch allocation for each query or 
`serialize(&self)` call without storing a `OnceLock` cache on `TDigestMut`.
   - **Tradeoffs**: Zero extra struct footprint, zero cached retention, and 
trivial `Clone`; however, calling multiple read methods sequentially on an 
uncompacted `TDigestMut` repeats sorting and merging unless callers manually 
invoke `compress(&mut self)` first.
   
   ## Benchmark & Verification Criteria
   
   - Compare before/after performance using `benchmarks/tdigest/` (`update`, 
`compress`, `merge`, `query`, `serde`) under the methodology in 
`CONTRIBUTING.md` ("Fixed workloads and timing"):
     - Confirm zero regression on `TDigestMut::update` and `TDigestMut::merge` 
workloads.
     - Add or run repeated-query / serialize-after-update scenarios measuring 
cold vs repeated `&self` reads on a dirty `TDigestMut` (evaluating Option A vs 
Option B amortization).
   - Verify query results (`cdf`, `pmf`, `rank`, `quantile`, `quantiles`) on 
`TDigestMut` match `TDigestMut::freeze()` (`TDigest`) across empty, 
uncompacted, and compacted digests, and verify `TDigestMut::serialize(&self)` 
round-trips and C++/Java compatibility in 
`tests-integration/tests/serde_tests/tdigest.rs`.
   - Confirm `cargo x check`, `cargo x test`, `cargo x lint`, and `cargo x 
bench -- --test` pass, and record the signature change (`&mut self` to `&self`) 
in `CHANGELOG.md`.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to