Skip to content

Commit

Permalink
Change how Redex reads class freqs
Browse files Browse the repository at this point in the history
Summary:
Previously, the mappings from QPL id to QPL name were needed to relate the class freqs QPL-id-based interactions to the betamap QPL-name-based interactions

Since the betamap now has interaction id markers, this is not necessary

Reviewed By: jimmycFB

Differential Revision: D68562115

fbshipit-source-id: 76ac435bf268507ed6d66e5d61bc989f26211e3f
  • Loading branch information
itang00 authored and facebook-github-bot committed Jan 30, 2025
1 parent 25e5685 commit 7aae65a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 51 deletions.
31 changes: 1 addition & 30 deletions libredex/ConfigFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,6 @@ const std::unordered_set<DexType*>& ConfigFiles::get_do_not_devirt_anon() {
return m_no_devirtualize_annos;
}

std::unordered_map<std::string, std::string>
ConfigFiles::load_qpl_interactions_map() {
std::unordered_map<std::string, std::string> interaction_starts;
Json::Value interactions_json;
get_json_config().get("betamap_interactions", {}, interactions_json);

if (interactions_json.empty()) {
return interaction_starts;
}

for (auto& interaction_pair_json : interactions_json) {
auto id = interaction_pair_json[0].asString();
auto name = interaction_pair_json[1].asString();
interaction_starts[id] = name;
}
return interaction_starts;
}

