[libcxx-commits] [PATCH] D67273: [libc++] Remove unnecessary assignment in exclusive_scan

Billy Robert O'Neal III via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Sep 6 10:10:25 PDT 2019


BillyONeal added a comment.

I think combining reusing the value like your old implementation does with avoiding the extra assignment like my implementation does ends up being optimal, something like this (though I have not tested yet and just woke up)

  if (_UFirst != _ULast) {
      _Ty _Tmp(_Reduce_op(_Val, *_UFirst)); // temp to enable _First == _Dest, also requirement missing
      *_UDest = _Val;
      ++_UDest;
      while (++_UFirst != _ULast) {
          _Val = _STD move(_Tmp); // Requirement missing from N4713
          _Tmp = _Reduce_op(_Val, *_UFirst);
          *_UDest = _Val;
          ++_UDest;
      }
  }

Or this version which makes a temporary iterator instead of a temporary _Ty, which is usually cheaper:

  if (_UFirst != _ULast) {
      auto _UNext = _UFirst;
      for (;;) {
          *_UDest = _Val;
          ++_UDest;
          if (++_UNext == _ULast) {
              break;
          }
  
          _Val    = _Reduce_op(_STD move(_Val), *_UFirst); // temp to enable _First == _Dest, also requirement missing
          _UFirst = _UNext;
      }
  }

I have not run either through tests yet...


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D67273/new/

https://reviews.llvm.org/D67273





More information about the libcxx-commits mailing list