================
@@ -0,0 +1,62 @@
+.. title:: clang-tidy - performance-expensive-value-or
+
+performance-expensive-value-or
+==============================
+
+Finds calls to ``value_or`` on optional types where the underlying value type
+is expensive to copy. While ``value()`` and ``operator*`` return references,
+``value_or`` always returns by value, which involves copying the contained
+value.
+
+The check is applied to types that are not trivially copyable or whose size
+exceeds a configurable threshold. It supports ``std::optional``,
+``boost::optional``, ``absl::optional``, and other optional-like types via
+configuration.
+
+Example:
+
+.. code-block:: c++
+
+    #include <optional>
+    #include <string>
+
+    void example(std::optional<std::string> opt) {
+      // Warning: copies the std::string out of the optional.
+      auto val = opt.value_or("default");
+
+      // Alternatives that avoid the copy:
+      const std::string fallback = "default";
+      const auto &ref = opt.has_value() ? *opt : fallback;
+    }
+
+Options
+-------
+
+.. option:: SizeThreshold
+
+   The minimum size in bytes (exclusive) above which a trivially-copyable type
+   is considered expensive to copy. Types with ``sizeof(T) > SizeThreshold``
+   trigger the warning even if they are trivially copyable. Types at or below
+   this threshold only trigger if they are not trivially copyable.
+   Default is `8`.
----------------
gamesh411 wrote:

Yeah, maybe you are right.
For most architectures, I guess 16 bytes would be around 2 registers worth of 
extra "load" to optimise around, and that seems fair.

https://github.com/llvm/llvm-project/pull/200166
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to