Bug 1287277 - Avoid unnecessary string copies in MatchAutoCompleteFunction::fixupURISpec. r=mak
authorSimon Lindholm <simon.lindholm10@gmail.com>
Thu, 14 Jul 2016 01:25:46 +0200
changeset 332247 dcb3c6e5c58b9f855c9c2aef44f7a10428a4e802
parent 332246 bcfdd291e2f150aa667b7b43ed756ff7516e6ea1
child 332248 ca925530409a23a33fe6bc35e6cbed8a71016cff
push id9858
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 14:37:10 +0000
treeherdermozilla-aurora@203106ef6cb6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1287277
milestone50.0a1
Bug 1287277 - Avoid unnecessary string copies in MatchAutoCompleteFunction::fixupURISpec. r=mak
toolkit/components/places/SQLFunctions.cpp
toolkit/components/places/SQLFunctions.h
--- a/toolkit/components/places/SQLFunctions.cpp
+++ b/toolkit/components/places/SQLFunctions.cpp
@@ -206,46 +206,50 @@ namespace places {
       NS_LITERAL_CSTRING("autocomplete_match"), kArgIndexLength, function
     );
     NS_ENSURE_SUCCESS(rv, rv);
 
     return NS_OK;
   }
 
   /* static */
-  void
-  MatchAutoCompleteFunction::fixupURISpec(const nsCString &aURISpec,
+  nsDependentCSubstring
+  MatchAutoCompleteFunction::fixupURISpec(const nsACString &aURISpec,
                                           int32_t aMatchBehavior,
-                                          nsCString &_fixedSpec)
+                                          nsACString &aSpecBuf)
   {
-    nsCString unescapedSpec;
-    (void)NS_UnescapeURL(aURISpec, esc_SkipControl | esc_AlwaysCopy,
-                         unescapedSpec);
+    nsDependentCSubstring fixedSpec;
 
-    // If this unescaped string is valid UTF-8, we'll use it.  Otherwise,
-    // we will simply use our original string.
-    NS_ASSERTION(_fixedSpec.IsEmpty(),
-                 "Passing a non-empty string as an out parameter!");
-    if (IsUTF8(unescapedSpec))
-      _fixedSpec.Assign(unescapedSpec);
-    else
-      _fixedSpec.Assign(aURISpec);
+    // Try to unescape the string.  If that succeeds and yields a different
+    // string which is also valid UTF-8, we'll use it.
+    // Otherwise, we will simply use our original string.
+    bool unescaped = NS_UnescapeURL(aURISpec.BeginReading(),
+      aURISpec.Length(), esc_SkipControl, aSpecBuf);
+    if (unescaped && IsUTF8(aSpecBuf)) {
+      fixedSpec.Rebind(aSpecBuf, 0);
+    } else {
+      fixedSpec.Rebind(aURISpec, 0);
+    }
 
     if (aMatchBehavior == mozIPlacesAutoComplete::MATCH_ANYWHERE_UNMODIFIED)
-      return;
+      return fixedSpec;
 
-    if (StringBeginsWith(_fixedSpec, NS_LITERAL_CSTRING("http://")))
-      _fixedSpec.Cut(0, 7);
-    else if (StringBeginsWith(_fixedSpec, NS_LITERAL_CSTRING("https://")))
-      _fixedSpec.Cut(0, 8);
-    else if (StringBeginsWith(_fixedSpec, NS_LITERAL_CSTRING("ftp://")))
-      _fixedSpec.Cut(0, 6);
+    if (StringBeginsWith(fixedSpec, NS_LITERAL_CSTRING("http://"))) {
+      fixedSpec.Rebind(fixedSpec, 7);
+    } else if (StringBeginsWith(fixedSpec, NS_LITERAL_CSTRING("https://"))) {
+      fixedSpec.Rebind(fixedSpec, 8);
+    } else if (StringBeginsWith(fixedSpec, NS_LITERAL_CSTRING("ftp://"))) {
+      fixedSpec.Rebind(fixedSpec, 6);
+    }
 
-    if (StringBeginsWith(_fixedSpec, NS_LITERAL_CSTRING("www.")))
-      _fixedSpec.Cut(0, 4);
+    if (StringBeginsWith(fixedSpec, NS_LITERAL_CSTRING("www."))) {
+      fixedSpec.Rebind(fixedSpec, 4);
+    }
+
+    return fixedSpec;
   }
 
   /* static */
   bool
   MatchAutoCompleteFunction::findAnywhere(const nsDependentCSubstring &aToken,
                                           const nsACString &aSourceString)
   {
     // We can't use FindInReadable here; it works only for ASCII.
@@ -388,18 +392,19 @@ namespace places {
       NS_ADDREF(*_result = new IntegerVariant(0));
       return NS_OK;
     }
 
     // Obtain our search function.
     searchFunctionPtr searchFunction = getSearchFunction(matchBehavior);
 
     // Clean up our URI spec and prepare it for searching.
-    nsCString fixedUrl;
-    fixupURISpec(url, matchBehavior, fixedUrl);
+    nsCString fixedUrlBuf;
+    nsDependentCSubstring fixedUrl =
+      fixupURISpec(url, matchBehavior, fixedUrlBuf);
     // Limit the number of chars we search through.
     const nsDependentCSubstring& trimmedUrl =
       Substring(fixedUrl, 0, MAX_CHARS_TO_SEARCH_THROUGH);
 
     nsDependentCString title = getSharedString(aArguments, kArgIndexTitle);
     // Limit the number of chars we search through.
     const nsDependentCSubstring& trimmedTitle =
       Substring(title, 0, MAX_CHARS_TO_SEARCH_THROUGH);
--- a/toolkit/components/places/SQLFunctions.h
+++ b/toolkit/components/places/SQLFunctions.h
@@ -161,21 +161,23 @@ private:
    * unescaping escaped characters and removing certain specs that we do not
    * care to search for.
    *
    * @param aURISpec
    *        The spec of the URI to prepare for searching.
    * @param aMatchBehavior
    *        The matching behavior to use defined by one of the
    *        mozIPlacesAutoComplete::MATCH_* values.
-   * @param _fixedSpec
-   *        An out parameter that is the fixed up string.
+   * @param aSpecBuf
+   *        A string buffer that the returned slice can point into, if needed.
+   * @return the fixed up string.
    */
-  static void fixupURISpec(const nsCString &aURISpec, int32_t aMatchBehavior,
-                           nsCString &_fixedSpec);
+  static nsDependentCSubstring fixupURISpec(const nsACString &aURISpec,
+                                            int32_t aMatchBehavior,
+                                            nsACString &aSpecBuf);
 };
 
 
 ////////////////////////////////////////////////////////////////////////////////
 //// Frecency Calculation Function
 
 /**
  * This function is used to calculate frecency for a page.