diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp index 9215867edd65..c71b0a17496a 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp @@ -125,7 +125,9 @@ bool StackDepotReverseMap::IdDescPair::IdComparator( return a.id < b.id; } -StackDepotReverseMap::StackDepotReverseMap() { +void StackDepotReverseMap::Init() const { + if (LIKELY(map_.capacity())) + return; map_.reserve(StackDepotGetStats().n_uniq_ids + 100); for (int idx = 0; idx < StackDepot::kTabSize; idx++) { atomic_uintptr_t *p = &theDepot.tab[idx]; @@ -140,6 +142,7 @@ StackDepotReverseMap::StackDepotReverseMap() { } StackTrace StackDepotReverseMap::Get(u32 id) const { + Init(); if (!map_.size()) return StackTrace(); IdDescPair pair = {id, nullptr}; diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.h b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.h index d4c2ee358351..6f79fffeea8a 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.h @@ -49,7 +49,7 @@ void StackDepotPrintAll(); // which were stored before it was instantiated. class StackDepotReverseMap { public: - StackDepotReverseMap(); + StackDepotReverseMap() = default; StackTrace Get(u32 id) const; private: @@ -60,7 +60,9 @@ class StackDepotReverseMap { static bool IdComparator(const IdDescPair &a, const IdDescPair &b); }; - InternalMmapVector map_; + void Init() const; + + mutable InternalMmapVector map_; // Disallow evil constructors. StackDepotReverseMap(const StackDepotReverseMap&);