From 122c7229936fa8811b78eea78c311b51c53fc836 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 31 Dec 2015 16:17:52 +0800 Subject: [PATCH] More LIKELY/UNLIKELY in Writer --- include/rapidjson/writer.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 13db449..63b88f5 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -146,7 +146,7 @@ public: RAPIDJSON_ASSERT(!level_stack_.template Top()->inArray); level_stack_.template Pop(1); bool ret = WriteEndObject(); - if (level_stack_.Empty()) // end of json text + if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text os_->Flush(); return ret; } @@ -163,7 +163,7 @@ public: RAPIDJSON_ASSERT(level_stack_.template Top()->inArray); level_stack_.template Pop(1); bool ret = WriteEndArray(); - if (level_stack_.Empty()) // end of json text + if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text os_->Flush(); return ret; } @@ -271,12 +271,12 @@ protected: PutUnsafe(*os_, '\"'); GenericStringStream is(str); - while (is.Tell() < length) { + while (RAPIDJSON_LIKELY(is.Tell() < length)) { const Ch c = is.Peek(); if (!TargetEncoding::supportUnicode && static_cast(c) >= 0x80) { // Unicode escaping unsigned codepoint; - if (!SourceEncoding::Decode(is, &codepoint)) + if (RAPIDJSON_UNLIKELY(!SourceEncoding::Decode(is, &codepoint))) return false; PutUnsafe(*os_, '\\'); PutUnsafe(*os_, 'u'); @@ -304,7 +304,7 @@ protected: PutUnsafe(*os_, hexDigits[(trail ) & 15]); } } - else if ((sizeof(Ch) == 1 || static_cast(c) < 256) && escape[static_cast(c)]) { + else if ((sizeof(Ch) == 1 || static_cast(c) < 256) && RAPIDJSON_UNLIKELY(escape[static_cast(c)])) { is.Take(); PutUnsafe(*os_, '\\'); PutUnsafe(*os_, static_cast(escape[static_cast(c)])); @@ -316,7 +316,7 @@ protected: } } else - if (!Transcoder::TranscodeUnsafe(is, *os_)) + if (RAPIDJSON_UNLIKELY(!(Transcoder::TranscodeUnsafe(is, *os_)))) return false; } PutUnsafe(*os_, '\"'); @@ -330,7 +330,7 @@ protected: void Prefix(Type type) { (void)type; - if (level_stack_.GetSize() != 0) { // this value is not at root + if (RAPIDJSON_LIKELY(level_stack_.GetSize() != 0)) { // this value is not at root Level* level = level_stack_.template Top(); if (level->valueCount > 0) { if (level->inArray)