Improve performance for locks and value conversions

refs #11612
This commit is contained in:
Gunnar Beutner 2016-04-19 09:37:41 +02:00
parent c6a015e317
commit d2cd4b6667
5 changed files with 17 additions and 8 deletions

View File

@ -91,4 +91,13 @@
# define I2_BASE_API I2_IMPORT
#endif /* I2_BASE_BUILD */
#if defined(__GNUC__)
# define likely(x) __builtin_expect(!!(x), 1)
# define unlikely(x) __builtin_expect(!!(x), 0)
#else
# define likely(x) (x)
# define unlikely(x) (x)
#endif
#endif /* I2BASE_H */

View File

@ -191,7 +191,7 @@ inline void intrusive_ptr_release(Object *object)
refs = __sync_sub_and_fetch(&object->m_References, 1);
#endif /* _WIN32 */
if (refs == 0) {
if (unlikely(refs == 0)) {
#ifdef I2_LEAK_DEBUG
TypeRemoveObject(object);
#endif /* I2_LEAK_DEBUG */

View File

@ -62,14 +62,14 @@ public:
#ifdef _WIN32
# ifdef _WIN64
while (InterlockedCompareExchange64((LONGLONG *)&object->m_Mutex, I2MUTEX_LOCKED, I2MUTEX_UNLOCKED) != I2MUTEX_UNLOCKED) {
while (likely(InterlockedCompareExchange64((LONGLONG *)&object->m_Mutex, I2MUTEX_LOCKED, I2MUTEX_UNLOCKED) != I2MUTEX_UNLOCKED)) {
# else /* _WIN64 */
while (InterlockedCompareExchange(&object->m_Mutex, I2MUTEX_LOCKED, I2MUTEX_UNLOCKED) != I2MUTEX_UNLOCKED) {
while (likely(InterlockedCompareExchange(&object->m_Mutex, I2MUTEX_LOCKED, I2MUTEX_UNLOCKED) != I2MUTEX_UNLOCKED)) {
# endif /* _WIN64 */
#else /* _WIN32 */
while (!__sync_bool_compare_and_swap(&object->m_Mutex, I2MUTEX_UNLOCKED, I2MUTEX_LOCKED)) {
while (likely(!__sync_bool_compare_and_swap(&object->m_Mutex, I2MUTEX_UNLOCKED, I2MUTEX_LOCKED))) {
#endif /* _WIN32 */
if (object->m_Mutex > I2MUTEX_LOCKED) {
if (likely(object->m_Mutex > I2MUTEX_LOCKED)) {
boost::recursive_mutex *mtx = reinterpret_cast<boost::recursive_mutex *>(object->m_Mutex);
mtx->lock();

View File

@ -148,7 +148,7 @@ public:
if (!IsObject())
BOOST_THROW_EXCEPTION(std::runtime_error("Cannot convert value of type '" + GetTypeName() + "' to an object."));
Object::Ptr object = boost::get<Object::Ptr>(m_Value);
const Object::Ptr& object = boost::get<Object::Ptr>(m_Value);
ASSERT(object);

View File

@ -193,10 +193,10 @@ public:
static inline Value GetField(const Value& context, const String& field, bool sandboxed = false, const DebugInfo& debugInfo = DebugInfo())
{
if (context.IsEmpty() && !context.IsString())
if (unlikely(context.IsEmpty() && !context.IsString()))
return Empty;
if (!context.IsObject())
if (unlikely(!context.IsObject()))
return GetPrototypeField(context, field, true, debugInfo);
Object::Ptr object = context;