Bug 1632128 - De-virtualize IDBFileRequest::SetResult(Callback). r=dom-workers-and-storage-reviewers,janv
authorSimon Giesecke <sgiesecke@mozilla.com>
Thu, 14 May 2020 09:42:21 +0000
changeset 593597 4c55bf108c9b69828419e969612bfe2356c34830
parent 593596 b1d098d0edcfa5028d8193db74e3c22dd32ce7ef
child 593598 1e9b275f2afa33c9db1b643839157bba31dab7c1
push id13186
push userffxbld-merge
push dateMon, 01 Jun 2020 09:52:46 +0000
treeherdermozilla-beta@3e7c70a1e4a1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdom-workers-and-storage-reviewers, janv
bugs1632128
milestone78.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 1632128 - De-virtualize IDBFileRequest::SetResult(Callback). r=dom-workers-and-storage-reviewers,janv Differential Revision: https://phabricator.services.mozilla.com/D74875
dom/indexedDB/ActorsChild.cpp
dom/indexedDB/IDBFileRequest.cpp
dom/indexedDB/IDBFileRequest.h
--- a/dom/indexedDB/ActorsChild.cpp
+++ b/dom/indexedDB/ActorsChild.cpp
@@ -890,18 +890,17 @@ class MOZ_STACK_CLASS AutoSetCurrentFile
       // Reset old value.
       *mThreadLocalSlot = mPreviousFileHandle;
     }
   }
 
   IDBFileHandle* FileHandle() const { return mFileHandle; }
 };
 