std::unordered_map<const DexString*, std::vector<uint8_t>>
ConfigFiles::load_class_frequencies() {
if (m_class_frequency_filename.empty()) {
Expand All @@ -237,23 +219,12 @@ ConfigFiles::load_class_frequencies() {
std::ifstream input(m_class_frequency_filename, std::ios_base::in);

std::unordered_map<const DexString*, std::vector<uint8_t>> class_freq_map;
std::vector<std::string> qpl_ids;

std::string line;
std::getline(input, line);
// line containing all interactions
boost::trim(line);
boost::split(qpl_ids, line, boost::is_any_of(" "));

std::unordered_map<std::string, std::string> qpl_interactions_map =
load_qpl_interactions_map();
if (qpl_interactions_map.empty()) {
boost::split(m_interactions, line, boost::is_any_of(" "));
} else {
for (const std::string& qpl_id : qpl_ids) {
m_interactions.push_back(qpl_interactions_map.at(qpl_id));
}
}
boost::split(m_interactions, line, boost::is_any_of(" "));

while (std::getline(input, line)) {
// each line follows the format
Expand Down
1 change: 0 additions & 1 deletion libredex/ConfigFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ struct ConfigFiles {
std::vector<std::string> load_coldstart_classes();
std::unordered_map<const DexString*, std::vector<uint8_t>>
load_class_frequencies();
std::unordered_map<std::string, std::string> load_qpl_interactions_map();
std::unordered_map<std::string, std::vector<std::string>> load_class_lists();
void ensure_agg_method_stats_loaded();
void ensure_secondary_method_stats_loaded() const;
Expand Down
1 change: 0 additions & 1 deletion libredex/GlobalConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ void GlobalConfig::bind_config() {
bind("ingest_baseline_profile_data", false, bool_param);
bind("baseline_profile_config", "", string_param);
bind("preprocessed_baseline_profile_directory", "", string_param);
bind("betamap_interactions", {}, json_param);

for (const auto& entry : m_registry) {
m_global_configs.emplace(entry.name,
Expand Down
39 changes: 20 additions & 19 deletions opt/interdex/InterDex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ constexpr const char* BG_SET_END_FORMAT = "LBackgroundSetEnd";
constexpr const char* COLD_START_20PCT_END_FORMAT = "LColdStart20PctEnd";
constexpr const char* COLD_START_1PCT_END_FORMAT = "LColdStart1PctEnd";

constexpr const char* INTERACTION_ID_FORMAT = "LINTERACTION_ID_";
constexpr const char* START_FORMAT = "_Start;";

static DexInfo EMPTY_DEX_INFO;

std::unordered_set<DexClass*> find_unrefenced_coldstart_classes(
Expand Down Expand Up @@ -234,6 +237,11 @@ void exclude_extra_dynamically_dead_class(
}
}

bool is_interaction_id_start_marker(std::string_view betamap_entry) {
return boost::algorithm::starts_with(betamap_entry, INTERACTION_ID_FORMAT) &&
boost::algorithm::ends_with(betamap_entry, START_FORMAT);
}

} // namespace

namespace interdex {
Expand Down Expand Up @@ -261,13 +269,15 @@ void InterDex::get_movable_coldstart_classes(
for (auto* type : interdex_types) {
DexClass* cls = type_class(type);
if (!cls) {
if (boost::algorithm::starts_with(type->get_name()->str(),
BG_SET_START_FORMAT)) {
curr_idx = backgroundset_idx;
continue;
auto index_it = interactions.end();
auto cls_name = type->get_name()->str();
if (is_interaction_id_start_marker(cls_name)) {
index_it = std::find(
interactions.begin(), interactions.end(),
cls_name.substr(strlen(INTERACTION_ID_FORMAT),
cls_name.size() - strlen(INTERACTION_ID_FORMAT) -
strlen(START_FORMAT)));
}
auto index_it = std::find(interactions.begin(), interactions.end(),
type->str().substr(1, type->str().size() - 8));
if (index_it == interactions.end()) {
continue;
}
Expand Down Expand Up @@ -525,7 +535,6 @@ void InterDex::emit_interdex_classes(
}

auto class_freqs = m_conf.get_class_frequencies();
const std::vector<std::string>& interactions = m_conf.get_interactions();
std::unordered_map<const DexClass*, std::string> move_coldstart_classes;
if (m_move_coldstart_classes) {
get_movable_coldstart_classes(interdex_types, move_coldstart_classes);
Expand Down Expand Up @@ -611,9 +620,7 @@ void InterDex::emit_interdex_classes(
}
}
if (m_move_coldstart_classes &&
std::find(interactions.begin(), interactions.end(),
type->str().substr(1, type->str().size() - 8)) !=
interactions.end()) {
is_interaction_id_start_marker(type->str())) {
curr_interaction = type->str();
}
}
Expand All @@ -632,7 +639,8 @@ void InterDex::emit_interdex_classes(

if (m_move_coldstart_classes && move_coldstart_classes.count(cls)) {
auto interaction = move_coldstart_classes.at(cls);
if (!boost::starts_with(curr_interaction, "L" + interaction)) {
if (!boost::starts_with(curr_interaction,
INTERACTION_ID_FORMAT + interaction)) {
continue;
} else {
TRACE(IDEX, 3, "moving %s into %s", SHOW(cls),
Expand Down Expand Up @@ -746,10 +754,6 @@ void InterDex::load_interdex_types() {
std::unordered_set<DexType*> moved_or_double{};
std::unordered_set<DexType*> transitive_added{};

const std::vector<std::string>* interactions;
if (m_move_coldstart_classes) {
interactions = &m_conf.get_interactions();
}
for (const auto& entry : interdexorder) {
DexType* type = DexType::get_type(entry);
if (!type) {
Expand Down Expand Up @@ -799,10 +803,7 @@ void InterDex::load_interdex_types() {
"[interdex order]: Found 1pct cold start end class marker %s.",
entry.c_str());
// trim off the _Start suffix to get the interaction name
} else if (m_move_coldstart_classes &&
std::find(interactions->begin(), interactions->end(),
entry.substr(1, entry.size() - 8)) !=
interactions->end()) {
} else if (is_interaction_id_start_marker(entry)) {
type = DexType::make_type(entry);
} else {
continue;
Expand Down

0 comments on commit 7aae65a

Please sign in to comment.