Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 987841a

Browse filesBrowse files
addaleaxaduh95
authored andcommitted
src: avoid unnecessary string allocations in SPrintF impl
If we can use a `std::string_view` instead of a `std::string`, let's just do that instead. PR-URL: #60052 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent 6e386c0 commit 987841a
Copy full SHA for 987841a

File tree

Expand file treeCollapse file tree

2 files changed

+35
-12
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+35
-12
lines changed
Open diff view settings
Collapse file

‎src/debug_utils-inl.h‎

Copy file name to clipboardExpand all lines: src/debug_utils-inl.h
+33-12Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,41 @@
1111

1212
namespace node {
1313

14+
template <typename T>
15+
concept StringViewConvertible = requires(T a) {
16+
{
17+
a.ToStringView()
18+
} -> std::convertible_to<std::string_view>;
19+
};
20+
template <typename T>
21+
concept StringConvertible = requires(T a) {
22+
{
23+
a.ToString()
24+
} -> std::convertible_to<std::string>;
25+
};
26+
1427
struct ToStringHelper {
1528
template <typename T>
16-
static std::string Convert(
17-
const T& value,
18-
std::string(T::* to_string)() const = &T::ToString) {
19-
return (value.*to_string)();
29+
requires(StringConvertible<T>) && (!StringViewConvertible<T>)
30+
static std::string Convert(const T& value) {
31+
return value.ToString();
2032
}
33+
template <typename T>
34+
requires StringViewConvertible<T>
35+
static std::string_view Convert(const T& value) {
36+
return value.ToStringView();
37+
}
38+
2139
template <typename T,
2240
typename test_for_number = typename std::
2341
enable_if<std::is_arithmetic<T>::value, bool>::type,
2442
typename dummy = bool>
2543
static std::string Convert(const T& value) { return std::to_string(value); }
26-
static std::string Convert(const char* value) {
44+
static std::string_view Convert(const char* value) {
2745
return value != nullptr ? value : "(null)";
2846
}
2947
static std::string Convert(const std::string& value) { return value; }
30-
static std::string Convert(std::string_view value) {
31-
return std::string(value);
32-
}
48+
static std::string_view Convert(std::string_view value) { return value; }
3349
static std::string Convert(bool value) { return value ? "true" : "false"; }
3450
template <unsigned BASE_BITS,
3551
typename T,
@@ -50,18 +66,23 @@ struct ToStringHelper {
5066
template <unsigned BASE_BITS,
5167
typename T,
5268
typename = std::enable_if_t<!std::is_integral_v<T>>>
53-
static std::string BaseConvert(T& value) { // NOLINT(runtime/references)
69+
static auto BaseConvert(T&& value) {
5470
return Convert(std::forward<T>(value));
5571
}
5672
};
5773

5874
template <typename T>
59-
std::string ToString(const T& value) {
75+
auto ToStringOrStringView(const T& value) {
6076
return ToStringHelper::Convert(value);
6177
}
6278

79+
template <typename T>
80+
std::string ToString(const T& value) {
81+
return std::string(ToStringOrStringView(value));
82+
}
83+
6384
template <unsigned BASE_BITS, typename T>
64-
std::string ToBaseString(const T& value) {
85+
auto ToBaseString(const T& value) {
6586
return ToStringHelper::BaseConvert<BASE_BITS>(value);
6687
}
6788

@@ -106,7 +127,7 @@ std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string)
106127
case 'i':
107128
case 'u':
108129
case 's':
109-
ret += ToString(arg);
130+
ret += ToStringOrStringView(arg);
110131
break;
111132
case 'o':
112133
ret += ToBaseString<3>(arg);
Collapse file

‎src/debug_utils.h‎

Copy file name to clipboardExpand all lines: src/debug_utils.h
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Environment;
2626

2727
template <typename T>
2828
inline std::string ToString(const T& value);
29+
template <typename T>
30+
inline auto ToStringOrStringView(const T& value);
2931

3032
// C++-style variant of sprintf()/fprintf() that:
3133
// - Returns an std::string

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.