spot/src/ltlast/automatop.cc
Alexandre Duret-Lutz 8e4e692e7f Change the way references are counted to speedup cloning.
Before this patch, every time you cloned a formula, the clone
visitor would recurse into the entire AST to increment the
reference count of all nodes.  When running ltl2tgba_fm on
the formula generated by "LTLcounterLinear.pl 8", approx 27% of
the time was spent in the clone visitor.

After this patch, cloning a formula is just an increment of the
reference count of the top node.  Children are decremented only
when the top node's ref count is decremented to zero.  With this
change, clone() and destroy() become constant time, the
ltl2tgba_fm spend only 0.01% of the time cloning formulae.

* src/ltlast/automatop.cc (~automatop): Decrement children.
(instance): Decrement children if the instance already exists.
* src/ltlast/binop.cc, src/ltlast/multop.cc, src/ltlast/unop.cc:
Likewise.
* src/ltlvisit/clone.cc (clone): Simplify, now we only need to
call ref().
* src/ltlvisit/destroy.cc (destroy): Simplify, now we only need
to call unref().
(destroy_visitor): Remove, no longer needed.
2009-11-09 07:13:31 +01:00

142 lines
3.3 KiB
C++

// Copyright (C) 2008, 2009 Laboratoire de Recherche et Developpement
// de l'Epita (LRDE)
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Spot; see the file COPYING. If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
#include <iostream>
#include "automatop.hh"
#include "nfa.hh"
#include "visitor.hh"
namespace spot
{
namespace ltl
{
automatop::automatop(const nfa::ptr nfa, vec* v, bool negated)
: nfa_(nfa), children_(v), negated_(negated)
{
dump_ = negated ? "!" : "";
dump_ += nfa->get_name();
dump_ += "(";
dump_ += (*v)[0]->dump();
for (unsigned n = 1; n < v->size(); ++n)
dump_ += ", " + (*v)[n]->dump();
dump_ += ")";
set_key_();
}
automatop::~automatop()
{
// Get this instance out of the instance map.
triplet p(std::make_pair(nfa(), negated_), children_);
map::iterator i = instances.find(p);
assert (i != instances.end());
instances.erase(i);
// Dereference children.
for (unsigned n = 0; n < size(); ++n)
formula::unref(nth(n));
delete children_;
}
void
automatop::accept(visitor& v)
{
v.visit(this);
}
void
automatop::accept(const_visitor& v) const
{
v.visit(this);
}
automatop::map automatop::instances;
automatop*
automatop::instance(const nfa::ptr nfa, vec* v, bool negated)
{
assert(nfa != 0);
triplet p(std::make_pair(nfa, negated), v);
map::iterator i = instances.find(p);
if (i != instances.end())
{
// The instance already exists.
for (vec::iterator vi = v->begin(); vi != v->end(); ++vi)
formula::unref(*vi);
delete v;
return static_cast<automatop*>(i->second->ref());
}
automatop* res = new automatop(nfa, v, negated);
instances[p] = res;
return static_cast<automatop*>(res->ref());
}
unsigned
automatop::size() const
{
return children_->size();
}
const formula*
automatop::nth(unsigned n) const
{
return (*children_)[n];
}
formula*
automatop::nth(unsigned n)
{
return (*children_)[n];
}
const spot::ltl::nfa::ptr
automatop::nfa() const
{
assert(nfa_ != 0);
return nfa_;
}
bool
automatop::is_negated() const
{
return negated_;
}
unsigned
automatop::instance_count()
{
return instances.size();
}
std::ostream&
automatop::dump_instances(std::ostream& os)
{
for (map::iterator i = instances.begin(); i != instances.end(); ++i)
{
os << i->second << " = "
<< i->second->ref_count_() << " * "
<< i->second->dump()
<< std::endl;
}
return os;
}
}
}