From 38a520c23005b5eae6e6bae3778791907ec556fa Mon Sep 17 00:00:00 2001 From: "Christopher R. Palmer" Date: Mon, 4 Jan 2016 20:29:02 -0500 Subject: aapt: Use a std::map instead of a SortedVector Android's SortedVectorImpl uses arrays that it must insert into the middle of. Each insertion is O(N) time because it must move on average half the elements of the array to make room for the new element. That is, O(N^2) time to build this sorted vector. std::map on the other hand normally uses red/black trees and has a cost of NlogN to add N elements to it. Change-Id: I5da0363ba806ab615b2aad0fb2a43ef9a9bec327 --- tools/aapt/StringPool.cpp | 21 +++++++++++---------- tools/aapt/StringPool.h | 3 ++- 2 files changed, 13 insertions(+), 11 deletions(-) (limited to 'tools/aapt') diff --git a/tools/aapt/StringPool.cpp b/tools/aapt/StringPool.cpp index e449896..0d59fae 100644 --- a/tools/aapt/StringPool.cpp +++ b/tools/aapt/StringPool.cpp @@ -127,7 +127,7 @@ int StringPool::entry::compare(const entry& o) const { } StringPool::StringPool(bool utf8) : - mUTF8(utf8), mValues(-1) + mUTF8(utf8) { } @@ -144,8 +144,8 @@ ssize_t StringPool::add(const String16& value, const Vector& s ssize_t StringPool::add(const String16& value, bool mergeDuplicates, const String8* configTypeName, const ResTable_config* config) { - ssize_t vidx = mValues.indexOfKey(value); - ssize_t pos = vidx >= 0 ? mValues.valueAt(vidx) : -1; + auto it = mValues.find(value); + ssize_t pos = it != mValues.end() ? it->second : -1; ssize_t eidx = pos >= 0 ? mEntryArray.itemAt(pos) : -1; if (eidx < 0) { eidx = mEntries.add(entry(value)); @@ -192,21 +192,21 @@ ssize_t StringPool::add(const String16& value, } } - const bool first = vidx < 0; + const bool first = (it == mValues.end()); const bool styled = (pos >= 0 && (size_t)pos < mEntryStyleArray.size()) ? mEntryStyleArray[pos].spans.size() : 0; if (first || styled || !mergeDuplicates) { pos = mEntryArray.add(eidx); if (first) { - vidx = mValues.add(value, pos); + mValues[value] = pos; } entry& ent = mEntries.editItemAt(eidx); ent.indices.add(pos); } if (kIsDebug) { - printf("Adding string %s to pool: pos=%zd eidx=%zd vidx=%zd\n", - String8(value).string(), SSIZE(pos), SSIZE(eidx), SSIZE(vidx)); + printf("Adding string %s to pool: pos=%zd eidx=%zd\n", + String8(value).string(), SSIZE(pos), SSIZE(eidx)); } return pos; @@ -371,7 +371,7 @@ void StringPool::sortByConfig() mValues.clear(); for (size_t i=0; i* StringPool::offsetsForString(const String16& val) const { - ssize_t pos = mValues.valueFor(val); - if (pos < 0) { + auto it = mValues.find(val); + if (it == mValues.end()) { return NULL; } + ssize_t pos = it->second; return &mEntries[mEntryArray[pos]].indices; } diff --git a/tools/aapt/StringPool.h b/tools/aapt/StringPool.h index dbe8c85..3014a3b 100644 --- a/tools/aapt/StringPool.h +++ b/tools/aapt/StringPool.h @@ -19,6 +19,7 @@ #include #include #include +#include #include @@ -175,7 +176,7 @@ private: // Unique set of all the strings added to the pool, mapped to // the first index of mEntryArray where the value was added. - DefaultKeyedVector mValues; + std::map mValues; // This array maps from the original position a string was placed at // in mEntryArray to its new position after being sorted with sortByConfig(). Vector mOriginalPosToNewPos; -- cgit v1.1