Avoid excessive allocation of std::set<> nodes in SwapSpace.
This does not affect the overall memory usage but avoids
a lot of deallocations followed by allocation.
Measured compilation of a big app using heap track:
bytes allocated in total (ignoring deallocations): 4.14GB -> 4.04GB
calls to allocation functions: 21662554 -> 19545823
Test: m test-art-host-gtest
Test: Manually check that oat file for the big app remains identical.
Bug: 34053922
Change-Id: I00568422ba5510550986e29f30bace9ae6245269
diff --git a/compiler/utils/swap_space.h b/compiler/utils/swap_space.h
index 9600907..c286b82 100644
--- a/compiler/utils/swap_space.h
+++ b/compiler/utils/swap_space.h
@@ -45,8 +45,10 @@
private:
// Chunk of space.
struct SpaceChunk {
- uint8_t* ptr;
- size_t size;
+ // We need mutable members as we keep these objects in a std::set<> (providing only const
+ // access) but we modify these members while carefully preserving the std::set<> ordering.
+ mutable uint8_t* ptr;
+ mutable size_t size;
uintptr_t Start() const {
return reinterpret_cast<uintptr_t>(ptr);
@@ -66,13 +68,21 @@
typedef std::set<SpaceChunk, SortChunkByPtr> FreeByStartSet;
// Map size to an iterator to free_by_start_'s entry.
- typedef std::pair<size_t, FreeByStartSet::const_iterator> FreeBySizeEntry;
+ struct FreeBySizeEntry {
+ FreeBySizeEntry(size_t sz, FreeByStartSet::const_iterator entry)
+ : size(sz), free_by_start_entry(entry) { }
+
+ // We need mutable members as we keep these objects in a std::set<> (providing only const
+ // access) but we modify these members while carefully preserving the std::set<> ordering.
+ mutable size_t size;
+ mutable FreeByStartSet::const_iterator free_by_start_entry;
+ };
struct FreeBySizeComparator {
bool operator()(const FreeBySizeEntry& lhs, const FreeBySizeEntry& rhs) {
- if (lhs.first != rhs.first) {
- return lhs.first < rhs.first;
+ if (lhs.size != rhs.size) {
+ return lhs.size < rhs.size;
} else {
- return lhs.second->Start() < rhs.second->Start();
+ return lhs.free_by_start_entry->Start() < rhs.free_by_start_entry->Start();
}
}
};