mcclellancompile_util: reduce malloc traffic, tidy

This commit is contained in:
Justin Viiret 2017-04-10 10:32:37 +10:00 committed by Matthew Barr
parent 4cc998e4ab
commit 304bac3286

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015-2016, Intel Corporation * Copyright (c) 2015-2017, Intel Corporation
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -43,6 +43,12 @@ namespace ue2 {
#define INIT_STATE 1 #define INIT_STATE 1
static
bool state_has_reports(const raw_dfa &raw, dstate_id_t s) {
const auto &ds = raw.states[s];
return !ds.reports.empty() || !ds.reports_eod.empty();
}
static static
u32 count_dots(const raw_dfa &raw) { u32 count_dots(const raw_dfa &raw) {
assert(raw.start_anchored == INIT_STATE); assert(raw.start_anchored == INIT_STATE);
@ -60,8 +66,7 @@ u32 count_dots(const raw_dfa &raw) {
} }
} }
if (!raw.states[raw.states[i].next[0]].reports.empty() if (state_has_reports(raw, raw.states[i].next[0])) {
|| !raw.states[raw.states[i].next[0]].reports_eod.empty()) {
goto validate; goto validate;
} }
@ -163,37 +168,33 @@ u32 calc_min_dist_from_bob(raw_dfa &raw, vector<u32> *dist_in) {
} }
static static
void find_in_edges(const raw_dfa &raw, vector<vector<dstate_id_t> > *in_edges) { vector<vector<dstate_id_t>> find_in_edges(const raw_dfa &raw) {
in_edges->clear(); vector<vector<dstate_id_t>> in_edges(raw.states.size());
in_edges->resize(raw.states.size()); flat_set<dstate_id_t> seen;
ue2::unordered_set<dstate_id_t> seen;
for (u32 s = 1; s < raw.states.size(); s++) { for (u32 s = 1; s < raw.states.size(); s++) {
seen.clear(); seen.clear();
for (u32 j = 0; j < raw.alpha_size; j++) { for (u32 j = 0; j < raw.alpha_size; j++) {
dstate_id_t t = raw.states[s].next[j]; dstate_id_t t = raw.states[s].next[j];
if (contains(seen, t)) { if (!seen.insert(t).second) {
continue; continue;
} }
seen.insert(t); in_edges[t].push_back(s);
(*in_edges)[t].push_back(s);
}
} }
} }
return in_edges;
}
static static
void calc_min_dist_to_accept(const raw_dfa &raw, vector<u32> calc_min_dist_to_accept(const raw_dfa &raw,
const vector<vector<dstate_id_t> > &in_edges, const vector<vector<dstate_id_t>> &in_edges) {
vector<u32> *accept_dist) { vector<u32> dist(raw.states.size(), ~0U);
vector<u32> &dist = *accept_dist;
dist.clear();
dist.resize(raw.states.size(), ~0U);
/* for reporting states to start from */ /* for reporting states to start from */
deque<dstate_id_t> to_visit; deque<dstate_id_t> to_visit;
for (u32 s = 0; s < raw.states.size(); s++) { for (u32 s = 0; s < raw.states.size(); s++) {
if (!raw.states[s].reports.empty() if (state_has_reports(raw, s)) {
|| !raw.states[s].reports_eod.empty()) {
to_visit.push_back(s); to_visit.push_back(s);
dist[s] = 0; dist[s] = 0;
} }
@ -210,9 +211,7 @@ void calc_min_dist_to_accept(const raw_dfa &raw,
assert(d >= last_d); assert(d >= last_d);
assert(d != ~0U); assert(d != ~0U);
for (vector<dstate_id_t>::const_iterator it = in_edges[s].begin(); for (auto t : in_edges[s]) {
it != in_edges[s].end(); ++it) {
dstate_id_t t = *it;
if (t == DEAD_STATE) { if (t == DEAD_STATE) {
continue; continue;
} }
@ -226,6 +225,8 @@ void calc_min_dist_to_accept(const raw_dfa &raw,
last_d = d; last_d = d;
} }
return dist;
} }
bool prune_overlong(raw_dfa &raw, u32 max_offset) { bool prune_overlong(raw_dfa &raw, u32 max_offset) {
@ -237,13 +238,7 @@ bool prune_overlong(raw_dfa &raw, u32 max_offset) {
return false; return false;
} }
vector<vector<dstate_id_t> > in_edges; vector<u32> accept_dist = calc_min_dist_to_accept(raw, find_in_edges(raw));
find_in_edges(raw, &in_edges);
vector<u32> accept_dist;
calc_min_dist_to_accept(raw, in_edges, &accept_dist);
in_edges.clear();
/* look over the states and filter out any which cannot reach a report /* look over the states and filter out any which cannot reach a report
* states before max_offset */ * states before max_offset */
@ -267,7 +262,7 @@ bool prune_overlong(raw_dfa &raw, u32 max_offset) {
/* swap states */ /* swap states */
DEBUG_PRINTF("pruned %zu -> %u\n", raw.states.size(), count); DEBUG_PRINTF("pruned %zu -> %u\n", raw.states.size(), count);
raw.states.swap(new_states); raw.states = std::move(new_states);
new_states.clear(); new_states.clear();
/* update edges and daddys to refer to the new ids */ /* update edges and daddys to refer to the new ids */