Bug 1683227 - Add a note to Maybe::valueOr() about a suboptimal usage. r=sg DONTBUILD
authorTing-Yu Lin <tlin@mozilla.com>
Mon, 21 Dec 2020 18:32:26 +0000
changeset 561472 e9c936e74e4727af494a52e7124d02cbf5e5728c
parent 561471 47e600a37e250c3f8f27803673d15ce4aa9b5698
child 561473 5ce98ac27b80df887b2ae957267645d2cb0c5ba0
push id38050
push usermalexandru@mozilla.com
push dateTue, 22 Dec 2020 09:35:33 +0000
treeherdermozilla-central@54a95a4088a2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssg
bugs1683227
milestone86.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1683227 - Add a note to Maybe::valueOr() about a suboptimal usage. r=sg DONTBUILD Differential Revision: https://phabricator.services.mozilla.com/D100071
mfbt/Maybe.h
--- a/mfbt/Maybe.h
+++ b/mfbt/Maybe.h
@@ -491,16 +491,23 @@ class MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_
    * Returns the value (possibly |Nothing()|) by moving it out of this Maybe<T>
    * and leaving |Nothing()| in its place.
    */
   Maybe<T> take() { return std::exchange(*this, Nothing()); }
 
   /*
    * Returns the contents of this Maybe<T> by value. If |isNothing()|, returns
    * the default value provided.
+   *
+   * Note: If the value passed to aDefault is not the result of a trivial
+   * expression, but expensive to evaluate, e.g. |valueOr(ExpensiveFunction())|,
+   * use |valueOrFrom| instead, e.g.
+   * |valueOrFrom([arg] { return ExpensiveFunction(arg); })|. This ensures
+   * that the expensive expression is only evaluated when its result will
+   * actually be used.
    */
   template <typename V>
   constexpr T valueOr(V&& aDefault) const {
     if (isSome()) {
       return ref();
     }
     return std::forward<V>(aDefault);
   }