Bug 1225923 - part 1 - convert all needs-to-copy instances of AppendElement(nsDependentString(...)); r=erahm
authorNathan Froyd <froydnj@mozilla.com>
Wed, 18 Nov 2015 10:25:19 -0500
changeset 309087 fe7c4e4d145119da9e35b29214078e8b6b6acee9
parent 309086 42d9f12af41a7491bfde4875ac6fc44a0c77afa1
child 309088 c18269dc70b8446906adaf809ad6b44e3c347327
push id5513
push userraliiev@mozilla.com
push dateMon, 25 Jan 2016 13:55:34 +0000
treeherdermozilla-beta@5ee97dd05b5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerserahm
bugs1225923
milestone45.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 1225923 - part 1 - convert all needs-to-copy instances of AppendElement(nsDependentString(...)); r=erahm When people write: array.AppendElement(nsDependentString(...)); (resp. nsDependentCString), it's not clear whether they expect the newly constructed dependent string to live in the array, or whether they're just making a nsString-like holder whose contents can be freely copied into the array's newly-created nsString. Sometimes the latter is what you prefer, and sometimes the former. In all cases, however, the latter behavior is what you get. Let's try to make that behavior more explicit by pre-constructing nsString elements and then using Assign to show that copying is taking place. This patch involves no functional change in behavior (it ought to be epsilon faster due to using AppendElements, rather than repeatedly calling AppendElement).
extensions/spellcheck/src/mozPersonalDictionary.cpp
extensions/spellcheck/src/mozSpellChecker.cpp
storage/mozStorageStatement.cpp
widget/windows/nsDeviceContextSpecWin.cpp
xpcom/ds/nsINIParserImpl.cpp
--- a/extensions/spellcheck/src/mozPersonalDictionary.cpp
+++ b/extensions/spellcheck/src/mozPersonalDictionary.cpp
@@ -370,19 +370,21 @@ NS_IMETHODIMP mozPersonalDictionary::Sav
   if(NS_FAILED(res)) return res;
 
   nsCOMPtr<nsIEventTarget> target =
     do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &res);
   if (NS_WARN_IF(NS_FAILED(res))) {
     return res;
   }
 
-  nsTArray<nsString> array(mDictionaryTable.Count());
+  nsTArray<nsString> array;
+  nsString* elems = array.AppendElements(mDictionaryTable.Count());
   for (auto iter = mDictionaryTable.Iter(); !iter.Done(); iter.Next()) {
-    array.AppendElement(nsDependentString(iter.Get()->GetKey()));
+    elems->Assign(iter.Get()->GetKey());
+    elems++;
   }
 
   nsCOMPtr<nsIRunnable> runnable =
     new mozPersonalDictionarySave(this, theFile, mozilla::Move(array));
   res = target->Dispatch(runnable, NS_DISPATCH_NORMAL);
   if (NS_WARN_IF(NS_FAILED(res))) {
     return res;
   }
@@ -391,19 +393,21 @@ NS_IMETHODIMP mozPersonalDictionary::Sav
 
 NS_IMETHODIMP mozPersonalDictionary::GetWordList(nsIStringEnumerator **aWords)
 {
   NS_ENSURE_ARG_POINTER(aWords);
   *aWords = nullptr;
 
   WaitForLoad();
 
-  nsTArray<nsString> *array = new nsTArray<nsString>(mDictionaryTable.Count());
+  nsTArray<nsString> *array = new nsTArray<nsString>();
+  nsString* elems = array->AppendElements(mDictionaryTable.Count());
   for (auto iter = mDictionaryTable.Iter(); !iter.Done(); iter.Next()) {
-    array->AppendElement(nsDependentString(iter.Get()->GetKey()));
+    elems->Assign(iter.Get()->GetKey());
+    elems++;
   }
 
   array->Sort();
 
   return NS_NewAdoptingStringEnumerator(aWords, array);
 }
 
 NS_IMETHODIMP mozPersonalDictionary::Check(const char16_t *aWord, const char16_t *aLanguage, bool *aResult)
