Fixing predicate inlining
authorYuri Putivsky <putivsky@fb.com>
Thu, 26 Mar 2015 19:04:03 +0000 (12:04 -0700)
committerafrind <afrind@fb.com>
Thu, 2 Apr 2015 19:00:31 +0000 (12:00 -0700)
Summary:
as title.

Test Plan: run unit tests

Reviewed By: yfeldblum@fb.com

Subscribers: ming, fugalh, folly-diffs@, jsedgwick, yfeldblum, ridge@

FB internal diff: D1946589

folly/wangle/concurrent/ThreadPoolExecutor.h

index 32a0330da9f59d50afa59b8a90e1d6d2d4dc0721..c8ca7bb13ce1cf74305e8accdb1d185f0381827d 100644 (file)
@@ -174,12 +174,24 @@ class ThreadPoolExecutor : public virtual Executor {
   class ThreadList {
    public:
     void add(const ThreadPtr& state) {
-      auto it = std::lower_bound(vec_.begin(), vec_.end(), state, compare);
+      auto it = std::lower_bound(vec_.begin(), vec_.end(), state,
+          // compare method is a static method of class
+          // and therefore cannot be inlined by compiler
+          // as a template predicate of the STL algorithm
+          // but wrapped up with the lambda function (lambda will be inlined)
+          // compiler can inline compare method as well
+          [&](const ThreadPtr& ts1, const ThreadPtr& ts2) -> bool { // inline
+            return compare(ts1, ts2);
+          });
       vec_.insert(it, state);
     }
 
     void remove(const ThreadPtr& state) {
-      auto itPair = std::equal_range(vec_.begin(), vec_.end(), state, compare);
+      auto itPair = std::equal_range(vec_.begin(), vec_.end(), state,
+          // the same as above
+          [&](const ThreadPtr& ts1, const ThreadPtr& ts2) -> bool { // inline
+            return compare(ts1, ts2);
+          });
       CHECK(itPair.first != vec_.end());
       CHECK(std::next(itPair.first) == itPair.second);
       vec_.erase(itPair.first);