Speed up ll_assert_aligned

Although only used in Debug mode, this showed up in a profiling
with a very significant amount of CPU cycles (in a state when
the viewer bogged down to 1 FPS because of drawing terrain,
but still - significant compared to the code that was being
executed (ie, 10%)).

This patch should bring that down from a few hunderd to one clock
cycle.
This commit is contained in:
Aleric Inglewood
2013-11-26 17:46:51 +01:00
parent f8d9bcf487
commit 8357a2b90b
2 changed files with 29 additions and 11 deletions

View File

@@ -61,17 +61,13 @@ BOOL LLMemory::sEnableMemoryFailurePrevention = FALSE;
LLPrivateMemoryPoolManager::mem_allocation_info_t LLPrivateMemoryPoolManager::sMemAllocationTracker;
#endif
void ll_assert_aligned_func(uintptr_t ptr,U32 alignment)
{
#ifdef SHOW_ASSERT
void singu_alignment_check_failed(void)
{
// Redundant, place to set breakpoints.
if (ptr%alignment!=0)
{
llwarns << "alignment check failed" << llendl;
}
llassert(ptr%alignment==0);
#endif
llassert(false);
}
#endif
//static
void LLMemory::initClass()

View File

@@ -42,10 +42,32 @@ class LLMutex ;
#define LL_CHECK_MEMORY
#endif
LL_COMMON_API void ll_assert_aligned_func(uintptr_t ptr,U32 alignment);
//<singu>
// ll_assert_aligned seems to only exist to set breakpoints in case an alignment check fails.
// However, the implementation was horrible: the test was done using a integer modulo after
// calling a function; which is like 500 times slower then the below. That turned out to be
// significant compared to CPU cycles used to do vector calculations in side of which this test
// is used.
//
// This implementation uses a faster, inlined test, and then still calls a function when
// that fails to set a break point there if needed.
//
// This uses the fact that 'alignment' is literal int (aka, '16' or '64') that is a power of two.
// As a result, the modulo is converted by the compiler to a logical AND with alignment-1, what
// it cannot do if you don't inline the test.
#ifdef SHOW_ASSERT
#define ll_assert_aligned(ptr,alignment) ll_assert_aligned_func(reinterpret_cast<uintptr_t>(ptr),((U32)alignment))
LL_COMMON_API void singu_alignment_check_failed(void);
#define ll_assert_aligned(ptr,alignment) \
do \
{ \
if (LL_UNLIKELY(reinterpret_cast<intptr_t>(ptr) % alignment)) \
{ \
singu_alignment_check_failed(); \
} \
} \
while(0)
//</singu>
#else
#define ll_assert_aligned(ptr,alignment)
#endif