Speedup syntactic_implication() by using a cache.

* src/ltlvisit/syntimpl.hh (syntactic_implication,
syntactic_implication_neg): Move as member of ...
(syntactic_implication_cache): ... this new class, that holds
a cache of results to speedup these functions.
* src/ltlvisit/syntimpl.cc: Adjust to use (lookup, populate,
and cleanup) the cache.
* src/ltltest/syntimpl.cc: Likewise.
* src/ltlvisit/reduce.hh (reduce): Take an optional
syntactic_implication_cache parameter.
* src/ltlvisit/reduce.cc: Adjust to use a
syntactic_implication_cache.
* src/ltltest/equals.cc: Call dump_instances() to help debugging.
This commit is contained in:
Alexandre Duret-Lutz 2010-12-09 18:53:31 +01:00
parent 20c088a45a
commit 4ef7805e73
6 changed files with 211 additions and 99 deletions

View file

@ -40,8 +40,9 @@ namespace spot
{
public:
inf_right_recurse_visitor(const formula *f)
: result_(false), f(f)
inf_right_recurse_visitor(const formula *f,
syntactic_implication_cache* c)
: result_(false), f(f), c(c)
{
}
@ -100,16 +101,16 @@ namespace spot
return;
const unop* op = static_cast<const unop*>(f);
if (op->op() == unop::X)
result_ = syntactic_implication(op->child(), f1);
result_ = c->syntactic_implication(op->child(), f1);
}
return;
case unop::F:
/* F(a) = true U a */
result_ = syntactic_implication(f, f1);
result_ = c->syntactic_implication(f, f1);
return;
case unop::G:
/* G(a) = false R a */
if (syntactic_implication(f, constant::false_instance()))
if (c->syntactic_implication(f, constant::false_instance()))
result_ = true;
return;
case unop::Finish:
@ -137,7 +138,7 @@ namespace spot
return;
case binop::U:
case binop::W:
if (syntactic_implication(f, f2))
if (c->syntactic_implication(f, f2))
result_ = true;
return;
case binop::R:
@ -145,8 +146,8 @@ namespace spot
{
const binop* fb = static_cast<const binop*>(f);
if (fb->op() == binop::R
&& syntactic_implication(fb->first(), f1)
&& syntactic_implication(fb->second(), f2))
&& c->syntactic_implication(fb->first(), f1)
&& c->syntactic_implication(fb->second(), f2))
{
result_ = true;
return;
@ -157,14 +158,14 @@ namespace spot
const unop* fu = static_cast<const unop*>(f);
if (fu->op() == unop::G
&& f1 == constant::false_instance()
&& syntactic_implication(fu->child(), f2))
&& c->syntactic_implication(fu->child(), f2))
{
result_ = true;
return;
}
}
if (syntactic_implication(f, f1)
&& syntactic_implication(f, f2))
if (c->syntactic_implication(f, f1)
&& c->syntactic_implication(f, f2))
result_ = true;
return;
case binop::M:
@ -172,8 +173,8 @@ namespace spot
{
const binop* fb = static_cast<const binop*>(f);
if (fb->op() == binop::M
&& syntactic_implication(fb->first(), f1)
&& syntactic_implication(fb->second(), f2))
&& c->syntactic_implication(fb->first(), f1)
&& c->syntactic_implication(fb->second(), f2))
{
result_ = true;
return;
@ -184,14 +185,14 @@ namespace spot
const unop* fu = static_cast<const unop*>(f);
if (fu->op() == unop::F
&& f2 == constant::true_instance()
&& syntactic_implication(fu->child(), f1))
&& c->syntactic_implication(fu->child(), f1))
{
result_ = true;
return;
}
}
if (syntactic_implication(f, f1)
&& syntactic_implication(f, f2))
if (c->syntactic_implication(f, f1)
&& c->syntactic_implication(f, f2))
result_ = true;
return;
}
@ -214,13 +215,13 @@ namespace spot
{
case multop::And:
for (unsigned i = 0; i < mos; ++i)
if (!syntactic_implication(f, mo->nth(i)))
if (!c->syntactic_implication(f, mo->nth(i)))
return;
result_ = true;
break;
case multop::Or:
for (unsigned i = 0; i < mos && !result_; ++i)
if (syntactic_implication(f, mo->nth(i)))
if (c->syntactic_implication(f, mo->nth(i)))
result_ = true;
break;
case multop::Concat:
@ -233,6 +234,7 @@ namespace spot
protected:
bool result_; /* true if f < f1, false otherwise. */
const formula* f;
syntactic_implication_cache* c;
};
/////////////////////////////////////////////////////////////////////////
@ -241,8 +243,9 @@ namespace spot
{
public:
inf_left_recurse_visitor(const formula *f)
: result_(false), f(f)
inf_left_recurse_visitor(const formula *f,
syntactic_implication_cache* c)
: result_(false), f(f), c(c)
{
}
@ -258,8 +261,8 @@ namespace spot
return false;
const binop* fb = static_cast<const binop*>(f);
if (fb->op() == f2->op()
&& syntactic_implication(f2->first(), fb->first())
&& syntactic_implication(f2->second(), fb->second()))
&& c->syntactic_implication(f2->first(), fb->first())
&& c->syntactic_implication(f2->second(), fb->second()))
return true;
return false;
}
@ -281,7 +284,7 @@ namespace spot
void
visit(const atomic_prop* ap)
{
inf_right_recurse_visitor v(ap);
inf_right_recurse_visitor v(ap, c);
const_cast<formula*>(f)->accept(v);
result_ = v.result();
}
@ -292,10 +295,10 @@ namespace spot
}
void
visit(const constant* c)
visit(const constant* cst)
{
inf_right_recurse_visitor v(c);
switch (c->val())
inf_right_recurse_visitor v(cst, c);
switch (cst->val())
{
case constant::True:
const_cast<formula*>(f)->accept(v);
@ -316,7 +319,7 @@ namespace spot
visit(const unop* uo)
{
const formula* f1 = uo->child();
inf_right_recurse_visitor v(uo);
inf_right_recurse_visitor v(uo, c);
switch (uo->op())
{
case unop::Not:
@ -328,7 +331,7 @@ namespace spot
{
const unop* op = static_cast<const unop*>(f);
if (op->op() == unop::X)
result_ = syntactic_implication(f1, op->child());
result_ = c->syntactic_implication(f1, op->child());
}
return;
case unop::F:
@ -343,7 +346,7 @@ namespace spot
tmp->destroy();
return;
}
if (syntactic_implication(tmp, f))
if (c->syntactic_implication(tmp, f))
result_ = true;
tmp->destroy();
return;
@ -360,7 +363,7 @@ namespace spot
tmp->destroy();
return;
}
if (syntactic_implication(tmp, f))
if (c->syntactic_implication(tmp, f))
result_ = true;
tmp->destroy();
return;
@ -400,8 +403,8 @@ namespace spot
{
const binop* fb = static_cast<const binop*>(f);
if (fb->op() == binop::U
&& syntactic_implication(f1, fb->first())
&& syntactic_implication(f2, fb->second()))
&& c->syntactic_implication(f1, fb->first())
&& c->syntactic_implication(f2, fb->second()))
{
result_ = true;
return;
@ -412,14 +415,14 @@ namespace spot
const unop* fu = static_cast<const unop*>(f);
if (fu->op() == unop::F
&& f1 == constant::true_instance()
&& syntactic_implication(f2, fu->child()))
&& c->syntactic_implication(f2, fu->child()))
{
result_ = true;
return;
}
}
if (syntactic_implication(f1, f)
&& syntactic_implication(f2, f))
if (c->syntactic_implication(f1, f)
&& c->syntactic_implication(f2, f))
result_ = true;
return;
case binop::W:
@ -428,8 +431,8 @@ namespace spot
{
const binop* fb = static_cast<const binop*>(f);
if (fb->op() == binop::W
&& syntactic_implication(f1, fb->first())
&& syntactic_implication(f2, fb->second()))
&& c->syntactic_implication(f1, fb->first())
&& c->syntactic_implication(f2, fb->second()))
{
result_ = true;
return;
@ -440,14 +443,14 @@ namespace spot
const unop* fu = static_cast<const unop*>(f);
if (fu && fu->op() == unop::G
&& f2 == constant::false_instance()
&& syntactic_implication(f1, fu->child()))
&& c->syntactic_implication(f1, fu->child()))
{
result_ = true;
return;
}
}
if (syntactic_implication(f1, f)
&& syntactic_implication(f2, f))
if (c->syntactic_implication(f1, f)
&& c->syntactic_implication(f2, f))
result_ = true;
return;
case binop::R:
@ -456,13 +459,13 @@ namespace spot
const unop* fu = static_cast<const unop*>(f);
if (fu->op() == unop::G
&& f1 == constant::false_instance()
&& syntactic_implication(f2, fu->child()))
&& c->syntactic_implication(f2, fu->child()))
{
result_ = true;
return;
}
}
if (syntactic_implication(f2, f))
if (c->syntactic_implication(f2, f))
result_ = true;
return;
case binop::M:
@ -471,13 +474,13 @@ namespace spot
const unop* fu = static_cast<const unop*>(f);
if (fu->op() == unop::F
&& f2 == constant::true_instance()
&& syntactic_implication(f1, fu->child()))
&& c->syntactic_implication(f1, fu->child()))
{
result_ = true;
return;
}
}
if (syntactic_implication(f2, f))
if (c->syntactic_implication(f2, f))
result_ = true;
return;
}
@ -500,12 +503,12 @@ namespace spot
{
case multop::And:
for (unsigned i = 0; (i < mos) && !result_; ++i)
if (syntactic_implication(mo->nth(i), f))
if (c->syntactic_implication(mo->nth(i), f))
result_ = true;
break;
case multop::Or:
for (unsigned i = 0; i < mos; ++i)
if (!syntactic_implication(mo->nth(i), f))
if (!c->syntactic_implication(mo->nth(i), f))
return;
result_ = true;
break;
@ -519,6 +522,7 @@ namespace spot
protected:
bool result_; /* true if f1 < f, 1 otherwise. */
const formula* f;
syntactic_implication_cache* c;
};
} // anonymous
@ -526,7 +530,8 @@ namespace spot
// This is called by syntactic_implication() after the
// formulae have been normalized.
bool
syntactic_implication(const formula* f1, const formula* f2)
syntactic_implication_cache::syntactic_implication(const formula* f1,
const formula* f2)
{
if (f1 == f2)
return true;
@ -534,22 +539,43 @@ namespace spot
|| f1 == constant::false_instance())
return true;
inf_left_recurse_visitor v1(f2);
inf_right_recurse_visitor v2(f1);
// Cache lookup
{
pairf p(f1, f2);
cache_t::const_iterator i = cache_.find(p);
if (i != cache_.end())
return i->second;
}
bool result = false;
inf_left_recurse_visitor v1(f2, this);
const_cast<formula*>(f1)->accept(v1);
if (v1.result())
return true;
{
result = true;
}
else
{
inf_right_recurse_visitor v2(f1, this);
const_cast<formula*>(f2)->accept(v2);
if (v2.result())
result = true;
}
const_cast<formula*>(f2)->accept(v2);
if (v2.result())
return true;
// Cache result
{
pairf p(f1->clone(), f2->clone());
cache_[p] = result;
}
return false;
return result;
}
bool
syntactic_implication_neg(const formula* f1, const formula* f2, bool right)
syntactic_implication_cache::syntactic_implication_neg(const formula* f1,
const formula* f2,
bool right)
{
formula* l = f1->clone();
formula* r = f2->clone();
@ -558,6 +584,20 @@ namespace spot
else
l = unop::instance(unop::Not, l);
// Cache lookup
{
pairf p(l, r);
cache_t::const_iterator i = cache_.find(p);
if (i != cache_.end())
{
l->destroy();
r->destroy();
return i->second;
}
}
// Save the cache key for latter.
pairf p(l->clone(), r->clone());
formula* tmp = unabbreviate_logic(l);
l->destroy();
l = simplify_f_g(tmp);
@ -577,7 +617,33 @@ namespace spot
bool result = syntactic_implication(l, r);
l->destroy();
r->destroy();
// Cache result if is has not be done by syntactic_implication() already.
if (l != p.first || r != p.second)
{
cache_[p] = result;
}
else
{
p.first->destroy();
p.second->destroy();
}
return result;
}
syntactic_implication_cache::~syntactic_implication_cache()
{
cache_t::const_iterator i = cache_.begin();
while (i != cache_.end())
{
// Advance the iterator before deleting the key.
pairf p = i->first;
++i;
p.first->destroy();
p.second->destroy();
}
}
}
}