Skip to content

Commit

Permalink
perf and memory smash fixes to internal node count routine
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolajBjorner committed Apr 13, 2023
1 parent f0afbcb commit 7cd8edc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
27 changes: 8 additions & 19 deletions src/ast/for_each_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ unsigned get_num_exprs(expr * n) {
}


static void get_num_internal_exprs(unsigned_vector& counts, sbuffer<expr*>& todo, expr * n) {
void get_num_internal_exprs(unsigned_vector& counts, ptr_vector<expr>& todo, expr * n) {
counts.reserve(n->get_id() + 1);
unsigned& rc = counts[n->get_id()];
if (rc > 0) {
Expand All @@ -55,7 +55,6 @@ static void get_num_internal_exprs(unsigned_vector& counts, sbuffer<expr*>& todo
rc = n->get_ref_count() - 1;
unsigned i = todo.size();
todo.push_back(n);
unsigned count = 0;
for (; i < todo.size(); ++i) {
n = todo[i];
if (!is_app(n))
Expand All @@ -74,27 +73,17 @@ static void get_num_internal_exprs(unsigned_vector& counts, sbuffer<expr*>& todo
}
}

unsigned get_num_internal_exprs(expr * n) {
unsigned_vector counts;
sbuffer<expr*> todo;
unsigned count_internal_nodes(unsigned_vector& counts, ptr_vector<expr>& todo) {
unsigned internal_nodes = 0;
get_num_internal_exprs(counts, todo, n);
for (expr* t : todo)
if (counts[t->get_id()] == 0)
++internal_nodes;
return internal_nodes;
}

unsigned get_num_internal_exprs(unsigned sz, expr * const * args) {
unsigned_vector counts;
sbuffer<expr*> todo;
unsigned internal_nodes = 0;
for (unsigned i = 0; i < sz; ++i)
get_num_internal_exprs(counts, todo, args[i]);
for (expr* t : todo)
for (expr* t : todo) {
if (counts[t->get_id()] == 0)
++internal_nodes;
else
counts[t->get_id()] = 0;
}
todo.reset();
return internal_nodes;

}


Expand Down
5 changes: 3 additions & 2 deletions src/ast/for_each_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,13 @@ struct for_each_expr_proc : public EscapeProc {
unsigned get_num_exprs(expr * n);
unsigned get_num_exprs(expr * n, expr_mark & visited);
unsigned get_num_exprs(expr * n, expr_fast_mark1 & visited);
unsigned get_num_internal_exprs(expr * n);
unsigned get_num_internal_exprs(unsigned sz, expr * const * args);
void get_num_internal_exprs(unsigned_vector& counts, ptr_vector<expr>& todo, expr * n);
unsigned count_internal_nodes(unsigned_vector& counts, ptr_vector<expr>& todo);

bool has_skolem_functions(expr * n);

// pre-order traversal of subterms

class subterms {
bool m_include_bound = false;
expr_ref_vector m_es;
Expand Down
17 changes: 13 additions & 4 deletions src/ast/rewriter/bool_rewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,19 +269,28 @@ br_status bool_rewriter::mk_nflat_or_core(unsigned num_args, expr * const * args
return BR_DONE;
}

#if 1
br_status st;
st = m_hoist.mk_or(buffer.size(), buffer.data(), result);
expr_ref r(m());
st = m_hoist.mk_or(buffer.size(), buffer.data(), r);
if (st != BR_FAILED) {
unsigned count1 = get_num_internal_exprs(result);
unsigned count2 = get_num_internal_exprs(buffer.size(), buffer.data());
m_counts1.reserve(m().get_num_asts() + 1);
m_counts2.reserve(m().get_num_asts() + 1);
get_num_internal_exprs(m_counts1, m_todo1, r);
for (unsigned i = 0; i < num_args; ++i)
get_num_internal_exprs(m_counts2, m_todo2, args[i]);
unsigned count1 = count_internal_nodes(m_counts1, m_todo1);
unsigned count2 = count_internal_nodes(m_counts2, m_todo2);
if (count1 > count2)
st = BR_FAILED;
}
if (st != BR_FAILED)
result = r;
if (st == BR_DONE)
return BR_REWRITE1;
if (st != BR_FAILED)
return st;

#endif
if (s) {
ast_lt lt;
std::sort(buffer.begin(), buffer.end(), lt);
Expand Down
2 changes: 2 additions & 0 deletions src/ast/rewriter/bool_rewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class bool_rewriter {
unsigned m_local_ctx_limit;
unsigned m_local_ctx_cost;
bool m_elim_ite;
ptr_vector<expr> m_todo1, m_todo2;
unsigned_vector m_counts1, m_counts2;

br_status mk_flat_and_core(unsigned num_args, expr * const * args, expr_ref & result);
br_status mk_flat_or_core(unsigned num_args, expr * const * args, expr_ref & result);
Expand Down

0 comments on commit 7cd8edc

Please sign in to comment.