-class MOZ_STACK_CLASS FileHandleResultHelper final
-    : public IDBFileRequest::ResultCallback {
+class MOZ_STACK_CLASS FileHandleResultHelper final {
   IDBFileRequest* const mFileRequest;
   AutoSetCurrentFileHandle mAutoFileHandle;
 
   mozilla::Variant<File*, const nsCString*, const FileRequestMetadata*,
                    const JS::Handle<JS::Value>*>
       mResult;
 
  public:
@@ -915,18 +914,18 @@ class MOZ_STACK_CLASS FileHandleResultHe
     MOZ_ASSERT(aFileHandle);
     MOZ_ASSERT(aResult);
   }
 
   IDBFileRequest* FileRequest() const { return mFileRequest; }
 
   IDBFileHandle* FileHandle() const { return mAutoFileHandle.FileHandle(); }
 
-  virtual nsresult GetResult(JSContext* aCx,
-                             JS::MutableHandle<JS::Value> aResult) override {
+  nsresult operator()(JSContext* aCx,
+                      JS::MutableHandle<JS::Value> aResult) const {
     MOZ_ASSERT(aCx);
     MOZ_ASSERT(mFileRequest);
 
 #ifdef __clang__
 #  pragma clang diagnostic push
 #  pragma clang diagnostic ignored "-Wunused-lambda-capture"
 #endif
     return mResult.match([aCx, aResult, this](auto* aPtr) {
@@ -944,17 +943,17 @@ class MOZ_STACK_CLASS FileHandleResultHe
     if (NS_WARN_IF(!ok)) {
       return NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR;
     }
 
     return NS_OK;
   }
 
   nsresult GetResult(JSContext* aCx, const nsCString* aString,
-                     JS::MutableHandle<JS::Value> aResult) {
+                     JS::MutableHandle<JS::Value> aResult) const {
     const nsCString& data = *aString;
 
     nsresult rv;
 
     if (!mFileRequest->HasEncoding()) {
       JS::Rooted<JSObject*> arrayBuffer(aCx);
       rv = nsContentUtils::CreateArrayBuffer(aCx, data, arrayBuffer.address());
       if (NS_WARN_IF(NS_FAILED(rv))) {
@@ -1059,17 +1058,17 @@ void DispatchFileHandleSuccessEvent(File
 
   if (fileHandle->IsAborted()) {
     fileRequest->FireError(NS_ERROR_DOM_FILEHANDLE_ABORT_ERR);
     return;
   }
 
   MOZ_ASSERT(fileHandle->IsOpen());
 
-  fileRequest->SetResultCallback(aResultHelper);
+  fileRequest->SetResult(*aResultHelper);
 
   MOZ_ASSERT(fileHandle->IsOpen() || fileHandle->IsAborted());
 }
 
 auto GetKeyOperator(const IDBCursorDirection aDirection) {
   switch (aDirection) {
     case IDBCursorDirection::Next:
     case IDBCursorDirection::Nextunique:
--- a/dom/indexedDB/IDBFileRequest.cpp
+++ b/dom/indexedDB/IDBFileRequest.cpp
@@ -59,37 +59,16 @@ void IDBFileRequest::FireProgressEvent(u
   init.mLoaded = aLoaded;
   init.mTotal = aTotal;
 
   RefPtr<ProgressEvent> event =
       ProgressEvent::Constructor(this, NS_LITERAL_STRING("progress"), init);
   DispatchTrustedEvent(event);
 }
 
-void IDBFileRequest::SetResultCallback(ResultCallback* aCallback) {
-  AssertIsOnOwningThread();
-  MOZ_ASSERT(aCallback);
-
-  AutoJSAPI autoJS;
-  if (NS_WARN_IF(!autoJS.Init(GetOwnerGlobal()))) {
-    FireError(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
-    return;
-  }
-
-  JSContext* cx = autoJS.cx();
-
-  JS::Rooted<JS::Value> result(cx);
-  nsresult rv = aCallback->GetResult(cx, &result);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    FireError(rv);
-  } else {
-    FireSuccess(result);
-  }
-}
-
 NS_IMPL_ADDREF_INHERITED(IDBFileRequest, DOMRequest)
 NS_IMPL_RELEASE_INHERITED(IDBFileRequest, DOMRequest)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBFileRequest)
 NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
 
 NS_IMPL_CYCLE_COLLECTION_INHERITED(IDBFileRequest, DOMRequest, mFileHandle)
 
--- a/dom/indexedDB/IDBFileRequest.h
+++ b/dom/indexedDB/IDBFileRequest.h
@@ -25,33 +25,50 @@ class IDBFileRequest final : public DOMR
   RefPtr<IDBFileHandle> mFileHandle;
 
   nsString mEncoding;
 
   bool mWrapAsDOMRequest;
   bool mHasEncoding;
 
  public:
-  class ResultCallback;
-
   [[nodiscard]] static RefPtr<IDBFileRequest> Create(IDBFileHandle* aFileHandle,
                                                      bool aWrapAsDOMRequest);
 
   void SetEncoding(const nsAString& aEncoding) {
     mEncoding = aEncoding;
     mHasEncoding = true;
   }
 
   const nsAString& GetEncoding() const { return mEncoding; }
 
   bool HasEncoding() const { return mHasEncoding; }
 
   void FireProgressEvent(uint64_t aLoaded, uint64_t aTotal);
 
-  void SetResultCallback(ResultCallback* aCallback);
+  template <typename ResultCallback>
+  void SetResult(const ResultCallback& aCallback) {
+    AssertIsOnOwningThread();
+
+    AutoJSAPI autoJS;
+    if (NS_WARN_IF(!autoJS.Init(GetOwnerGlobal()))) {
+      FireError(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
+      return;
+    }
+
+    JSContext* cx = autoJS.cx();
+
+    JS::Rooted<JS::Value> result(cx);
+    nsresult rv = aCallback(cx, &result);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      FireError(rv);
+    } else {
+      FireSuccess(result);
+    }
+  }
 
   // WebIDL
   IDBFileHandle* GetFileHandle() const {
     AssertIsOnOwningThread();
     return mFileHandle;
   }
 
   IDBFileHandle* GetLockedFile() const {
@@ -76,21 +93,12 @@ class IDBFileRequest final : public DOMR
                                JS::Handle<JSObject*> aGivenProto) override;
 
  private:
   IDBFileRequest(IDBFileHandle* aFileHandle, bool aWrapAsDOMRequest);
 
   ~IDBFileRequest();
 };
 
-class NS_NO_VTABLE IDBFileRequest::ResultCallback {
- public:
-  virtual nsresult GetResult(JSContext* aCx,
-                             JS::MutableHandle<JS::Value> aResult) = 0;
-
- protected:
-  ResultCallback() = default;
-};
-
 }  // namespace dom
 }  // namespace mozilla
 
 #endif  // mozilla_dom_idbfilerequest_h__