From: Rick Altherr Date: Sun, 31 Jan 2016 16:55:49 +0000 (-0800) Subject: rtlil: speed up SigSpec::sort_and_unify() X-Git-Tag: yosys-0.6~30^2~1 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=0265d7b1006e0946eb4635d73a6d49d31b3e4235;p=yosys.git rtlil: speed up SigSpec::sort_and_unify() std::set<> internally is often a red-black tree which is fairly expensive to create but fast to lookup. In the case of sort_and_unify(), a set<> is constructed as a temporary object to attempt to speed up lookups. Being a temporarily, however, the cost of creation far outweights the lookup improvement and is a net performance loss. Instead, sort the vector<> that already exists and then apply std::unique(). --- diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index ca4480576..ee0f44a38 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -2573,8 +2573,18 @@ void RTLIL::SigSpec::sort() void RTLIL::SigSpec::sort_and_unify() { + unpack(); cover("kernel.rtlil.sigspec.sort_and_unify"); - *this = this->to_sigbit_set(); + + // A copy of the bits vector is used to prevent duplicating the logic from + // SigSpec::SigSpec(std::vector). This incurrs an extra copy but + // that isn't showing up as significant in profiles. + std::vector unique_bits = bits_; + std::sort(unique_bits.begin(), unique_bits.end()); + auto last = std::unique(unique_bits.begin(), unique_bits.end()); + unique_bits.erase(last, unique_bits.end()); + + *this = unique_bits; } void RTLIL::SigSpec::replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with)