github-actions[bot] commented on code in PR #68498:
URL: https://github.com/apache/doris/pull/68498#discussion_r4135916156


##########
be/src/core/value/quantile_state.cpp:
##########
@@ -28,7 +31,81 @@
 #include "util/tdigest.h"
 #include "util/unaligned.h"
 
+#ifdef BE_TEST
+#include "cpp/sync_point.h"
+#endif
+
 namespace doris {
+
+// Shares a digest across QuantileState copies and detaches it before sample
+// writes. Readers use shared locks; compression uses an exclusive lock.
+struct QuantileState::TDigestHolder {
+    explicit TDigestHolder(float compression) : digest(compression) {}
+    TDigestHolder(const TDigestHolder& other) : digest(other.digest) {}
+
+    std::shared_lock<std::shared_mutex> lock_processed_digest() {
+        std::shared_lock read_lock(mutex);
+        if (digest.have_unprocessed()) {
+            read_lock.unlock();
+            {
+                std::unique_lock write_lock(mutex);
+                if (digest.have_unprocessed()) {
+                    digest.compress();
+                }
+            }
+            read_lock.lock();
+        }
+        return read_lock;
+    }
+
+    TDigest digest;
+    std::shared_mutex mutex;
+};
+
+QuantileState QuantileState::copy_for_result() const {
+    if (_type != TDIGEST) {
+        return *this;
+    }
+    QuantileState result(_compression);
+    result._type = TDIGEST;
+    {
+        std::shared_lock lock(_tdigest_ptr->mutex);
+        result._tdigest_ptr = std::make_shared<TDigestHolder>(*_tdigest_ptr);
+    }
+    result._tdigest_ptr->digest.compact();

Review Comment:
   [P2] Avoid sorting the full window prefix for each saved state. In a growing 
`quantile_union(q)` window, the analytic sink saves a result after each row. 
This call copies the live digest, and `compact()` sorts its entire unprocessed 
prefix; because only the copy is compressed, the next row sorts that prefix 
again (up to the 80,000-centroid write threshold at compression 10000). The 
added BE test's dirty seed and 1000 saved rows therefore drive roughly 4.6 
million centroid visits to the sort. Process the live holder under its mutex 
before copying, or reuse a processed snapshot while preserving independent 
saved results. This is a new `quantile_union` result-insertion path, distinct 
from the older `percentile_approx` cache thread.



-- 
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