Skip to content

[core] Change debug_string from returning a string to streaming to an ostream. - #47893

Merged
rynewang merged 5 commits into
ray-project:masterfrom
rynewang:ostream-debug-string
Oct 8, 2024
Merged

rynewang merged 5 commits into
ray-project:masterfrom
rynewang:ostream-debug-string

Conversation

@rynewang

@rynewang rynewang commented Oct 3, 2024 •

Copy link
Copy Markdown
Contributor

We have a conveinence function debug_string used in Ray logs: it prints printables (operator<<), containers, pairs. However it returns a std::string which is feed into RAY_LOG(). This makes a copy.

Changes the signature to return a DebugStringWrapper which holds const reference to the argument, and is printable for all already supported types. Additionally supports std::tuple.

This should only have marginal perf benefits since we typically don't debug_string a very big data structure.

@jjyao

jjyao commented Oct 4, 2024

Copy link
Copy Markdown
Contributor

@dentiny could you help review this?

@dentiny dentiny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This PR LGTM, I have a few other supplements, for example, std::byte and absl::Stringify, etc.

I would like to check and potentially make other PRs if necessary. :)

Comment thread src/ray/util/container_util.h Resolved
Comment thread src/ray/util/container_util.h Resolved
Signed-off-by: Ruiyang Wang <[email protected]>

// The actual interface.
template <typename T>
DebugStringWrapper<T> debug_string(const T &t) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here, prefer SnakeCase if we follows google coding style

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.

for this function, we intended to mimic std library functions so we used camel_case. This also saves us from changing all callsites.

dentiny

This comment was marked as outdated.

@dentiny

dentiny commented Oct 4, 2024

Copy link
Copy Markdown
Contributor

However it returns a std::string which is feed into RAY_LOG(). This makes a copy.

I'm not sure what do you mean by this. A string is created, definitely.
But the streaming overload you call is std::ostream& operator<<(const std::string& s)?

