================
@@ -0,0 +1,68 @@
+.. title:: clang-tidy - performance-replace-with-string-view
+
+performance-replace-with-string-view
+====================================
+
+Looks for functions returning ``std::[w|u8|u16|u32]string`` and suggests to
+change it to ``std::[...]string_view`` for performance reasons if possible.
+
+Rationale:
+
+Each time a new ``std::string`` is created from a literal, a copy of that
+literal is allocated either in ``std::string``'s internal buffer
+(for short literals) or in a heap.
+
+For the cases where ``std::string`` is returned from a function,
+such allocations can be eliminated sometimes by using ``std::string_view``
+as a return type.
+
+This check looks for such functions returning ``std::string``
+baked from the literals and suggests replacing the return type to
``std::string_view``.
+
+It handles ``std::string``, ``std::wstring``, ``std::u8string``,
+``std::u16string`` and ``std::u32string`` along with their aliases and selects
+the proper kind of ``std::string_view`` to return.
+
+Example:
+
+Consider the following code:
+
+.. code-block:: c++
+
+ std::string foo(int i) {
+ switch(i)
+ {
+ case 1:
+ return "case 1";
+ case 2:
+ return "case 2";
+ case 3:
+ return "case 3";
+ default:
+ return "default";
+ }
+ }
+
+In the code above a new ``std::string`` object is created on each
+function invocation, making a copy of a string literal and possibly
+allocating a memory in a heap.
----------------
EugeneZelenko wrote:
```suggestion
In the code above a new ``std::string`` object is created on each function
invocation, making a copy of a string literal and possibly allocating a memory
in a heap.
```
Please use as much space in 80-characters limit as possible.
https://github.com/llvm/llvm-project/pull/172170
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits