<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Feature Request: Spot redundant lambda and std::function<> construction/destruction in loops"
   href="http://llvm.org/bugs/show_bug.cgi?id=20190">20190</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Feature Request: Spot redundant lambda and std::function<> construction/destruction in loops
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Static Analyzer
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>kremenek@apple.com
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>s_llvm@nedprod.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>An inefficient code pattern I keep seeing looks a bit like this:

std::vector<std::function<void(int)>> vs;
for(...)
{
  double &f;
  vs.push_back(std::bind([](int, double) {...}, _1, f));
}

This code needlessly constructs a lambda every loop iteration, plus a
std::function every loop iteration. A more efficient implementation might be
this:

std::vector<std::function<void(int)>> vs;
for(...)
{
  double &f;
  static auto c=[](int, double) {...};
  vs.push_back(std::bind(c, _1, f));
}

Which saves on a lambda construction. Or even better again:

std::vector<std::function<void(int)>> vs;
std::function<void(int, double)> c=[](int, double) {...};
for(...)
{
  double &f;
  vs.push_back(std::bind(c, _1, f));
}

Which also saves on a std::function construction.

This may seem asinine for the clang static analyser to warn about, but many
programmers don't seem to realise that declaring lambdas inside a loop equals a
construction/destruction round. I myself find myself doing it because it's easy
to forget you're in a loop, and then you kick yourself later when you see the
assembler copying memory around needlessly.

A clang static analyser warning here would be useful.

Niall</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>