[LLVMdev] Question about SmallVector implementation detail

Chris Lattner clattner at apple.com
Sun Jul 4 10:34:11 PDT 2010


On Jul 4, 2010, at 12:04 AM, bombela wrote:

> Hello,
> 
> I have just a little question about the SmallVector implemention.
> 
> In SmallVectorImpl, the following method is currently implemented as:
> 
>   void push_back(const T &Elt) {
>     if (this->EndX < this->CapacityX) {
>     Retry:
>       new (this->end()) T(Elt);
>       this->setEnd(this->end()+1);
>       return;
>     }
>     this->grow();
>     goto Retry;
>   }
> 
> ~/llvm-project/llvm/include/llvm/ADT/SmallVector.h:327 (svn rev: 107560)
> This function was wrote/last modified by lattner.
> 
> Why a goto?
> For CPU branch prediction in favor of the positive condition result?
> I'am interested by the reason of that, that's look curious for me.

It's a micro-optimization for code layout and code size.  The idea is that we want this to get inlined, so tail duplicating the if condition would be bad.  This would be a good candidate for __builtin_expect as well.

In the big picture, it probably doesn't matter much either way :)

-Chris





More information about the llvm-dev mailing list