From f7a64c5eae3a03930aad8f5ba855647e060dc10f Mon Sep 17 00:00:00 2001 From: miloyip Date: Sat, 18 Apr 2015 21:31:25 +0800 Subject: [PATCH] Add RAPIDJSON_LIKELY/UNLIKELY and apply them in stack --- include/rapidjson/internal/stack.h | 2 +- include/rapidjson/rapidjson.h | 29 +++++++++++++++++++++++++++++ test/perftest/rapidjsontest.cpp | 6 ++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/internal/stack.h b/include/rapidjson/internal/stack.h index 2f2c76a..b256eb9 100644 --- a/include/rapidjson/internal/stack.h +++ b/include/rapidjson/internal/stack.h @@ -98,7 +98,7 @@ public: template RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) { // Expand the stack if needed - if (stackTop_ + sizeof(T) * count >= stackEnd_) + if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count >= stackEnd_)) Expand(count); T* ret = reinterpret_cast(stackTop_); diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index 602dabd..0efe33f 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -343,6 +343,35 @@ RAPIDJSON_NAMESPACE_END RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE #endif +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_LIKELY, RAPIDJSON_UNLIKELY + +//! Compiler branching hint for expression with high probability to be true. +/*! + \ingroup RAPIDJSON_CONFIG + \param x Boolean expression likely to be true. +*/ +#ifndef RAPIDJSON_LIKELY +#if defined(__GNUC__) || defined(__clang__) +#define RAPIDJSON_LIKELY(x) __builtin_expect(!!(x), 1) +#else +#define RAPIDJSON_LIKELY(x) x +#endif +#endif + +//! Compiler branching hint for expression with low probability to be true. +/*! + \ingroup RAPIDJSON_CONFIG + \param x Boolean expression unlikely to be true. +*/ +#ifndef RAPIDJSON_UNLIKELY +#if defined(__GNUC__) || defined(__clang__) +#define RAPIDJSON_UNLIKELY(x) __builtin_expect(!!(x), 0) +#else +#define RAPIDJSON_UNLIKELY(x) x +#endif +#endif + /////////////////////////////////////////////////////////////////////////////// // Helpers diff --git a/test/perftest/rapidjsontest.cpp b/test/perftest/rapidjsontest.cpp index 875c5eb..6a0fe43 100644 --- a/test/perftest/rapidjsontest.cpp +++ b/test/perftest/rapidjsontest.cpp @@ -346,4 +346,10 @@ TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_FileReadStream)) { } } +TEST_F(RapidJson, StringBuffer) { + StringBuffer sb; + for (int i = 0; i < 32 * 1024 * 1024; i++) + sb.Put(i & 0x7f); +} + #endif // TEST_RAPIDJSON