Skip to content

Commit

Permalink
Guiding the compiler: when to inline inside append, emplace_back
Browse files Browse the repository at this point in the history
  • Loading branch information
OleErikPeistorpet committed Jun 5, 2024
1 parent 9683d09 commit a2c6059
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
15 changes: 15 additions & 0 deletions auxi/core_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
#endif


#ifndef OEL_HAS_LIKELY
#if (__cplusplus >= 202001 and (!defined __clang__ or __clang_major__ >= 12)) or (defined _MSC_VER and _MSC_VER >= 1929)
#define OEL_HAS_LIKELY 1
#else
#define OEL_HAS_LIKELY 0
#endif
#endif

#if OEL_HAS_LIKELY
#define OEL_UNLIKELY [[unlikely]]
#else
#define OEL_UNLIKELY
#endif


#ifdef __GNUC__
#define OEL_ALWAYS_INLINE __attribute__((always_inline))
#else
Expand Down
62 changes: 34 additions & 28 deletions dynarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,19 +401,9 @@ class dynarray
(void) _debugSizeUpdater{_m};
}

#ifdef _MSC_VER
__declspec(noinline) // to get the compiler to inline calling function
#endif
void _growBy(size_type const count)
{
auto const s = size();
_realloc(_calcCapAdd(count, s), s);
}

void _growByOne()
{
_realloc(_calcCapAddOne(), size());
}
// These are not defined inline as a compiler hint
void _growBy(size_type const);
void _growByOne();


template< typename UninitFiller >
Expand Down Expand Up @@ -522,7 +512,7 @@ class dynarray
template< typename InputIter >
InputIter _doAppend(InputIter src, size_type const count)
{
if (_spareCapacity() < count)
if (_spareCapacity() < count) OEL_UNLIKELY
_growBy(count);

if constexpr (can_memmove_with<T *, InputIter>)
Expand Down Expand Up @@ -681,6 +671,23 @@ typename dynarray<T, Alloc>::iterator
return _detail::MakeDynarrIter(_m, pPos);
}


template< typename T, typename Alloc >
#if defined _MSC_VER and !OEL_HAS_LIKELY
__declspec(noinline) // to get the compiler to inline calling function
#endif
void dynarray<T, Alloc>::_growBy(size_type const count)
{
auto const s = size();
_realloc(_calcCapAdd(count, s), s);
}

template< typename T, typename Alloc >
void dynarray<T, Alloc>::_growByOne()
{
_realloc(_calcCapAddOne(), size());
}

template< typename T, typename Alloc >
template< typename... Args >
inline T & dynarray<T, Alloc>::emplace_back(Args &&... args) &
Expand All @@ -695,6 +702,19 @@ inline T & dynarray<T, Alloc>::emplace_back(Args &&... args) &
return *(_m.end++);
}

template< typename T, typename Alloc >
inline void dynarray<T, Alloc>::append(size_type count, const T & val)
{
if (_spareCapacity() < count) OEL_UNLIKELY
_growBy(count);

auto const pos = _m.end;
_uninitFill::template call< forward_t<const T &> >(pos, pos + count, _m, val);

_debugSizeUpdater guard{_m};
_m.end += count;
}


template< typename T, typename Alloc >
dynarray<T, Alloc>::dynarray(dynarray && other, Alloc a)
Expand Down Expand Up @@ -805,20 +825,6 @@ void dynarray<T, Alloc>::shrink_to_fit()
}


template< typename T, typename Alloc >
inline void dynarray<T, Alloc>::append(size_type count, const T & val)
{
if (_spareCapacity() < count)
_growBy(count);

auto const pos = _m.end;
_uninitFill::template call< forward_t<const T &> >(pos, pos + count, _m, val);

_debugSizeUpdater guard{_m};
_m.end += count;
}


template< typename T, typename Alloc >
inline void dynarray<T, Alloc>::pop_back() noexcept
{
Expand Down

0 comments on commit a2c6059

Please sign in to comment.