template <typename... Ts>
std::string debug_string(const std::map<Ts...> &c) {
return _container_debug_string(c);
std::ostream &operator<<(std::ostream &os, DebugStringWrapper<std::map<Ts...>> c) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think using known type directly is a good starting point, but it would be nice to templatize.
For example, how do you plan to handle array-list container, like vector, array, inline vector, etc.

@dentiny dentiny Oct 4, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Your implementation is really good. Though this debug_string is intended to be non-deserializable, very limited and we specifically only support several "blessed containers" to avoid accidental leak of user info (e.g. we can't print protobuf messages, though we can write good templates to support them).

@rynewang

rynewang commented Oct 4, 2024

Copy link
Copy Markdown
Contributor Author

However it returns a std::string which is feed into RAY_LOG(). This makes a copy.

I'm not sure what do you mean by this. A string is created, definitely. But the streaming overload you call is std::ostream& operator<<(const std::string& s)?

Code:

RAY_LOG(INFO) << debug_string(my_dict);

Previously:

  • debug_string returns std::string: copying data into the string
  • the string is <<ed into stream, copying data into the stream

Now:

  • debug_string returns a lazy wrapper, no copy
  • the wrapper is <<ed into stream, copying data into the stream

so 1 fewer time of copy.

@dentiny

dentiny commented Oct 4, 2024

Copy link
Copy Markdown
Contributor

Code:

RAY_LOG(INFO) << debug_string(my_dict);

Previously:

  • debug_string returns std::string: copying data into the string
  • the string is <<ed into stream, copying data into the stream

Now:

  • debug_string returns a lazy wrapper, no copy
  • the wrapper is <<ed into stream, copying data into the stream

so 1 fewer time of copy.

Thank you for the explanation!

@dentiny

dentiny commented Oct 6, 2024

Copy link
Copy Markdown
Contributor

@rynewang If we could merge the PR, I could followup with some small changes :)

@rynewang

rynewang commented Oct 6, 2024

Copy link
Copy Markdown
Contributor Author

Needs @jjyao 's approval

@rynewang rynewang added the go add ONLY when ready to merge, run all tests label Oct 6, 2024
@rynewang
rynewang enabled auto-merge (squash) October 7, 2024 22:13
@rynewang
rynewang merged commit fdb99ab into ray-project:master Oct 8, 2024
rynewang pushed a commit that referenced this pull request Oct 10, 2024
Followup on #47893, add more
"blessed container types" to debug string function.

Signed-off-by: dentiny <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
… ostream. (ray-project#47893)

We have a convenience function `debug_string` used in Ray logs: it
prints printables (operator<<), containers, pairs. However it returns a
std::string which is feed into RAY_LOG(). This makes a copy.

Changes the signature to return a `DebugStringWrapper` which holds const
reference to the argument, and is printable for all already supported
types. Additionally supports std::tuple.

This should only have marginal perf benefits since we typically don't
debug_string a very big data structure.

Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
Followup on ray-project#47893, add more
"blessed container types" to debug string function.

Signed-off-by: dentiny <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
… ostream. (ray-project#47893)

We have a convenience function `debug_string` used in Ray logs: it
prints printables (operator<<), containers, pairs. However it returns a
std::string which is feed into RAY_LOG(). This makes a copy.

Changes the signature to return a `DebugStringWrapper` which holds const
reference to the argument, and is printable for all already supported
types. Additionally supports std::tuple.

This should only have marginal perf benefits since we typically don't
debug_string a very big data structure.

Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
Followup on ray-project#47893, add more
"blessed container types" to debug string function.

Signed-off-by: dentiny <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
… ostream. (ray-project#47893)

We have a convenience function `debug_string` used in Ray logs: it
prints printables (operator<<), containers, pairs. However it returns a
std::string which is feed into RAY_LOG(). This makes a copy.

Changes the signature to return a `DebugStringWrapper` which holds const
reference to the argument, and is printable for all already supported
types. Additionally supports std::tuple.

This should only have marginal perf benefits since we typically don't
debug_string a very big data structure.

Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
Followup on ray-project#47893, add more
"blessed container types" to debug string function.

Signed-off-by: dentiny <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
… ostream. (ray-project#47893)

We have a convenience function `debug_string` used in Ray logs: it
prints printables (operator<<), containers, pairs. However it returns a
std::string which is feed into RAY_LOG(). This makes a copy.

Changes the signature to return a `DebugStringWrapper` which holds const
reference to the argument, and is printable for all already supported
types. Additionally supports std::tuple.

This should only have marginal perf benefits since we typically don't
debug_string a very big data structure.

Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
Followup on ray-project#47893, add more
"blessed container types" to debug string function.

Signed-off-by: dentiny <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
… ostream. (ray-project#47893)

We have a convenience function `debug_string` used in Ray logs: it
prints printables (operator<<), containers, pairs. However it returns a
std::string which is feed into RAY_LOG(). This makes a copy.

Changes the signature to return a `DebugStringWrapper` which holds const
reference to the argument, and is printable for all already supported
types. Additionally supports std::tuple.

This should only have marginal perf benefits since we typically don't
debug_string a very big data structure.

Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
Followup on ray-project#47893, add more
"blessed container types" to debug string function.

Signed-off-by: dentiny <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
… ostream. (ray-project#47893)

We have a convenience function `debug_string` used in Ray logs: it
prints printables (operator<<), containers, pairs. However it returns a
std::string which is feed into RAY_LOG(). This makes a copy.

Changes the signature to return a `DebugStringWrapper` which holds const
reference to the argument, and is printable for all already supported
types. Additionally supports std::tuple.

This should only have marginal perf benefits since we typically don't
debug_string a very big data structure.

Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
Followup on ray-project#47893, add more
"blessed container types" to debug string function.

Signed-off-by: dentiny <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
… ostream. (ray-project#47893)

We have a convenience function `debug_string` used in Ray logs: it
prints printables (operator<<), containers, pairs. However it returns a
std::string which is feed into RAY_LOG(). This makes a copy.

Changes the signature to return a `DebugStringWrapper` which holds const
reference to the argument, and is printable for all already supported
types. Additionally supports std::tuple.

This should only have marginal perf benefits since we typically don't
debug_string a very big data structure.

Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
Followup on ray-project#47893, add more
"blessed container types" to debug string function.

Signed-off-by: dentiny <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
… ostream. (ray-project#47893)

We have a convenience function `debug_string` used in Ray logs: it
prints printables (operator<<), containers, pairs. However it returns a
std::string which is feed into RAY_LOG(). This makes a copy.

Changes the signature to return a `DebugStringWrapper` which holds const
reference to the argument, and is printable for all already supported
types. Additionally supports std::tuple.

This should only have marginal perf benefits since we typically don't
debug_string a very big data structure.

Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
Followup on ray-project#47893, add more
"blessed container types" to debug string function.

Signed-off-by: dentiny <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
… ostream. (ray-project#47893)

We have a convenience function `debug_string` used in Ray logs: it
prints printables (operator<<), containers, pairs. However it returns a
std::string which is feed into RAY_LOG(). This makes a copy.

Changes the signature to return a `DebugStringWrapper` which holds const
reference to the argument, and is printable for all already supported
types. Additionally supports std::tuple.

This should only have marginal perf benefits since we typically don't
debug_string a very big data structure.

Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
Followup on ray-project#47893, add more
"blessed container types" to debug string function.

Signed-off-by: dentiny <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants