From: Rupert Swarbrick Date: Tue, 26 May 2020 14:19:39 +0000 (+0100) Subject: Minor optimisation in Module::wire() and Module::cell() X-Git-Tag: working-ls180~532^2 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=7ff306ccdb47415e3a483adad87076327399434f;p=yosys.git Minor optimisation in Module::wire() and Module::cell() The existing code does a search to figure out whether id is in the dict (with the call to count()), and then looks it up again to get the result (with the call to at()). This version calls find() instead, avoiding the double lookup. Code size increases slightly (6kb). I think this is because the contents of find() are getting inlined, and then inlined into lots of the callsites for cell() and wire(). Looking at the compiled code before this patch, you just get a (non-inlined) call to count() followed by a call to at(). After the patch, the contents of find() have been inlined (so you see do_hash, then do_lookup). The result for each function is about 30 bytes / 40% bigger, which presumably also enlarges call-sites that inline it. --- diff --git a/kernel/rtlil.h b/kernel/rtlil.h index 11c45bbec..76c5b5304 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -1134,8 +1134,14 @@ public: return design->selected_member(name, member->name); } - RTLIL::Wire* wire(RTLIL::IdString id) { return wires_.count(id) ? wires_.at(id) : nullptr; } - RTLIL::Cell* cell(RTLIL::IdString id) { return cells_.count(id) ? cells_.at(id) : nullptr; } + RTLIL::Wire* wire(RTLIL::IdString id) { + auto it = wires_.find(id); + return it == wires_.end() ? nullptr : it->second; + } + RTLIL::Cell* cell(RTLIL::IdString id) { + auto it = cells_.find(id); + return it == cells_.end() ? nullptr : it->second; + } RTLIL::ObjRange wires() { return RTLIL::ObjRange(&wires_, &refcount_wires_); } RTLIL::ObjRange cells() { return RTLIL::ObjRange(&cells_, &refcount_cells_); }