Skip to content

Commit c9fd926

Browse files
author
Jeff Brown
committed
Use quicksort to sort the string pool.
The current implementation of Vector::sort uses insertion sort on the assumption that the data is mostly sorted. It isn't. This change brings the total time spent sorting packages by config down to 500ms from about 93 seconds. Bug: 6186278 Change-Id: Iec8da11e09297acd6c73733d063b0fa9dacf69f7
1 parent 61361f3 commit c9fd926

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

tools/aapt/StringPool.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ status_t StringPool::addStyleSpan(size_t idx, const entry_style_span& span)
213213
return NO_ERROR;
214214
}
215215

216-
int StringPool::config_sort(const size_t* lhs, const size_t* rhs, void* state)
216+
int StringPool::config_sort(void* state, const void* lhs, const void* rhs)
217217
{
218218
StringPool* pool = (StringPool*)state;
219-
const entry& lhe = pool->mEntries[pool->mEntryArray[*lhs]];
220-
const entry& rhe = pool->mEntries[pool->mEntryArray[*rhs]];
219+
const entry& lhe = pool->mEntries[pool->mEntryArray[*static_cast<const size_t*>(lhs)]];
220+
const entry& rhe = pool->mEntries[pool->mEntryArray[*static_cast<const size_t*>(rhs)]];
221221
return lhe.compare(rhe);
222222
}
223223

@@ -232,13 +232,17 @@ void StringPool::sortByConfig()
232232
// At that point it maps from the new position in the array to the
233233
// original position the entry appeared.
234234
Vector<size_t> newPosToOriginalPos;
235-
for (size_t i=0; i<mEntryArray.size(); i++) {
235+
newPosToOriginalPos.setCapacity(N);
236+
for (size_t i=0; i < N; i++) {
236237
newPosToOriginalPos.add(i);
237238
}
238239

239240
// Sort the array.
240241
NOISY(printf("SORTING STRINGS BY CONFIGURATION...\n"));
241-
newPosToOriginalPos.sort(config_sort, this);
242+
// Vector::sort uses insertion sort, which is very slow for this data set.
243+
// Use quicksort instead because we don't need a stable sort here.
244+
qsort_r(newPosToOriginalPos.editArray(), N, sizeof(size_t), this, config_sort);
245+
//newPosToOriginalPos.sort(config_sort, this);
242246
NOISY(printf("DONE SORTING STRINGS BY CONFIGURATION.\n"));
243247

244248
// Create the reverse mapping from the original position in the array

tools/aapt/StringPool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class StringPool
139139
const Vector<size_t>* offsetsForString(const String16& val) const;
140140

141141
private:
142-
static int config_sort(const size_t* lhs, const size_t* rhs, void* state);
142+
static int config_sort(void* state, const void* lhs, const void* rhs);
143143

144144
const bool mUTF8;
145145

0 commit comments

Comments
 (0)