--- a/extensions/spellcheck/src/mozSpellChecker.cpp
+++ b/extensions/spellcheck/src/mozSpellChecker.cpp
@@ -148,19 +148,20 @@ mozSpellChecker::CheckWord(const nsAStri
   result = mSpellCheckingEngine->Check(PromiseFlatString(aWord).get(), &correct);
   NS_ENSURE_SUCCESS(result, result);
   if(!correct){
     if(aSuggestions){
       uint32_t count,i;
       char16_t **words;
       
       result = mSpellCheckingEngine->Suggest(PromiseFlatString(aWord).get(), &words, &count);
-      NS_ENSURE_SUCCESS(result, result); 
+      NS_ENSURE_SUCCESS(result, result);
+      nsString* suggestions = aSuggestions->AppendElements(count);
       for(i=0;i<count;i++){
-        aSuggestions->AppendElement(nsDependentString(words[i]));
+	suggestions[i].Assign(words[i]);
       }
       
       if (count)
         NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(count, words);
     }
     *aIsMisspelled = true;
   }
   return NS_OK;
--- a/storage/mozStorageStatement.cpp
+++ b/storage/mozStorageStatement.cpp
@@ -143,19 +143,20 @@ Statement::initialize(Connection *aDBCon
                                       mDBStatement));
 
   mDBConnection = aDBConnection;
   mNativeConnection = aNativeConnection;
   mParamCount = ::sqlite3_bind_parameter_count(mDBStatement);
   mResultColumnCount = ::sqlite3_column_count(mDBStatement);
   mColumnNames.Clear();
 
+  nsCString* columnNames = mColumnNames.AppendElements(mResultColumnCount);
   for (uint32_t i = 0; i < mResultColumnCount; i++) {
       const char *name = ::sqlite3_column_name(mDBStatement, i);
-      (void)mColumnNames.AppendElement(nsDependentCString(name));
+      columnNames[i].Assign(name);
   }
 
 #ifdef DEBUG
   // We want to try and test for LIKE and that consumers are using
   // escapeStringForLIKE instead of just trusting user input.  The idea to
   // check to see if they are binding a parameter after like instead of just
   // using a string.  We only do this in debug builds because it's expensive!
   const nsCaseInsensitiveCStringComparator c;
--- a/widget/windows/nsDeviceContextSpecWin.cpp
+++ b/widget/windows/nsDeviceContextSpecWin.cpp
@@ -683,19 +683,20 @@ nsPrinterEnumeratorWin::GetPrinterNameLi
   }
 
   uint32_t numPrinters = GlobalPrinters::GetInstance()->GetNumPrinters();
   nsTArray<nsString> *printers = new nsTArray<nsString>(numPrinters);
   if (!printers)
     return NS_ERROR_OUT_OF_MEMORY;
 
   uint32_t printerInx = 0;
+  nsString* names = printers->AppendElements(numPrinters);
   while( printerInx < numPrinters ) {
     LPWSTR name = GlobalPrinters::GetInstance()->GetItemFromList(printerInx++);
-    printers->AppendElement(nsDependentString(name));
+    names[printerInx].Assign(name);
   }
 
   return NS_NewAdoptingStringEnumerator(aPrinterNameList, printers);
 }
 
 //----------------------------------------------------------------------------------
 // Display the AdvancedDocumentProperties for the selected Printer
 NS_IMETHODIMP nsPrinterEnumeratorWin::DisplayPropertiesDlg(const char16_t *aPrinterName, nsIPrintSettings* aPrintSettings)
--- a/xpcom/ds/nsINIParserImpl.cpp
+++ b/xpcom/ds/nsINIParserImpl.cpp
@@ -71,17 +71,17 @@ nsINIParserFactory::LockFactory(bool aLo
 
 NS_IMPL_ISUPPORTS(nsINIParserImpl,
                   nsIINIParser)
 
 static bool
 SectionCB(const char* aSection, void* aClosure)
 {
   nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
-  strings->AppendElement(nsDependentCString(aSection));
+  strings->AppendElement()->Assign(aSection);
   return true;
 }
 
 NS_IMETHODIMP
 nsINIParserImpl::GetSections(nsIUTF8StringEnumerator** aResult)
 {
   nsTArray<nsCString>* strings = new nsTArray<nsCString>;
   if (!strings) {
@@ -99,17 +99,17 @@ nsINIParserImpl::GetSections(nsIUTF8Stri
 
   return rv;
 }
 
 static bool
 KeyCB(const char* aKey, const char* aValue, void* aClosure)
 {
   nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
-  strings->AppendElement(nsDependentCString(aKey));
+  strings->AppendElement()->Assign(aKey);
   return true;
 }
 
 NS_IMETHODIMP
 nsINIParserImpl::GetKeys(const nsACString& aSection,
                          nsIUTF8StringEnumerator** aResult)
 {
   nsTArray<nsCString>* strings = new nsTArray<nsCString>;