From 3e1070239a920cc94b020a621acf4daeccebb140 Mon Sep 17 00:00:00 2001 From: Vladimir Marko Date: Wed, 22 Feb 2017 10:57:03 +0000 Subject: 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 --- compiler/utils/swap_space.h | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'compiler/utils/swap_space.h') diff --git a/compiler/utils/swap_space.h b/compiler/utils/swap_space.h index 9600907278..c286b820fe 100644 --- a/compiler/utils/swap_space.h +++ b/compiler/utils/swap_space.h @@ -45,8 +45,10 @@ class SwapSpace { 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(ptr); @@ -66,13 +68,21 @@ class SwapSpace { typedef std::set FreeByStartSet; // Map size to an iterator to free_by_start_'s entry. - typedef std::pair 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(); } } }; -- cgit v1.2.3-59-g8ed1b