Bug 1544882 - Split ScriptSource::setCompressedSource into ScriptSource::{convertTo,initializeWith}CompressedSource so that ScriptSource::data's state transitions are clearer. r=arai
authorJeff Walden <jwalden@mit.edu>
Mon, 15 Apr 2019 18:09:26 -0700
changeset 530256 7e238465037a8a9f7fedb033e50c9d4e9cb9c5f6
parent 530255 762b78b66248061140984b2a2876a329c6479389
child 530257 d4d8e4bf895f84f9a6a62080b6eebb6100ccd484
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)
reviewersarai
bugs1544882
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 1544882 - Split ScriptSource::setCompressedSource into ScriptSource::{convertTo,initializeWith}CompressedSource so that ScriptSource::data's state transitions are clearer. r=arai Differential Revision: https://phabricator.services.mozilla.com/D27771
js/src/vm/JSScript.cpp
js/src/vm/JSScript.h
--- a/js/src/vm/JSScript.cpp
+++ b/js/src/vm/JSScript.cpp
@@ -2193,45 +2193,53 @@ bool ScriptSource::tryCompressOffThread(
   if (!task) {
     ReportOutOfMemory(cx);
     return false;
   }
   return EnqueueOffThreadCompression(cx, std::move(task));
 }
 
 template <typename Unit>
