Skip to content

Commit

Permalink
use InsertOnlyConcurrentMap
Browse files Browse the repository at this point in the history
Summary: This is a behavior-preserving change.

Reviewed By: agampe

Differential Revision: D49352093

fbshipit-source-id: d23b56b7885f3a8b508f7796322628c51d641c64
  • Loading branch information
Nikolai Tillmann authored and facebook-github-bot committed Sep 19, 2023
1 parent ef04756 commit cb73202
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
33 changes: 12 additions & 21 deletions libredex/InitClassesWithSideEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const InitClasses* InitClassesWithSideEffects::compute(
const DexClass* cls,
const method::ClInitHasNoSideEffectsPredicate& clinit_has_no_side_effects,
const std::unordered_set<DexMethod*>* non_true_virtuals) {
auto res =
m_init_classes.get(cls->get_type(), std::shared_ptr<InitClasses>());
if (res) {
return res.get();
const DexType* key = cls->get_type();
const auto* cached = m_init_classes.get(key);
if (cached) {
return cached;
}

InitClasses classes;
Expand All @@ -42,21 +42,12 @@ const InitClasses* InitClassesWithSideEffects::compute(
super_classes->end());
}
}
m_init_classes.update(
cls->get_type(),
[&res, &classes, this](const DexType*,
std::shared_ptr<InitClasses>& value, bool exist) {
if (exist) {
always_assert(classes == *value);
} else {
if (classes.empty()) {
m_trivial_init_classes++;
}
value = std::make_shared<InitClasses>(std::move(classes));
}
res = value;
});
return res.get();
auto [ptr, emplaced] =
m_init_classes.get_or_emplace_and_assert_equal(key, std::move(classes));
if (emplaced && ptr->empty()) {
m_trivial_init_classes++;
}
return ptr;
}

InitClassesWithSideEffects::InitClassesWithSideEffects(
Expand All @@ -79,7 +70,7 @@ InitClassesWithSideEffects::InitClassesWithSideEffects(
[&](const DexType* type) {
auto it = prev_init_classes.find(type);
if (it != prev_init_classes.end()) {
return it->second->empty();
return it->second.empty();
}
auto cls = type_class(type);
return cls && (cls->is_external() ||
Expand All @@ -106,7 +97,7 @@ InitClassesWithSideEffects::InitClassesWithSideEffects(

const InitClasses* InitClassesWithSideEffects::get(const DexType* type) const {
auto it = m_init_classes.find(type);
return it == m_init_classes.end() ? &m_empty_init_classes : it->second.get();
return it == m_init_classes.end() ? &m_empty_init_classes : &it->second;
}

const DexType* InitClassesWithSideEffects::refine(const DexType* type) const {
Expand Down
2 changes: 1 addition & 1 deletion libredex/InitClassesWithSideEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using InitClasses = std::vector<const DexClass*>;
*/
class InitClassesWithSideEffects {
private:
ConcurrentMap<const DexType*, std::shared_ptr<InitClasses>> m_init_classes;
InsertOnlyConcurrentMap<const DexType*, InitClasses> m_init_classes;
std::atomic<size_t> m_trivial_init_classes{0};
InitClasses m_empty_init_classes;
bool m_create_init_class_insns;
Expand Down

0 comments on commit cb73202

Please sign in to comment.