Note that there are some explanatory texts on larger screens.

plurals
  1. POSpeed of lambda vs. inlining the function
    text
    copied!<p>I have speed issues with lambda functions. Here is the code:</p> <pre><code>Lit Simplifier::lit_diff_watches(const OccurClause&amp; a, const OccurClause&amp; b) { set_seen_for_lits(b, 1); size_t num = 0; Lit toret = lit_Undef; const auto check_seen = [&amp;] (const Lit lit) { if (seen[lit.toInt()] == 0) { toret = lit; num++; } }; for_each_lit(a, check_seen); /*switch(a.ws.getType()) { case CMSat::watch_binary_t: check_seen(a.lit); check_seen(a.ws.lit2()); break; case CMSat::watch_tertiary_t: check_seen(a.lit); check_seen(a.ws.lit2()); check_seen(a.ws.lit3()); break; case CMSat::watch_clause_t: { const Clause&amp; clause = *solver-&gt;clAllocator-&gt;getPointer(a.ws.getOffset()); for(const Lit lit: clause) { check_seen(lit); } break; } }*/ set_seen_for_lits(b, 0); if (num == 1) return toret; else return lit_Undef; } </code></pre> <p>The <code>for_each_lit</code> function's signature is:</p> <pre><code>void for_each_lit( const OccurClause&amp; cl , std::function&lt;void (const Lit lit)&gt; func ); </code></pre> <p>The function <code>lit_diff_watches</code> runs millions of times, and it takes 3.3s on an example. However, when I uncomment the switch, and comment out <code>for_each_line</code> (which is a copy-paste of the switch), I get 1.7s for the same exact run. Note that 99% of the time, <code>watch_binary_t</code> or <code>watch_tertiary_t</code> happens, i.e. only a very few instructions should be executed per <code>lit_diff_watches</code> function call.</p> <p>Can you please tell me what I'm doing wrong? The behavior is the same for both GCC 4.7 and current llvm-svn (25th of Nov 2013), the timing differences are minor. I'm guessing that the function call is not inlined but I'm not an expert. I would like to have this fixed, because this <code>switch(..){..}</code> is in many-many places in the code, and the use of lambdas &amp; <code>for_each_lit</code> would significantly clean up the code. However, I cannot loose so much speed over this. 10-20% would be fine, but a slowdown of almost 2x is too much.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload