Bug 1370070 - Avoid unnecessary string creation. r=smaug
authorEric Rahm <erahm@mozilla.com>
Fri, 09 Jun 2017 15:56:35 -0700
changeset 411525 b18762d91f3fe1d6c828e6657189811b67b7548a
parent 411524 9bd1aecf586ba635b7c5cd1ef802ccb8491e540d
child 411526 f7987c98de3ecb90a07e58a1337e10ae2d83bb4e
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1370070
milestone55.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 1370070 - Avoid unnecessary string creation. r=smaug MozReview-Commit-ID: LLQQrCpK630
dom/base/nsContentUtils.cpp
dom/base/nsContentUtils.h
dom/file/FileReader.cpp
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -4488,31 +4488,37 @@ nsContentUtils::GetSubdocumentWithOuterW
 
   return nullptr;
 }
 
 // Convert the string from the given encoding to Unicode.
 /* static */
 nsresult
 nsContentUtils::ConvertStringFromEncoding(const nsACString& aEncoding,
-                                          const nsACString& aInput,
+                                          const char* aInput,
+                                          uint32_t aInputLen,
                                           nsAString& aOutput)
 {
+  CheckedInt32 len = aInputLen;
+  if (!len.isValid()) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
   nsAutoCString encoding;
   if (aEncoding.IsEmpty()) {
     encoding.AssignLiteral("UTF-8");
   } else {
     encoding.Assign(aEncoding);
   }
 
   ErrorResult rv;
   nsAutoPtr<TextDecoder> decoder(new TextDecoder());
   decoder->InitWithEncoding(encoding, false);
 
-  decoder->Decode(aInput.BeginReading(), aInput.Length(), false,
+  decoder->Decode(aInput, len.value(), false,
                   aOutput, rv);
   return rv.StealNSResult();
 }
 
 /* static */
 bool
 nsContentUtils::CheckForBOM(const unsigned char* aBuffer, uint32_t aLength,
                             nsACString& aCharset)
--- a/dom/base/nsContentUtils.h
+++ b/dom/base/nsContentUtils.h
@@ -619,18 +619,26 @@ public:
 
   /**
    * Convert aInput (in encoding aEncoding) to UTF16 in aOutput.
    *
    * @param aEncoding the Gecko-canonical name of the encoding or the empty
    *                  string (meaning UTF-8)
    */
   static nsresult ConvertStringFromEncoding(const nsACString& aEncoding,
+                                            const char* aInput,
+                                            uint32_t aInputLen,
+                                            nsAString& aOutput);
+
+  static nsresult ConvertStringFromEncoding(const nsACString& aEncoding,
                                             const nsACString& aInput,
-                                            nsAString& aOutput);
+                                            nsAString& aOutput) {
+    return ConvertStringFromEncoding(
+        aEncoding, aInput.BeginReading(), aInput.Length(), aOutput);
+  }
 
   /**
    * Determine whether a buffer begins with a BOM for UTF-8, UTF-16LE,
    * UTF-16BE
    *
    * @param aBuffer the buffer to check
    * @param aLength the length of the buffer
    * @param aCharset empty if not found
--- a/dom/file/FileReader.cpp
+++ b/dom/file/FileReader.cpp
@@ -475,18 +475,18 @@ FileReader::GetAsText(Blob *aBlob,
                                        &charsetEnd);
       if (!EncodingUtils::FindEncodingForLabel(specifiedCharset, encoding)) {
         // Type property failed. Use UTF-8.
         encoding.AssignLiteral("UTF-8");
       }
     }
   }
 
-  nsDependentCSubstring data(aFileData, aDataLen);
-  return nsContentUtils::ConvertStringFromEncoding(encoding, data, aResult);
+  return nsContentUtils::ConvertStringFromEncoding(
+      encoding, aFileData, aDataLen, aResult);
 }
 
 nsresult
 FileReader::GetAsDataURL(Blob *aBlob,
                          const char *aFileData,
                          uint32_t aDataLen,
                          nsAString& aResult)
 {