Skip to content

refactor(tdigest): convert the flags for reverse in TDigest::Rank to a compile-time argument#3268

Merged
LindaSummer merged 32 commits into
apache:unstablefrom
donghao526:refactor/tdigest-rank
Dec 13, 2025
Merged

refactor(tdigest): convert the flags for reverse in TDigest::Rank to a compile-time argument#3268
LindaSummer merged 32 commits into
apache:unstablefrom
donghao526:refactor/tdigest-rank

Conversation

@donghao526
Copy link
Copy Markdown
Contributor

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

Comment thread src/types/tdigest.h Outdated
Comment thread src/types/redis_tdigest.h Outdated
Comment thread src/types/redis_tdigest.h Outdated
Comment thread src/types/redis_tdigest.h Outdated
const std::vector<Centroid>& centroids_;
};

class ReverseIterator final : public BaseIterator {
Copy link
Copy Markdown
Member

@PragmaTwice PragmaTwice Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have something like template <bool Reverse> class Iterator instead of virtual functions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, I have refactored the code.

@LindaSummer
Copy link
Copy Markdown
Member

Hi @donghao526 ,

Thanks very much for your effort! ❤
I will go through this PR in three days.

Best Regards,
Edward

@donghao526
Copy link
Copy Markdown
Contributor Author

donghao526 commented Nov 23, 2025

Hi @donghao526 ,

Thanks very much for your effort! ❤ I will go through this PR in three days.

Best Regards, Edward

Hi @LindaSummer

Thanks a lot. You can review the code at your convenience.😊

Copy link
Copy Markdown
Member

@LindaSummer LindaSummer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @donghao526 ,

Thanks very much for your contribution! ❤

Generally LGTM.

Left some comments.

Best Regards,
Edward

Comment thread src/types/redis_tdigest.h Outdated
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()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, I will refactor it.
Thanks!😊

Comment thread src/types/redis_tdigest.h Outdated
Comment thread src/types/tdigest.h Outdated
return TDigestRankImpl<TD, false>(std::forward<TD>(td), inputs, result);
}
}
} No newline at end of file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an endline for this file.

Comment thread src/types/redis_tdigest.h Outdated
Comment on lines +231 to +232
template <bool Reverse>
rocksdb::Status TDigest::Rank(engine::Context& ctx, const Slice& digest_name, const std::vector<double>& inputs,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necesary in this PR.

Copy link
Copy Markdown
Member

@PragmaTwice PragmaTwice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good. I'll leave it to @LindaSummer.

LindaSummer
LindaSummer previously approved these changes Nov 27, 2025
Copy link
Copy Markdown
Member

@LindaSummer LindaSummer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @donghao526 ,

Thanks very much for your contribution! ❤

LGTM. Left some optional nitpick comments.

Best Regards,
Edward

Comment thread src/types/redis_tdigest.h Outdated
};

std::unique_ptr<Iterator> Begin() const {
if constexpr (Reverse) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if constexpr (Reverse) {
return std::make_unique<Iterator>(getCbeginIter(centroids_), centroids_);

Maybe here could also use the util.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the Begin and End functions are outside the Iterator class, I made getCbeginIter and getCendIter static. Is this a good solution?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/types/redis_tdigest.h Outdated
}
std::unique_ptr<Iterator> End() const {
if (centroids_.empty()) {
if constexpr (Reverse) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Copy Markdown
Member

@LindaSummer LindaSummer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @donghao526 ,

Thanks very much for your contribution!❤

LGTM, left one comment for namespace design.

Best Regards,
Edward

Comment thread src/types/redis_tdigest.h Outdated

namespace redis {

namespace detail {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

@donghao526 donghao526 Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @LindaSummer

  1. I have exposed two non-template functions, TDigest::Rank and TDigest::RevRank, and placed their definitions in the .cc file.
  2. I have placed the GetCBeginIter and GetCEndIter functions inside an anonymous namespace.

Thanks a lot!

Copy link
Copy Markdown
Member

@LindaSummer LindaSummer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @donghao526 ,

Thanks very much for your contribution! ❤

LGTM

Best Regards,
Edward

@LindaSummer LindaSummer enabled auto-merge (squash) December 7, 2025 05:15
@donghao526
Copy link
Copy Markdown
Contributor Author

Hi @donghao526 ,

Thanks very much for your contribution! ❤

LGTM

Best Regards,
Edward

Thanks for your review😊

@LindaSummer
Copy link
Copy Markdown
Member

Hi @donghao526 ,

The CI has some issue and I'm working on #3290 .

This PR will be merged after this issue solved.

Best Regards,
Edward

@LindaSummer LindaSummer merged commit 65db00a into apache:unstable Dec 13, 2025
35 checks passed
@sonarqubecloud
Copy link
Copy Markdown

@donghao526 donghao526 deleted the refactor/tdigest-rank branch December 14, 2025 03:29
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