Skip to content
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

inet_address: Remove to_sstring() in favor of fmt::to_string #18712

Closed

Conversation

xemul
Copy link
Contributor

@xemul xemul commented May 16, 2024

The existing inet_address::to_string() calls fmt::format("{}", *this) anyway. However, the to_string() method is declared in .cc file, while form formatter is in the header and is equipeed with constexprs so that converting an address to string is done as much as possible compile-time.

Also, though minor, fmt::to_string(foo) is believed to be even faster than fmt::format("{}", foo).

@xemul xemul added the backport/none Backport is not required label May 16, 2024
@xemul xemul requested a review from tchaikov May 16, 2024 15:04
@nyh
Copy link
Contributor

nyh commented May 16, 2024

Your patch is lacking motivation: In classic OO programming, a method is better than a global function. Among other things, when you have a method on a method ip.to_sstring() instead of a free function fmt::to_string(ip) you have an opportunity to document what this to_sstring() actually does, what format it promises to uses, and so on. So what is the motivation to go the other way - replace a method by a free function?

Another result of your change is that we convert the address into an std::string instead of a seastar::string. Is this good? bad? It should at least be discussed and I would have liked to see some motivation for the change.

An example of the std::string vs. seastar::sstring issue is the following code (after your patch):

std::map<sstring, sstring> nodes_status;
g.for_each_endpoint_state([&] (const gms::inet_address& node, const gms::endpoint_state&) {
       nodes_status.emplace(fmt::to_string(node), g.is_alive(node) ? "UP" : "DOWN");

Note how you build a map of seastar::sstring, yet you put into it a std::string returned by fmt::to_string(). This requires unnecessary conversions between the two. In this case you should probably make the map have a std::string key instead of sstring. It's not a bad idea (in Alternator I also moved away from sstring to std::string as much as possible), but I think it needs to be done more carefully.

@xemul
Copy link
Contributor Author

xemul commented May 16, 2024

The existing inet_address::to_string() calls fmt::format("{}", *this) anyway. However, the to_string() method is declared in .cc file, while formatter is in header and is equipeed with constexprs so that converting an address to string is done as much as possible compile-time. Also, though minor, fmt::to_string(foo) is believed to be even faster that fmt::format("{}", foo).

The existing inet_address::to_string() calls fmt::format("{}", *this)
anyway. However, the to_string() method is declared in .cc file, while
form formatter is in the header and is equipeed with constexprs so
that converting an address to string is done as much as possible
compile-time.

Also, though minor, fmt::to_string(foo) is believed to be even faster
than fmt::format("{}", foo).

Signed-off-by: Pavel Emelyanov <xemul@scylladb.com>
@xemul xemul force-pushed the br-inet-address-no-custom-formatters branch from d7d330a to 521a7c8 Compare May 17, 2024 09:52
@xemul
Copy link
Contributor Author

xemul commented May 17, 2024

upd:

  • added motivation to patch commit message

@xemul
Copy link
Contributor Author

xemul commented May 17, 2024

Regarding to_string() method. Having formatter is beneficial, because if you have log.debug("{}", object.to_string()) the conversion to string happens even for low enough log-level, but log.debug("{}", object) when the object has formatter doesn't convert object to string when not needed

Copy link
Contributor

@tchaikov tchaikov left a comment

Choose a reason for hiding this comment

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

lgtm

@scylladb-promoter
Copy link
Contributor

🟢 CI State: SUCCESS

✅ - Build
✅ - Container Test
✅ - dtest
✅ - dtest with topology changes
✅ - Unit Tests

Build Details:

  • Duration: 3 hr 48 min
  • Builder: i-0a83f309df996450d (m5d.12xlarge)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/none Backport is not required promoted-to-master
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants