Bug 1386103 (part 1, attempt 3) - Specify nsAuto[C]String storage size via template parameter. r=dbaron.
authorNicholas Nethercote <nnethercote@mozilla.com>
Wed, 09 Aug 2017 20:41:38 +1000
changeset 427564 14de940bb317a497f50f6e80a1b5439fe9d049a6
parent 427563 b0bdc6c0e7ef5fdb86e8fd3fb276b2c9cab364ad
child 427565 a5f0bad7a33adbe4a0802a65977069dec6a002d5
push id1567
push userjlorenzo@mozilla.com
push dateThu, 02 Nov 2017 12:36:05 +0000
treeherdermozilla-release@e512c14a0406 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdbaron
bugs1386103
milestone57.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 1386103 (part 1, attempt 3) - Specify nsAuto[C]String storage size via template parameter. r=dbaron.
dom/indexedDB/ProfilerHelpers.h
security/manager/ssl/nsNSSCertificate.h
storage/mozStorageSQLFunctions.cpp
xpcom/string/nsStringFwd.h
xpcom/string/nsTString.h
xpcom/string/string-template-def-char.h
xpcom/string/string-template-def-unichar.h
xpcom/string/string-template-undef.h
--- a/dom/indexedDB/ProfilerHelpers.h
+++ b/dom/indexedDB/ProfilerHelpers.h
@@ -56,17 +56,17 @@ public:
       }
     }
   }
 
   explicit
   LoggingIdString(const nsID& aID)
   {
     static_assert(NSID_LENGTH > 1, "NSID_LENGTH is set incorrectly!");
-    static_assert(NSID_LENGTH <= kDefaultStorageSize,
+    static_assert(NSID_LENGTH <= kStorageSize,
                   "nID string won't fit in our storage!");
     MOZ_ASSERT(Capacity() > NSID_LENGTH);
 
     if (IndexedDatabaseManager::GetLoggingMode() !=
           IndexedDatabaseManager::Logging_Disabled) {
       // NSID_LENGTH counts the null terminator, SetLength() does not.
       SetLength(NSID_LENGTH - 1);
 
--- a/security/manager/ssl/nsNSSCertificate.h
+++ b/security/manager/ssl/nsNSSCertificate.h
@@ -15,17 +15,16 @@
 #include "nsISimpleEnumerator.h"
 #include "nsIX509Cert.h"
 #include "nsIX509CertDB.h"
 #include "nsIX509CertList.h"
 #include "nsNSSShutDown.h"
 
 namespace mozilla { namespace pkix { class DERArray; } }
 
-class nsAutoString;
 class nsINSSComponent;
 class nsIASN1Sequence;
 
 class nsNSSCertificate final : public nsIX509Cert,
                                public nsISerializable,
                                public nsIClassInfo,
                                public nsNSSShutDownObject
 {
--- a/storage/mozStorageSQLFunctions.cpp
+++ b/storage/mozStorageSQLFunctions.cpp
@@ -160,18 +160,18 @@ levenshteinDistance(const nsAString &aSt
     // Note that the row width is sLen + 1 and the column height is tLen + 1,
     // where sLen is the length of the string "s" and tLen is the length of "t".
     // The first row and the first column are initialized as shown, and
     // the algorithm computes the remaining cells row-by-row, and
     // left-to-right within each row.  The computation only requires that
     // we be able to see the current row and the previous one.
 
     // Allocate memory for two rows.
-    AutoTArray<int, nsAutoString::kDefaultStorageSize> row1;
-    AutoTArray<int, nsAutoString::kDefaultStorageSize> row2;
+    AutoTArray<int, nsAutoString::kStorageSize> row1;
+    AutoTArray<int, nsAutoString::kStorageSize> row2;
 
     // Declare the raw pointers that will actually be used to access the memory.
     int *prevRow = row1.AppendElements(sLen + 1);
     int *currRow = row2.AppendElements(sLen + 1);
 
     // Initialize the first row.
     for (uint32_t i = 0; i <= sLen; i++)
         prevRow[i] = i;
--- a/xpcom/string/nsStringFwd.h
+++ b/xpcom/string/nsStringFwd.h
@@ -19,32 +19,36 @@ namespace mozilla {
 namespace detail {
 
 class nsStringRepr;
 class nsCStringRepr;
 
 } // namespace detail
 } // namespace mozilla
 
+static const size_t AutoStringDefaultStorageSize = 64;
+
 // Double-byte (char16_t) string types.
 class nsAString;
 class nsSubstringTuple;
 class nsString;
-class nsAutoString;
+template<size_t N> class nsAutoStringN;
+using nsAutoString = nsAutoStringN<AutoStringDefaultStorageSize>;
 class nsDependentString;
 class nsDependentSubstring;
 class nsPromiseFlatString;
 class nsStringComparator;
 class nsDefaultStringComparator;
 
 // Single-byte (char) string types.
 class nsACString;
 class nsCSubstringTuple;
 class nsCString;
-class nsAutoCString;
+template<size_t N> class nsAutoCStringN;
+using nsAutoCString = nsAutoCStringN<AutoStringDefaultStorageSize>;
 class nsDependentCString;
 class nsDependentCSubstring;
 class nsPromiseFlatCString;
 class nsCStringComparator;
 class nsDefaultCStringComparator;
 class nsXPIDLCString; // deprecated
 
 #endif /* !defined(nsStringFwd_h___) */
--- a/xpcom/string/nsTString.h
+++ b/xpcom/string/nsTString.h
@@ -555,73 +555,75 @@ protected:
  * nsTAutoString_CharT
  *
  * Subclass of nsTString_CharT that adds support for stack-based string
  * allocation.  It is normally not a good idea to use this class on the
  * heap, because it will allocate space which may be wasted if the string
  * it contains is significantly smaller or any larger than 64 characters.
  *
  * NAMES:
- *   nsAutoString for wide characters
- *   nsAutoCString for narrow characters
+ *   nsAutoStringN / nsAutoString for wide characters
+ *   nsAutoCStringN / nsAutoCString for narrow characters
  */
-class MOZ_NON_MEMMOVABLE nsTAutoString_CharT : public nsTFixedString_CharT
+template<size_t N>
+class MOZ_NON_MEMMOVABLE nsTAutoStringN_CharT : public nsTFixedString_CharT
 {
 public:
 
-  typedef nsTAutoString_CharT self_type;
+  typedef nsTAutoStringN_CharT<N> self_type;
 
 public:
 
   /**
    * constructors
    */
 
-  nsTAutoString_CharT()
-    : fixed_string_type(mStorage, kDefaultStorageSize, 0)
+  nsTAutoStringN_CharT()
+    : fixed_string_type(mStorage, N, 0)
   {
   }
 
   explicit
-  nsTAutoString_CharT(char_type aChar)
-    : fixed_string_type(mStorage, kDefaultStorageSize, 0)
+  nsTAutoStringN_CharT(char_type aChar)
+    : fixed_string_type(mStorage, N, 0)
   {
     Assign(aChar);
   }
 
   explicit
-  nsTAutoString_CharT(const char_type* aData, size_type aLength = size_type(-1))
-    : fixed_string_type(mStorage, kDefaultStorageSize, 0)
+  nsTAutoStringN_CharT(const char_type* aData,
+                       size_type aLength = size_type(-1))
+    : fixed_string_type(mStorage, N, 0)
   {
     Assign(aData, aLength);
   }
 
 #if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
   explicit
-  nsTAutoString_CharT(char16ptr_t aData, size_type aLength = size_type(-1))
-    : nsTAutoString_CharT(static_cast<const char16_t*>(aData), aLength)
+  nsTAutoStringN_CharT(char16ptr_t aData, size_type aLength = size_type(-1))
+    : nsTAutoStringN_CharT(static_cast<const char16_t*>(aData), aLength)
   {
   }
 #endif
 
-  nsTAutoString_CharT(const self_type& aStr)
-    : fixed_string_type(mStorage, kDefaultStorageSize, 0)
+  nsTAutoStringN_CharT(const self_type& aStr)
+    : fixed_string_type(mStorage, N, 0)
   {
     Assign(aStr);
   }
 
   explicit
-  nsTAutoString_CharT(const substring_type& aStr)
-    : fixed_string_type(mStorage, kDefaultStorageSize, 0)
+  nsTAutoStringN_CharT(const substring_type& aStr)
+    : fixed_string_type(mStorage, N, 0)
   {
     Assign(aStr);
   }
 
-  MOZ_IMPLICIT nsTAutoString_CharT(const substring_tuple_type& aTuple)
-    : fixed_string_type(mStorage, kDefaultStorageSize, 0)
+  MOZ_IMPLICIT nsTAutoStringN_CharT(const substring_tuple_type& aTuple)
+    : fixed_string_type(mStorage, N, 0)
   {
     Assign(aTuple);
   }
 
   // |operator=| does not inherit, so we must define our own
   self_type& operator=(char_type aChar)
   {
     Assign(aChar);
@@ -650,26 +652,26 @@ public:
     return *this;
   }
   self_type& operator=(const substring_tuple_type& aTuple)
   {
     Assign(aTuple);
     return *this;
   }
 
-  enum
-  {
-    kDefaultStorageSize = 64
-  };
+  static const size_t kStorageSize = N;
 
 private:
 
-  char_type mStorage[kDefaultStorageSize];
+  char_type mStorage[N];
 };
 
+// We define this typedef instead of providing a default value for N so that so
+// there is a default typename that doesn't require angle brackets.
+using nsTAutoString_CharT = nsTAutoStringN_CharT<AutoStringDefaultStorageSize>;
 
 //
 // nsAutoString stores pointers into itself which are invalidated when an
 // nsTArray is resized, so nsTArray must not be instantiated with nsAutoString
 // elements!
 //
 template<class E> class nsTArrayElementTraits;
 template<>
--- a/xpcom/string/string-template-def-char.h
+++ b/xpcom/string/string-template-def-char.h
@@ -6,16 +6,17 @@
 // IWYU pragma: private, include "nsString.h"
 
 #define CharT                               char
 #define CharT_is_char                       1
 #define nsTAString_IncompatibleCharT        nsAString
 #define nsTString_CharT                     nsCString
 #define nsTStringRepr_CharT                 nsCStringRepr
 #define nsTFixedString_CharT                nsFixedCString
+#define nsTAutoStringN_CharT                nsAutoCStringN
 #define nsTAutoString_CharT                 nsAutoCString
 #define nsTSubstring_CharT                  nsACString
 #define PrintfAppend_CharT                  PrintfAppend_nsACString
 #define nsTSubstringTuple_CharT             nsCSubstringTuple
 #define nsTStringComparator_CharT           nsCStringComparator
 #define nsTDefaultStringComparator_CharT    nsDefaultCStringComparator
 #define nsTDependentString_CharT            nsDependentCString
 #define nsTDependentSubstring_CharT         nsDependentCSubstring
--- a/xpcom/string/string-template-def-unichar.h
+++ b/xpcom/string/string-template-def-unichar.h
@@ -6,16 +6,17 @@
 // IWYU pragma: private, include "nsString.h"
 
 #define CharT                               char16_t
 #define CharT_is_PRUnichar                  1
 #define nsTAString_IncompatibleCharT        nsACString
 #define nsTString_CharT                     nsString
 #define nsTStringRepr_CharT                 nsStringRepr
 #define nsTFixedString_CharT                nsFixedString
+#define nsTAutoStringN_CharT                nsAutoStringN
 #define nsTAutoString_CharT                 nsAutoString
 #define nsTSubstring_CharT                  nsAString
 #define PrintfAppend_CharT                  PrintfAppend_nsAString
 #define nsTSubstringTuple_CharT             nsSubstringTuple
 #define nsTStringComparator_CharT           nsStringComparator
 #define nsTDefaultStringComparator_CharT    nsDefaultStringComparator
 #define nsTDependentString_CharT            nsDependentString
 #define nsTDependentSubstring_CharT         nsDependentSubstring
--- a/xpcom/string/string-template-undef.h
+++ b/xpcom/string/string-template-undef.h
@@ -7,16 +7,17 @@
 
 #undef CharT
 #undef CharT_is_PRUnichar
 #undef CharT_is_char
 #undef nsTAString_IncompatibleCharT
 #undef nsTString_CharT
 #undef nsTStringRepr_CharT
 #undef nsTFixedString_CharT
+#undef nsTAutoStringN_CharT
 #undef nsTAutoString_CharT
 #undef nsTSubstring_CharT
 #undef PrintfAppend_CharT
 #undef nsTSubstringTuple_CharT
 #undef nsTStringComparator_CharT
 #undef nsTDefaultStringComparator_CharT
 #undef nsTDependentString_CharT
 #undef nsTDependentSubstring_CharT