Bug 1547478 - Make the |CodeCompressedData| generic lambda inside |ScriptSource::xdrData| instead be a static member function in |ScriptSource|, avoiding the need to pass an ignored |Unit| argument to it purely for overload selection. r=tcampbell
authorJeff Walden <jwalden@mit.edu>
Mon, 29 Apr 2019 12:51:45 -0700
changeset 530704 5fe6df92def0d5de136974cc5c3a9183eb9b7041
parent 530703 0494a9f3230c58de2120b5a02dca77f11144298d
child 530705 b96f067ff11790f61449906ecf7f764abfa2a933
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstcampbell
bugs1547478
milestone68.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 1547478 - Make the |CodeCompressedData| generic lambda inside |ScriptSource::xdrData| instead be a static member function in |ScriptSource|, avoiding the need to pass an ignored |Unit| argument to it purely for overload selection. r=tcampbell Differential Revision: https://phabricator.services.mozilla.com/D29264
js/src/vm/JSScript.cpp
js/src/vm/JSScript.h
--- a/js/src/vm/JSScript.cpp
+++ b/js/src/vm/JSScript.cpp
@@ -2670,16 +2670,74 @@ XDRResult ScriptSource::codeUncompressed
   if (mode == XDR_ENCODE) {
     uncompressedLength = ss->data.as<Uncompressed<Unit>>().length();
   }
   MOZ_TRY(xdr->codeUint32(&uncompressedLength));
 
   return ss->xdrUncompressedSource(xdr, sizeof(Unit), uncompressedLength);
 }
 
+template <typename Unit, XDRMode mode>
+/* static */
+XDRResult ScriptSource::codeCompressedData(XDRState<mode>* const xdr,
+                                           ScriptSource* const ss,
+                                           bool retrievable) {
+  static_assert(std::is_same<Unit, Utf8Unit>::value ||
+                    std::is_same<Unit, char16_t>::value,
+                "should handle UTF-8 and UTF-16");
+
+  if (mode == XDR_ENCODE) {
+    MOZ_ASSERT(ss->data.is<Compressed<Unit>>());
+  }
+
+  MOZ_ASSERT(retrievable == ss->sourceRetrievable());
+
+  if (retrievable) {
+    // It's unnecessary to code compressed data if it can just be retrieved
+    // using the source hook.
+    if (mode == XDR_DECODE) {
+      ss->data = SourceType(Retrievable<Unit>());
+    }
+    return Ok();
+  }
+
+  uint32_t uncompressedLength;
+  if (mode == XDR_ENCODE) {
+    uncompressedLength = ss->data.as<Compressed<Unit>>().uncompressedLength;
+  }
+  MOZ_TRY(xdr->codeUint32(&uncompressedLength));
+
+  uint32_t compressedLength;
+  if (mode == XDR_ENCODE) {
+    compressedLength = ss->data.as<Compressed<Unit>>().raw.length();
+  }
+  MOZ_TRY(xdr->codeUint32(&compressedLength));
+
+  if (mode == XDR_DECODE) {
+    // Compressed data is always single-byte chars.
+    auto bytes = xdr->cx()->template make_pod_array<char>(compressedLength);
+    if (!bytes) {
+      return xdr->fail(JS::TranscodeResult_Throw);
+    }
+    MOZ_TRY(xdr->codeBytes(bytes.get(), compressedLength));
+
+    if (!ss->initializeWithCompressedSource<Unit>(xdr->cx(), std::move(bytes),
+                                                  compressedLength,
+                                                  uncompressedLength)) {
+      return xdr->fail(JS::TranscodeResult_Throw);
+    }
+  } else {
+    void* bytes =
+        const_cast<char*>(ss->data.as<Compressed<Unit>>().raw.chars());
+    MOZ_TRY(xdr->codeBytes(bytes, compressedLength));
+  }
+
+  return Ok();
+}
+
 template <XDRMode mode>
 /* static */
 XDRResult ScriptSource::xdrData(XDRState<mode>* const xdr,
                                 ScriptSource* const ss) {
   // Retrievability is kept outside |ScriptSource::data| (and not solely as
   // distinct variant types within it) because retrievable compressed or
   // uncompressed data need not be XDR'd.
   uint8_t retrievable;
@@ -2745,70 +2803,16 @@ XDRResult ScriptSource::xdrData(XDRState
       // Fail in debug, but only soft-fail in release, if the type is invalid.
       MOZ_ASSERT_UNREACHABLE("bad tag");
       return xdr->fail(JS::TranscodeResult_Failure_BadDecode);
     }
 
     tag = static_cast<DataType>(type);
   }
 
