-
Notifications
You must be signed in to change notification settings - Fork 6.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[core] Implement a universal printer #51151
base: master
Are you sure you want to change the base?
Conversation
5f161b0
to
61646d5
Compare
This looks like more complexity & indirection than we need for string formatting, prefer to keep this simple |
Could you please tell me another way to print all values? |
@edoakes As discussed offline, I added example usage to the PR description. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will continue review later, in general though, have we considered a separate util repo from which we can try to upstream stuff to absl or other util repos
namespace ray { | ||
|
||
// TODO(hjiang): Later version of abseil contains abseil string defined trait. | ||
template <typename T, typename = void, typename = void> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are the two extra void template params for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the fallback template argument (basically for SFINAE, fallback template type should match L31-L34)
SinkType, | ||
std::void_t<decltype(AbslStringify(std::declval<SinkType &>(), | ||
std::declval<const T &>()))>> | ||
: std::true_type {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you write this differently with concepts, that should make this easier to read? (in perfect cpp20 world)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah in my prev company all in concepts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And yes, if we do have C++20 I will update to concepts and modern type traits
|
||
template <typename C> | ||
void PrintArray(std::ostream &os, const C &container) const { | ||
static_assert(is_container_v<C>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be good to describe more specifically your definition of "array" with like something that's indexable. also a more specific is_array_v
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is_array_v
is not correct, both contiguous container and non-continuous one like deque should apply
Signed-off-by: dentiny <dentinyhao@gmail.com>
f08a280
to
53c7780
Compare
Learned from my prev company how to use ordered visitor to implement a universal printer, which could handle ANY type.
Checkout unit tests:
ray/src/ray/util/strings/debug_printer_test.cc
Lines 31 to 136 in 414c895
Example usage 1:
We could replace all usage of
RAY_CHECK
toRAY_CHECK_EQ
, etcray/src/ray/util/logging.h
Line 166 in 8b26f5c
In the past it's not feasible because current implementation assume all type ostream-printable.
Detailed example:
ray/src/ray/core_worker/fiber.h
Line 155 in 2bd1a56
ray/src/ray/core_worker/task_event_buffer.cc
Line 74 in 2bd1a56
ray/src/ray/core_worker/task_event_buffer.cc
Line 130 in 2bd1a56
The list goes on.
Example usage 2:
https://github.com/ray-project/ray/blob/master/src/ray/util/container_util.h
Current implementation hard codes all container type we've met, with the debug printer we no longer need it.