From 58282fd36f462c32b9273be85916eeaedd1e9fce Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 8 Oct 2014 08:42:37 -0700 Subject: Optimize compact_pointer_parent We now no longer need the compact_alignment type so replace it with a constant. --- src/pugixml.cpp | 50 ++++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 30 deletions(-) (limited to 'src/pugixml.cpp') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index bdb8425..1694a68 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -640,15 +640,14 @@ PUGI__NS_BEGIN }; static const unsigned int compact_alignment_log2 = 2; - - typedef uint32_t compact_alignment; + static const unsigned int compact_alignment = 1 << compact_alignment_log2; class compact_header { public: compact_header(xml_memory_page* page, unsigned int flags) { - ptrdiff_t page_offset = reinterpret_cast(this) - reinterpret_cast(page); + ptrdiff_t page_offset = (reinterpret_cast(this) - reinterpret_cast(page)) >> compact_alignment_log2; assert(page_offset >= 0 && page_offset < (1 << 16)); this->page0 = static_cast(page_offset); @@ -675,7 +674,7 @@ PUGI__NS_BEGIN { unsigned int page_offset = page0 + (page1 << 8); - return const_cast(reinterpret_cast(reinterpret_cast(this) - page_offset)); + return const_cast(reinterpret_cast(reinterpret_cast(this) - (page_offset << compact_alignment_log2))); } private: @@ -716,7 +715,7 @@ PUGI__NS_BEGIN { if (value) { - ptrdiff_t offset = ((reinterpret_cast(value) - reinterpret_cast(this) + int(sizeof(compact_alignment) - 1)) >> compact_alignment_log2) - start; + ptrdiff_t offset = ((reinterpret_cast(value) - reinterpret_cast(this) + int(compact_alignment - 1)) >> compact_alignment_log2) - start; if (static_cast(offset) <= 253) _data = static_cast(offset + 1); @@ -737,7 +736,7 @@ PUGI__NS_BEGIN { if (_data < 255) { - uintptr_t base = reinterpret_cast(this) & ~(sizeof(compact_alignment) - 1); + uintptr_t base = reinterpret_cast(this) & ~(compact_alignment - 1); return reinterpret_cast(base + ((_data - (1 - start)) << compact_alignment_log2)); } @@ -769,31 +768,27 @@ PUGI__NS_BEGIN *this = rhs + 0; } - void operator=(T* value_) + void operator=(T* value) { - if (value_) + if (value) { - compact_alignment* base = get_base(); - compact_alignment* value = reinterpret_cast(value_); + ptrdiff_t offset = ((reinterpret_cast(value) - reinterpret_cast(this) + int(compact_alignment - 1)) >> compact_alignment_log2) + 253; - if (value <= base && value >= base - 252) - _data = static_cast((base - value) + 1); + if (static_cast(offset) <= 253) + _data = static_cast(offset + 1); else { xml_memory_page* page = compact_get_page(this, header_offset); if (page->compact_parent == 0) - { page->compact_parent = value; + + if (page->compact_parent == value) _data = 254; - } - else if (page->compact_parent == value) - { - _data = 254; - } else { compact_set_value(this, value); + _data = 255; } } @@ -806,16 +801,16 @@ PUGI__NS_BEGIN { if (_data) { - if (_data == 255) - return compact_get_value(this); - else if (_data == 254) - return static_cast(compact_get_page(this, header_offset)->compact_parent); - else + if (_data < 254) { - compact_alignment* base = get_base(); + uintptr_t base = reinterpret_cast(this) & ~(compact_alignment - 1); - return reinterpret_cast(base - (_data - 1)); + return reinterpret_cast(base + ((_data - (1 + 253)) << compact_alignment_log2)); } + else if (_data == 254) + return static_cast(compact_get_page(this, header_offset)->compact_parent); + else + return compact_get_value(this); } else return 0; @@ -828,11 +823,6 @@ PUGI__NS_BEGIN private: unsigned char _data; - - compact_alignment* get_base() const - { - return reinterpret_cast(reinterpret_cast(this) & ~(sizeof(compact_alignment) - 1)); - } }; template class compact_string -- cgit v1.2.3