From 20e2041f14f8ebb842df4ebdac9ba3dc0955e670 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 2 May 2015 15:57:46 -0700 Subject: Reorder conditions in compact_string implementation Now compact_string matches compact_pointer_parent. Turns out PUGI__UNLIKELY is good at reordering conditions but usually does not really affect performance. Since MSVC should treat "if" branches as taken and does not support branch probabilities, don't use them if we don't need to. --- src/pugixml.cpp | 58 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 1a7acb3..568eb17 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -849,7 +849,7 @@ PUGI__NS_BEGIN { xml_memory_page* page = compact_get_page(this, header_offset); - if (page->compact_shared_parent == 0) + if (PUGI__UNLIKELY(page->compact_shared_parent == 0)) page->compact_shared_parent = value; if (page->compact_shared_parent == value) @@ -872,17 +872,15 @@ PUGI__NS_BEGIN operator T*() const { - int data = _data; - - if (data) + if (_data) { - if (data < 65534) + if (_data < 65534) { uintptr_t base = reinterpret_cast(this) & ~(compact_alignment - 1); - return reinterpret_cast(base + ((data - 1 - 65533) << compact_alignment_log2)); + return reinterpret_cast(base + ((_data - 1 - 65533) << compact_alignment_log2)); } - else if (data == 65534) + else if (_data == 65534) return static_cast(compact_get_page(this, header_offset)->compact_shared_parent); else return compact_get_value(this); @@ -923,36 +921,36 @@ PUGI__NS_BEGIN ptrdiff_t offset = value - page->compact_string_base; - if (PUGI__UNLIKELY(static_cast(offset) >= (65535 << 7))) - { - compact_set_value(this, value); - - _data = 255; - } - else + if (static_cast(offset) < (65535 << 7)) { uint16_t* base = reinterpret_cast(reinterpret_cast(this) - base_offset); - if (PUGI__UNLIKELY(*base)) + if (*base == 0) + { + *base = static_cast((offset >> 7) + 1); + _data = static_cast((offset & 127) + 1); + } + else { ptrdiff_t remainder = offset - ((*base - 1) << 7); - if (PUGI__UNLIKELY(static_cast(remainder) >= 254)) + if (static_cast(remainder) < 254) { - compact_set_value(this, value); - - _data = 255; + _data = static_cast(remainder + 1); } else { - _data = static_cast(remainder + 1); + compact_set_value(this, value); + + _data = 255; } } - else - { - *base = static_cast((offset >> 7) + 1); - _data = static_cast((offset & 127) + 1); - } + } + else + { + compact_set_value(this, value); + + _data = 255; } } else @@ -965,11 +963,7 @@ PUGI__NS_BEGIN { if (_data) { - if (PUGI__UNLIKELY(_data == 255)) - { - return compact_get_value(this); - } - else + if (_data < 255) { xml_memory_page* page = compact_get_page(this, header_offset); @@ -980,6 +974,10 @@ PUGI__NS_BEGIN return page->compact_string_base + offset; } + else + { + return compact_get_value(this); + } } else return 0; -- cgit v1.2.3