refactor(tdigest): convert the flags for reverse in TDigest::Rank to a compile-time argument#3268
Conversation
| const std::vector<Centroid>& centroids_; | ||
| }; | ||
|
|
||
| class ReverseIterator final : public BaseIterator { |
There was a problem hiding this comment.
Could we have something like template <bool Reverse> class Iterator instead of virtual functions?
There was a problem hiding this comment.
Of course, I have refactored the code.
|
Hi @donghao526 , Thanks very much for your effort! ❤ Best Regards, |
Hi @LindaSummer Thanks a lot. You can review the code at your convenience.😊 |
LindaSummer
left a comment
There was a problem hiding this comment.
Hi @donghao526 ,
Thanks very much for your contribution! ❤
Generally LGTM.
Left some comments.
Best Regards,
Edward
| Iterator(IterType iter, const std::vector<Centroid>& centroids) : iter_(iter), centroids_(centroids) {} | ||
| std::unique_ptr<Iterator> Clone() const { | ||
| if constexpr (Reverse) { | ||
| if (iter_ != centroids_.crend()) { |
There was a problem hiding this comment.
Maybe we could add a private method like below.
class Ietrator {
...
private:
template <typename Container>
decltype(auto) get_cbegin_iter(Container centroids) const {
if constexpr (Reverse) {
return centroids.rcbegin();
} else {
return centroids.cbegin() ;
}
}
template <typename Container>
decltype(auto) get_cend_iter(Container centroids) const {
if constexpr (Reverse) {
return centroids.rcend();
} else {
return centroids.cend() ;
}
}Then we could unify the centroids.cebgin() and centroids.cend().
This could help us reduce the constexpr if in every iterator getter.
There was a problem hiding this comment.
Good suggestion, I will refactor it.
Thanks!😊
| return TDigestRankImpl<TD, false>(std::forward<TD>(td), inputs, result); | ||
| } | ||
| } | ||
| } No newline at end of file |
There was a problem hiding this comment.
Add an endline for this file.
Co-authored-by: Edward Xu <xuxiangad@foxmail.com>
… into refactor/tdigest-rank
| template <bool Reverse> | ||
| rocksdb::Status TDigest::Rank(engine::Context& ctx, const Slice& digest_name, const std::vector<double>& inputs, |
There was a problem hiding this comment.
I think maybe we can just expose two non-template function TDigest::Rank and TDigest::RevRank so that we can put the definition of this function into the .cc file instead of header?
PragmaTwice
left a comment
There was a problem hiding this comment.
Generally looks good. I'll leave it to @LindaSummer.
LindaSummer
left a comment
There was a problem hiding this comment.
Hi @donghao526 ,
Thanks very much for your contribution! ❤
LGTM. Left some optional nitpick comments.
Best Regards,
Edward
| }; | ||
|
|
||
| std::unique_ptr<Iterator> Begin() const { | ||
| if constexpr (Reverse) { |
There was a problem hiding this comment.
| if constexpr (Reverse) { | |
| return std::make_unique<Iterator>(getCbeginIter(centroids_), centroids_); |
Maybe here could also use the util.
There was a problem hiding this comment.
As the Begin and End functions are outside the Iterator class, I made getCbeginIter and getCendIter static. Is this a good solution?
There was a problem hiding this comment.
I think the getbegin and getend are both stateless, static is ok.
maybe they could be a free inline function protected by a detail private namespace if you prefer this way.
| } | ||
| std::unique_ptr<Iterator> End() const { | ||
| if (centroids_.empty()) { | ||
| if constexpr (Reverse) { |
… into refactor/tdigest-rank
LindaSummer
left a comment
There was a problem hiding this comment.
Hi @donghao526 ,
Thanks very much for your contribution!❤
LGTM, left one comment for namespace design.
Best Regards,
Edward
|
|
||
| namespace redis { | ||
|
|
||
| namespace detail { |
There was a problem hiding this comment.
To reduce conflict with other symbols in redis namespace, it would be better guarded by a namespace like tdigest.
After we refactor this in cc file, this could just be in an anonymous namespace.
There was a problem hiding this comment.
Hi @LindaSummer
- I have exposed two non-template functions, TDigest::Rank and TDigest::RevRank, and placed their definitions in the .cc file.
- I have placed the GetCBeginIter and GetCEndIter functions inside an anonymous namespace.
Thanks a lot!
… into refactor/tdigest-rank
…etter organization
…Rank methods into prepareRankData function
… into refactor/tdigest-rank
Thanks for your review😊 |
|
Hi @donghao526 , The CI has some issue and I'm working on #3290 . This PR will be merged after this issue solved. Best Regards, |
|



The reverse flag in the current
TDigest::Rankimplementation is a runtime value. Therefore, it should be converted to a compile-time argument.