diff options
author | Arseny Kapoulkine <arseny.kapoulkine@gmail.com> | 2015-05-02 15:57:46 -0700 |
---|---|---|
committer | Arseny Kapoulkine <arseny.kapoulkine@gmail.com> | 2015-05-02 15:57:46 -0700 |
commit | 20e2041f14f8ebb842df4ebdac9ba3dc0955e670 (patch) | |
tree | 3cb000b8ba7be0a02fe0c708285653cda580d911 | |
parent | f8915c8eab0241d2a9621e28893518c010aa0cb5 (diff) |
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.
-rw-r--r-- | src/pugixml.cpp | 58 |
1 files changed, 28 insertions, 30 deletions
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<uintptr_t>(this) & ~(compact_alignment - 1); - return reinterpret_cast<T*>(base + ((data - 1 - 65533) << compact_alignment_log2)); + return reinterpret_cast<T*>(base + ((_data - 1 - 65533) << compact_alignment_log2)); } - else if (data == 65534) + else if (_data == 65534) return static_cast<T*>(compact_get_page(this, header_offset)->compact_shared_parent); else return compact_get_value<header_offset, T>(this); @@ -923,36 +921,36 @@ PUGI__NS_BEGIN ptrdiff_t offset = value - page->compact_string_base; - if (PUGI__UNLIKELY(static_cast<uintptr_t>(offset) >= (65535 << 7))) - { - compact_set_value<header_offset>(this, value); - - _data = 255; - } - else + if (static_cast<uintptr_t>(offset) < (65535 << 7)) { uint16_t* base = reinterpret_cast<uint16_t*>(reinterpret_cast<char*>(this) - base_offset); - if (PUGI__UNLIKELY(*base)) + if (*base == 0) + { + *base = static_cast<uint16_t>((offset >> 7) + 1); + _data = static_cast<unsigned char>((offset & 127) + 1); + } + else { ptrdiff_t remainder = offset - ((*base - 1) << 7); - if (PUGI__UNLIKELY(static_cast<uintptr_t>(remainder) >= 254)) + if (static_cast<uintptr_t>(remainder) < 254) { - compact_set_value<header_offset>(this, value); - - _data = 255; + _data = static_cast<unsigned char>(remainder + 1); } else { - _data = static_cast<unsigned char>(remainder + 1); + compact_set_value<header_offset>(this, value); + + _data = 255; } } - else - { - *base = static_cast<uint16_t>((offset >> 7) + 1); - _data = static_cast<unsigned char>((offset & 127) + 1); - } + } + else + { + compact_set_value<header_offset>(this, value); + + _data = 255; } } else @@ -965,11 +963,7 @@ PUGI__NS_BEGIN { if (_data) { - if (PUGI__UNLIKELY(_data == 255)) - { - return compact_get_value<header_offset, char_t>(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<header_offset, char_t>(this); + } } else return 0; |