Bug 1332825 - Use move semantics in MozPromise::All() and AllPromiseHolder - r=jwwang
authorGerald Squelart <gsquelart@mozilla.com>
Sat, 21 Jan 2017 17:33:49 +1100
changeset 377873 8ac365ab3cfb611c5e85f24f533ac6399cdf0240
parent 377872 3666ff5c8e0a4a1a457c1af087fd14c09bf11a01
child 377874 e7c232eb0efa4076d3402a919671edeff7e9de28
push id1419
push userjlund@mozilla.com
push dateMon, 10 Apr 2017 20:44:07 +0000
treeherdermozilla-release@5e6801b73ef6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjwwang
bugs1332825
milestone53.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 1332825 - Use move semantics in MozPromise::All() and AllPromiseHolder - r=jwwang MozPromise::All sets up 'Then' lambdas on all sub-promises, each one taking the resolve/reject object by value. Since this value will not be used again in the lambda, it is safe to Move it, and from there MozPromiseHolder::Resolve/Reject can also Move it again into the holder storage, potentially saving two copies per Resolve/Reject. Also, once all sub-promises have been resolved, the resolve-values can be Move'd into the joining promise's Resolve function. MozReview-Commit-ID: 5oxgNEnE5lq
xpcom/threads/MozPromise.h
--- a/xpcom/threads/MozPromise.h
+++ b/xpcom/threads/MozPromise.h
@@ -215,45 +215,45 @@ private:
   public:
     explicit AllPromiseHolder(size_t aDependentPromises)
       : mPromise(new typename AllPromiseType::Private(__func__))
       , mOutstandingPromises(aDependentPromises)
     {
       mResolveValues.SetLength(aDependentPromises);
     }
 
-    void Resolve(size_t aIndex, const ResolveValueType& aResolveValue)
+    void Resolve(size_t aIndex, ResolveValueType&& aResolveValue)
     {
       if (!mPromise) {
         // Already rejected.
         return;
       }
 
-      mResolveValues[aIndex].emplace(aResolveValue);
+      mResolveValues[aIndex].emplace(Move(aResolveValue));
       if (--mOutstandingPromises == 0) {
         nsTArray<ResolveValueType> resolveValues;
         resolveValues.SetCapacity(mResolveValues.Length());
         for (size_t i = 0; i < mResolveValues.Length(); ++i) {
-          resolveValues.AppendElement(mResolveValues[i].ref());
+          resolveValues.AppendElement(Move(mResolveValues[i].ref()));
         }
 
-        mPromise->Resolve(resolveValues, __func__);
+        mPromise->Resolve(Move(resolveValues), __func__);
         mPromise = nullptr;
         mResolveValues.Clear();
       }
     }
 
-    void Reject(const RejectValueType& aRejectValue)
+    void Reject(RejectValueType&& aRejectValue)
     {
       if (!mPromise) {
         // Already rejected.
         return;
       }
 
-      mPromise->Reject(aRejectValue, __func__);
+      mPromise->Reject(Move(aRejectValue), __func__);
       mPromise = nullptr;
       mResolveValues.Clear();
     }
 
     AllPromiseType* Promise() { return mPromise; }
 
   private:
     nsTArray<Maybe<ResolveValueType>> mResolveValues;
@@ -262,18 +262,18 @@ private:
   };
 public:
 
   static RefPtr<AllPromiseType> All(AbstractThread* aProcessingThread, nsTArray<RefPtr<MozPromise>>& aPromises)
   {
     RefPtr<AllPromiseHolder> holder = new AllPromiseHolder(aPromises.Length());
     for (size_t i = 0; i < aPromises.Length(); ++i) {
       aPromises[i]->Then(aProcessingThread, __func__,
-        [holder, i] (ResolveValueType aResolveValue) -> void { holder->Resolve(i, aResolveValue); },
-        [holder] (RejectValueType aRejectValue) -> void { holder->Reject(aRejectValue); }
+        [holder, i] (ResolveValueType aResolveValue) -> void { holder->Resolve(i, Move(aResolveValue)); },
+        [holder] (RejectValueType aRejectValue) -> void { holder->Reject(Move(aRejectValue)); }
       );
     }
     return holder->Promise();
   }
 
   class Request : public MozPromiseRefcountable
   {
   public: