refactor(butil): replace integer string conversions - #3534
Conversation
|
|
There was a problem hiding this comment.
🟡 Changes recommended
The updated StringNumberConversionsTest.IntToString no longer exercises the compatibility wrappers (butil::IntToString/butil::UintToString), reducing coverage for the APIs explicitly kept by this PR.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR refactors integer-to-string conversions across butil and related call sites by replacing internal uses of butil::{Int,Uint}ToString with std::to_string, while keeping the public wrapper APIs for compatibility.
Changes:
- Replace internal call sites of legacy integer conversion helpers with
std::to_string. - Re-implement
butil::IntToString(int)andbutil::UintToString(unsigned)as thin wrappers overstd::to_string. - Update affected unit tests and examples to use
std::to_stringat call sites.
File summaries
| File | Description |
|---|---|
| test/string_number_conversions_unittest.cc | Adjusts integer conversion expectations (currently switches to std::to_string in the IntToString test). |
| test/simple_thread_unittest.cc | Uses std::to_string when composing thread names; drops dependency on string_number_conversions. |
| test/shared_memory_unittest.cc | Uses std::to_string in shared memory name construction. |
| test/brpc_http_rpc_protocol_unittest.cpp | Uses std::to_string for HTTP header value formatting. |
| src/butil/version.cc | Replaces IntToString with std::to_string in version parsing/stringification logic. |
| src/butil/threading/simple_thread.cc | Uses std::to_string for thread ID formatting in SimpleThread::ThreadMain. |
| src/butil/strings/string_number_conversions.cc | Re-implements IntToString/UintToString wrappers via std::to_string. |
| src/butil/location.cc | Uses std::to_string for line number formatting in Location::ToString. |
| example/baidu_proxy_and_generic_call/proxy.cpp | Uses std::to_string for response user fields error codes. |
Review details
- Files reviewed: 9/9 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
bb8f829 to
51a4183
Compare
There was a problem hiding this comment.
🟢 Approval recommended
The changes are a mechanical, low-risk refactor with appropriate header updates and no observed behavioral issues in the modified call sites.
Review details
- Files reviewed: 8/8 changed files
- Comments generated: 0 new
- Review effort level: Lite
chenBright
left a comment
There was a problem hiding this comment.
I don't see a clear advantage or necessity in switching to std::to_string here. It's just a stylistic swap. I'd suggest not making this change.
|
Why should we re-implement the methods that are already provided by the standard library? There is no benefit at all. |
|
Thanks for the cleanup. However, I think we need more benchmark results before making this change. Replacing an existing implementation with std::to_string simply because the standard library provides it is not, by itself, a sufficient reason to change. We should demonstrate that the replacement is at least comparable in performance and does not introduce regressions on the toolchains we support. In particular, the implementation of std::to_string varies across libstdc++ versions. Modern versions have optimized integer conversion, while older versions may implement it through vsnprintf, which can be significantly slower than the current butil implementation. Could you please provide benchmarks covering representative integer types/values and, ideally, both older and newer supported GCC/libstdc++ versions? This would give us enough evidence to evaluate the trade-off rather than making the switch solely for API modernization. |
|
I have already explained that this PR is not intended to enhance performance, but rather to avoid duplication. |
What problem does this PR solve?
Problem Summary:
Replace internal uses of legacy
butilinteger-to-string helpers withstd::to_stringto reduce duplicated conversion logic.What is changed and the side effects?
Changed:
std::to_stringfor internalIntToStringandUintToStringcall sites.butil::IntToString(int)andbutil::UintToString(unsigned)APIs as compatibility wrappers backed bystd::to_string.intinputs.Side effects:
Performance effects: No benchmark was run; no expected material impact.
Breaking backward compatibility: No. The existing public helper APIs remain available.
Check List:
cmake --build build --target BUTIL_LIB -j6cmake --build build --target test_butil -j6StringNumberConversionsTest.IntToStringHttpTest.http_head