-  auto CodeCompressedData = [xdr, ss, &retrievable](auto unit) -> XDRResult {
-    using Unit = decltype(unit);
-
-    static_assert(std::is_same<Unit, Utf8Unit>::value ||
-                      std::is_same<Unit, char16_t>::value,
-                  "should handle UTF-8 and UTF-16");
-
-    if (mode == XDR_ENCODE) {
-      MOZ_ASSERT(ss->data.is<Compressed<Unit>>());
-    }
-
-    if (retrievable) {
-      // It's unnecessary to code compressed data if it can just be retrieved
-      // using the source hook.
-      if (mode == XDR_DECODE) {
-        ss->data = SourceType(Retrievable<Unit>());
-      }
-      return Ok();
-    }
-
-    uint32_t uncompressedLength = 0;
-    if (mode == XDR_ENCODE) {
-      uncompressedLength = ss->data.as<Compressed<Unit>>().uncompressedLength;
-    }
-    MOZ_TRY(xdr->codeUint32(&uncompressedLength));
-
-    uint32_t compressedLength;
-    if (mode == XDR_ENCODE) {
-      compressedLength = ss->data.as<Compressed<Unit>>().raw.length();
-    }
-    MOZ_TRY(xdr->codeUint32(&compressedLength));
-
-    if (mode == XDR_DECODE) {
-      // Compressed data is always single-byte chars.
-      auto bytes = xdr->cx()->template make_pod_array<char>(compressedLength);
-      if (!bytes) {
-        return xdr->fail(JS::TranscodeResult_Throw);
-      }
-      MOZ_TRY(xdr->codeBytes(bytes.get(), compressedLength));
-
-      if (!ss->initializeWithCompressedSource<Unit>(xdr->cx(), std::move(bytes),
-                                                    compressedLength,
-                                                    uncompressedLength)) {
-        return xdr->fail(JS::TranscodeResult_Throw);
-      }
-    } else {
-      void* bytes =
-          const_cast<char*>(ss->data.as<Compressed<Unit>>().raw.chars());
-      MOZ_TRY(xdr->codeBytes(bytes, compressedLength));
-    }
-
-    return Ok();
-  };
-
   auto CodeBinASTData = [xdr
 #if defined(JS_BUILD_BINAST)
                          ,
                          ss
 #endif
   ]() -> XDRResult {
 #if !defined(JS_BUILD_BINAST)
     return xdr->fail(JS::TranscodeResult_Throw);
@@ -2927,25 +2931,23 @@ XDRResult ScriptSource::xdrData(XDRState
     }
 
     return Ok();
 #endif  // !defined(JS_BUILD_BINAST)
   };
 
   switch (tag) {
     case DataType::CompressedUtf8:
-      // The argument here is just for overloading -- its value doesn't matter.
-      return CodeCompressedData(Utf8Unit('0'));
+      return ScriptSource::codeCompressedData<Utf8Unit>(xdr, ss, retrievable);
 
     case DataType::UncompressedUtf8:
       return ScriptSource::codeUncompressedData<Utf8Unit>(xdr, ss, retrievable);
 
     case DataType::CompressedUtf16:
-      // The argument here is just for overloading -- its value doesn't matter.
-      return CodeCompressedData(char16_t('0'));
+      return ScriptSource::codeCompressedData<char16_t>(xdr, ss, retrievable);
 
     case DataType::UncompressedUtf16:
       return ScriptSource::codeUncompressedData<char16_t>(xdr, ss, retrievable);
 
     case DataType::Missing: {
       MOZ_ASSERT(ss->data.is<Missing>(),
                  "ScriptSource::data is initialized as missing, so neither "
                  "encoding nor decoding has to change anything");
--- a/js/src/vm/JSScript.h
+++ b/js/src/vm/JSScript.h
@@ -1146,16 +1146,21 @@ class ScriptSource {
   }
 
  private:
   template <typename Unit, XDRMode mode>
   static MOZ_MUST_USE XDRResult codeUncompressedData(XDRState<mode>* const xdr,
                                                      ScriptSource* const ss,
                                                      bool retrievable);
 
+  template <typename Unit, XDRMode mode>
+  static MOZ_MUST_USE XDRResult codeCompressedData(XDRState<mode>* const xdr,
+                                                   ScriptSource* const ss,
+                                                   bool retrievable);
+
   template <XDRMode mode>
   static MOZ_MUST_USE XDRResult xdrData(XDRState<mode>* const xdr,
                                         ScriptSource* const ss);
 
  public:
   template <XDRMode mode>
   static MOZ_MUST_USE XDRResult
   XDR(XDRState<mode>* xdr, const mozilla::Maybe<JS::CompileOptions>& options,