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).
This commit is contained in:
Nathan Froyd 2015-11-18 10:25:19 -05:00
parent 26c11eebf4
commit 84e3d19a36
5 changed files with 17 additions and 10 deletions

View File

@ -375,9 +375,11 @@ NS_IMETHODIMP mozPersonalDictionary::Save()
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 =
@ -396,9 +398,11 @@ NS_IMETHODIMP mozPersonalDictionary::GetWordList(nsIStringEnumerator **aWords)
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();

View File

@ -153,9 +153,10 @@ mozSpellChecker::CheckWord(const nsAString &aWord, bool *aIsMisspelled, nsTArray
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)

View File

@ -148,9 +148,10 @@ Statement::initialize(Connection *aDBConnection,
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

View File

@ -688,9 +688,10 @@ nsPrinterEnumeratorWin::GetPrinterNameList(nsIStringEnumerator **aPrinterNameLis
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);

View File

@ -76,7 +76,7 @@ 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;
}
@ -104,7 +104,7 @@ 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;
}