-void ScriptSource::setCompressedSource(SharedImmutableString raw,
-                                       size_t uncompressedLength) {
-  MOZ_ASSERT(data.is<Missing>() || hasUncompressedSource());
-  MOZ_ASSERT_IF(hasUncompressedSource(), length() == uncompressedLength);
+void ScriptSource::convertToCompressedSource(SharedImmutableString compressed,
+                                             size_t uncompressedLength) {
+  MOZ_ASSERT(data.is<Uncompressed<Unit>>(),
+             "should only be converting uncompressed source to compressed "
+             "source identically encoded");
+  MOZ_ASSERT(length() == uncompressedLength);
 
   if (pinnedUnitsStack_) {
     MOZ_ASSERT(pendingCompressed_.empty());
-    pendingCompressed_.construct<Compressed<Unit>>(std::move(raw),
+    pendingCompressed_.construct<Compressed<Unit>>(std::move(compressed),
                                                    uncompressedLength);
   } else {
-    data = SourceType(Compressed<Unit>(std::move(raw), uncompressedLength));
+    data =
+        SourceType(Compressed<Unit>(std::move(compressed), uncompressedLength));
   }
 }
 
 template <typename Unit>
-MOZ_MUST_USE bool ScriptSource::setCompressedSource(JSContext* cx,
-                                                    UniqueChars&& compressed,
-                                                    size_t rawLength,
-                                                    size_t sourceLength) {
-  MOZ_ASSERT(compressed);
+MOZ_MUST_USE bool ScriptSource::initializeWithCompressedSource(
+    JSContext* cx, UniqueChars&& compressed, size_t rawLength,
+    size_t sourceLength) {
+  MOZ_ASSERT(data.is<Missing>(), "shouldn't be double-initializing");
+  MOZ_ASSERT(compressed != nullptr);
 
   auto& cache = cx->zone()->runtimeFromAnyThread()->sharedImmutableStrings();
   auto deduped = cache.getOrCreate(std::move(compressed), rawLength);
   if (!deduped) {
     ReportOutOfMemory(cx);
     return false;
   }
 
-  setCompressedSource<Unit>(std::move(*deduped), sourceLength);
+  MOZ_ASSERT(pinnedUnitsStack_ == nullptr,
+             "shouldn't be initializing a ScriptSource while its characters "
+             "are pinned -- that only makes sense with a ScriptSource actively "
+             "being inspected");
+  data = SourceType(Compressed<Unit>(std::move(*deduped), sourceLength));
+
   return true;
 }
 
 template <typename Unit>
 bool ScriptSource::assignSource(JSContext* cx,
                                 const ReadOnlyCompileOptions& options,
                                 SourceText<Unit>& srcBuf) {
   MOZ_ASSERT(data.is<Missing>(),
@@ -2395,25 +2403,25 @@ void SourceCompressionTask::runTask() {
   }
 
   ScriptSource* source = sourceHolder_.get();
   MOZ_ASSERT(source->hasUncompressedSource());
 
   source->performTaskWork(this);
 }
 
-void ScriptSource::setCompressedSourceFromTask(
+void ScriptSource::convertToCompressedSourceFromTask(
     SharedImmutableString compressed) {
-  data.match(SetCompressedSourceFromTask(this, compressed));
+  data.match(ConvertToCompressedSourceFromTask(this, compressed));
 }
 
 void SourceCompressionTask::complete() {
   if (!shouldCancel() && resultString_.isSome()) {
     ScriptSource* source = sourceHolder_.get();
-    source->setCompressedSourceFromTask(std::move(*resultString_));
+    source->convertToCompressedSourceFromTask(std::move(*resultString_));
   }
 }
 
 void ScriptSource::addSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf,
                                           JS::ScriptSourceInfo* info) const {
   info->misc += mallocSizeOf(this) + mallocSizeOf(filename_.get()) +
                 mallocSizeOf(introducerFilename_.get());
   info->numScripts++;
@@ -2662,20 +2670,20 @@ XDRResult ScriptSource::XDR(XDRState<mod
           // 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 (!(srcCharSize == 1 ? ss->setCompressedSource<Utf8Unit>(
+          if (!(srcCharSize == 1 ? ss->initializeWithCompressedSource<Utf8Unit>(
                                        xdr->cx(), std::move(bytes),
                                        compressedLength, uncompressedLength)
-                                 : ss->setCompressedSource<char16_t>(
+                                 : ss->initializeWithCompressedSource<char16_t>(
                                        xdr->cx(), std::move(bytes),
                                        compressedLength, uncompressedLength))) {
             return xdr->fail(JS::TranscodeResult_Throw);
           }
         } else {
           void* bytes = srcCharSize == 1 ? ss->compressedData<Utf8Unit>()
                                          : ss->compressedData<char16_t>();
           MOZ_TRY(xdr->codeBytes(bytes, compressedLength));
--- a/js/src/vm/JSScript.h
+++ b/js/src/vm/JSScript.h
@@ -541,17 +541,17 @@ class ScriptSource {
   };
 
   using SourceType =
       mozilla::Variant<Compressed<mozilla::Utf8Unit>,
                        Uncompressed<mozilla::Utf8Unit>, Compressed<char16_t>,
                        Uncompressed<char16_t>, Missing, BinAST>;
   SourceType data;
 
-  // If the GC attempts to call setCompressedSource with PinnedUnits
+  // If the GC attempts to call convertToCompressedSource with PinnedUnits
   // present, the first PinnedUnits (that is, bottom of the stack) will set
   // the compressed chars upon destruction.
   PinnedUnitsBase* pinnedUnitsStack_;
   mozilla::MaybeOneOf<Compressed<mozilla::Utf8Unit>, Compressed<char16_t>>
       pendingCompressed_;
 
   // The filename of this script.
   UniqueChars filename_;
@@ -963,25 +963,30 @@ class ScriptSource {
                               size_t length);
 
   template <typename Unit>
   void setSource(
       typename SourceTypeTraits<Unit>::SharedImmutableString uncompressed);
 
   MOZ_MUST_USE bool tryCompressOffThread(JSContext* cx);
 
-  // The Unit parameter determines which type of compressed source is
-  // recorded, but raw compressed source is always single-byte.
+  // Convert this ScriptSource from storing uncompressed source of the given
+  // type, to storing compressed source.  (Raw compressed source is always
+  // single-byte; |Unit| just records the encoding of the uncompressed source.)
   template <typename Unit>
-  void setCompressedSource(SharedImmutableString compressed,
-                           size_t sourceLength);
-
+  void convertToCompressedSource(SharedImmutableString compressed,
+                                 size_t sourceLength);
+
+  // Initialize a fresh ScriptSource as containing compressed source of the
+  // indicated original encoding.
   template <typename Unit>
-  MOZ_MUST_USE bool setCompressedSource(JSContext* cx, UniqueChars&& raw,
-                                        size_t rawLength, size_t sourceLength);
+  MOZ_MUST_USE bool initializeWithCompressedSource(JSContext* cx,
+                                                   UniqueChars&& raw,
+                                                   size_t rawLength,
+                                                   size_t sourceLength);
 
 #if defined(JS_BUILD_BINAST)
 
   /*
    * Do not take ownership of the given `buf`. Store the canonical, shared
    * and de-duplicated version. If there is no extant shared version of
    * `buf`, make a copy.
    */
@@ -997,28 +1002,28 @@ class ScriptSource {
 
   const uint8_t* binASTSource();
 
 #endif /* JS_BUILD_BINAST */
 
  private:
   void performTaskWork(SourceCompressionTask* task);
 
-  struct SetCompressedSourceFromTask {
+  struct ConvertToCompressedSourceFromTask {
     ScriptSource* const source_;
     SharedImmutableString& compressed_;
 
-    SetCompressedSourceFromTask(ScriptSource* source,
-                                SharedImmutableString& compressed)
+    ConvertToCompressedSourceFromTask(ScriptSource* source,
+                                      SharedImmutableString& compressed)
         : source_(source), compressed_(compressed) {}
 
     template <typename Unit>
     void operator()(const Uncompressed<Unit>&) {
-      source_->setCompressedSource<Unit>(std::move(compressed_),
-                                         source_->length());
+      source_->convertToCompressedSource<Unit>(std::move(compressed_),
+                                               source_->length());
     }
 
     template <typename Unit>
     void operator()(const Compressed<Unit>&) {
       MOZ_CRASH(
           "can't set compressed source when source is already "
           "compressed -- ScriptSource::tryCompressOffThread "
           "shouldn't have queued up this task?");
@@ -1033,17 +1038,17 @@ class ScriptSource {
     void operator()(const Missing&) {
       MOZ_CRASH(
           "doesn't make sense to set compressed source for "
           "missing source -- ScriptSource::tryCompressOffThread "
           "shouldn't have queued up this task?");
     }
   };
 
-  void setCompressedSourceFromTask(SharedImmutableString compressed);
+  void convertToCompressedSourceFromTask(SharedImmutableString compressed);
 
  private:
   // It'd be better to make this function take <XDRMode, Unit>, as both
   // specializations of this function contain nested Unit-parametrized
   // helper classes that do everything the function needs to do.  But then
   // we'd need template function partial specialization to hold XDRMode
   // constant while varying Unit, so that idea's no dice.
   template <XDRMode mode>