From 45a1b743358f041cee6b590f4f419b3a3c4eb9b8 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sun, 5 Oct 2014 22:21:38 -0700 Subject: Initial compact storage prototype The storage uses one hash table for fallbacks and simple difference encoding for node and string pointers. This is a work in progress implementation - while node pointers seem to work properly, string encoding is inefficient and parent pointers could use more tuning. No performance or compatibility work has been done either. --- src/pugixml.cpp | 523 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 496 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 58191ae..c0a2980 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -145,6 +145,13 @@ PUGI__NS_BEGIN PUGI__NS_END #endif +// Micro compact helper +#ifdef PUGIXML_COMPACT +# define PUGI__COMPACT(p) ((p).get()) +#else +# define PUGI__COMPACT(p) (p) +#endif + // Memory allocation PUGI__NS_BEGIN PUGI__FN void* default_allocate(size_t size) @@ -293,6 +300,11 @@ PUGI__NS_BEGIN result->busy_size = 0; result->freed_size = 0; + #ifdef PUGIXML_COMPACT + result->compact_string_base = 0; + result->compact_parent = 0; + #endif + return result; } @@ -303,6 +315,11 @@ PUGI__NS_BEGIN size_t busy_size; size_t freed_size; + + #ifdef PUGIXML_COMPACT + char_t* compact_string_base; + void* compact_parent; + #endif }; struct xml_memory_string_header @@ -315,6 +332,9 @@ PUGI__NS_BEGIN { xml_allocator(xml_memory_page* root): _root(root), _busy_size(root->busy_size) { + #ifdef PUGIXML_COMPACT + _hash = 0; + #endif } xml_memory_page* allocate_page(size_t data_size) @@ -446,6 +466,10 @@ PUGI__NS_BEGIN xml_memory_page* _root; size_t _busy_size; + + #ifdef PUGIXML_COMPACT + class compact_hash_table* _hash; + #endif }; PUGI__FN_NO_INLINE void* xml_allocator::allocate_memory_oob(size_t size, xml_memory_page*& out_page) @@ -488,6 +512,429 @@ PUGI__NS_BEGIN } PUGI__NS_END +#ifdef PUGIXML_COMPACT +size_t pugi_compact_stats[128]; + +PUGI__NS_BEGIN + class compact_hash_table + { + public: + compact_hash_table(): _items(0), _capacity(0), _count(0) + { + } + + void clear() + { + if (_items) + { + xml_memory::deallocate(_items); + _items = 0; + _capacity = 0; + _count = 0; + } + } + + void** find(const void* key) + { + assert(key); + + if (_capacity == 0) return 0; + + size_t hashmod = _capacity - 1; + size_t bucket = hash(key) & hashmod; + + for (size_t probe = 0; probe <= hashmod; ++probe) + { + item_t& probe_item = _items[bucket]; + + if (probe_item.key == key) + return &probe_item.value; + + if (probe_item.key == 0) + return 0; + + // hash collision, quadratic probing + bucket = (bucket + probe + 1) & hashmod; + } + + assert(!"Hash table is full"); + return 0; + } + + void** insert(const void* key, size_t tag) + { + assert(key); + + if (_count >= _capacity * 3 / 4) + rehash(); + + size_t hashmod = _capacity - 1; + size_t bucket = hash(key) & hashmod; + + for (size_t probe = 0; probe <= hashmod; ++probe) + { + item_t& probe_item = _items[bucket]; + + if (probe_item.key == 0) + { + if (tag) + pugi_compact_stats[tag]++; + + probe_item.key = key; + _count++; + return &probe_item.value; + } + + if (probe_item.key == key) + return &probe_item.value; + + // hash collision, quadratic probing + bucket = (bucket + probe + 1) & hashmod; + } + + assert(!"Hash table is full"); + return 0; + } + + private: + struct item_t + { + const void* key; + void* value; + }; + + item_t* _items; + size_t _capacity; + + size_t _count; + + void rehash() + { + compact_hash_table rt; + rt._capacity = (_capacity == 0) ? 256 : _capacity * 2; + rt._items = static_cast(xml_memory::allocate(sizeof(item_t) * rt._capacity)); + + assert(rt._items); + + memset(rt._items, 0, sizeof(item_t) * rt._capacity); + + for (size_t i = 0; i < _capacity; ++i) + if (_items[i].key) + *rt.insert(_items[i].key, 0) = _items[i].value; + + if (_items) + xml_memory::deallocate(_items); + + _capacity = rt._capacity; + _items = rt._items; + } + + // http://burtleburtle.net/bob/hash/integer.html + static unsigned int hash(const void* key) + { + unsigned int a = static_cast(reinterpret_cast(key)); + + a = (a ^ 61) ^ (a >> 16); + a = a + (a << 3); + a = a ^ (a >> 4); + a = a * 0x27d4eb2d; + a = a ^ (a >> 15); + + return a; + } + }; + + typedef uint32_t compact_alignment; + + class compact_header + { + public: + compact_header(xml_memory_page* page, unsigned int flags) + { + ptrdiff_t page_offset = reinterpret_cast(this) - reinterpret_cast(page); + assert(page_offset >= 0 && page_offset < (1 << 16)); + + this->page0 = static_cast(page_offset); + this->page1 = static_cast(page_offset >> 8); + this->flags = static_cast(flags); + } + + void operator&=(unsigned int modflags) + { + flags &= modflags; + } + + void operator|=(unsigned int modflags) + { + flags |= modflags; + } + + operator uintptr_t const() const + { + return reinterpret_cast(get_page()) | flags; + } + + xml_memory_page* get_page() const + { + unsigned int page_offset = page0 + (page1 << 8); + + return const_cast(reinterpret_cast(reinterpret_cast(this) - page_offset)); + } + + private: + unsigned char page0, page1; + unsigned char flags; + }; + + xml_memory_page* compact_get_page(const void* object, int header_offset) + { + const compact_header* header = reinterpret_cast(static_cast(object) - header_offset); + + return header->get_page(); + } + + template class compact_pointer + { + public: + compact_pointer(): _data(0) + { + } + + void operator=(const compact_pointer& rhs) + { + *this = rhs.get(); + } + + void operator=(T* value_) + { + if (value_) + { + compact_alignment* base = get_base(); + compact_alignment* value = reinterpret_cast(value_); + + if (direction > 0) + { + if (value >= base && value <= base + 253) + _data = static_cast((value - base) + 1); + else + { + *compact_get_page(this, header_offset)->allocator->_hash->insert(this, tag) = value; + + _data = 255; + } + } + else if (direction < 0) + { + if (value <= base && value >= base - 252) + _data = static_cast((base - value) + 1); + else + { + xml_memory_page* page = compact_get_page(this, header_offset); + + if (page->compact_parent == 0) + { + page->compact_parent = value; + _data = 254; + } + else if (page->compact_parent == value) + { + _data = 254; + } + else + { + *page->allocator->_hash->insert(this, tag) = value; + _data = 255; + } + } + } + else + { + if (value >= base - 126 && value <= base + 127) + _data = static_cast((value - base) + 127); + else + { + *compact_get_page(this, header_offset)->allocator->_hash->insert(this, tag) = value; + + _data = 255; + } + } + } + else + _data = 0; + } + + operator T* const() const + { + if (_data) + { + if (_data == 255) + return static_cast(*compact_get_page(this, header_offset)->allocator->_hash->find(this)); + else if (direction < 0 && _data == 254) + return static_cast(compact_get_page(this, header_offset)->compact_parent); + else + { + compact_alignment* base = get_base(); + + if (direction > 0) + return reinterpret_cast(base + (_data - 1)); + else if (direction < 0) + return reinterpret_cast(base - (_data - 1)); + else + return reinterpret_cast(base + (_data - 127)); + } + } + else + return 0; + } + + T* operator->() const + { + return operator T* const(); + } + + T* get() const + { + return operator T* const(); + } + + private: + unsigned char _data; + + compact_alignment* get_base() const + { + if (direction > 0) + return reinterpret_cast((reinterpret_cast(this) + sizeof(compact_alignment)) & ~(sizeof(compact_alignment) - 1)); + else + return reinterpret_cast(reinterpret_cast(this) & ~(sizeof(compact_alignment) - 1)); + } + }; + + template class compact_string + { + public: + compact_string(): _data(0) + { + } + + void operator=(const compact_string& rhs) + { + *this = rhs.get(); + } + + void operator=(char_t* value) + { + if (value) + { + xml_memory_page* page = compact_get_page(this, header_offset); + + if (page->compact_string_base == 0) + { + page->compact_string_base = value; + + _data = 1; + } + else + { + ptrdiff_t offset = value - page->compact_string_base; + + if (offset >= 0 && offset <= 65533) + _data = static_cast(offset + 1); + else + { + *page->allocator->_hash->insert(this, tag) = value; + + _data = 65535; + } + } + } + else + _data = 0; + } + + operator char_t* const() const + { + if (_data) + { + xml_memory_page* page = compact_get_page(this, header_offset); + + if (_data < 65535) + { + return page->compact_string_base + (_data - 1); + } + else + { + return static_cast(*page->allocator->_hash->find(this)); + } + } + else + return 0; + } + + char_t* get() const + { + return operator char_t* const(); + } + + private: + unsigned short _data; + }; + +PUGI__NS_END +#endif + +#ifdef PUGIXML_COMPACT +namespace pugi +{ + /// A 'name=value' XML attribute structure. + struct xml_attribute_struct + { + /// Default ctor + xml_attribute_struct(impl::xml_memory_page* page): header(page, 0) + { + PUGI__STATIC_ASSERT(sizeof(xml_attribute_struct) == 12); + + pugi_compact_stats[10]++; + } + + impl::compact_header header; + + unsigned char padding[3]; + + impl::compact_string<6, /*tag*/11> name; ///< Pointer to attribute name. + impl::compact_string<8, /*tag*/12> value; ///< Pointer to attribute value. + + impl::compact_pointer prev_attribute_c; ///< Previous attribute (cyclic list) + impl::compact_pointer next_attribute; ///< Next attribute + }; + + /// An XML document tree node. + struct xml_node_struct + { + /// Default ctor + /// \param type - node type + xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(page, type - 1) + { + PUGI__STATIC_ASSERT(sizeof(xml_node_struct) == 12); + + pugi_compact_stats[20]++; + } + + impl::compact_header header; + + impl::compact_pointer parent; ///< Pointer to parent + + impl::compact_string<4, /*tag*/21> name; ///< Pointer to element name. + impl::compact_string<6, /*tag*/22> value; ///< Pointer to any associated string data. + + impl::compact_pointer first_child; ///< First child + + impl::compact_pointer prev_sibling_c; ///< Left brother (cyclic list) + impl::compact_pointer next_sibling; ///< Right brother + + impl::compact_pointer first_attribute; ///< First attribute + }; +} +#else namespace pugi { /// A 'name=value' XML attribute structure. @@ -531,6 +978,7 @@ namespace pugi xml_attribute_struct* first_attribute; ///< First attribute }; } +#endif PUGI__NS_BEGIN struct xml_extra_buffer @@ -543,11 +991,18 @@ PUGI__NS_BEGIN { xml_document_struct(xml_memory_page* page): xml_node_struct(page, node_document), xml_allocator(page), buffer(0), extra_buffers(0) { + #ifdef PUGIXML_COMPACT + _hash = &hash; + #endif } const char_t* buffer; xml_extra_buffer* extra_buffers; + + #ifdef PUGIXML_COMPACT + compact_hash_table hash; + #endif }; inline xml_allocator& get_allocator(const xml_node_struct* node) @@ -1679,7 +2134,8 @@ PUGI__NS_BEGIN return target_length >= length && (target_length < reuse_threshold || target_length - length < target_length / 2); } - PUGI__FN bool strcpy_insitu(char_t*& dest, uintptr_t& header, uintptr_t header_mask, const char_t* source) + template + PUGI__FN bool strcpy_insitu(String& dest, Header& header, uintptr_t header_mask, const char_t* source) { assert(header); @@ -2826,7 +3282,7 @@ PUGI__NS_BEGIN return make_parse_result(PUGI__OPTSET(parse_fragment) ? status_ok : status_no_document_element); // get last child of the root before parsing - xml_node_struct* last_root_child = root->first_child ? root->first_child->prev_sibling_c : 0; + xml_node_struct* last_root_child = root->first_child ? PUGI__COMPACT(root->first_child->prev_sibling_c) : 0; // create parser on stack xml_parser parser(alloc_); @@ -2854,7 +3310,7 @@ PUGI__NS_BEGIN return make_parse_result(status_unrecognized_tag, length - 1); // check if there are any element nodes parsed - xml_node_struct* first_root_child_parsed = last_root_child ? last_root_child->next_sibling : root->first_child; + xml_node_struct* first_root_child_parsed = last_root_child ? PUGI__COMPACT(last_root_child->next_sibling) : root->first_child; if (!PUGI__OPTSET(parse_fragment) && !has_element_node_siblings(first_root_child_parsed)) return make_parse_result(status_no_document_element, length - 1); @@ -3612,7 +4068,8 @@ PUGI__NS_BEGIN return true; } - PUGI__FN void node_copy_string(char_t*& dest, uintptr_t& header, uintptr_t header_mask, char_t* source, uintptr_t& source_header, xml_allocator* alloc) + template + PUGI__FN void node_copy_string(String& dest, Header& header, uintptr_t header_mask, char_t* source, Header& source_header, xml_allocator* alloc) { if (source) { @@ -3813,7 +4270,8 @@ PUGI__NS_BEGIN #endif // set value with conversion functions - PUGI__FN bool set_value_buffer(char_t*& dest, uintptr_t& header, uintptr_t header_mask, char (&buf)[128]) + template + PUGI__FN bool set_value_buffer(String& dest, Header& header, uintptr_t header_mask, char (&buf)[128]) { #ifdef PUGIXML_WCHAR_MODE char_t wbuf[128]; @@ -3825,7 +4283,8 @@ PUGI__NS_BEGIN #endif } - PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, int value) + template + PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, int value) { char buf[128]; sprintf(buf, "%d", value); @@ -3833,7 +4292,8 @@ PUGI__NS_BEGIN return set_value_buffer(dest, header, header_mask, buf); } - PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, unsigned int value) + template + PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, unsigned int value) { char buf[128]; sprintf(buf, "%u", value); @@ -3841,7 +4301,8 @@ PUGI__NS_BEGIN return set_value_buffer(dest, header, header_mask, buf); } - PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, double value) + template + PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, double value) { char buf[128]; sprintf(buf, "%g", value); @@ -3849,13 +4310,15 @@ PUGI__NS_BEGIN return set_value_buffer(dest, header, header_mask, buf); } - PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, bool value) + template + PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, bool value) { return strcpy_insitu(dest, header, header_mask, value ? PUGIXML_TEXT("true") : PUGIXML_TEXT("false")); } #ifdef PUGIXML_HAS_LONG_LONG - PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, long long value) + template + PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, long long value) { char buf[128]; sprintf(buf, "%lld", value); @@ -3863,7 +4326,8 @@ PUGI__NS_BEGIN return set_value_buffer(dest, header, header_mask, buf); } - PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, unsigned long long value) + template + PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, unsigned long long value) { char buf[128]; sprintf(buf, "%llu", value); @@ -4348,38 +4812,38 @@ namespace pugi PUGI__FN int xml_attribute::as_int(int def) const { - return impl::get_value_int(_attr ? _attr->value : 0, def); + return impl::get_value_int(_attr ? PUGI__COMPACT(_attr->value) : 0, def); } PUGI__FN unsigned int xml_attribute::as_uint(unsigned int def) const { - return impl::get_value_uint(_attr ? _attr->value : 0, def); + return impl::get_value_uint(_attr ? PUGI__COMPACT(_attr->value) : 0, def); } PUGI__FN double xml_attribute::as_double(double def) const { - return impl::get_value_double(_attr ? _attr->value : 0, def); + return impl::get_value_double(_attr ? PUGI__COMPACT(_attr->value) : 0, def); } PUGI__FN float xml_attribute::as_float(float def) const { - return impl::get_value_float(_attr ? _attr->value : 0, def); + return impl::get_value_float(_attr ? PUGI__COMPACT(_attr->value) : 0, def); } PUGI__FN bool xml_attribute::as_bool(bool def) const { - return impl::get_value_bool(_attr ? _attr->value : 0, def); + return impl::get_value_bool(_attr ? PUGI__COMPACT(_attr->value) : 0, def); } #ifdef PUGIXML_HAS_LONG_LONG PUGI__FN long long xml_attribute::as_llong(long long def) const { - return impl::get_value_llong(_attr ? _attr->value : 0, def); + return impl::get_value_llong(_attr ? PUGI__COMPACT(_attr->value) : 0, def); } PUGI__FN unsigned long long xml_attribute::as_ullong(unsigned long long def) const { - return impl::get_value_ullong(_attr ? _attr->value : 0, def); + return impl::get_value_ullong(_attr ? PUGI__COMPACT(_attr->value) : 0, def); } #endif @@ -4546,7 +5010,7 @@ namespace pugi PUGI__FN xml_node::iterator xml_node::begin() const { - return iterator(_root ? _root->first_child : 0, _root); + return iterator(_root ? PUGI__COMPACT(_root->first_child) : 0, _root); } PUGI__FN xml_node::iterator xml_node::end() const @@ -4556,7 +5020,7 @@ namespace pugi PUGI__FN xml_node::attribute_iterator xml_node::attributes_begin() const { - return attribute_iterator(_root ? _root->first_attribute : 0, _root); + return attribute_iterator(_root ? PUGI__COMPACT(_root->first_attribute) : 0, _root); } PUGI__FN xml_node::attribute_iterator xml_node::attributes_end() const @@ -5452,35 +5916,35 @@ namespace pugi { xml_node_struct* d = _data(); - return impl::get_value_int(d ? d->value : 0, def); + return impl::get_value_int(d ? PUGI__COMPACT(d->value) : 0, def); } PUGI__FN unsigned int xml_text::as_uint(unsigned int def) const { xml_node_struct* d = _data(); - return impl::get_value_uint(d ? d->value : 0, def); + return impl::get_value_uint(d ? PUGI__COMPACT(d->value) : 0, def); } PUGI__FN double xml_text::as_double(double def) const { xml_node_struct* d = _data(); - return impl::get_value_double(d ? d->value : 0, def); + return impl::get_value_double(d ? PUGI__COMPACT(d->value) : 0, def); } PUGI__FN float xml_text::as_float(float def) const { xml_node_struct* d = _data(); - return impl::get_value_float(d ? d->value : 0, def); + return impl::get_value_float(d ? PUGI__COMPACT(d->value) : 0, def); } PUGI__FN bool xml_text::as_bool(bool def) const { xml_node_struct* d = _data(); - return impl::get_value_bool(d ? d->value : 0, def); + return impl::get_value_bool(d ? PUGI__COMPACT(d->value) : 0, def); } #ifdef PUGIXML_HAS_LONG_LONG @@ -5488,14 +5952,14 @@ namespace pugi { xml_node_struct* d = _data(); - return impl::get_value_llong(d ? d->value : 0, def); + return impl::get_value_llong(d ? PUGI__COMPACT(d->value) : 0, def); } PUGI__FN unsigned long long xml_text::as_ullong(unsigned long long def) const { xml_node_struct* d = _data(); - return impl::get_value_ullong(d ? d->value : 0, def); + return impl::get_value_ullong(d ? PUGI__COMPACT(d->value) : 0, def); } #endif @@ -5924,6 +6388,11 @@ namespace pugi page = next; } + #ifdef PUGIXML_COMPACT + // destroy hash table + static_cast(_root)->hash.clear(); + #endif + _root = 0; } -- cgit v1.2.3 From edb57c96a883ed76001cb127e39a458aaf5e8731 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Mon, 6 Oct 2014 22:25:49 -0700 Subject: Switch to a 3-byte representation for compact strings To make this possible name and value in the node structure had to be merged into one contents field. Not sure what to do with node_pi, since it is the only type that required both. --- src/pugixml.cpp | 180 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 99 insertions(+), 81 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index c0a2980..0cbf088 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -276,11 +276,13 @@ PUGI__NS_BEGIN static const uintptr_t xml_memory_page_alignment = 64; static const uintptr_t xml_memory_page_pointer_mask = ~(xml_memory_page_alignment - 1); static const uintptr_t xml_memory_page_contents_shared_mask = 32; + static const uintptr_t xml_memory_page_contents_allocated_mask = 16; static const uintptr_t xml_memory_page_name_allocated_mask = 16; static const uintptr_t xml_memory_page_value_allocated_mask = 8; static const uintptr_t xml_memory_page_type_mask = 7; static const uintptr_t xml_memory_page_name_allocated_or_shared_mask = xml_memory_page_name_allocated_mask | xml_memory_page_contents_shared_mask; static const uintptr_t xml_memory_page_value_allocated_or_shared_mask = xml_memory_page_value_allocated_mask | xml_memory_page_contents_shared_mask; + static const uintptr_t xml_memory_page_contents_allocated_or_shared_mask = xml_memory_page_contents_allocated_mask | xml_memory_page_contents_shared_mask; #define PUGI__NODETYPE(n) static_cast(((n)->header & impl::xml_memory_page_type_mask) + 1) @@ -812,7 +814,7 @@ PUGI__NS_BEGIN template class compact_string { public: - compact_string(): _data(0) + compact_string(): _data0(0), _data1(0), _data2(0) { } @@ -831,35 +833,49 @@ PUGI__NS_BEGIN { page->compact_string_base = value; - _data = 1; + _data0 = 1; + _data1 = 0; + _data2 = 0; } else { ptrdiff_t offset = value - page->compact_string_base; - if (offset >= 0 && offset <= 65533) - _data = static_cast(offset + 1); + if (offset >= 0 && offset <= 1677213) + { + _data0 = static_cast(offset + 1); + _data1 = static_cast((offset + 1) >> 8); + _data2 = static_cast((offset + 1) >> 16); + } else { *page->allocator->_hash->insert(this, tag) = value; - _data = 65535; + _data0 = 255; + _data1 = 255; + _data2 = 255; } } } else - _data = 0; + { + _data0 = 0; + _data1 = 0; + _data2 = 0; + } } operator char_t* const() const { - if (_data) + unsigned int data = _data0 + (_data1 << 8) + (_data2 << 16); + + if (data) { xml_memory_page* page = compact_get_page(this, header_offset); - if (_data < 65535) + if (data < 16777215) { - return page->compact_string_base + (_data - 1); + return page->compact_string_base + (data - 1); } else { @@ -876,7 +892,9 @@ PUGI__NS_BEGIN } private: - unsigned short _data; + unsigned char _data0; + unsigned char _data1; + unsigned char _data2; }; PUGI__NS_END @@ -898,10 +916,10 @@ namespace pugi impl::compact_header header; - unsigned char padding[3]; + unsigned char padding; - impl::compact_string<6, /*tag*/11> name; ///< Pointer to attribute name. - impl::compact_string<8, /*tag*/12> value; ///< Pointer to attribute value. + impl::compact_string<4, /*tag*/11> name; ///< Pointer to attribute name. + impl::compact_string<7, /*tag*/12> value; ///< Pointer to attribute value. impl::compact_pointer prev_attribute_c; ///< Previous attribute (cyclic list) impl::compact_pointer next_attribute; ///< Next attribute @@ -921,17 +939,18 @@ namespace pugi impl::compact_header header; - impl::compact_pointer parent; ///< Pointer to parent + unsigned char padding; - impl::compact_string<4, /*tag*/21> name; ///< Pointer to element name. - impl::compact_string<6, /*tag*/22> value; ///< Pointer to any associated string data. + impl::compact_string<4, /*tag*/21> contents; ///< Pointer to element name. - impl::compact_pointer first_child; ///< First child + impl::compact_pointer parent; ///< Pointer to parent - impl::compact_pointer prev_sibling_c; ///< Left brother (cyclic list) - impl::compact_pointer next_sibling; ///< Right brother + impl::compact_pointer first_child; ///< First child - impl::compact_pointer first_attribute; ///< First attribute + impl::compact_pointer prev_sibling_c; ///< Left brother (cyclic list) + impl::compact_pointer next_sibling; ///< Right brother + + impl::compact_pointer first_attribute; ///< First attribute }; } #else @@ -959,7 +978,7 @@ namespace pugi { /// Default ctor /// \param type - node type - xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(reinterpret_cast(page) | (type - 1)), parent(0), name(0), value(0), first_child(0), prev_sibling_c(0), next_sibling(0), first_attribute(0) + xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(reinterpret_cast(page) | (type - 1)), parent(0), contents(0), first_child(0), prev_sibling_c(0), next_sibling(0), first_attribute(0) { } @@ -967,8 +986,7 @@ namespace pugi xml_node_struct* parent; ///< Pointer to parent - char_t* name; ///< Pointer to element name. - char_t* value; ///< Pointer to any associated string data. + char_t* contents; ///< Pointer to element name/value xml_node_struct* first_child; ///< First child @@ -1052,8 +1070,7 @@ PUGI__NS_BEGIN { uintptr_t header = n->header; - if (header & impl::xml_memory_page_name_allocated_mask) alloc.deallocate_string(n->name); - if (header & impl::xml_memory_page_value_allocated_mask) alloc.deallocate_string(n->value); + if (header & impl::xml_memory_page_contents_allocated_mask) alloc.deallocate_string(n->contents); for (xml_attribute_struct* attr = n->first_attribute; attr; ) { @@ -2827,14 +2844,14 @@ PUGI__NS_BEGIN if (PUGI__OPTSET(parse_comments)) { PUGI__PUSHNODE(node_comment); // Append a new node on the tree. - cursor->value = s; // Save the offset. + cursor->contents = s; // Save the offset. } if (PUGI__OPTSET(parse_eol) && PUGI__OPTSET(parse_comments)) { s = strconv_comment(s, endch); - if (!s) PUGI__THROW_ERROR(status_bad_comment, cursor->value); + if (!s) PUGI__THROW_ERROR(status_bad_comment, cursor->contents); } else { @@ -2860,13 +2877,13 @@ PUGI__NS_BEGIN if (PUGI__OPTSET(parse_cdata)) { PUGI__PUSHNODE(node_cdata); // Append a new node on the tree. - cursor->value = s; // Save the offset. + cursor->contents = s; // Save the offset. if (PUGI__OPTSET(parse_eol)) { s = strconv_cdata(s, endch); - if (!s) PUGI__THROW_ERROR(status_bad_cdata, cursor->value); + if (!s) PUGI__THROW_ERROR(status_bad_cdata, cursor->contents); } else { @@ -2910,7 +2927,7 @@ PUGI__NS_BEGIN PUGI__PUSHNODE(node_doctype); - cursor->value = mark; + cursor->contents = mark; PUGI__POPNODE(); } @@ -2956,7 +2973,7 @@ PUGI__NS_BEGIN PUGI__PUSHNODE(node_pi); } - cursor->name = target; + cursor->contents = target; PUGI__ENDSEG(); @@ -2990,7 +3007,7 @@ PUGI__NS_BEGIN else { // store value and step over > - cursor->value = value; + // TODO: node_pi value:cursor->value = value; PUGI__POPNODE(); PUGI__ENDSEG(); @@ -3035,7 +3052,7 @@ PUGI__NS_BEGIN { PUGI__PUSHNODE(node_element); // Append a new node to the tree. - cursor->name = s; + cursor->contents = s; PUGI__SCANWHILE_UNROLL(PUGI__IS_CHARTYPE(ss, ct_symbol)); // Scan for a terminator. PUGI__ENDSEG(); // Save char in 'ch', terminate & step over. @@ -3149,7 +3166,7 @@ PUGI__NS_BEGIN { ++s; - char_t* name = cursor->name; + char_t* name = cursor->contents; if (!name) PUGI__THROW_ERROR(status_end_element_mismatch, s); while (PUGI__IS_CHARTYPE(*s, ct_symbol)) @@ -3220,7 +3237,7 @@ PUGI__NS_BEGIN if (cursor->parent || PUGI__OPTSET(parse_fragment)) { PUGI__PUSHNODE(node_pcdata); // Append a new node on the tree. - cursor->value = s; // Save the offset. + cursor->contents = s; // Save the offset. s = strconv_pcdata(s); @@ -4088,8 +4105,7 @@ PUGI__NS_BEGIN PUGI__FN void node_copy_contents(xml_node_struct* dn, xml_node_struct* sn, xml_allocator* shared_alloc) { - node_copy_string(dn->name, dn->header, xml_memory_page_name_allocated_mask, sn->name, sn->header, shared_alloc); - node_copy_string(dn->value, dn->header, xml_memory_page_value_allocated_mask, sn->value, sn->header, shared_alloc); + node_copy_string(dn->contents, dn->header, xml_memory_page_contents_allocated_mask, sn->contents, sn->header, shared_alloc); for (xml_attribute_struct* sa = sn->first_attribute; sa; sa = sa->next_attribute) { @@ -4680,6 +4696,20 @@ PUGI__NS_BEGIN return res; } + + inline bool has_name(xml_node_struct* node) + { + static const bool result[] = { false, false, true, false, false, false, true, true, false }; + + return result[PUGI__NODETYPE(node)]; + } + + inline bool has_value(xml_node_struct* node) + { + static const bool result[] = { false, false, false, true, true, true, false, false, true }; + + return result[PUGI__NODETYPE(node)]; + } PUGI__NS_END namespace pugi @@ -5080,7 +5110,7 @@ namespace pugi PUGI__FN const char_t* xml_node::name() const { - return (_root && _root->name) ? _root->name : PUGIXML_TEXT(""); + return (_root && impl::has_name(_root) && _root->contents) ? _root->contents : PUGIXML_TEXT(""); } PUGI__FN xml_node_type xml_node::type() const @@ -5090,7 +5120,7 @@ namespace pugi PUGI__FN const char_t* xml_node::value() const { - return (_root && _root->value) ? _root->value : PUGIXML_TEXT(""); + return (_root && impl::has_value(_root) && _root->contents) ? _root->contents : PUGIXML_TEXT(""); } PUGI__FN xml_node xml_node::child(const char_t* name_) const @@ -5098,7 +5128,7 @@ namespace pugi if (!_root) return xml_node(); for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) - if (i->name && impl::strequal(name_, i->name)) return xml_node(i); + if (impl::has_name(i) && i->contents && impl::strequal(name_, i->contents)) return xml_node(i); return xml_node(); } @@ -5119,7 +5149,7 @@ namespace pugi if (!_root) return xml_node(); for (xml_node_struct* i = _root->next_sibling; i; i = i->next_sibling) - if (i->name && impl::strequal(name_, i->name)) return xml_node(i); + if (impl::has_name(i) && i->contents && impl::strequal(name_, i->contents)) return xml_node(i); return xml_node(); } @@ -5134,7 +5164,7 @@ namespace pugi if (!_root) return xml_node(); for (xml_node_struct* i = _root->prev_sibling_c; i->next_sibling; i = i->prev_sibling_c) - if (i->name && impl::strequal(name_, i->name)) return xml_node(i); + if (impl::has_name(i) && i->contents && impl::strequal(name_, i->contents)) return xml_node(i); return xml_node(); } @@ -5171,8 +5201,8 @@ namespace pugi if (!_root) return PUGIXML_TEXT(""); for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) - if (i->value && impl::is_text_node(i)) - return i->value; + if (impl::has_value(i) && i->contents && impl::is_text_node(i)) + return i->contents; return PUGIXML_TEXT(""); } @@ -5209,7 +5239,7 @@ namespace pugi case node_pi: case node_declaration: case node_element: - return impl::strcpy_insitu(_root->name, _root->header, impl::xml_memory_page_name_allocated_mask, rhs); + return impl::strcpy_insitu(_root->contents, _root->header, impl::xml_memory_page_contents_allocated_mask, rhs); default: return false; @@ -5225,7 +5255,7 @@ namespace pugi case node_pcdata: case node_comment: case node_doctype: - return impl::strcpy_insitu(_root->value, _root->header, impl::xml_memory_page_value_allocated_mask, rhs); + return impl::strcpy_insitu(_root->contents, _root->header, impl::xml_memory_page_contents_allocated_mask, rhs); default: return false; @@ -5613,15 +5643,15 @@ namespace pugi if (!extra) return impl::make_parse_result(status_out_of_memory); // save name; name of the root has to be NULL before parsing - otherwise closing node mismatches will not be detected at the top level - char_t* rootname = _root->name; - _root->name = 0; + char_t* rootname = _root->contents; + _root->contents = 0; // parse char_t* buffer = 0; xml_parse_result res = impl::load_buffer_impl(doc, _root, const_cast(contents), size, options, encoding, false, false, &buffer); // restore name - _root->name = rootname; + _root->contents = rootname; // add extra buffer to the list extra->buffer = buffer; @@ -5636,7 +5666,7 @@ namespace pugi if (!_root) return xml_node(); for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) - if (i->name && impl::strequal(name_, i->name)) + if (impl::has_name(i) && i->contents && impl::strequal(name_, i->contents)) { for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute) if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value : PUGIXML_TEXT(""))) @@ -5714,7 +5744,7 @@ namespace pugi { for (xml_node_struct* j = found._root->first_child; j; j = j->next_sibling) { - if (j->name && impl::strequalrange(j->name, path_segment, static_cast(path_segment_end - path_segment))) + if (impl::has_name(j) && j->contents && impl::strequalrange(j->contents, path_segment, static_cast(path_segment_end - path_segment))) { xml_node subsearch = xml_node(j).first_element_by_path(next_segment, delimiter); @@ -5824,19 +5854,8 @@ namespace pugi case node_document: return 0; - case node_element: - case node_declaration: - case node_pi: - return (_root->header & impl::xml_memory_page_name_allocated_or_shared_mask) ? -1 : _root->name - buffer; - - case node_pcdata: - case node_cdata: - case node_comment: - case node_doctype: - return (_root->header & impl::xml_memory_page_value_allocated_or_shared_mask) ? -1 : _root->value - buffer; - default: - return -1; + return (_root->header & impl::xml_memory_page_contents_allocated_or_shared_mask) ? -1 : _root->contents - buffer; } } @@ -5902,49 +5921,49 @@ namespace pugi { xml_node_struct* d = _data(); - return (d && d->value) ? d->value : PUGIXML_TEXT(""); + return (d && d->contents) ? d->contents : PUGIXML_TEXT(""); } PUGI__FN const char_t* xml_text::as_string(const char_t* def) const { xml_node_struct* d = _data(); - return (d && d->value) ? d->value : def; + return (d && d->contents) ? d->contents : def; } PUGI__FN int xml_text::as_int(int def) const { xml_node_struct* d = _data(); - return impl::get_value_int(d ? PUGI__COMPACT(d->value) : 0, def); + return impl::get_value_int(d ? PUGI__COMPACT(d->contents) : 0, def); } PUGI__FN unsigned int xml_text::as_uint(unsigned int def) const { xml_node_struct* d = _data(); - return impl::get_value_uint(d ? PUGI__COMPACT(d->value) : 0, def); + return impl::get_value_uint(d ? PUGI__COMPACT(d->contents) : 0, def); } PUGI__FN double xml_text::as_double(double def) const { xml_node_struct* d = _data(); - return impl::get_value_double(d ? PUGI__COMPACT(d->value) : 0, def); + return impl::get_value_double(d ? PUGI__COMPACT(d->contents) : 0, def); } PUGI__FN float xml_text::as_float(float def) const { xml_node_struct* d = _data(); - return impl::get_value_float(d ? PUGI__COMPACT(d->value) : 0, def); + return impl::get_value_float(d ? PUGI__COMPACT(d->contents) : 0, def); } PUGI__FN bool xml_text::as_bool(bool def) const { xml_node_struct* d = _data(); - return impl::get_value_bool(d ? PUGI__COMPACT(d->value) : 0, def); + return impl::get_value_bool(d ? PUGI__COMPACT(d->contents) : 0, def); } #ifdef PUGIXML_HAS_LONG_LONG @@ -5952,14 +5971,14 @@ namespace pugi { xml_node_struct* d = _data(); - return impl::get_value_llong(d ? PUGI__COMPACT(d->value) : 0, def); + return impl::get_value_llong(d ? PUGI__COMPACT(d->contents) : 0, def); } PUGI__FN unsigned long long xml_text::as_ullong(unsigned long long def) const { xml_node_struct* d = _data(); - return impl::get_value_ullong(d ? PUGI__COMPACT(d->value) : 0, def); + return impl::get_value_ullong(d ? PUGI__COMPACT(d->contents) : 0, def); } #endif @@ -5967,35 +5986,35 @@ namespace pugi { xml_node_struct* dn = _data_new(); - return dn ? impl::strcpy_insitu(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; + return dn ? impl::strcpy_insitu(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; } PUGI__FN bool xml_text::set(int rhs) { xml_node_struct* dn = _data_new(); - return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; + return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; } PUGI__FN bool xml_text::set(unsigned int rhs) { xml_node_struct* dn = _data_new(); - return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; + return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; } PUGI__FN bool xml_text::set(double rhs) { xml_node_struct* dn = _data_new(); - return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; + return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; } PUGI__FN bool xml_text::set(bool rhs) { xml_node_struct* dn = _data_new(); - return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; + return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; } #ifdef PUGIXML_HAS_LONG_LONG @@ -6003,14 +6022,14 @@ namespace pugi { xml_node_struct* dn = _data_new(); - return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; + return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; } PUGI__FN bool xml_text::set(unsigned long long rhs) { xml_node_struct* dn = _data_new(); - return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; + return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; } #endif @@ -7358,8 +7377,7 @@ PUGI__NS_BEGIN { if ((get_document(node).header & xml_memory_page_contents_shared_mask) == 0) { - if (node->name && (node->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0) return node->name; - if (node->value && (node->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0) return node->value; + if (node->contents && (node->header & impl::xml_memory_page_contents_allocated_or_shared_mask) == 0) return node->contents; } return 0; -- cgit v1.2.3 From 43622107d745a198cf7bffa43acc7bbb190dccfd Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Tue, 7 Oct 2014 20:01:19 -0700 Subject: Remove PUGI__COMPACT helper Also remove get() methods on pointer wrappers - this makes the surface area smaller so we can create more of them easier. --- src/pugixml.cpp | 57 ++++++++++++++++++++------------------------------------- 1 file changed, 20 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 0cbf088..9d75b20 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -145,13 +145,6 @@ PUGI__NS_BEGIN PUGI__NS_END #endif -// Micro compact helper -#ifdef PUGIXML_COMPACT -# define PUGI__COMPACT(p) ((p).get()) -#else -# define PUGI__COMPACT(p) (p) -#endif - // Memory allocation PUGI__NS_BEGIN PUGI__FN void* default_allocate(size_t size) @@ -704,7 +697,7 @@ PUGI__NS_BEGIN void operator=(const compact_pointer& rhs) { - *this = rhs.get(); + *this = rhs + 0; } void operator=(T* value_) @@ -794,11 +787,6 @@ PUGI__NS_BEGIN return operator T* const(); } - T* get() const - { - return operator T* const(); - } - private: unsigned char _data; @@ -820,7 +808,7 @@ PUGI__NS_BEGIN void operator=(const compact_string& rhs) { - *this = rhs.get(); + *this = rhs + 0; } void operator=(char_t* value) @@ -886,11 +874,6 @@ PUGI__NS_BEGIN return 0; } - char_t* get() const - { - return operator char_t* const(); - } - private: unsigned char _data0; unsigned char _data1; @@ -3299,7 +3282,7 @@ PUGI__NS_BEGIN return make_parse_result(PUGI__OPTSET(parse_fragment) ? status_ok : status_no_document_element); // get last child of the root before parsing - xml_node_struct* last_root_child = root->first_child ? PUGI__COMPACT(root->first_child->prev_sibling_c) : 0; + xml_node_struct* last_root_child = root->first_child ? root->first_child->prev_sibling_c + 0 : 0; // create parser on stack xml_parser parser(alloc_); @@ -3327,7 +3310,7 @@ PUGI__NS_BEGIN return make_parse_result(status_unrecognized_tag, length - 1); // check if there are any element nodes parsed - xml_node_struct* first_root_child_parsed = last_root_child ? PUGI__COMPACT(last_root_child->next_sibling) : root->first_child; + xml_node_struct* first_root_child_parsed = last_root_child ? last_root_child->next_sibling + 0 : root->first_child; if (!PUGI__OPTSET(parse_fragment) && !has_element_node_siblings(first_root_child_parsed)) return make_parse_result(status_no_document_element, length - 1); @@ -4842,38 +4825,38 @@ namespace pugi PUGI__FN int xml_attribute::as_int(int def) const { - return impl::get_value_int(_attr ? PUGI__COMPACT(_attr->value) : 0, def); + return impl::get_value_int(_attr ? _attr->value + 0 : 0, def); } PUGI__FN unsigned int xml_attribute::as_uint(unsigned int def) const { - return impl::get_value_uint(_attr ? PUGI__COMPACT(_attr->value) : 0, def); + return impl::get_value_uint(_attr ? _attr->value + 0 : 0, def); } PUGI__FN double xml_attribute::as_double(double def) const { - return impl::get_value_double(_attr ? PUGI__COMPACT(_attr->value) : 0, def); + return impl::get_value_double(_attr ? _attr->value + 0 : 0, def); } PUGI__FN float xml_attribute::as_float(float def) const { - return impl::get_value_float(_attr ? PUGI__COMPACT(_attr->value) : 0, def); + return impl::get_value_float(_attr ? _attr->value + 0 : 0, def); } PUGI__FN bool xml_attribute::as_bool(bool def) const { - return impl::get_value_bool(_attr ? PUGI__COMPACT(_attr->value) : 0, def); + return impl::get_value_bool(_attr ? _attr->value + 0 : 0, def); } #ifdef PUGIXML_HAS_LONG_LONG PUGI__FN long long xml_attribute::as_llong(long long def) const { - return impl::get_value_llong(_attr ? PUGI__COMPACT(_attr->value) : 0, def); + return impl::get_value_llong(_attr ? _attr->value + 0 : 0, def); } PUGI__FN unsigned long long xml_attribute::as_ullong(unsigned long long def) const { - return impl::get_value_ullong(_attr ? PUGI__COMPACT(_attr->value) : 0, def); + return impl::get_value_ullong(_attr ? _attr->value + 0 : 0, def); } #endif @@ -5040,7 +5023,7 @@ namespace pugi PUGI__FN xml_node::iterator xml_node::begin() const { - return iterator(_root ? PUGI__COMPACT(_root->first_child) : 0, _root); + return iterator(_root ? _root->first_child + 0 : 0, _root); } PUGI__FN xml_node::iterator xml_node::end() const @@ -5050,7 +5033,7 @@ namespace pugi PUGI__FN xml_node::attribute_iterator xml_node::attributes_begin() const { - return attribute_iterator(_root ? PUGI__COMPACT(_root->first_attribute) : 0, _root); + return attribute_iterator(_root ? _root->first_attribute + 0 : 0, _root); } PUGI__FN xml_node::attribute_iterator xml_node::attributes_end() const @@ -5935,35 +5918,35 @@ namespace pugi { xml_node_struct* d = _data(); - return impl::get_value_int(d ? PUGI__COMPACT(d->contents) : 0, def); + return impl::get_value_int(d ? d->contents + 0 : 0, def); } PUGI__FN unsigned int xml_text::as_uint(unsigned int def) const { xml_node_struct* d = _data(); - return impl::get_value_uint(d ? PUGI__COMPACT(d->contents) : 0, def); + return impl::get_value_uint(d ? d->contents + 0 : 0, def); } PUGI__FN double xml_text::as_double(double def) const { xml_node_struct* d = _data(); - return impl::get_value_double(d ? PUGI__COMPACT(d->contents) : 0, def); + return impl::get_value_double(d ? d->contents + 0 : 0, def); } PUGI__FN float xml_text::as_float(float def) const { xml_node_struct* d = _data(); - return impl::get_value_float(d ? PUGI__COMPACT(d->contents) : 0, def); + return impl::get_value_float(d ? d->contents + 0 : 0, def); } PUGI__FN bool xml_text::as_bool(bool def) const { xml_node_struct* d = _data(); - return impl::get_value_bool(d ? PUGI__COMPACT(d->contents) : 0, def); + return impl::get_value_bool(d ? d->contents + 0 : 0, def); } #ifdef PUGIXML_HAS_LONG_LONG @@ -5971,14 +5954,14 @@ namespace pugi { xml_node_struct* d = _data(); - return impl::get_value_llong(d ? PUGI__COMPACT(d->contents) : 0, def); + return impl::get_value_llong(d ? d->contents + 0 : 0, def); } PUGI__FN unsigned long long xml_text::as_ullong(unsigned long long def) const { xml_node_struct* d = _data(); - return impl::get_value_ullong(d ? PUGI__COMPACT(d->contents) : 0, def); + return impl::get_value_ullong(d ? d->contents + 0 : 0, def); } #endif -- cgit v1.2.3 From 80d6f5a7d0e1e60b928573d783192186613a42a8 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Tue, 7 Oct 2014 20:16:32 -0700 Subject: Rework compact_pointer implementation Split the implementation into a generic one with adjustable range and a special implementation for parent (may need to use 2 bytes on that one later). Optimize compact_string and compact_pointer to use minimal amount of math and move slow hash paths into no-inline functions so that compiler can inline the fast-paths. Merge compact_pointer_generic and compact_pointer_forward and optimize. --- src/pugixml.cpp | 186 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 103 insertions(+), 83 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 9d75b20..bdb8425 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -639,6 +639,8 @@ PUGI__NS_BEGIN } }; + static const unsigned int compact_alignment_log2 = 2; + typedef uint32_t compact_alignment; class compact_header @@ -681,14 +683,24 @@ PUGI__NS_BEGIN unsigned char flags; }; - xml_memory_page* compact_get_page(const void* object, int header_offset) + PUGI__FN xml_memory_page* compact_get_page(const void* object, int header_offset) { const compact_header* header = reinterpret_cast(static_cast(object) - header_offset); return header->get_page(); } - template class compact_pointer + template PUGI__FN_NO_INLINE T* compact_get_value(const void* object) + { + return static_cast(*compact_get_page(object, header_offset)->allocator->_hash->find(object)); + } + + template PUGI__FN_NO_INLINE void compact_set_value(const void* object, T* value) + { + *compact_get_page(object, header_offset)->allocator->_hash->insert(object, tag) = value; + } + + template class compact_pointer { public: compact_pointer(): _data(0) @@ -700,6 +712,63 @@ PUGI__NS_BEGIN *this = rhs + 0; } + void operator=(T* value) + { + if (value) + { + ptrdiff_t offset = ((reinterpret_cast(value) - reinterpret_cast(this) + int(sizeof(compact_alignment) - 1)) >> compact_alignment_log2) - start; + + if (static_cast(offset) <= 253) + _data = static_cast(offset + 1); + else + { + compact_set_value(this, value); + + _data = 255; + } + } + else + _data = 0; + } + + operator T* const() const + { + if (_data) + { + if (_data < 255) + { + uintptr_t base = reinterpret_cast(this) & ~(sizeof(compact_alignment) - 1); + + return reinterpret_cast(base + ((_data - (1 - start)) << compact_alignment_log2)); + } + else + return compact_get_value(this); + } + else + return 0; + } + + T* operator->() const + { + return operator T* const(); + } + + private: + unsigned char _data; + }; + + template class compact_pointer_parent + { + public: + compact_pointer_parent(): _data(0) + { + } + + void operator=(const compact_pointer_parent& rhs) + { + *this = rhs + 0; + } + void operator=(T* value_) { if (value_) @@ -707,49 +776,24 @@ PUGI__NS_BEGIN compact_alignment* base = get_base(); compact_alignment* value = reinterpret_cast(value_); - if (direction > 0) + if (value <= base && value >= base - 252) + _data = static_cast((base - value) + 1); + else { - if (value >= base && value <= base + 253) - _data = static_cast((value - base) + 1); - else - { - *compact_get_page(this, header_offset)->allocator->_hash->insert(this, tag) = value; + xml_memory_page* page = compact_get_page(this, header_offset); - _data = 255; + if (page->compact_parent == 0) + { + page->compact_parent = value; + _data = 254; } - } - else if (direction < 0) - { - if (value <= base && value >= base - 252) - _data = static_cast((base - value) + 1); - else + else if (page->compact_parent == value) { - xml_memory_page* page = compact_get_page(this, header_offset); - - if (page->compact_parent == 0) - { - page->compact_parent = value; - _data = 254; - } - else if (page->compact_parent == value) - { - _data = 254; - } - else - { - *page->allocator->_hash->insert(this, tag) = value; - _data = 255; - } + _data = 254; } - } - else - { - if (value >= base - 126 && value <= base + 127) - _data = static_cast((value - base) + 127); else { - *compact_get_page(this, header_offset)->allocator->_hash->insert(this, tag) = value; - + compact_set_value(this, value); _data = 255; } } @@ -763,19 +807,14 @@ PUGI__NS_BEGIN if (_data) { if (_data == 255) - return static_cast(*compact_get_page(this, header_offset)->allocator->_hash->find(this)); - else if (direction < 0 && _data == 254) + return compact_get_value(this); + else if (_data == 254) return static_cast(compact_get_page(this, header_offset)->compact_parent); else { compact_alignment* base = get_base(); - if (direction > 0) - return reinterpret_cast(base + (_data - 1)); - else if (direction < 0) - return reinterpret_cast(base - (_data - 1)); - else - return reinterpret_cast(base + (_data - 127)); + return reinterpret_cast(base - (_data - 1)); } } else @@ -792,10 +831,7 @@ PUGI__NS_BEGIN compact_alignment* get_base() const { - if (direction > 0) - return reinterpret_cast((reinterpret_cast(this) + sizeof(compact_alignment)) & ~(sizeof(compact_alignment) - 1)); - else - return reinterpret_cast(reinterpret_cast(this) & ~(sizeof(compact_alignment) - 1)); + return reinterpret_cast(reinterpret_cast(this) & ~(sizeof(compact_alignment) - 1)); } }; @@ -818,32 +854,20 @@ PUGI__NS_BEGIN xml_memory_page* page = compact_get_page(this, header_offset); if (page->compact_string_base == 0) - { page->compact_string_base = value; - _data0 = 1; - _data1 = 0; - _data2 = 0; - } - else - { - ptrdiff_t offset = value - page->compact_string_base; + ptrdiff_t offset = value - page->compact_string_base; - if (offset >= 0 && offset <= 1677213) - { - _data0 = static_cast(offset + 1); - _data1 = static_cast((offset + 1) >> 8); - _data2 = static_cast((offset + 1) >> 16); - } - else - { - *page->allocator->_hash->insert(this, tag) = value; + if (static_cast(offset) >= 16777213) + { + compact_set_value(this, value); - _data0 = 255; - _data1 = 255; - _data2 = 255; - } + offset = 16777214; } + + _data0 = static_cast(offset + 1); + _data1 = static_cast((offset + 1) >> 8); + _data2 = static_cast((offset + 1) >> 16); } else { @@ -862,13 +886,9 @@ PUGI__NS_BEGIN xml_memory_page* page = compact_get_page(this, header_offset); if (data < 16777215) - { return page->compact_string_base + (data - 1); - } else - { - return static_cast(*page->allocator->_hash->find(this)); - } + return compact_get_value(this); } else return 0; @@ -904,8 +924,8 @@ namespace pugi impl::compact_string<4, /*tag*/11> name; ///< Pointer to attribute name. impl::compact_string<7, /*tag*/12> value; ///< Pointer to attribute value. - impl::compact_pointer prev_attribute_c; ///< Previous attribute (cyclic list) - impl::compact_pointer next_attribute; ///< Next attribute + impl::compact_pointer prev_attribute_c; ///< Previous attribute (cyclic list) + impl::compact_pointer next_attribute; ///< Next attribute }; /// An XML document tree node. @@ -926,14 +946,14 @@ namespace pugi impl::compact_string<4, /*tag*/21> contents; ///< Pointer to element name. - impl::compact_pointer parent; ///< Pointer to parent + impl::compact_pointer_parent parent; ///< Pointer to parent - impl::compact_pointer first_child; ///< First child + impl::compact_pointer first_child; ///< First child - impl::compact_pointer prev_sibling_c; ///< Left brother (cyclic list) - impl::compact_pointer next_sibling; ///< Right brother + impl::compact_pointer prev_sibling_c; ///< Left brother (cyclic list) + impl::compact_pointer next_sibling; ///< Right brother - impl::compact_pointer first_attribute; ///< First attribute + impl::compact_pointer first_attribute; ///< First attribute }; } #else -- cgit v1.2.3 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') 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 From 224e702d1f879a9ecd784289b42720a278ee3e01 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 8 Oct 2014 20:09:29 -0700 Subject: Change compact_pointer_parent to use 2 bytes Parent pointers need to be able to reach everywhere within a page to minimize shared parent pointer reuse unless it's absolutely necessary. This reduces parent hash utilization on all test cases to <1%. Rename compact_parent to compact_shared_parent. --- src/pugixml.cpp | 57 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 1694a68..0853776 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -297,7 +297,7 @@ PUGI__NS_BEGIN #ifdef PUGIXML_COMPACT result->compact_string_base = 0; - result->compact_parent = 0; + result->compact_shared_parent = 0; #endif return result; @@ -313,7 +313,7 @@ PUGI__NS_BEGIN #ifdef PUGIXML_COMPACT char_t* compact_string_base; - void* compact_parent; + void* compact_shared_parent; #endif }; @@ -759,7 +759,7 @@ PUGI__NS_BEGIN template class compact_pointer_parent { public: - compact_pointer_parent(): _data(0) + compact_pointer_parent(): _data0(0), _data1(0) { } @@ -772,43 +772,55 @@ PUGI__NS_BEGIN { if (value) { - ptrdiff_t offset = ((reinterpret_cast(value) - reinterpret_cast(this) + int(compact_alignment - 1)) >> compact_alignment_log2) + 253; + ptrdiff_t offset = ((reinterpret_cast(value) - reinterpret_cast(this) + int(compact_alignment - 1)) >> compact_alignment_log2) + 65533; - if (static_cast(offset) <= 253) - _data = static_cast(offset + 1); + if (static_cast(offset) <= 65533) + { + _data0 = static_cast(offset + 1); + _data1 = static_cast((offset + 1) >> 8); + } else { xml_memory_page* page = compact_get_page(this, header_offset); - if (page->compact_parent == 0) - page->compact_parent = value; + if (page->compact_shared_parent == 0) + page->compact_shared_parent = value; - if (page->compact_parent == value) - _data = 254; + if (page->compact_shared_parent == value) + { + _data0 = 254; + _data1 = 255; + } else { compact_set_value(this, value); - _data = 255; + _data0 = 255; + _data1 = 255; } } } else - _data = 0; + { + _data0 = 0; + _data1 = 0; + } } operator T* const() const { - if (_data) + unsigned int data = _data0 + (_data1 << 8); + + if (data) { - if (_data < 254) + if (data < 65534) { uintptr_t base = reinterpret_cast(this) & ~(compact_alignment - 1); - return reinterpret_cast(base + ((_data - (1 + 253)) << compact_alignment_log2)); + return reinterpret_cast(base + ((data - (1 + 65533)) << compact_alignment_log2)); } - else if (_data == 254) - return static_cast(compact_get_page(this, header_offset)->compact_parent); + else if (data == 65534) + return static_cast(compact_get_page(this, header_offset)->compact_shared_parent); else return compact_get_value(this); } @@ -822,7 +834,8 @@ PUGI__NS_BEGIN } private: - unsigned char _data; + unsigned char _data0; + unsigned char _data1; }; template class compact_string @@ -932,15 +945,13 @@ namespace pugi impl::compact_header header; - unsigned char padding; - - impl::compact_string<4, /*tag*/21> contents; ///< Pointer to element name. + impl::compact_string<3, /*tag*/21> contents; ///< Pointer to element name. - impl::compact_pointer_parent parent; ///< Pointer to parent + impl::compact_pointer_parent parent; ///< Pointer to parent impl::compact_pointer first_child; ///< First child - impl::compact_pointer prev_sibling_c; ///< Left brother (cyclic list) + impl::compact_pointer prev_sibling_c; ///< Left brother (cyclic list) impl::compact_pointer next_sibling; ///< Right brother impl::compact_pointer first_attribute; ///< First attribute -- cgit v1.2.3 From fbaab4dcad9eafc2c17f8030bb3c924fca644795 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Fri, 10 Oct 2014 19:29:25 -0700 Subject: Move compact_hash_table before xml_allocator. This helps streamline class dependencies and will make subsequent changes smaller. --- src/pugixml.cpp | 266 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 135 insertions(+), 131 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index d2124a6..677ae7b 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -257,6 +257,140 @@ PUGI__NS_BEGIN PUGI__NS_END #endif +#ifdef PUGIXML_COMPACT +size_t pugi_compact_stats[128]; + +PUGI__NS_BEGIN + class compact_hash_table + { + public: + compact_hash_table(): _items(0), _capacity(0), _count(0) + { + } + + void clear() + { + if (_items) + { + xml_memory::deallocate(_items); + _items = 0; + _capacity = 0; + _count = 0; + } + } + + void** find(const void* key) + { + assert(key); + + if (_capacity == 0) return 0; + + size_t hashmod = _capacity - 1; + size_t bucket = hash(key) & hashmod; + + for (size_t probe = 0; probe <= hashmod; ++probe) + { + item_t& probe_item = _items[bucket]; + + if (probe_item.key == key) + return &probe_item.value; + + if (probe_item.key == 0) + return 0; + + // hash collision, quadratic probing + bucket = (bucket + probe + 1) & hashmod; + } + + assert(!"Hash table is full"); + return 0; + } + + void** insert(const void* key, size_t tag) + { + assert(key); + + if (_count >= _capacity * 3 / 4) + rehash(); + + size_t hashmod = _capacity - 1; + size_t bucket = hash(key) & hashmod; + + for (size_t probe = 0; probe <= hashmod; ++probe) + { + item_t& probe_item = _items[bucket]; + + if (probe_item.key == 0) + { + if (tag) + pugi_compact_stats[tag]++; + + probe_item.key = key; + _count++; + return &probe_item.value; + } + + if (probe_item.key == key) + return &probe_item.value; + + // hash collision, quadratic probing + bucket = (bucket + probe + 1) & hashmod; + } + + assert(!"Hash table is full"); + return 0; + } + + private: + struct item_t + { + const void* key; + void* value; + }; + + item_t* _items; + size_t _capacity; + + size_t _count; + + void rehash() + { + compact_hash_table rt; + rt._capacity = (_capacity == 0) ? 256 : _capacity * 2; + rt._items = static_cast(xml_memory::allocate(sizeof(item_t) * rt._capacity)); + + assert(rt._items); + + memset(rt._items, 0, sizeof(item_t) * rt._capacity); + + for (size_t i = 0; i < _capacity; ++i) + if (_items[i].key) + *rt.insert(_items[i].key, 0) = _items[i].value; + + if (_items) + xml_memory::deallocate(_items); + + _capacity = rt._capacity; + _items = rt._items; + } + + // http://burtleburtle.net/bob/hash/integer.html + static unsigned int hash(const void* key) + { + unsigned int a = static_cast(reinterpret_cast(key)); + + a = (a ^ 61) ^ (a >> 16); + a = a + (a << 3); + a = a ^ (a >> 4); + a = a * 0x27d4eb2d; + a = a ^ (a >> 15); + + return a; + } + }; +PUGI__NS_END +#endif + PUGI__NS_BEGIN static const size_t xml_memory_page_size = #ifdef PUGIXML_MEMORY_PAGE_SIZE @@ -463,7 +597,7 @@ PUGI__NS_BEGIN size_t _busy_size; #ifdef PUGIXML_COMPACT - class compact_hash_table* _hash; + compact_hash_table* _hash; #endif }; @@ -508,137 +642,7 @@ PUGI__NS_BEGIN PUGI__NS_END #ifdef PUGIXML_COMPACT -size_t pugi_compact_stats[128]; - PUGI__NS_BEGIN - class compact_hash_table - { - public: - compact_hash_table(): _items(0), _capacity(0), _count(0) - { - } - - void clear() - { - if (_items) - { - xml_memory::deallocate(_items); - _items = 0; - _capacity = 0; - _count = 0; - } - } - - void** find(const void* key) - { - assert(key); - - if (_capacity == 0) return 0; - - size_t hashmod = _capacity - 1; - size_t bucket = hash(key) & hashmod; - - for (size_t probe = 0; probe <= hashmod; ++probe) - { - item_t& probe_item = _items[bucket]; - - if (probe_item.key == key) - return &probe_item.value; - - if (probe_item.key == 0) - return 0; - - // hash collision, quadratic probing - bucket = (bucket + probe + 1) & hashmod; - } - - assert(!"Hash table is full"); - return 0; - } - - void** insert(const void* key, size_t tag) - { - assert(key); - - if (_count >= _capacity * 3 / 4) - rehash(); - - size_t hashmod = _capacity - 1; - size_t bucket = hash(key) & hashmod; - - for (size_t probe = 0; probe <= hashmod; ++probe) - { - item_t& probe_item = _items[bucket]; - - if (probe_item.key == 0) - { - if (tag) - pugi_compact_stats[tag]++; - - probe_item.key = key; - _count++; - return &probe_item.value; - } - - if (probe_item.key == key) - return &probe_item.value; - - // hash collision, quadratic probing - bucket = (bucket + probe + 1) & hashmod; - } - - assert(!"Hash table is full"); - return 0; - } - - private: - struct item_t - { - const void* key; - void* value; - }; - - item_t* _items; - size_t _capacity; - - size_t _count; - - void rehash() - { - compact_hash_table rt; - rt._capacity = (_capacity == 0) ? 256 : _capacity * 2; - rt._items = static_cast(xml_memory::allocate(sizeof(item_t) * rt._capacity)); - - assert(rt._items); - - memset(rt._items, 0, sizeof(item_t) * rt._capacity); - - for (size_t i = 0; i < _capacity; ++i) - if (_items[i].key) - *rt.insert(_items[i].key, 0) = _items[i].value; - - if (_items) - xml_memory::deallocate(_items); - - _capacity = rt._capacity; - _items = rt._items; - } - - // http://burtleburtle.net/bob/hash/integer.html - static unsigned int hash(const void* key) - { - unsigned int a = static_cast(reinterpret_cast(key)); - - a = (a ^ 61) ^ (a >> 16); - a = a + (a << 3); - a = a ^ (a >> 4); - a = a * 0x27d4eb2d; - a = a ^ (a >> 15); - - return a; - } - }; - static const unsigned int compact_alignment_log2 = 2; static const unsigned int compact_alignment = 1 << compact_alignment_log2; -- cgit v1.2.3 From e6dd761ca36a6c27a8bc960d09767787aa997836 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Fri, 10 Oct 2014 19:31:30 -0700 Subject: Split hash table operations into reserve and insert Insert is now unsafe - since we don't have a way to handle rehash() failures transparently we need to reserve space beforehand. Reserve is now called before every tree-mutating operations and it guarantees that we can perform 16 arbitrary pointer mutations after that. This fixes all test crashes with compact mode. --- src/pugixml.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 82 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 677ae7b..7a8d7c1 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -309,9 +309,7 @@ PUGI__NS_BEGIN void** insert(const void* key, size_t tag) { assert(key); - - if (_count >= _capacity * 3 / 4) - rehash(); + assert(_count < _capacity * 3 / 4); size_t hashmod = _capacity - 1; size_t bucket = hash(key) & hashmod; @@ -341,6 +339,14 @@ PUGI__NS_BEGIN return 0; } + bool reserve() + { + if (_count + 16 >= _capacity * 3 / 4) + return rehash(); + + return true; + } + private: struct item_t { @@ -353,13 +359,14 @@ PUGI__NS_BEGIN size_t _count; - void rehash() + bool rehash() { compact_hash_table rt; - rt._capacity = (_capacity == 0) ? 256 : _capacity * 2; + rt._capacity = (_capacity == 0) ? 32 : _capacity * 2; rt._items = static_cast(xml_memory::allocate(sizeof(item_t) * rt._capacity)); - assert(rt._items); + if (!rt._items) + return false; memset(rt._items, 0, sizeof(item_t) * rt._capacity); @@ -372,6 +379,8 @@ PUGI__NS_BEGIN _capacity = rt._capacity; _items = rt._items; + + return true; } // http://burtleburtle.net/bob/hash/integer.html @@ -593,6 +602,15 @@ PUGI__NS_BEGIN deallocate_memory(header, full_size, page); } + bool reserve() + { + #ifdef PUGIXML_COMPACT + return _hash->reserve(); + #else + return true; + #endif + } + xml_memory_page* _root; size_t _busy_size; @@ -1269,6 +1287,8 @@ PUGI__NS_BEGIN PUGI__FN_NO_INLINE xml_node_struct* append_new_node(xml_node_struct* node, xml_allocator& alloc, xml_node_type type = node_element) { + if (!alloc.reserve()) return 0; + xml_node_struct* child = allocate_node(alloc, type); if (!child) return 0; @@ -1279,6 +1299,8 @@ PUGI__NS_BEGIN PUGI__FN_NO_INLINE xml_attribute_struct* append_new_attribute(xml_node_struct* node, xml_allocator& alloc) { + if (!alloc.reserve()) return 0; + xml_attribute_struct* attr = allocate_attribute(alloc); if (!attr) return 0; @@ -2252,6 +2274,8 @@ PUGI__NS_BEGIN { xml_allocator* alloc = reinterpret_cast(header & xml_memory_page_pointer_mask)->allocator; + if (!alloc->reserve()) return false; + // allocate new buffer char_t* buf = alloc->allocate_string(source_length + 1); if (!buf) return false; @@ -5345,7 +5369,10 @@ namespace pugi { if (type() != node_element && type() != node_declaration) return xml_attribute(); - xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root))); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_attribute(); + + xml_attribute a(impl::allocate_attribute(alloc)); if (!a) return xml_attribute(); impl::append_attribute(a._attr, _root); @@ -5359,7 +5386,10 @@ namespace pugi { if (type() != node_element && type() != node_declaration) return xml_attribute(); - xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root))); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_attribute(); + + xml_attribute a(impl::allocate_attribute(alloc)); if (!a) return xml_attribute(); impl::prepend_attribute(a._attr, _root); @@ -5374,7 +5404,10 @@ namespace pugi if (type() != node_element && type() != node_declaration) return xml_attribute(); if (!attr || !impl::is_attribute_of(attr._attr, _root)) return xml_attribute(); - xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root))); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_attribute(); + + xml_attribute a(impl::allocate_attribute(alloc)); if (!a) return xml_attribute(); impl::insert_attribute_before(a._attr, attr._attr, _root); @@ -5389,7 +5422,10 @@ namespace pugi if (type() != node_element && type() != node_declaration) return xml_attribute(); if (!attr || !impl::is_attribute_of(attr._attr, _root)) return xml_attribute(); - xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root))); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_attribute(); + + xml_attribute a(impl::allocate_attribute(alloc)); if (!a) return xml_attribute(); impl::insert_attribute_after(a._attr, attr._attr, _root); @@ -5443,7 +5479,10 @@ namespace pugi { if (!impl::allow_insert_child(this->type(), type_)) return xml_node(); - xml_node n(impl::allocate_node(impl::get_allocator(_root), type_)); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_node(); + + xml_node n(impl::allocate_node(alloc, type_)); if (!n) return xml_node(); impl::append_node(n._root, _root); @@ -5456,8 +5495,11 @@ namespace pugi PUGI__FN xml_node xml_node::prepend_child(xml_node_type type_) { if (!impl::allow_insert_child(this->type(), type_)) return xml_node(); + + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_node(); - xml_node n(impl::allocate_node(impl::get_allocator(_root), type_)); + xml_node n(impl::allocate_node(alloc, type_)); if (!n) return xml_node(); impl::prepend_node(n._root, _root); @@ -5471,8 +5513,11 @@ namespace pugi { if (!impl::allow_insert_child(this->type(), type_)) return xml_node(); if (!node._root || node._root->parent != _root) return xml_node(); + + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_node(); - xml_node n(impl::allocate_node(impl::get_allocator(_root), type_)); + xml_node n(impl::allocate_node(alloc, type_)); if (!n) return xml_node(); impl::insert_node_before(n._root, node._root); @@ -5486,8 +5531,11 @@ namespace pugi { if (!impl::allow_insert_child(this->type(), type_)) return xml_node(); if (!node._root || node._root->parent != _root) return xml_node(); + + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_node(); - xml_node n(impl::allocate_node(impl::get_allocator(_root), type_)); + xml_node n(impl::allocate_node(alloc, type_)); if (!n) return xml_node(); impl::insert_node_after(n._root, node._root); @@ -5573,6 +5621,9 @@ namespace pugi { if (!impl::allow_move(*this, moved)) return xml_node(); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_node(); + // disable document_buffer_order optimization since moving nodes around changes document order without changing buffer pointers impl::get_document(_root).header |= impl::xml_memory_page_contents_shared_mask; @@ -5586,6 +5637,9 @@ namespace pugi { if (!impl::allow_move(*this, moved)) return xml_node(); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_node(); + // disable document_buffer_order optimization since moving nodes around changes document order without changing buffer pointers impl::get_document(_root).header |= impl::xml_memory_page_contents_shared_mask; @@ -5601,6 +5655,9 @@ namespace pugi if (!node._root || node._root->parent != _root) return xml_node(); if (moved._root == node._root) return xml_node(); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_node(); + // disable document_buffer_order optimization since moving nodes around changes document order without changing buffer pointers impl::get_document(_root).header |= impl::xml_memory_page_contents_shared_mask; @@ -5616,6 +5673,9 @@ namespace pugi if (!node._root || node._root->parent != _root) return xml_node(); if (moved._root == node._root) return xml_node(); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_node(); + // disable document_buffer_order optimization since moving nodes around changes document order without changing buffer pointers impl::get_document(_root).header |= impl::xml_memory_page_contents_shared_mask; @@ -5635,8 +5695,11 @@ namespace pugi if (!_root || !a._attr) return false; if (!impl::is_attribute_of(a._attr, _root)) return false; + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return false; + impl::remove_attribute(a._attr, _root); - impl::destroy_attribute(a._attr, impl::get_allocator(_root)); + impl::destroy_attribute(a._attr, alloc); return true; } @@ -5650,8 +5713,11 @@ namespace pugi { if (!_root || !n._root || n._root->parent != _root) return false; + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return false; + impl::remove_node(n._root); - impl::destroy_node(n._root, impl::get_allocator(_root)); + impl::destroy_node(n._root, alloc); return true; } -- cgit v1.2.3 From 52371bf5fea2ddd316857c5005dd384d7edf78ec Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 11 Oct 2014 00:55:39 -0700 Subject: Compact implementation refactoring Remove compact stats and tags, replace pointer hash with murmur3 32-bit finalizer. --- src/pugixml.cpp | 67 +++++++++++++++++++++++++-------------------------------- 1 file changed, 29 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 748ef2b..f496b37 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -258,8 +258,6 @@ PUGI__NS_END #endif #ifdef PUGIXML_COMPACT -size_t pugi_compact_stats[128]; - PUGI__NS_BEGIN class compact_hash_table { @@ -306,7 +304,7 @@ PUGI__NS_BEGIN return 0; } - void** insert(const void* key, size_t tag) + void** insert(const void* key) { assert(key); assert(_count < _capacity * 3 / 4); @@ -320,9 +318,6 @@ PUGI__NS_BEGIN if (probe_item.key == 0) { - if (tag) - pugi_compact_stats[tag]++; - probe_item.key = key; _count++; return &probe_item.value; @@ -372,7 +367,7 @@ PUGI__NS_BEGIN for (size_t i = 0; i < _capacity; ++i) if (_items[i].key) - *rt.insert(_items[i].key, 0) = _items[i].value; + *rt.insert(_items[i].key) = _items[i].value; if (_items) xml_memory::deallocate(_items); @@ -383,18 +378,18 @@ PUGI__NS_BEGIN return true; } - // http://burtleburtle.net/bob/hash/integer.html static unsigned int hash(const void* key) { - unsigned int a = static_cast(reinterpret_cast(key)); + unsigned int h = static_cast(reinterpret_cast(key)); - a = (a ^ 61) ^ (a >> 16); - a = a + (a << 3); - a = a ^ (a >> 4); - a = a * 0x27d4eb2d; - a = a ^ (a >> 15); + // MurmurHash3 32-bit finalizer + h ^= h >> 16; + h *= 0x85ebca6bu; + h ^= h >> 13; + h *= 0xc2b2ae35u; + h ^= h >> 16; - return a; + return h; } }; PUGI__NS_END @@ -716,12 +711,12 @@ PUGI__NS_BEGIN return static_cast(*compact_get_page(object, header_offset)->allocator->_hash->find(object)); } - template PUGI__FN_NO_INLINE void compact_set_value(const void* object, T* value) + template PUGI__FN_NO_INLINE void compact_set_value(const void* object, T* value) { - *compact_get_page(object, header_offset)->allocator->_hash->insert(object, tag) = value; + *compact_get_page(object, header_offset)->allocator->_hash->insert(object) = value; } - template class compact_pointer + template class compact_pointer { public: compact_pointer(): _data(0) @@ -743,7 +738,7 @@ PUGI__NS_BEGIN _data = static_cast(offset + 1); else { - compact_set_value(this, value); + compact_set_value(this, value); _data = 255; } @@ -778,7 +773,7 @@ PUGI__NS_BEGIN unsigned char _data; }; - template class compact_pointer_parent + template class compact_pointer_parent { public: compact_pointer_parent(): _data0(0), _data1(0) @@ -815,7 +810,7 @@ PUGI__NS_BEGIN } else { - compact_set_value(this, value); + compact_set_value(this, value); _data0 = 255; _data1 = 255; @@ -860,7 +855,7 @@ PUGI__NS_BEGIN unsigned char _data1; }; - template class compact_string + template class compact_string { public: compact_string(): _data0(0), _data1(0), _data2(0) @@ -885,7 +880,7 @@ PUGI__NS_BEGIN if (static_cast(offset) >= 16777213) { - compact_set_value(this, value); + compact_set_value(this, value); offset = 16777214; } @@ -938,19 +933,17 @@ namespace pugi xml_attribute_struct(impl::xml_memory_page* page): header(page, 0) { PUGI__STATIC_ASSERT(sizeof(xml_attribute_struct) == 12); - - pugi_compact_stats[10]++; } impl::compact_header header; unsigned char padding; - impl::compact_string<4, /*tag*/11> name; ///< Pointer to attribute name. - impl::compact_string<7, /*tag*/12> value; ///< Pointer to attribute value. + impl::compact_string<4> name; ///< Pointer to attribute name. + impl::compact_string<7> value; ///< Pointer to attribute value. - impl::compact_pointer prev_attribute_c; ///< Previous attribute (cyclic list) - impl::compact_pointer next_attribute; ///< Next attribute + impl::compact_pointer prev_attribute_c; ///< Previous attribute (cyclic list) + impl::compact_pointer next_attribute; ///< Next attribute }; /// An XML document tree node. @@ -961,22 +954,20 @@ namespace pugi xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(page, type - 1) { PUGI__STATIC_ASSERT(sizeof(xml_node_struct) == 12); - - pugi_compact_stats[20]++; } impl::compact_header header; - impl::compact_string<3, /*tag*/21> contents; ///< Pointer to element name. + impl::compact_string<3> contents; ///< Pointer to element name. - impl::compact_pointer_parent parent; ///< Pointer to parent + impl::compact_pointer_parent parent; ///< Pointer to parent - impl::compact_pointer first_child; ///< First child + impl::compact_pointer first_child; ///< First child - impl::compact_pointer prev_sibling_c; ///< Left brother (cyclic list) - impl::compact_pointer next_sibling; ///< Right brother + impl::compact_pointer prev_sibling_c; ///< Left brother (cyclic list) + impl::compact_pointer next_sibling; ///< Right brother - impl::compact_pointer first_attribute; ///< First attribute + impl::compact_pointer first_attribute; ///< First attribute }; } #else @@ -992,7 +983,7 @@ namespace pugi uintptr_t header; - char_t* name; ///< Pointer to attribute name. + char_t* name; ///< Pointer to attribute name. char_t* value; ///< Pointer to attribute value. xml_attribute_struct* prev_attribute_c; ///< Previous attribute (cyclic list) -- cgit v1.2.3 From fcd1876a21593bd62580383874b57c734d629a0c Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 11 Oct 2014 01:35:24 -0700 Subject: Fix compact mode for 64-bit architectures --- src/pugixml.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index f496b37..ccc8276 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -656,8 +656,8 @@ PUGI__NS_END #ifdef PUGIXML_COMPACT PUGI__NS_BEGIN - static const unsigned int compact_alignment_log2 = 2; - static const unsigned int compact_alignment = 1 << compact_alignment_log2; + static const uintptr_t compact_alignment_log2 = 2; + static const uintptr_t compact_alignment = 1 << compact_alignment_log2; class compact_header { @@ -672,12 +672,12 @@ PUGI__NS_BEGIN this->flags = static_cast(flags); } - void operator&=(unsigned int modflags) + void operator&=(uintptr_t modflags) { flags &= modflags; } - void operator|=(unsigned int modflags) + void operator|=(uintptr_t modflags) { flags |= modflags; } @@ -826,7 +826,7 @@ PUGI__NS_BEGIN operator T* const() const { - unsigned int data = _data0 + (_data1 << 8); + int data = _data0 + (_data1 << 8); if (data) { -- cgit v1.2.3 From 21695288ecb32358034de0eaf56408cc9b994f86 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Tue, 21 Oct 2014 23:32:44 -0700 Subject: Fix compact mode --- src/pugixml.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index c8dfb6c..2f1fe81 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -5004,12 +5004,12 @@ namespace pugi PUGI__FN const char_t* xml_attribute::name() const { - return (_attr && _attr->name) ? _attr->name : PUGIXML_TEXT(""); + return (_attr && _attr->name) ? _attr->name + 0 : PUGIXML_TEXT(""); } PUGI__FN const char_t* xml_attribute::value() const { - return (_attr && _attr->value) ? _attr->value : PUGIXML_TEXT(""); + return (_attr && _attr->value) ? _attr->value + 0 : PUGIXML_TEXT(""); } PUGI__FN size_t xml_attribute::hash_value() const @@ -5230,7 +5230,7 @@ namespace pugi PUGI__FN const char_t* xml_node::name() const { - return (_root && impl::has_name(_root) && _root->contents) ? _root->contents : PUGIXML_TEXT(""); + return (_root && impl::has_name(_root) && _root->contents) ? _root->contents + 0 : PUGIXML_TEXT(""); } PUGI__FN xml_node_type xml_node::type() const @@ -5240,7 +5240,7 @@ namespace pugi PUGI__FN const char_t* xml_node::value() const { - return (_root && impl::has_value(_root) && _root->contents) ? _root->contents : PUGIXML_TEXT(""); + return (_root && impl::has_value(_root) && _root->contents) ? _root->contents + 0 : PUGIXML_TEXT(""); } PUGI__FN xml_node xml_node::child(const char_t* name_) const @@ -5785,7 +5785,7 @@ namespace pugi if (impl::has_name(i) && i->contents && impl::strequal(name_, i->contents)) { for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute) - if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value : PUGIXML_TEXT(""))) + if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value + 0 : PUGIXML_TEXT(""))) return xml_node(i); } @@ -5798,7 +5798,7 @@ namespace pugi for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute) - if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value : PUGIXML_TEXT(""))) + if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value + 0 : PUGIXML_TEXT(""))) return xml_node(i); return xml_node(); @@ -6037,7 +6037,7 @@ namespace pugi { xml_node_struct* d = _data(); - return (d && d->contents) ? d->contents : PUGIXML_TEXT(""); + return (d && d->contents) ? d->contents + 0 : PUGIXML_TEXT(""); } PUGI__FN const char_t* xml_text::as_string(const char_t* def) const @@ -9083,7 +9083,7 @@ PUGI__NS_BEGIN { assert(a); - const char_t* name = a->name ? a->name : PUGIXML_TEXT(""); + const char_t* name = a->name ? a->name + 0 : PUGIXML_TEXT(""); switch (_test) { @@ -9128,7 +9128,7 @@ PUGI__NS_BEGIN switch (_test) { case nodetest_name: - if (type == node_element && n->name && strequal(n->name, _data.nodetest)) + if (type == node_element && n->contents && strequal(n->contents, _data.nodetest)) { ns.push_back(xml_node(n), alloc); return true; @@ -9164,7 +9164,7 @@ PUGI__NS_BEGIN break; case nodetest_pi: - if (type == node_pi && n->name && strequal(n->name, _data.nodetest)) + if (type == node_pi && n->contents && strequal(n->contents, _data.nodetest)) { ns.push_back(xml_node(n), alloc); return true; @@ -9180,7 +9180,7 @@ PUGI__NS_BEGIN break; case nodetest_all_in_namespace: - if (type == node_element && n->name && starts_with(n->name, _data.nodetest)) + if (type == node_element && n->contents && starts_with(n->contents, _data.nodetest)) { ns.push_back(xml_node(n), alloc); return true; -- cgit v1.2.3 From 650c67a663515a3b76144a7dc16b26df0a8f2372 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Tue, 28 Oct 2014 20:14:17 -0700 Subject: Fix compilation after merge. --- src/pugixml.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index b565482..163af21 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -3968,7 +3968,7 @@ PUGI__NS_BEGIN PUGI__FN bool node_output_start(xml_buffered_writer& writer, xml_node_struct* node, unsigned int flags) { const char_t* default_name = PUGIXML_TEXT(":anonymous"); - const char_t* name = node->name ? node->name : default_name; + const char_t* name = node->contents ? node->contents : default_name; writer.write('<'); writer.write_string(name); @@ -3997,7 +3997,7 @@ PUGI__NS_BEGIN { writer.write('>'); - const char_t* value = first->value ? first->value : PUGIXML_TEXT(""); + const char_t* value = first->contents ? first->contents : PUGIXML_TEXT(""); if (PUGI__NODETYPE(first) == node_pcdata) text_output(writer, value, ctx_special_pcdata, flags); @@ -4022,7 +4022,7 @@ PUGI__NS_BEGIN PUGI__FN void node_output_end(xml_buffered_writer& writer, xml_node_struct* node, unsigned int flags) { const char_t* default_name = PUGIXML_TEXT(":anonymous"); - const char_t* name = node->name ? node->name : default_name; + const char_t* name = node->contents ? node->contents : default_name; writer.write('<', '/'); writer.write_string(name); @@ -4040,28 +4040,28 @@ PUGI__NS_BEGIN switch (PUGI__NODETYPE(node)) { case node_pcdata: - text_output(writer, node->value ? node->value : PUGIXML_TEXT(""), ctx_special_pcdata, flags); + text_output(writer, node->contents ? node->contents : PUGIXML_TEXT(""), ctx_special_pcdata, flags); if ((flags & format_raw) == 0) writer.write('\n'); break; case node_cdata: - text_output_cdata(writer, node->value ? node->value : PUGIXML_TEXT("")); + text_output_cdata(writer, node->contents ? node->contents : PUGIXML_TEXT("")); if ((flags & format_raw) == 0) writer.write('\n'); break; case node_comment: - node_output_comment(writer, node->value ? node->value : PUGIXML_TEXT("")); + node_output_comment(writer, node->contents ? node->contents : PUGIXML_TEXT("")); if ((flags & format_raw) == 0) writer.write('\n'); break; case node_pi: writer.write('<', '?'); - writer.write_string(node->name ? node->name : default_name); + writer.write_string(node->contents ? node->contents : default_name); - if (node->value) + if (node->contents) { writer.write(' '); - writer.write_string(node->value); + writer.write_string(node->contents); } writer.write('?', '>'); @@ -4070,7 +4070,7 @@ PUGI__NS_BEGIN case node_declaration: writer.write('<', '?'); - writer.write_string(node->name ? node->name : default_name); + writer.write_string(node->contents ? node->contents : default_name); node_output_attributes(writer, node, flags); writer.write('?', '>'); if ((flags & format_raw) == 0) writer.write('\n'); @@ -4080,10 +4080,10 @@ PUGI__NS_BEGIN writer.write('<', '!', 'D', 'O', 'C'); writer.write('T', 'Y', 'P', 'E'); - if (node->value) + if (node->contents) { writer.write(' '); - writer.write_string(node->value); + writer.write_string(node->contents); } writer.write('>'); -- cgit v1.2.3 From f39a73f6e1056e84986d3ed8b2bc59e2bcf87e91 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Tue, 4 Nov 2014 10:05:31 +0100 Subject: Fix gcc warnings in compact mode --- src/pugixml.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 5e9c4b8..0fb0fe9 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -685,7 +685,7 @@ PUGI__NS_BEGIN flags |= modflags; } - operator uintptr_t const() const + operator uintptr_t() const { return reinterpret_cast(get_page()) | flags; } @@ -750,7 +750,7 @@ PUGI__NS_BEGIN _data = 0; } - operator T* const() const + operator T*() const { if (_data) { @@ -769,7 +769,7 @@ PUGI__NS_BEGIN T* operator->() const { - return operator T* const(); + return operator T*(); } private: @@ -827,7 +827,7 @@ PUGI__NS_BEGIN } } - operator T* const() const + operator T*() const { int data = _data0 + (_data1 << 8); @@ -850,7 +850,7 @@ PUGI__NS_BEGIN T* operator->() const { - return operator T* const(); + return operator T*(); } private: @@ -900,7 +900,7 @@ PUGI__NS_BEGIN } } - operator char_t* const() const + operator char_t*() const { unsigned int data = _data0 + (_data1 << 8) + (_data2 << 16); -- cgit v1.2.3 From 224b9b7ba76311b6a5ab8efec97c16180f721843 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Thu, 6 Nov 2014 09:20:34 +0100 Subject: Add a separate storage class for PI nodes This allows us to add pi value to restore target support for PI nodes without increasing the memory usage for other nodes. Right now the PI node has a separate header that's used for allocated bit; this allows us to reduce header bitcount in the future. --- src/pugixml.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 0fb0fe9..ef9400f 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -972,6 +972,19 @@ namespace pugi impl::compact_pointer first_attribute; ///< First attribute }; + + struct xml_node_pi_struct: xml_node_struct + { + xml_node_pi_struct(impl::xml_memory_page* page): xml_node_struct(page, node_pi), pi_header(page, 0) + { + PUGI__STATIC_ASSERT(sizeof(xml_node_pi_struct) == 20); + } + + impl::compact_header pi_header; + impl::compact_string<3> pi_value; + + unsigned char padding[2]; + }; } #else namespace pugi @@ -1015,6 +1028,16 @@ namespace pugi xml_attribute_struct* first_attribute; ///< First attribute }; + + struct xml_node_pi_struct: xml_node_struct + { + xml_node_pi_struct(impl::xml_memory_page* page): xml_node_struct(page, node_pi), pi_header(reinterpret_cast(page)), pi_value(0) + { + } + + uintptr_t pi_header; + char_t* pi_value; + }; } #endif @@ -1070,10 +1093,20 @@ PUGI__NS_BEGIN inline xml_node_struct* allocate_node(xml_allocator& alloc, xml_node_type type) { - xml_memory_page* page; - void* memory = alloc.allocate_memory(sizeof(xml_node_struct), page); + if (type != node_pi) + { + xml_memory_page* page; + void* memory = alloc.allocate_memory(sizeof(xml_node_struct), page); + + return new (memory) xml_node_struct(page, type); + } + else + { + xml_memory_page* page; + void* memory = alloc.allocate_memory(sizeof(xml_node_pi_struct), page); - return new (memory) xml_node_struct(page, type); + return new (memory) xml_node_pi_struct(page); + } } inline void destroy_attribute(xml_attribute_struct* a, xml_allocator& alloc) @@ -3097,7 +3130,8 @@ PUGI__NS_BEGIN else { // store value and step over > - // TODO: node_pi value:cursor->value = value; + static_cast(cursor)->pi_value = value; + PUGI__POPNODE(); PUGI__ENDSEG(); @@ -4060,10 +4094,10 @@ PUGI__NS_BEGIN writer.write('<', '?'); writer.write_string(node->contents ? node->contents : default_name); - if (node->contents) + if (static_cast(node)->pi_value) { writer.write(' '); - writer.write_string(node->contents); + writer.write_string(static_cast(node)->pi_value); } writer.write('?', '>'); @@ -4235,6 +4269,14 @@ PUGI__NS_BEGIN { node_copy_string(dn->contents, dn->header, xml_memory_page_contents_allocated_mask, sn->contents, sn->header, shared_alloc); + if (PUGI__NODETYPE(dn) == node_pi) + { + xml_node_pi_struct* dnp = static_cast(dn); + xml_node_pi_struct* snp = static_cast(sn); + + node_copy_string(dnp->pi_value, dnp->pi_header, xml_memory_page_contents_allocated_mask, snp->pi_value, snp->pi_header, shared_alloc); + } + for (xml_attribute_struct* sa = sn->first_attribute; sa; sa = sa->next_attribute) { xml_attribute_struct* da = append_new_attribute(dn, get_allocator(dn)); @@ -5251,7 +5293,16 @@ namespace pugi PUGI__FN const char_t* xml_node::value() const { - return (_root && impl::has_value(_root) && _root->contents) ? _root->contents + 0 : PUGIXML_TEXT(""); + if (_root) + { + if (impl::has_value(_root) && _root->contents) + return _root->contents; + + if (PUGI__NODETYPE(_root) == node_pi && static_cast(_root)->pi_value) + return static_cast(_root)->pi_value; + } + + return PUGIXML_TEXT(""); } PUGI__FN xml_node xml_node::child(const char_t* name_) const @@ -5382,6 +5433,12 @@ namespace pugi switch (type()) { case node_pi: + { + xml_node_pi_struct* pn = static_cast(_root); + + return impl::strcpy_insitu(pn->pi_value, pn->pi_header, impl::xml_memory_page_contents_allocated_mask, rhs); + } + case node_cdata: case node_pcdata: case node_comment: -- cgit v1.2.3 From 56349939e423c29c5bda99e0f2d070d46ffce6d6 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Thu, 6 Nov 2014 09:33:05 +0100 Subject: Verify that compact page encoding is safe Since page size can be customized let's do a special validation check for compact encoding. Right now it's redundant since page size is limited by 64k in alloc_string, but that may change in the future. --- src/pugixml.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index ef9400f..e5fd4b2 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -667,6 +667,8 @@ PUGI__NS_BEGIN public: compact_header(xml_memory_page* page, unsigned int flags) { + PUGI__STATIC_ASSERT(sizeof(xml_memory_page) + xml_memory_page_size <= (1 << (16 + compact_alignment_log2))); + ptrdiff_t page_offset = (reinterpret_cast(this) - reinterpret_cast(page)) >> compact_alignment_log2; assert(page_offset >= 0 && page_offset < (1 << 16)); -- cgit v1.2.3 From 5cad3652d934ded5a3d5c423ca40f71c7b4661d8 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Thu, 6 Nov 2014 10:02:27 +0100 Subject: Fix compact mode compilation Clang and gcc seem to treat string literals differently... --- src/pugixml.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index e5fd4b2..025c687 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -4035,7 +4035,7 @@ PUGI__NS_BEGIN { writer.write('>'); - const char_t* value = first->contents ? first->contents : PUGIXML_TEXT(""); + const char_t* value = first->contents ? first->contents + 0 : PUGIXML_TEXT(""); if (PUGI__NODETYPE(first) == node_pcdata) text_output(writer, value, ctx_special_pcdata, flags); @@ -4078,17 +4078,17 @@ PUGI__NS_BEGIN switch (PUGI__NODETYPE(node)) { case node_pcdata: - text_output(writer, node->contents ? node->contents : PUGIXML_TEXT(""), ctx_special_pcdata, flags); + text_output(writer, node->contents ? node->contents + 0 : PUGIXML_TEXT(""), ctx_special_pcdata, flags); if ((flags & format_raw) == 0) writer.write('\n'); break; case node_cdata: - text_output_cdata(writer, node->contents ? node->contents : PUGIXML_TEXT("")); + text_output_cdata(writer, node->contents ? node->contents + 0 : PUGIXML_TEXT("")); if ((flags & format_raw) == 0) writer.write('\n'); break; case node_comment: - node_output_comment(writer, node->contents ? node->contents : PUGIXML_TEXT("")); + node_output_comment(writer, node->contents ? node->contents + 0 : PUGIXML_TEXT("")); if ((flags & format_raw) == 0) writer.write('\n'); break; -- cgit v1.2.3 From cca23e636354dc73429a19e14e32cc9a5e632735 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 8 Nov 2014 20:05:12 +0100 Subject: Reduce required page alignment to 32 Since we no longer have a name/value pair in nodes we need one less bit to represent allocated flags. This reduces the page overhead by 32 bytes. --- src/pugixml.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index e92ae60..f0efa90 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -407,13 +407,19 @@ PUGI__NS_BEGIN #endif ; - static const uintptr_t xml_memory_page_alignment = 64; + static const uintptr_t xml_memory_page_alignment = 32; static const uintptr_t xml_memory_page_pointer_mask = ~(xml_memory_page_alignment - 1); - static const uintptr_t xml_memory_page_contents_shared_mask = 32; - static const uintptr_t xml_memory_page_contents_allocated_mask = 16; - static const uintptr_t xml_memory_page_name_allocated_mask = 16; - static const uintptr_t xml_memory_page_value_allocated_mask = 8; + + // extra metadata bits for xml_node_struct + static const uintptr_t xml_memory_page_contents_shared_mask = 16; + static const uintptr_t xml_memory_page_contents_allocated_mask = 8; static const uintptr_t xml_memory_page_type_mask = 7; + + // extra metadata bits for xml_attribute_struct + static const uintptr_t xml_memory_page_name_allocated_mask = 2; + static const uintptr_t xml_memory_page_value_allocated_mask = 1; + + // combined masks for string uniqueness static const uintptr_t xml_memory_page_name_allocated_or_shared_mask = xml_memory_page_name_allocated_mask | xml_memory_page_contents_shared_mask; static const uintptr_t xml_memory_page_value_allocated_or_shared_mask = xml_memory_page_value_allocated_mask | xml_memory_page_contents_shared_mask; static const uintptr_t xml_memory_page_contents_allocated_or_shared_mask = xml_memory_page_contents_allocated_mask | xml_memory_page_contents_shared_mask; -- cgit v1.2.3 From 4b8da65be9f5adb340d7edf32362bdb24f20833b Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Thu, 20 Nov 2014 23:49:59 -0800 Subject: Add allocator reserve for copying Since copying no longer relies on child insertion we have to also reserve space in the hash table for the allocator so that pointer manipulations are guaranteed to succeed. --- src/pugixml.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 46653d1..16a012e 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -5680,7 +5680,10 @@ namespace pugi xml_node_type type_ = proto.type(); if (!impl::allow_insert_child(type(), type_)) return xml_node(); - xml_node n(impl::allocate_node(impl::get_allocator(_root), type_)); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_node(); + + xml_node n(impl::allocate_node(alloc, type_)); if (!n) return xml_node(); impl::append_node(n._root, _root); @@ -5694,7 +5697,10 @@ namespace pugi xml_node_type type_ = proto.type(); if (!impl::allow_insert_child(type(), type_)) return xml_node(); - xml_node n(impl::allocate_node(impl::get_allocator(_root), type_)); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_node(); + + xml_node n(impl::allocate_node(alloc, type_)); if (!n) return xml_node(); impl::prepend_node(n._root, _root); @@ -5709,7 +5715,10 @@ namespace pugi if (!impl::allow_insert_child(type(), type_)) return xml_node(); if (!node._root || node._root->parent != _root) return xml_node(); - xml_node n(impl::allocate_node(impl::get_allocator(_root), type_)); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_node(); + + xml_node n(impl::allocate_node(alloc, type_)); if (!n) return xml_node(); impl::insert_node_after(n._root, node._root); @@ -5724,7 +5733,10 @@ namespace pugi if (!impl::allow_insert_child(type(), type_)) return xml_node(); if (!node._root || node._root->parent != _root) return xml_node(); - xml_node n(impl::allocate_node(impl::get_allocator(_root), type_)); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_node(); + + xml_node n(impl::allocate_node(alloc, type_)); if (!n) return xml_node(); impl::insert_node_before(n._root, node._root); -- cgit v1.2.3 From 50822aa2ac07175a96e65324627d5e31877ac40a Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Tue, 10 Mar 2015 21:15:13 -0700 Subject: Fix optimized string header encoding for compact mode Since in compact mode we only ever have a guaranteed alignment on 4, the pages are limited to 256k even if pointers are 64 bit. --- src/pugixml.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 16190c6..dd7ce21 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -423,6 +423,15 @@ PUGI__NS_BEGIN static const uintptr_t xml_memory_page_value_allocated_or_shared_mask = xml_memory_page_value_allocated_mask | xml_memory_page_contents_shared_mask; static const uintptr_t xml_memory_page_contents_allocated_or_shared_mask = xml_memory_page_contents_allocated_mask | xml_memory_page_contents_shared_mask; + // all allocated blocks have a certain guaranteed alignment + static const uintptr_t xml_memory_block_alignment = + #ifdef PUGIXML_COMPACT + 4 + #else + sizeof(void*) + #endif + ; + #define PUGI__NODETYPE(n) static_cast(((n)->header & impl::xml_memory_page_type_mask) + 1) struct xml_allocator; @@ -559,15 +568,15 @@ PUGI__NS_BEGIN char_t* allocate_string(size_t length) { - static const size_t max_encoded_offset = (1 << 16) * sizeof(void*); + static const size_t max_encoded_offset = (1 << 16) * xml_memory_block_alignment; PUGI__STATIC_ASSERT(xml_memory_page_size <= max_encoded_offset); // allocate memory for string and header block size_t size = sizeof(xml_memory_string_header) + length * sizeof(char_t); - // round size up to pointer alignment boundary - size_t full_size = (size + (sizeof(void*) - 1)) & ~(sizeof(void*) - 1); + // round size up to block alignment boundary + size_t full_size = (size + (xml_memory_block_alignment - 1)) & ~(xml_memory_block_alignment - 1); xml_memory_page* page; xml_memory_string_header* header = static_cast(allocate_memory(full_size, page)); @@ -577,14 +586,14 @@ PUGI__NS_BEGIN // setup header ptrdiff_t page_offset = reinterpret_cast(header) - reinterpret_cast(page) - sizeof(xml_memory_page); - assert(page_offset % sizeof(void*) == 0); + assert(page_offset % xml_memory_block_alignment == 0); assert(page_offset >= 0 && static_cast(page_offset) < max_encoded_offset); - header->page_offset = static_cast(static_cast(page_offset) / sizeof(void*)); + header->page_offset = static_cast(static_cast(page_offset) / xml_memory_block_alignment); // full_size == 0 for large strings that occupy the whole page - assert(full_size % sizeof(void*) == 0); + assert(full_size % xml_memory_block_alignment == 0); assert(full_size < max_encoded_offset || (page->busy_size == full_size && page_offset == 0)); - header->full_size = static_cast(full_size < max_encoded_offset ? full_size / sizeof(void*) : 0); + header->full_size = static_cast(full_size < max_encoded_offset ? full_size / xml_memory_block_alignment : 0); // round-trip through void* to avoid 'cast increases required alignment of target type' warning // header is guaranteed a pointer-sized alignment, which should be enough for char_t @@ -601,11 +610,11 @@ PUGI__NS_BEGIN assert(header); // deallocate - size_t page_offset = sizeof(xml_memory_page) + header->page_offset * sizeof(void*); + size_t page_offset = sizeof(xml_memory_page) + header->page_offset * xml_memory_block_alignment; xml_memory_page* page = reinterpret_cast(static_cast(reinterpret_cast(header) - page_offset)); // if full_size == 0 then this string occupies the whole page - size_t full_size = header->full_size == 0 ? page->busy_size : header->full_size * sizeof(void*); + size_t full_size = header->full_size == 0 ? page->busy_size : header->full_size * xml_memory_block_alignment; deallocate_memory(header, full_size, page); } @@ -677,6 +686,7 @@ PUGI__NS_BEGIN public: compact_header(xml_memory_page* page, unsigned int flags) { + PUGI__STATIC_ASSERT(xml_memory_block_alignment == compact_alignment); PUGI__STATIC_ASSERT(sizeof(xml_memory_page) + xml_memory_page_size <= (1 << (16 + compact_alignment_log2))); ptrdiff_t page_offset = (reinterpret_cast(this) - reinterpret_cast(page)) >> compact_alignment_log2; -- cgit v1.2.3 From 6c11a0c693da9ccf38225471d614bde162312427 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sun, 12 Apr 2015 03:14:08 -0700 Subject: Fix compilation and tests after merge. --- src/pugixml.cpp | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 7c640de..eff6760 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -5602,7 +5602,10 @@ namespace pugi if (!proto) return xml_attribute(); if (!impl::allow_insert_attribute(type())) return xml_attribute(); - xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root))); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_attribute(); + + xml_attribute a(impl::allocate_attribute(alloc)); if (!a) return xml_attribute(); impl::append_attribute(a._attr, _root); @@ -5616,7 +5619,10 @@ namespace pugi if (!proto) return xml_attribute(); if (!impl::allow_insert_attribute(type())) return xml_attribute(); - xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root))); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_attribute(); + + xml_attribute a(impl::allocate_attribute(alloc)); if (!a) return xml_attribute(); impl::prepend_attribute(a._attr, _root); @@ -5631,7 +5637,10 @@ namespace pugi if (!impl::allow_insert_attribute(type())) return xml_attribute(); if (!attr || !impl::is_attribute_of(attr._attr, _root)) return xml_attribute(); - xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root))); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_attribute(); + + xml_attribute a(impl::allocate_attribute(alloc)); if (!a) return xml_attribute(); impl::insert_attribute_after(a._attr, attr._attr, _root); @@ -5646,7 +5655,10 @@ namespace pugi if (!impl::allow_insert_attribute(type())) return xml_attribute(); if (!attr || !impl::is_attribute_of(attr._attr, _root)) return xml_attribute(); - xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root))); + impl::xml_allocator& alloc = impl::get_allocator(_root); + if (!alloc.reserve()) return xml_attribute(); + + xml_attribute a(impl::allocate_attribute(alloc)); if (!a) return xml_attribute(); impl::insert_attribute_before(a._attr, attr._attr, _root); @@ -6010,7 +6022,7 @@ namespace pugi for (xml_node_struct* i = _root; i; i = i->parent) { offset += (i != _root); - offset += i->name ? impl::strlength(i->name) : 0; + offset += (impl::has_name(i) && i->contents) ? impl::strlength(i->contents) : 0; } string_t result; @@ -6021,12 +6033,12 @@ namespace pugi if (j != _root) result[--offset] = delimiter; - if (j->name && *j->name) + if (impl::has_name(j) && j->contents && *j->contents) { - size_t length = impl::strlength(j->name); + size_t length = impl::strlength(j->contents); offset -= length; - memcpy(&result[offset], j->name, length * sizeof(char_t)); + memcpy(&result[offset], j->contents, length * sizeof(char_t)); } } -- cgit v1.2.3 From 33b2efe3181267a047af6087899cc27a2df805ff Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Tue, 21 Apr 2015 23:02:44 -0700 Subject: Optimize xml_allocator::reserve() Make sure compact_hash_table::rehash() is not inlined - that way reserve() is inlined so the fast path has no extra function calls. Also use subtraction instead of multiplication when checking capacity. --- src/pugixml.cpp | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index b2c13f5..1d0f5e8 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -338,7 +338,7 @@ PUGI__NS_BEGIN bool reserve() { - if (_count + 16 >= _capacity * 3 / 4) + if (_count + 16 >= _capacity - _capacity / 4) return rehash(); return true; @@ -356,29 +356,7 @@ PUGI__NS_BEGIN size_t _count; - bool rehash() - { - compact_hash_table rt; - rt._capacity = (_capacity == 0) ? 32 : _capacity * 2; - rt._items = static_cast(xml_memory::allocate(sizeof(item_t) * rt._capacity)); - - if (!rt._items) - return false; - - memset(rt._items, 0, sizeof(item_t) * rt._capacity); - - for (size_t i = 0; i < _capacity; ++i) - if (_items[i].key) - *rt.insert(_items[i].key) = _items[i].value; - - if (_items) - xml_memory::deallocate(_items); - - _capacity = rt._capacity; - _items = rt._items; - - return true; - } + bool rehash(); static unsigned int hash(const void* key) { @@ -394,6 +372,31 @@ PUGI__NS_BEGIN return h; } }; + + PUGI__FN_NO_INLINE bool compact_hash_table::rehash() + { + compact_hash_table rt; + rt._capacity = (_capacity == 0) ? 32 : _capacity * 2; + rt._items = static_cast(xml_memory::allocate(sizeof(item_t) * rt._capacity)); + + if (!rt._items) + return false; + + memset(rt._items, 0, sizeof(item_t) * rt._capacity); + + for (size_t i = 0; i < _capacity; ++i) + if (_items[i].key) + *rt.insert(_items[i].key) = _items[i].value; + + if (_items) + xml_memory::deallocate(_items); + + _capacity = rt._capacity; + _items = rt._items; + + return true; + } + PUGI__NS_END #endif -- cgit v1.2.3 From 4649914447d2e44a7df3e8d6dc1fc837503e8737 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 22 Apr 2015 07:34:43 -0700 Subject: Optimize and refactor compact_pointer implementations Clarify the offset applied when encoding the pointer difference. Make decoding diff slightly more clear - no effect on performance. Adjust branch weighting in compact_string encoding - 0.5% faster. Use uint16_t in compact_pointer_parent - 2% faster. --- src/pugixml.cpp | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 1d0f5e8..10208e7 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -760,7 +760,12 @@ PUGI__NS_BEGIN { if (value) { - ptrdiff_t offset = ((reinterpret_cast(value) - reinterpret_cast(this) + int(compact_alignment - 1)) >> compact_alignment_log2) - start; + // value is guaranteed to be compact-aligned; this is not + // our decoding is based on this aligned to compact alignment downwards (see operator T*) + // so for negative offsets (e.g. -3) we need to adjust the diff by compact_alignment - 1 to + // compensate for arithmetic shift behavior for negative values + ptrdiff_t diff = reinterpret_cast(value) - reinterpret_cast(this); + ptrdiff_t offset = ((diff + int(compact_alignment - 1)) >> compact_alignment_log2) - start; if (static_cast(offset) <= 253) _data = static_cast(offset + 1); @@ -783,7 +788,7 @@ PUGI__NS_BEGIN { uintptr_t base = reinterpret_cast(this) & ~(compact_alignment - 1); - return reinterpret_cast(base + ((_data - (1 - start)) << compact_alignment_log2)); + return reinterpret_cast(base + ((_data - 1 + start) << compact_alignment_log2)); } else return compact_get_value(this); @@ -804,7 +809,7 @@ PUGI__NS_BEGIN template class compact_pointer_parent { public: - compact_pointer_parent(): _data0(0), _data1(0) + compact_pointer_parent(): _data(0) { } @@ -817,12 +822,16 @@ PUGI__NS_BEGIN { if (value) { - ptrdiff_t offset = ((reinterpret_cast(value) - reinterpret_cast(this) + int(compact_alignment - 1)) >> compact_alignment_log2) + 65533; + // value is guaranteed to be compact-aligned; this is not + // our decoding is based on this aligned to compact alignment downwards (see operator T*) + // so for negative offsets (e.g. -3) we need to adjust the diff by compact_alignment - 1 to + // compensate for arithmetic shift behavior for negative values + ptrdiff_t diff = reinterpret_cast(value) - reinterpret_cast(this); + ptrdiff_t offset = ((diff + int(compact_alignment - 1)) >> compact_alignment_log2) + 65533; if (static_cast(offset) <= 65533) { - _data0 = static_cast(offset + 1); - _data1 = static_cast((offset + 1) >> 8); + _data = static_cast(offset + 1); } else { @@ -833,28 +842,25 @@ PUGI__NS_BEGIN if (page->compact_shared_parent == value) { - _data0 = 254; - _data1 = 255; + _data = 65534; } else { compact_set_value(this, value); - _data0 = 255; - _data1 = 255; + _data = 65535; } } } else { - _data0 = 0; - _data1 = 0; + _data = 0; } } operator T*() const { - int data = _data0 + (_data1 << 8); + int data = _data; if (data) { @@ -862,7 +868,7 @@ PUGI__NS_BEGIN { 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) return static_cast(compact_get_page(this, header_offset)->compact_shared_parent); @@ -879,8 +885,7 @@ PUGI__NS_BEGIN } private: - unsigned char _data0; - unsigned char _data1; + uint16_t _data; }; template class compact_string @@ -901,12 +906,12 @@ PUGI__NS_BEGIN { xml_memory_page* page = compact_get_page(this, header_offset); - if (page->compact_string_base == 0) + if (PUGI__UNLIKELY(page->compact_string_base == 0)) page->compact_string_base = value; ptrdiff_t offset = value - page->compact_string_base; - if (static_cast(offset) >= 16777213) + if (PUGI__UNLIKELY(static_cast(offset) >= 16777213)) { compact_set_value(this, value); -- cgit v1.2.3 From b87160013b697d9fb1b3b419b9801eecac9b3885 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 22 Apr 2015 07:51:02 -0700 Subject: Use has_name/has_value in set_name/set_value --- src/pugixml.cpp | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 10208e7..49959aa 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -5490,38 +5490,29 @@ namespace pugi PUGI__FN bool xml_node::set_name(const char_t* rhs) { - switch (type()) - { - case node_pi: - case node_declaration: - case node_element: + if (!_root) return false; + + if (impl::has_name(_root)) return impl::strcpy_insitu(_root->contents, _root->header, impl::xml_memory_page_contents_allocated_mask, rhs); - default: - return false; - } + return false; } PUGI__FN bool xml_node::set_value(const char_t* rhs) { - switch (type()) - { - case node_pi: + if (!_root) return false; + + if (impl::has_value(_root)) + return impl::strcpy_insitu(_root->contents, _root->header, impl::xml_memory_page_contents_allocated_mask, rhs); + + if (PUGI__NODETYPE(_root) == node_pi) { xml_node_pi_struct* pn = static_cast(_root); return impl::strcpy_insitu(pn->pi_value, pn->pi_header, impl::xml_memory_page_contents_allocated_mask, rhs); } - case node_cdata: - case node_pcdata: - case node_comment: - case node_doctype: - return impl::strcpy_insitu(_root->contents, _root->header, impl::xml_memory_page_contents_allocated_mask, rhs); - - default: - return false; - } + return false; } PUGI__FN xml_attribute xml_node::append_attribute(const char_t* name_) -- cgit v1.2.3 From 12744fd1fae0cf2bf26cfd17c4bff8b5488ebfdd Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 22 Apr 2015 07:52:20 -0700 Subject: Remove redundant has_value check --- src/pugixml.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 49959aa..5a318d3 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -5457,7 +5457,7 @@ namespace pugi if (!_root) return PUGIXML_TEXT(""); for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) - if (impl::has_value(i) && i->contents && impl::is_text_node(i)) + if (impl::is_text_node(i) && i->contents) return i->contents; return PUGIXML_TEXT(""); -- cgit v1.2.3 From e4e2259646ba5003e55f1969e6bde91ef37ff4b2 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 22 Apr 2015 08:26:47 -0700 Subject: Remove compact_header::operator uintptr_t We used this in two cases - to get the page pointer and to test flags. We now use PUGI__GETPAGE for getting the page pointer and operator& to test flags - this makes getting node type significantly faster since it does not require page pointer reconstruction. --- src/pugixml.cpp | 53 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 5a318d3..181d69a 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -435,6 +435,13 @@ PUGI__NS_BEGIN #endif ; + #ifdef PUGIXML_COMPACT + #define PUGI__GETPAGE_IMPL(header) (header).get_page() + #else + #define PUGI__GETPAGE_IMPL(header) reinterpret_cast((header) & impl::xml_memory_page_pointer_mask) + #endif + + #define PUGI__GETPAGE(n) PUGI__GETPAGE_IMPL((n)->header) #define PUGI__NODETYPE(n) static_cast(((n)->header & impl::xml_memory_page_type_mask) + 1) struct xml_allocator; @@ -691,6 +698,7 @@ PUGI__NS_BEGIN { PUGI__STATIC_ASSERT(xml_memory_block_alignment == compact_alignment); PUGI__STATIC_ASSERT(sizeof(xml_memory_page) + xml_memory_page_size <= (1 << (16 + compact_alignment_log2))); + PUGI__STATIC_ASSERT(xml_memory_page_pointer_mask & 0xff); ptrdiff_t page_offset = (reinterpret_cast(this) - reinterpret_cast(page)) >> compact_alignment_log2; assert(page_offset >= 0 && page_offset < (1 << 16)); @@ -700,19 +708,19 @@ PUGI__NS_BEGIN this->flags = static_cast(flags); } - void operator&=(uintptr_t modflags) + void operator&=(uintptr_t mod) { - flags &= modflags; + flags &= mod; } - void operator|=(uintptr_t modflags) + void operator|=(uintptr_t mod) { - flags |= modflags; + flags |= mod; } - operator uintptr_t() const + uintptr_t operator&(uintptr_t mod) const { - return reinterpret_cast(get_page()) | flags; + return flags & mod; } xml_memory_page* get_page() const @@ -1100,14 +1108,14 @@ PUGI__NS_BEGIN { assert(object); - return *reinterpret_cast(object->header & xml_memory_page_pointer_mask)->allocator; + return *PUGI__GETPAGE(object)->allocator; } template inline xml_document_struct& get_document(const Object* object) { assert(object); - return *static_cast(reinterpret_cast(object->header & xml_memory_page_pointer_mask)->allocator); + return *static_cast(PUGI__GETPAGE(object)->allocator); } PUGI__NS_END @@ -1141,19 +1149,19 @@ PUGI__NS_BEGIN inline void destroy_attribute(xml_attribute_struct* a, xml_allocator& alloc) { - uintptr_t header = a->header; + if (a->header & impl::xml_memory_page_name_allocated_mask) + alloc.deallocate_string(a->name); - if (header & impl::xml_memory_page_name_allocated_mask) alloc.deallocate_string(a->name); - if (header & impl::xml_memory_page_value_allocated_mask) alloc.deallocate_string(a->value); + if (a->header & impl::xml_memory_page_value_allocated_mask) + alloc.deallocate_string(a->value); - alloc.deallocate_memory(a, sizeof(xml_attribute_struct), reinterpret_cast(header & xml_memory_page_pointer_mask)); + alloc.deallocate_memory(a, sizeof(xml_attribute_struct), PUGI__GETPAGE(a)); } inline void destroy_node(xml_node_struct* n, xml_allocator& alloc) { - uintptr_t header = n->header; - - if (header & impl::xml_memory_page_contents_allocated_mask) alloc.deallocate_string(n->contents); + if (n->header & impl::xml_memory_page_contents_allocated_mask) + alloc.deallocate_string(n->contents); for (xml_attribute_struct* attr = n->first_attribute; attr; ) { @@ -1173,7 +1181,7 @@ PUGI__NS_BEGIN child = next; } - alloc.deallocate_memory(n, sizeof(xml_node_struct), reinterpret_cast(header & xml_memory_page_pointer_mask)); + alloc.deallocate_memory(n, sizeof(xml_node_struct), PUGI__GETPAGE(n)); } inline void append_node(xml_node_struct* child, xml_node_struct* node) @@ -2287,7 +2295,8 @@ PUGI__NS_BEGIN } #endif - inline bool strcpy_insitu_allow(size_t length, uintptr_t header, uintptr_t header_mask, char_t* target) + template + inline bool strcpy_insitu_allow(size_t length, const Header& header, uintptr_t header_mask, char_t* target) { // never reuse shared memory if (header & xml_memory_page_contents_shared_mask) return false; @@ -2306,14 +2315,12 @@ PUGI__NS_BEGIN template PUGI__FN bool strcpy_insitu(String& dest, Header& header, uintptr_t header_mask, const char_t* source) { - assert(header); - size_t source_length = strlength(source); if (source_length == 0) { // empty string and null pointer are equivalent, so just deallocate old memory - xml_allocator* alloc = reinterpret_cast(header & xml_memory_page_pointer_mask)->allocator; + xml_allocator* alloc = PUGI__GETPAGE_IMPL(header)->allocator; if (header & header_mask) alloc->deallocate_string(dest); @@ -2332,7 +2339,7 @@ PUGI__NS_BEGIN } else { - xml_allocator* alloc = reinterpret_cast(header & xml_memory_page_pointer_mask)->allocator; + xml_allocator* alloc = PUGI__GETPAGE_IMPL(header)->allocator; if (!alloc->reserve()) return false; @@ -6734,7 +6741,7 @@ namespace pugi } // destroy dynamic storage, leave sentinel page (it's in static memory) - impl::xml_memory_page* root_page = reinterpret_cast(_root->header & impl::xml_memory_page_pointer_mask); + impl::xml_memory_page* root_page = PUGI__GETPAGE(_root); assert(root_page && !root_page->prev); assert(reinterpret_cast(root_page) >= _memory && reinterpret_cast(root_page) < _memory + sizeof(_memory)); @@ -12375,6 +12382,8 @@ namespace pugi #undef PUGI__NS_END #undef PUGI__FN #undef PUGI__FN_NO_INLINE +#undef PUGI__GETPAGE_IMPL +#undef PUGI__GETPAGE #undef PUGI__NODETYPE #undef PUGI__IS_CHARTYPE_IMPL #undef PUGI__IS_CHARTYPE -- cgit v1.2.3 From 4223b4a3f0d2058fcfac551055550384b650f70a Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 22 Apr 2015 08:30:53 -0700 Subject: Make xml_node::value() structure consistent with set_* --- src/pugixml.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 181d69a..80d9d9f 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -5378,14 +5378,13 @@ namespace pugi PUGI__FN const char_t* xml_node::value() const { - if (_root) - { - if (impl::has_value(_root) && _root->contents) - return _root->contents; + if (!_root) return PUGIXML_TEXT(""); - if (PUGI__NODETYPE(_root) == node_pi && static_cast(_root)->pi_value) - return static_cast(_root)->pi_value; - } + if (impl::has_value(_root) && _root->contents) + return _root->contents; + + if (PUGI__NODETYPE(_root) == node_pi && static_cast(_root)->pi_value) + return static_cast(_root)->pi_value; return PUGIXML_TEXT(""); } -- cgit v1.2.3 From 3643b505a6f95e65037c5d906e7fb81ac16698cf Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 22 Apr 2015 08:38:52 -0700 Subject: Fix node_pi memory leak --- src/pugixml.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 80d9d9f..c5e77e8 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -1163,6 +1163,14 @@ PUGI__NS_BEGIN if (n->header & impl::xml_memory_page_contents_allocated_mask) alloc.deallocate_string(n->contents); + if (PUGI__NODETYPE(n) == node_pi) + { + xml_node_pi_struct* pn = static_cast(n); + + if (pn->pi_header & impl::xml_memory_page_contents_allocated_mask) + alloc.deallocate_string(pn->pi_value); + } + for (xml_attribute_struct* attr = n->first_attribute; attr; ) { xml_attribute_struct* next = attr->next_attribute; -- cgit v1.2.3 From 44e4f173480097087a5bffe7ae72857717f11f45 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 22 Apr 2015 09:51:54 -0700 Subject: Change xml_node_struct field order to match compact Also remove useless comments. --- src/pugixml.cpp | 56 +++++++++++++++++++++++--------------------------------- 1 file changed, 23 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index c5e77e8..b0e0078 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -967,10 +967,8 @@ PUGI__NS_END #ifdef PUGIXML_COMPACT namespace pugi { - /// A 'name=value' XML attribute structure. struct xml_attribute_struct { - /// Default ctor xml_attribute_struct(impl::xml_memory_page* page): header(page, 0) { PUGI__STATIC_ASSERT(sizeof(xml_attribute_struct) == 12); @@ -980,18 +978,15 @@ namespace pugi unsigned char padding; - impl::compact_string<4> name; ///< Pointer to attribute name. - impl::compact_string<7> value; ///< Pointer to attribute value. + impl::compact_string<4> name; + impl::compact_string<7> value; - impl::compact_pointer prev_attribute_c; ///< Previous attribute (cyclic list) - impl::compact_pointer next_attribute; ///< Next attribute + impl::compact_pointer prev_attribute_c; + impl::compact_pointer next_attribute; }; - /// An XML document tree node. struct xml_node_struct { - /// Default ctor - /// \param type - node type xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(page, type - 1) { PUGI__STATIC_ASSERT(sizeof(xml_node_struct) == 12); @@ -999,16 +994,16 @@ namespace pugi impl::compact_header header; - impl::compact_string<3> contents; ///< Pointer to element name. + impl::compact_string<3> contents; - impl::compact_pointer_parent parent; ///< Pointer to parent + impl::compact_pointer_parent parent; - impl::compact_pointer first_child; ///< First child + impl::compact_pointer first_child; - impl::compact_pointer prev_sibling_c; ///< Left brother (cyclic list) - impl::compact_pointer next_sibling; ///< Right brother + impl::compact_pointer prev_sibling_c; + impl::compact_pointer next_sibling; - impl::compact_pointer first_attribute; ///< First attribute + impl::compact_pointer first_attribute; }; struct xml_node_pi_struct: xml_node_struct @@ -1027,44 +1022,39 @@ namespace pugi #else namespace pugi { - /// A 'name=value' XML attribute structure. struct xml_attribute_struct { - /// Default ctor xml_attribute_struct(impl::xml_memory_page* page): header(reinterpret_cast(page)), name(0), value(0), prev_attribute_c(0), next_attribute(0) { } uintptr_t header; - char_t* name; ///< Pointer to attribute name. - char_t* value; ///< Pointer to attribute value. + char_t* name; + char_t* value; - xml_attribute_struct* prev_attribute_c; ///< Previous attribute (cyclic list) - xml_attribute_struct* next_attribute; ///< Next attribute + xml_attribute_struct* prev_attribute_c; + xml_attribute_struct* next_attribute; }; - /// An XML document tree node. struct xml_node_struct { - /// Default ctor - /// \param type - node type - xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(reinterpret_cast(page) | (type - 1)), parent(0), contents(0), first_child(0), prev_sibling_c(0), next_sibling(0), first_attribute(0) + xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(reinterpret_cast(page) | (type - 1)), contents(0), parent(0), first_child(0), prev_sibling_c(0), next_sibling(0), first_attribute(0) { } uintptr_t header; - xml_node_struct* parent; ///< Pointer to parent + char_t* contents; - char_t* contents; ///< Pointer to element name/value + xml_node_struct* parent; - xml_node_struct* first_child; ///< First child - - xml_node_struct* prev_sibling_c; ///< Left brother (cyclic list) - xml_node_struct* next_sibling; ///< Right brother - - xml_attribute_struct* first_attribute; ///< First attribute + xml_node_struct* first_child; + + xml_node_struct* prev_sibling_c; + xml_node_struct* next_sibling; + + xml_attribute_struct* first_attribute; }; struct xml_node_pi_struct: xml_node_struct -- cgit v1.2.3 From b2399f5ab5b678a59702b77e41692d15410a0c4a Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 29 Apr 2015 09:20:08 -0700 Subject: Refactor offset_debug Split a long line into multiple statements. --- src/pugixml.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index b0e0078..5e09aca 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -6186,7 +6186,12 @@ namespace pugi // document node always has an offset of 0 if (_root == &doc) return 0; - return _root->contents && (_root->header & impl::xml_memory_page_contents_allocated_or_shared_mask) == 0 ? _root->contents - doc.buffer : -1; + // we need contents to be inside buffer and not shared + // if it's shared we don't know if this is the original node + if (!_root->contents) return -1; + if (_root->header & impl::xml_memory_page_contents_allocated_or_shared_mask) return -1; + + return _root->contents - doc.buffer; } #ifdef __BORLANDC__ -- cgit v1.2.3 From bc5eb22b715322b244f07829a2815b1441dabf20 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Fri, 1 May 2015 20:03:17 -0700 Subject: Revert to name/value storage inside node This temporarily increases the node size to 16 bytes - we'll bring it back. It allows us to remove the horrible node_pi hack and to reduce the amount of changes against master. This comes at the price of not decreasing basline xml_node_struct size. The compact xml_node_struct is also increased by this change but a followup change will reduce *both* xml_attribute_struct and xml_node_struct (to 8/12 bytes). --- src/pugixml.cpp | 268 +++++++++++++++++++++----------------------------------- 1 file changed, 99 insertions(+), 169 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 5e09aca..654b1e8 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -409,22 +409,18 @@ PUGI__NS_BEGIN #endif ; - static const uintptr_t xml_memory_page_alignment = 32; + static const uintptr_t xml_memory_page_alignment = 64; static const uintptr_t xml_memory_page_pointer_mask = ~(xml_memory_page_alignment - 1); - // extra metadata bits for xml_node_struct - static const uintptr_t xml_memory_page_contents_shared_mask = 16; - static const uintptr_t xml_memory_page_contents_allocated_mask = 8; + // extra metadata bits + static const uintptr_t xml_memory_page_contents_shared_mask = 32; + static const uintptr_t xml_memory_page_name_allocated_mask = 16; + static const uintptr_t xml_memory_page_value_allocated_mask = 8; static const uintptr_t xml_memory_page_type_mask = 7; - // extra metadata bits for xml_attribute_struct - static const uintptr_t xml_memory_page_name_allocated_mask = 2; - static const uintptr_t xml_memory_page_value_allocated_mask = 1; - // combined masks for string uniqueness static const uintptr_t xml_memory_page_name_allocated_or_shared_mask = xml_memory_page_name_allocated_mask | xml_memory_page_contents_shared_mask; static const uintptr_t xml_memory_page_value_allocated_or_shared_mask = xml_memory_page_value_allocated_mask | xml_memory_page_contents_shared_mask; - static const uintptr_t xml_memory_page_contents_allocated_or_shared_mask = xml_memory_page_contents_allocated_mask | xml_memory_page_contents_shared_mask; // all allocated blocks have a certain guaranteed alignment static const uintptr_t xml_memory_block_alignment = @@ -989,34 +985,24 @@ namespace pugi { xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(page, type - 1) { - PUGI__STATIC_ASSERT(sizeof(xml_node_struct) == 12); + PUGI__STATIC_ASSERT(sizeof(xml_node_struct) == 16); } impl::compact_header header; - impl::compact_string<3> contents; - - impl::compact_pointer_parent parent; + impl::compact_string<3> name; + impl::compact_string<6> value; - impl::compact_pointer first_child; + unsigned char padding; - impl::compact_pointer prev_sibling_c; - impl::compact_pointer next_sibling; + impl::compact_pointer_parent parent; - impl::compact_pointer first_attribute; - }; - - struct xml_node_pi_struct: xml_node_struct - { - xml_node_pi_struct(impl::xml_memory_page* page): xml_node_struct(page, node_pi), pi_header(page, 0) - { - PUGI__STATIC_ASSERT(sizeof(xml_node_pi_struct) == 20); - } + impl::compact_pointer first_child; - impl::compact_header pi_header; - impl::compact_string<3> pi_value; + impl::compact_pointer prev_sibling_c; + impl::compact_pointer next_sibling; - unsigned char padding[2]; + impl::compact_pointer first_attribute; }; } #else @@ -1039,13 +1025,14 @@ namespace pugi struct xml_node_struct { - xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(reinterpret_cast(page) | (type - 1)), contents(0), parent(0), first_child(0), prev_sibling_c(0), next_sibling(0), first_attribute(0) + xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(reinterpret_cast(page) | (type - 1)), name(0), value(0), parent(0), first_child(0), prev_sibling_c(0), next_sibling(0), first_attribute(0) { } uintptr_t header; - char_t* contents; + char_t* name; + char_t* value; xml_node_struct* parent; @@ -1056,16 +1043,6 @@ namespace pugi xml_attribute_struct* first_attribute; }; - - struct xml_node_pi_struct: xml_node_struct - { - xml_node_pi_struct(impl::xml_memory_page* page): xml_node_struct(page, node_pi), pi_header(reinterpret_cast(page)), pi_value(0) - { - } - - uintptr_t pi_header; - char_t* pi_value; - }; } #endif @@ -1121,20 +1098,10 @@ PUGI__NS_BEGIN inline xml_node_struct* allocate_node(xml_allocator& alloc, xml_node_type type) { - if (type != node_pi) - { - xml_memory_page* page; - void* memory = alloc.allocate_memory(sizeof(xml_node_struct), page); - - return new (memory) xml_node_struct(page, type); - } - else - { - xml_memory_page* page; - void* memory = alloc.allocate_memory(sizeof(xml_node_pi_struct), page); + xml_memory_page* page; + void* memory = alloc.allocate_memory(sizeof(xml_node_struct), page); - return new (memory) xml_node_pi_struct(page); - } + return new (memory) xml_node_struct(page, type); } inline void destroy_attribute(xml_attribute_struct* a, xml_allocator& alloc) @@ -1150,16 +1117,11 @@ PUGI__NS_BEGIN inline void destroy_node(xml_node_struct* n, xml_allocator& alloc) { - if (n->header & impl::xml_memory_page_contents_allocated_mask) - alloc.deallocate_string(n->contents); + if (n->header & impl::xml_memory_page_name_allocated_mask) + alloc.deallocate_string(n->name); - if (PUGI__NODETYPE(n) == node_pi) - { - xml_node_pi_struct* pn = static_cast(n); - - if (pn->pi_header & impl::xml_memory_page_contents_allocated_mask) - alloc.deallocate_string(pn->pi_value); - } + if (n->header & impl::xml_memory_page_value_allocated_mask) + alloc.deallocate_string(n->value); for (xml_attribute_struct* attr = n->first_attribute; attr; ) { @@ -3016,14 +2978,14 @@ PUGI__NS_BEGIN if (PUGI__OPTSET(parse_comments)) { PUGI__PUSHNODE(node_comment); // Append a new node on the tree. - cursor->contents = s; // Save the offset. + cursor->value = s; // Save the offset. } if (PUGI__OPTSET(parse_eol) && PUGI__OPTSET(parse_comments)) { s = strconv_comment(s, endch); - if (!s) PUGI__THROW_ERROR(status_bad_comment, cursor->contents); + if (!s) PUGI__THROW_ERROR(status_bad_comment, cursor->value); } else { @@ -3049,13 +3011,13 @@ PUGI__NS_BEGIN if (PUGI__OPTSET(parse_cdata)) { PUGI__PUSHNODE(node_cdata); // Append a new node on the tree. - cursor->contents = s; // Save the offset. + cursor->value = s; // Save the offset. if (PUGI__OPTSET(parse_eol)) { s = strconv_cdata(s, endch); - if (!s) PUGI__THROW_ERROR(status_bad_cdata, cursor->contents); + if (!s) PUGI__THROW_ERROR(status_bad_cdata, cursor->value); } else { @@ -3099,7 +3061,7 @@ PUGI__NS_BEGIN PUGI__PUSHNODE(node_doctype); - cursor->contents = mark; + cursor->value = mark; } } else if (*s == 0 && endch == '-') PUGI__THROW_ERROR(status_bad_comment, s); @@ -3143,7 +3105,7 @@ PUGI__NS_BEGIN PUGI__PUSHNODE(node_pi); } - cursor->contents = target; + cursor->name = target; PUGI__ENDSEG(); @@ -3177,7 +3139,7 @@ PUGI__NS_BEGIN else { // store value and step over > - static_cast(cursor)->pi_value = value; + cursor->value = value; PUGI__POPNODE(); @@ -3223,7 +3185,7 @@ PUGI__NS_BEGIN { PUGI__PUSHNODE(node_element); // Append a new node to the tree. - cursor->contents = s; + cursor->name = s; PUGI__SCANWHILE_UNROLL(PUGI__IS_CHARTYPE(ss, ct_symbol)); // Scan for a terminator. PUGI__ENDSEG(); // Save char in 'ch', terminate & step over. @@ -3333,7 +3295,7 @@ PUGI__NS_BEGIN { ++s; - char_t* name = cursor->contents; + char_t* name = cursor->name; if (!name) PUGI__THROW_ERROR(status_end_element_mismatch, s); while (PUGI__IS_CHARTYPE(*s, ct_symbol)) @@ -3404,7 +3366,7 @@ PUGI__NS_BEGIN if (cursor->parent || PUGI__OPTSET(parse_fragment)) { PUGI__PUSHNODE(node_pcdata); // Append a new node on the tree. - cursor->contents = s; // Save the offset. + cursor->value = s; // Save the offset. s = strconv_pcdata(s); @@ -4067,7 +4029,7 @@ PUGI__NS_BEGIN PUGI__FN bool node_output_start(xml_buffered_writer& writer, xml_node_struct* node, const char_t* indent, size_t indent_length, unsigned int flags, unsigned int depth) { const char_t* default_name = PUGIXML_TEXT(":anonymous"); - const char_t* name = node->contents ? node->contents : default_name; + const char_t* name = node->name ? node->name : default_name; writer.write('<'); writer.write_string(name); @@ -4092,7 +4054,7 @@ PUGI__NS_BEGIN PUGI__FN void node_output_end(xml_buffered_writer& writer, xml_node_struct* node) { const char_t* default_name = PUGIXML_TEXT(":anonymous"); - const char_t* name = node->contents ? node->contents : default_name; + const char_t* name = node->name ? node->name : default_name; writer.write('<', '/'); writer.write_string(name); @@ -4106,25 +4068,25 @@ PUGI__NS_BEGIN switch (PUGI__NODETYPE(node)) { case node_pcdata: - text_output(writer, node->contents ? node->contents + 0 : PUGIXML_TEXT(""), ctx_special_pcdata, flags); + text_output(writer, node->value ? node->value + 0 : PUGIXML_TEXT(""), ctx_special_pcdata, flags); break; case node_cdata: - text_output_cdata(writer, node->contents ? node->contents + 0 : PUGIXML_TEXT("")); + text_output_cdata(writer, node->value ? node->value + 0 : PUGIXML_TEXT("")); break; case node_comment: - node_output_comment(writer, node->contents ? node->contents + 0 : PUGIXML_TEXT("")); + node_output_comment(writer, node->value ? node->value + 0 : PUGIXML_TEXT("")); break; case node_pi: writer.write('<', '?'); - writer.write_string(node->contents ? node->contents : default_name); + writer.write_string(node->name ? node->name : default_name); - if (static_cast(node)->pi_value) + if (node->value) { writer.write(' '); - node_output_pi_value(writer, static_cast(node)->pi_value); + node_output_pi_value(writer, node->value); } writer.write('?', '>'); @@ -4132,7 +4094,7 @@ PUGI__NS_BEGIN case node_declaration: writer.write('<', '?'); - writer.write_string(node->contents ? node->contents : default_name); + writer.write_string(node->name ? node->name : default_name); node_output_attributes(writer, node, PUGIXML_TEXT(""), 0, flags | format_raw, 0); writer.write('?', '>'); break; @@ -4141,10 +4103,10 @@ PUGI__NS_BEGIN writer.write('<', '!', 'D', 'O', 'C'); writer.write('T', 'Y', 'P', 'E'); - if (node->contents) + if (node->value) { writer.write(' '); - writer.write_string(node->contents); + writer.write_string(node->value); } writer.write('>'); @@ -4332,15 +4294,8 @@ PUGI__NS_BEGIN PUGI__FN void node_copy_contents(xml_node_struct* dn, xml_node_struct* sn, xml_allocator* shared_alloc) { - node_copy_string(dn->contents, dn->header, xml_memory_page_contents_allocated_mask, sn->contents, sn->header, shared_alloc); - - if (PUGI__NODETYPE(dn) == node_pi) - { - xml_node_pi_struct* dnp = static_cast(dn); - xml_node_pi_struct* snp = static_cast(sn); - - node_copy_string(dnp->pi_value, dnp->pi_header, xml_memory_page_contents_allocated_mask, snp->pi_value, snp->pi_header, shared_alloc); - } + node_copy_string(dn->name, dn->header, xml_memory_page_name_allocated_mask, sn->name, sn->header, shared_alloc); + node_copy_string(dn->value, dn->header, xml_memory_page_value_allocated_mask, sn->value, sn->header, shared_alloc); for (xml_attribute_struct* sa = sn->first_attribute; sa; sa = sa->next_attribute) { @@ -4939,20 +4894,6 @@ PUGI__NS_BEGIN return res; } - - inline bool has_name(xml_node_struct* node) - { - static const bool result[] = { false, false, true, false, false, false, true, true, false }; - - return result[PUGI__NODETYPE(node)]; - } - - inline bool has_value(xml_node_struct* node) - { - static const bool result[] = { false, false, false, true, true, true, false, false, true }; - - return result[PUGI__NODETYPE(node)]; - } PUGI__NS_END namespace pugi @@ -5366,7 +5307,7 @@ namespace pugi PUGI__FN const char_t* xml_node::name() const { - return (_root && impl::has_name(_root) && _root->contents) ? _root->contents + 0 : PUGIXML_TEXT(""); + return (_root && _root->name) ? _root->name + 0 : PUGIXML_TEXT(""); } PUGI__FN xml_node_type xml_node::type() const @@ -5376,15 +5317,7 @@ namespace pugi PUGI__FN const char_t* xml_node::value() const { - if (!_root) return PUGIXML_TEXT(""); - - if (impl::has_value(_root) && _root->contents) - return _root->contents; - - if (PUGI__NODETYPE(_root) == node_pi && static_cast(_root)->pi_value) - return static_cast(_root)->pi_value; - - return PUGIXML_TEXT(""); + return (_root && _root->value) ? _root->value + 0 : PUGIXML_TEXT(""); } PUGI__FN xml_node xml_node::child(const char_t* name_) const @@ -5392,7 +5325,7 @@ namespace pugi if (!_root) return xml_node(); for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) - if (impl::has_name(i) && i->contents && impl::strequal(name_, i->contents)) return xml_node(i); + if (i->name && impl::strequal(name_, i->name)) return xml_node(i); return xml_node(); } @@ -5413,7 +5346,7 @@ namespace pugi if (!_root) return xml_node(); for (xml_node_struct* i = _root->next_sibling; i; i = i->next_sibling) - if (impl::has_name(i) && i->contents && impl::strequal(name_, i->contents)) return xml_node(i); + if (i->name && impl::strequal(name_, i->name)) return xml_node(i); return xml_node(); } @@ -5428,7 +5361,7 @@ namespace pugi if (!_root) return xml_node(); for (xml_node_struct* i = _root->prev_sibling_c; i->next_sibling; i = i->prev_sibling_c) - if (impl::has_name(i) && i->contents && impl::strequal(name_, i->contents)) return xml_node(i); + if (i->name && impl::strequal(name_, i->name)) return xml_node(i); return xml_node(); } @@ -5461,8 +5394,8 @@ namespace pugi if (!_root) return PUGIXML_TEXT(""); for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) - if (impl::is_text_node(i) && i->contents) - return i->contents; + if (impl::is_text_node(i) && i->value) + return i->value; return PUGIXML_TEXT(""); } @@ -5494,29 +5427,22 @@ namespace pugi PUGI__FN bool xml_node::set_name(const char_t* rhs) { - if (!_root) return false; + static const bool has_name[] = { false, false, true, false, false, false, true, true, false }; - if (impl::has_name(_root)) - return impl::strcpy_insitu(_root->contents, _root->header, impl::xml_memory_page_contents_allocated_mask, rhs); + if (!_root || !has_name[PUGI__NODETYPE(_root)]) + return false; - return false; + return impl::strcpy_insitu(_root->name, _root->header, impl::xml_memory_page_name_allocated_mask, rhs); } PUGI__FN bool xml_node::set_value(const char_t* rhs) { - if (!_root) return false; - - if (impl::has_value(_root)) - return impl::strcpy_insitu(_root->contents, _root->header, impl::xml_memory_page_contents_allocated_mask, rhs); + static const bool has_value[] = { false, false, false, true, true, true, true, false, true }; - if (PUGI__NODETYPE(_root) == node_pi) - { - xml_node_pi_struct* pn = static_cast(_root); - - return impl::strcpy_insitu(pn->pi_value, pn->pi_header, impl::xml_memory_page_contents_allocated_mask, rhs); - } + if (!_root || !has_value[PUGI__NODETYPE(_root)]) + return false; - return false; + return impl::strcpy_insitu(_root->value, _root->header, impl::xml_memory_page_value_allocated_mask, rhs); } PUGI__FN xml_attribute xml_node::append_attribute(const char_t* name_) @@ -5969,12 +5895,12 @@ namespace pugi xml_node_struct* node; char_t* name; - ~name_sentry() { node->contents = name; } + ~name_sentry() { node->name = name; } }; - name_sentry sentry = { _root, _root->contents }; + name_sentry sentry = { _root, _root->name }; - sentry.node->contents = 0; + sentry.node->name = 0; return impl::load_buffer_impl(doc, _root, const_cast(contents), size, options, encoding, false, false, &extra->buffer); } @@ -5984,7 +5910,7 @@ namespace pugi if (!_root) return xml_node(); for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) - if (impl::has_name(i) && i->contents && impl::strequal(name_, i->contents)) + if (i->name && impl::strequal(name_, i->name)) { for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute) if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value + 0 : PUGIXML_TEXT(""))) @@ -6016,7 +5942,7 @@ namespace pugi for (xml_node_struct* i = _root; i; i = i->parent) { offset += (i != _root); - offset += (impl::has_name(i) && i->contents) ? impl::strlength(i->contents) : 0; + offset += i->name ? impl::strlength(i->name) : 0; } string_t result; @@ -6027,12 +5953,12 @@ namespace pugi if (j != _root) result[--offset] = delimiter; - if (impl::has_name(j) && j->contents && *j->contents) + if (j->name && *j->name) { - size_t length = impl::strlength(j->contents); + size_t length = impl::strlength(j->name); offset -= length; - memcpy(&result[offset], j->contents, length * sizeof(char_t)); + memcpy(&result[offset], j->name, length * sizeof(char_t)); } } @@ -6077,7 +6003,7 @@ namespace pugi { for (xml_node_struct* j = found._root->first_child; j; j = j->next_sibling) { - if (impl::has_name(j) && j->contents && impl::strequalrange(j->contents, path_segment, static_cast(path_segment_end - path_segment))) + if (j->name && impl::strequalrange(j->name, path_segment, static_cast(path_segment_end - path_segment))) { xml_node subsearch = xml_node(j).first_element_by_path(next_segment, delimiter); @@ -6188,10 +6114,13 @@ namespace pugi // we need contents to be inside buffer and not shared // if it's shared we don't know if this is the original node - if (!_root->contents) return -1; - if (_root->header & impl::xml_memory_page_contents_allocated_or_shared_mask) return -1; + if (_root->name && (_root->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0) + return _root->name - doc.buffer; + + if (_root->value && (_root->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0) + return _root->value - doc.buffer; - return _root->contents - doc.buffer; + return -1; } #ifdef __BORLANDC__ @@ -6256,49 +6185,49 @@ namespace pugi { xml_node_struct* d = _data(); - return (d && d->contents) ? d->contents + 0 : PUGIXML_TEXT(""); + return (d && d->value) ? d->value + 0 : PUGIXML_TEXT(""); } PUGI__FN const char_t* xml_text::as_string(const char_t* def) const { xml_node_struct* d = _data(); - return (d && d->contents) ? d->contents : def; + return (d && d->value) ? d->value : def; } PUGI__FN int xml_text::as_int(int def) const { xml_node_struct* d = _data(); - return impl::get_value_int(d ? d->contents + 0 : 0, def); + return impl::get_value_int(d ? d->value + 0 : 0, def); } PUGI__FN unsigned int xml_text::as_uint(unsigned int def) const { xml_node_struct* d = _data(); - return impl::get_value_uint(d ? d->contents + 0 : 0, def); + return impl::get_value_uint(d ? d->value + 0 : 0, def); } PUGI__FN double xml_text::as_double(double def) const { xml_node_struct* d = _data(); - return impl::get_value_double(d ? d->contents + 0 : 0, def); + return impl::get_value_double(d ? d->value + 0 : 0, def); } PUGI__FN float xml_text::as_float(float def) const { xml_node_struct* d = _data(); - return impl::get_value_float(d ? d->contents + 0 : 0, def); + return impl::get_value_float(d ? d->value + 0 : 0, def); } PUGI__FN bool xml_text::as_bool(bool def) const { xml_node_struct* d = _data(); - return impl::get_value_bool(d ? d->contents + 0 : 0, def); + return impl::get_value_bool(d ? d->value + 0 : 0, def); } #ifdef PUGIXML_HAS_LONG_LONG @@ -6306,14 +6235,14 @@ namespace pugi { xml_node_struct* d = _data(); - return impl::get_value_llong(d ? d->contents + 0 : 0, def); + return impl::get_value_llong(d ? d->value + 0 : 0, def); } PUGI__FN unsigned long long xml_text::as_ullong(unsigned long long def) const { xml_node_struct* d = _data(); - return impl::get_value_ullong(d ? d->contents + 0 : 0, def); + return impl::get_value_ullong(d ? d->value + 0 : 0, def); } #endif @@ -6321,42 +6250,42 @@ namespace pugi { xml_node_struct* dn = _data_new(); - return dn ? impl::strcpy_insitu(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; + return dn ? impl::strcpy_insitu(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; } PUGI__FN bool xml_text::set(int rhs) { xml_node_struct* dn = _data_new(); - return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; + return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; } PUGI__FN bool xml_text::set(unsigned int rhs) { xml_node_struct* dn = _data_new(); - return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; + return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; } PUGI__FN bool xml_text::set(float rhs) { xml_node_struct* dn = _data_new(); - return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; + return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; } PUGI__FN bool xml_text::set(double rhs) { xml_node_struct* dn = _data_new(); - return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; + return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; } PUGI__FN bool xml_text::set(bool rhs) { xml_node_struct* dn = _data_new(); - return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; + return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; } #ifdef PUGIXML_HAS_LONG_LONG @@ -6364,14 +6293,14 @@ namespace pugi { xml_node_struct* dn = _data_new(); - return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; + return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; } PUGI__FN bool xml_text::set(unsigned long long rhs) { xml_node_struct* dn = _data_new(); - return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false; + return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; } #endif @@ -7734,7 +7663,8 @@ PUGI__NS_BEGIN { if ((get_document(node).header & xml_memory_page_contents_shared_mask) == 0) { - if (node->contents && (node->header & impl::xml_memory_page_contents_allocated_or_shared_mask) == 0) return node->contents; + if (node->name && (node->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0) return node->name; + if (node->value && (node->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0) return node->value; } return 0; @@ -9482,7 +9412,7 @@ PUGI__NS_BEGIN switch (_test) { case nodetest_name: - if (type == node_element && n->contents && strequal(n->contents, _data.nodetest)) + if (type == node_element && n->name && strequal(n->name, _data.nodetest)) { ns.push_back(xml_node(n), alloc); return true; @@ -9518,7 +9448,7 @@ PUGI__NS_BEGIN break; case nodetest_pi: - if (type == node_pi && n->contents && strequal(n->contents, _data.nodetest)) + if (type == node_pi && n->name && strequal(n->name, _data.nodetest)) { ns.push_back(xml_node(n), alloc); return true; @@ -9534,7 +9464,7 @@ PUGI__NS_BEGIN break; case nodetest_all_in_namespace: - if (type == node_element && n->contents && starts_with(n->contents, _data.nodetest)) + if (type == node_element && n->name && starts_with(n->name, _data.nodetest)) { ns.push_back(xml_node(n), alloc); return true; -- cgit v1.2.3 From 3915f7b14430271905f4a4c808725304777e16c4 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Fri, 1 May 2015 21:09:26 -0700 Subject: Rename compact_string to compact_string_fat --- src/pugixml.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 654b1e8..f8762bf 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -892,14 +892,14 @@ PUGI__NS_BEGIN uint16_t _data; }; - template class compact_string + template class compact_string_fat { public: - compact_string(): _data0(0), _data1(0), _data2(0) + compact_string_fat(): _data0(0), _data1(0), _data2(0) { } - void operator=(const compact_string& rhs) + void operator=(const compact_string_fat& rhs) { *this = rhs + 0; } @@ -974,8 +974,8 @@ namespace pugi unsigned char padding; - impl::compact_string<4> name; - impl::compact_string<7> value; + impl::compact_string_fat<4> name; + impl::compact_string_fat<7> value; impl::compact_pointer prev_attribute_c; impl::compact_pointer next_attribute; @@ -990,11 +990,11 @@ namespace pugi impl::compact_header header; - impl::compact_string<3> name; - impl::compact_string<6> value; - unsigned char padding; + impl::compact_string_fat<4> name; + impl::compact_string_fat<7> value; + impl::compact_pointer_parent parent; impl::compact_pointer first_child; -- cgit v1.2.3 From e4c539a869eb4045711f2b7c2390b0f450be71a1 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Fri, 1 May 2015 22:47:53 -0700 Subject: Implement compact_string with shared storage --- src/pugixml.cpp | 77 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index f8762bf..70dd037 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -892,14 +892,14 @@ PUGI__NS_BEGIN uint16_t _data; }; - template class compact_string_fat + template class compact_string { public: - compact_string_fat(): _data0(0), _data1(0), _data2(0) + compact_string(): _data(0) { } - void operator=(const compact_string_fat& rhs) + void operator=(const compact_string& rhs) { *this = rhs + 0; } @@ -913,50 +913,59 @@ PUGI__NS_BEGIN if (PUGI__UNLIKELY(page->compact_string_base == 0)) page->compact_string_base = value; + uint16_t* base = reinterpret_cast(reinterpret_cast(this) - base_offset); + ptrdiff_t offset = value - page->compact_string_base; - if (PUGI__UNLIKELY(static_cast(offset) >= 16777213)) + if (*base == 0) + *base = static_cast(offset >> 7) + 1; + + ptrdiff_t remainder = offset - ((*base - 1) << 7); + + if (PUGI__UNLIKELY(static_cast(remainder) >= 254 || *base == 0)) { compact_set_value(this, value); - offset = 16777214; + _data = 255; + } + else + { + _data = static_cast(remainder + 1); } - - _data0 = static_cast(offset + 1); - _data1 = static_cast((offset + 1) >> 8); - _data2 = static_cast((offset + 1) >> 16); } else { - _data0 = 0; - _data1 = 0; - _data2 = 0; + _data = 0; } } operator char_t*() const { - unsigned int data = _data0 + (_data1 << 8) + (_data2 << 16); - - if (data) + if (_data) { - xml_memory_page* page = compact_get_page(this, header_offset); - - if (data < 16777215) - return page->compact_string_base + (data - 1); - else + if (PUGI__UNLIKELY(_data == 255)) + { return compact_get_value(this); + } + else + { + xml_memory_page* page = compact_get_page(this, header_offset); + + const uint16_t* base = reinterpret_cast(reinterpret_cast(this) - base_offset); + assert(*base); + + ptrdiff_t offset = ((*base - 1) << 7) + (_data - 1); + + return page->compact_string_base + offset; + } } else return 0; } private: - unsigned char _data0; - unsigned char _data1; - unsigned char _data2; + unsigned char _data; }; - PUGI__NS_END #endif @@ -965,17 +974,19 @@ namespace pugi { struct xml_attribute_struct { - xml_attribute_struct(impl::xml_memory_page* page): header(page, 0) + xml_attribute_struct(impl::xml_memory_page* page): header(page, 0), namevalue_base(0) { PUGI__STATIC_ASSERT(sizeof(xml_attribute_struct) == 12); } impl::compact_header header; - unsigned char padding; + unsigned char padding[3]; + + uint16_t namevalue_base; - impl::compact_string_fat<4> name; - impl::compact_string_fat<7> value; + impl::compact_string<8, 2> name; + impl::compact_string<9, 3> value; impl::compact_pointer prev_attribute_c; impl::compact_pointer next_attribute; @@ -983,17 +994,19 @@ namespace pugi struct xml_node_struct { - xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(page, type - 1) + xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(page, type - 1), namevalue_base(0) { PUGI__STATIC_ASSERT(sizeof(xml_node_struct) == 16); } impl::compact_header header; - unsigned char padding; + unsigned char padding[3]; + + uint16_t namevalue_base; - impl::compact_string_fat<4> name; - impl::compact_string_fat<7> value; + impl::compact_string<8, 2> name; + impl::compact_string<9, 3> value; impl::compact_pointer_parent parent; -- cgit v1.2.3 From dec4267fb15c3dacb767402d5a696090dae3648d Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 2 May 2015 08:56:39 -0700 Subject: Implement efficient compact_header storage Header is now just 2 bytes, with optional additonal 4 bytes that are only allocated for every 85 nodes / 128 attributes. --- src/pugixml.cpp | 117 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 70dd037..40e8ab8 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -409,8 +409,16 @@ PUGI__NS_BEGIN #endif ; +#ifdef PUGIXML_COMPACT + static const uintptr_t xml_memory_block_alignment = 4; + + static const uintptr_t xml_memory_page_alignment = sizeof(void*); +#else + static const uintptr_t xml_memory_block_alignment = sizeof(void*); + static const uintptr_t xml_memory_page_alignment = 64; static const uintptr_t xml_memory_page_pointer_mask = ~(xml_memory_page_alignment - 1); +#endif // extra metadata bits static const uintptr_t xml_memory_page_contents_shared_mask = 32; @@ -422,15 +430,6 @@ PUGI__NS_BEGIN static const uintptr_t xml_memory_page_name_allocated_or_shared_mask = xml_memory_page_name_allocated_mask | xml_memory_page_contents_shared_mask; static const uintptr_t xml_memory_page_value_allocated_or_shared_mask = xml_memory_page_value_allocated_mask | xml_memory_page_contents_shared_mask; - // all allocated blocks have a certain guaranteed alignment - static const uintptr_t xml_memory_block_alignment = - #ifdef PUGIXML_COMPACT - 4 - #else - sizeof(void*) - #endif - ; - #ifdef PUGIXML_COMPACT #define PUGI__GETPAGE_IMPL(header) (header).get_page() #else @@ -457,6 +456,7 @@ PUGI__NS_BEGIN #ifdef PUGIXML_COMPACT result->compact_string_base = 0; result->compact_shared_parent = 0; + result->compact_page_marker = 0; #endif return result; @@ -473,6 +473,7 @@ PUGI__NS_BEGIN #ifdef PUGIXML_COMPACT char_t* compact_string_base; void* compact_shared_parent; + char* compact_page_marker; #endif }; @@ -537,6 +538,35 @@ PUGI__NS_BEGIN return buf; } + void* allocate_object(size_t size, xml_memory_page*& out_page) + { + #ifdef PUGIXML_COMPACT + void* result = allocate_memory(size + sizeof(uint32_t), out_page); + if (!result) return 0; + + // adjust for marker + if (PUGI__UNLIKELY(static_cast(static_cast(result) - out_page->compact_page_marker) >= 256 * xml_memory_block_alignment)) + { + // insert new marker + uint32_t* marker = static_cast(result); + + *marker = reinterpret_cast(marker) - reinterpret_cast(out_page); + out_page->compact_page_marker = reinterpret_cast(marker); + + return marker + 1; + } + else + { + // roll back uint32_t part + _busy_size -= sizeof(uint32_t); + + return result; + } + #else + return allocate_memory(size, out_page); + #endif + } + void deallocate_memory(void* ptr, size_t size, xml_memory_page* page) { if (page == _root) page->busy_size = _busy_size; @@ -694,41 +724,39 @@ PUGI__NS_BEGIN { PUGI__STATIC_ASSERT(xml_memory_block_alignment == compact_alignment); PUGI__STATIC_ASSERT(sizeof(xml_memory_page) + xml_memory_page_size <= (1 << (16 + compact_alignment_log2))); - PUGI__STATIC_ASSERT(xml_memory_page_pointer_mask & 0xff); - ptrdiff_t page_offset = (reinterpret_cast(this) - reinterpret_cast(page)) >> compact_alignment_log2; - assert(page_offset >= 0 && page_offset < (1 << 16)); + ptrdiff_t offset = (reinterpret_cast(this) - page->compact_page_marker); + assert(offset >= 0 && offset < 256 << compact_alignment_log2); - this->page0 = static_cast(page_offset); - this->page1 = static_cast(page_offset >> 8); - this->flags = static_cast(flags); + _page = static_cast(offset >> compact_alignment_log2); + _flags = static_cast(flags); } void operator&=(uintptr_t mod) { - flags &= mod; + _flags &= mod; } void operator|=(uintptr_t mod) { - flags |= mod; + _flags |= mod; } uintptr_t operator&(uintptr_t mod) const { - return flags & mod; + return _flags & mod; } xml_memory_page* get_page() const { - unsigned int page_offset = page0 + (page1 << 8); + const char* page_marker = reinterpret_cast(this) - (_page << compact_alignment_log2); - return const_cast(reinterpret_cast(reinterpret_cast(this) - (page_offset << compact_alignment_log2))); + return const_cast(reinterpret_cast(page_marker - *reinterpret_cast(page_marker))); } private: - unsigned char page0, page1; - unsigned char flags; + unsigned char _page; + unsigned char _flags; }; PUGI__FN xml_memory_page* compact_get_page(const void* object, int header_offset) @@ -976,46 +1004,42 @@ namespace pugi { xml_attribute_struct(impl::xml_memory_page* page): header(page, 0), namevalue_base(0) { - PUGI__STATIC_ASSERT(sizeof(xml_attribute_struct) == 12); + PUGI__STATIC_ASSERT(sizeof(xml_attribute_struct) == 8); } impl::compact_header header; - unsigned char padding[3]; - uint16_t namevalue_base; - impl::compact_string<8, 2> name; - impl::compact_string<9, 3> value; + impl::compact_string<4, 2> name; + impl::compact_string<5, 3> value; - impl::compact_pointer prev_attribute_c; - impl::compact_pointer next_attribute; + impl::compact_pointer prev_attribute_c; + impl::compact_pointer next_attribute; }; struct xml_node_struct { xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(page, type - 1), namevalue_base(0) { - PUGI__STATIC_ASSERT(sizeof(xml_node_struct) == 16); + PUGI__STATIC_ASSERT(sizeof(xml_node_struct) == 12); } impl::compact_header header; - unsigned char padding[3]; - uint16_t namevalue_base; - impl::compact_string<8, 2> name; - impl::compact_string<9, 3> value; + impl::compact_string<4, 2> name; + impl::compact_string<5, 3> value; - impl::compact_pointer_parent parent; + impl::compact_pointer_parent parent; - impl::compact_pointer first_child; + impl::compact_pointer first_child; - impl::compact_pointer prev_sibling_c; - impl::compact_pointer next_sibling; + impl::compact_pointer prev_sibling_c; + impl::compact_pointer next_sibling; - impl::compact_pointer first_attribute; + impl::compact_pointer first_attribute; }; } #else @@ -1104,7 +1128,7 @@ PUGI__NS_BEGIN inline xml_attribute_struct* allocate_attribute(xml_allocator& alloc) { xml_memory_page* page; - void* memory = alloc.allocate_memory(sizeof(xml_attribute_struct), page); + void* memory = alloc.allocate_object(sizeof(xml_attribute_struct), page); return new (memory) xml_attribute_struct(page); } @@ -1112,7 +1136,7 @@ PUGI__NS_BEGIN inline xml_node_struct* allocate_node(xml_allocator& alloc, xml_node_type type) { xml_memory_page* page; - void* memory = alloc.allocate_memory(sizeof(xml_node_struct), page); + void* memory = alloc.allocate_object(sizeof(xml_node_struct), page); return new (memory) xml_node_struct(page, type); } @@ -6657,7 +6681,16 @@ namespace pugi page->busy_size = impl::xml_memory_page_size; // allocate new root - _root = new (reinterpret_cast(page) + sizeof(impl::xml_memory_page)) impl::xml_document_struct(page); + #ifdef PUGIXML_COMPACT + const size_t page_offset = sizeof(uint32_t); + + page->compact_page_marker = reinterpret_cast(page) + sizeof(impl::xml_memory_page); + *reinterpret_cast(page->compact_page_marker) = sizeof(impl::xml_memory_page); + #else + const size_t page_offset = 0; + #endif + + _root = new (reinterpret_cast(page) + sizeof(impl::xml_memory_page) + page_offset) impl::xml_document_struct(page); _root->prev_sibling_c = _root; // setup sentinel page -- cgit v1.2.3 From b1578e32a54e074584070498767152cc19f99008 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 2 May 2015 09:38:14 -0700 Subject: Fix node deallocation When we deallocate nodes/attributes that allocated the marker we have to adjust the size accordingly, and dismiss the marker in case it gets overwritten with something else... --- src/pugixml.cpp | 109 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 67 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 40e8ab8..eab6e6c 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -538,35 +538,6 @@ PUGI__NS_BEGIN return buf; } - void* allocate_object(size_t size, xml_memory_page*& out_page) - { - #ifdef PUGIXML_COMPACT - void* result = allocate_memory(size + sizeof(uint32_t), out_page); - if (!result) return 0; - - // adjust for marker - if (PUGI__UNLIKELY(static_cast(static_cast(result) - out_page->compact_page_marker) >= 256 * xml_memory_block_alignment)) - { - // insert new marker - uint32_t* marker = static_cast(result); - - *marker = reinterpret_cast(marker) - reinterpret_cast(out_page); - out_page->compact_page_marker = reinterpret_cast(marker); - - return marker + 1; - } - else - { - // roll back uint32_t part - _busy_size -= sizeof(uint32_t); - - return result; - } - #else - return allocate_memory(size, out_page); - #endif - } - void deallocate_memory(void* ptr, size_t size, xml_memory_page* page) { if (page == _root) page->busy_size = _busy_size; @@ -703,10 +674,9 @@ PUGI__NS_BEGIN _root->prev->next = page; _root->prev = page; - } - // allocate inside page - page->busy_size = size; + page->busy_size = size; + } return reinterpret_cast(page) + sizeof(xml_memory_page); } @@ -754,6 +724,11 @@ PUGI__NS_BEGIN return const_cast(reinterpret_cast(page_marker - *reinterpret_cast(page_marker))); } + bool has_marker() const + { + return _page == sizeof(uint32_t) >> compact_alignment_log2; + } + private: unsigned char _page; unsigned char _flags; @@ -1125,10 +1100,57 @@ PUGI__NS_END // Low-level DOM operations PUGI__NS_BEGIN +#ifdef PUGIXML_COMPACT + inline void* allocate_object(xml_allocator& alloc, size_t size, xml_memory_page*& out_page) + { + void* result = alloc.allocate_memory(size + sizeof(uint32_t), out_page); + if (!result) return 0; + + // adjust for marker + if (PUGI__UNLIKELY(static_cast(static_cast(result) - out_page->compact_page_marker) >= 256 * compact_alignment)) + { + // insert new marker + uint32_t* marker = static_cast(result); + + *marker = reinterpret_cast(marker) - reinterpret_cast(out_page); + out_page->compact_page_marker = reinterpret_cast(marker); + + return marker + 1; + } + else + { + // roll back uint32_t part + alloc._busy_size -= sizeof(uint32_t); + + return result; + } + } + + template + inline void deallocate_object(xml_allocator& alloc, T* ptr, size_t size, xml_memory_page* page) + { + // this is very crude... we should be able to do better? + if (ptr->header.has_marker()) + page->compact_page_marker = 0; + + alloc.deallocate_memory(ptr, size + ptr->header.has_marker() * sizeof(uint32_t), page); + } +#else + inline void* allocate_object(xml_allocator& alloc, size_t size, xml_memory_page*& out_page) + { + return alloc.allocate_memory(size, out_page); + } + + inline void deallocate_object(xml_allocator& alloc, void* ptr, size_t size, xml_memory_page* page) + { + alloc.deallocate_memory(ptr, size, page); + } +#endif + inline xml_attribute_struct* allocate_attribute(xml_allocator& alloc) { xml_memory_page* page; - void* memory = alloc.allocate_object(sizeof(xml_attribute_struct), page); + void* memory = allocate_object(alloc, sizeof(xml_attribute_struct), page); return new (memory) xml_attribute_struct(page); } @@ -1136,7 +1158,7 @@ PUGI__NS_BEGIN inline xml_node_struct* allocate_node(xml_allocator& alloc, xml_node_type type) { xml_memory_page* page; - void* memory = alloc.allocate_object(sizeof(xml_node_struct), page); + void* memory = allocate_object(alloc, sizeof(xml_node_struct), page); return new (memory) xml_node_struct(page, type); } @@ -1149,7 +1171,7 @@ PUGI__NS_BEGIN if (a->header & impl::xml_memory_page_value_allocated_mask) alloc.deallocate_string(a->value); - alloc.deallocate_memory(a, sizeof(xml_attribute_struct), PUGI__GETPAGE(a)); + deallocate_object(alloc, a, sizeof(xml_attribute_struct), PUGI__GETPAGE(a)); } inline void destroy_node(xml_node_struct* n, xml_allocator& alloc) @@ -1178,7 +1200,7 @@ PUGI__NS_BEGIN child = next; } - alloc.deallocate_memory(n, sizeof(xml_node_struct), PUGI__GETPAGE(n)); + deallocate_object(alloc, n, sizeof(xml_node_struct), PUGI__GETPAGE(n)); } inline void append_node(xml_node_struct* child, xml_node_struct* node) @@ -6668,8 +6690,14 @@ namespace pugi { assert(!_root); + #ifdef PUGIXML_COMPACT + const size_t page_offset = sizeof(uint32_t); + #else + const size_t page_offset = 0; + #endif + // initialize sentinel page - PUGI__STATIC_ASSERT(sizeof(impl::xml_memory_page) + sizeof(impl::xml_document_struct) + impl::xml_memory_page_alignment - sizeof(void*) <= sizeof(_memory)); + PUGI__STATIC_ASSERT(sizeof(impl::xml_memory_page) + sizeof(impl::xml_document_struct) + impl::xml_memory_page_alignment - sizeof(void*) + page_offset <= sizeof(_memory)); // align upwards to page boundary void* page_memory = reinterpret_cast((reinterpret_cast(_memory) + (impl::xml_memory_page_alignment - 1)) & ~(impl::xml_memory_page_alignment - 1)); @@ -6680,16 +6708,13 @@ namespace pugi page->busy_size = impl::xml_memory_page_size; - // allocate new root + // setup first page marker #ifdef PUGIXML_COMPACT - const size_t page_offset = sizeof(uint32_t); - page->compact_page_marker = reinterpret_cast(page) + sizeof(impl::xml_memory_page); *reinterpret_cast(page->compact_page_marker) = sizeof(impl::xml_memory_page); - #else - const size_t page_offset = 0; #endif + // allocate new root _root = new (reinterpret_cast(page) + sizeof(impl::xml_memory_page) + page_offset) impl::xml_document_struct(page); _root->prev_sibling_c = _root; -- cgit v1.2.3 From 613301ce5143f0ce5f00f914d27d309b2e2efd75 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 2 May 2015 14:52:27 -0700 Subject: Optimize compact_string First assignment uses a fast path; second assignment uses a specialized path as well. --- src/pugixml.cpp | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index eab6e6c..c98ee40 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -527,7 +527,8 @@ PUGI__NS_BEGIN void* allocate_memory(size_t size, xml_memory_page*& out_page) { - if (_busy_size + size > xml_memory_page_size) return allocate_memory_oob(size, out_page); + if (PUGI__UNLIKELY(_busy_size + size > xml_memory_page_size)) + return allocate_memory_oob(size, out_page); void* buf = reinterpret_cast(_root) + sizeof(xml_memory_page) + _busy_size; @@ -916,16 +917,9 @@ PUGI__NS_BEGIN if (PUGI__UNLIKELY(page->compact_string_base == 0)) page->compact_string_base = value; - uint16_t* base = reinterpret_cast(reinterpret_cast(this) - base_offset); - ptrdiff_t offset = value - page->compact_string_base; - if (*base == 0) - *base = static_cast(offset >> 7) + 1; - - ptrdiff_t remainder = offset - ((*base - 1) << 7); - - if (PUGI__UNLIKELY(static_cast(remainder) >= 254 || *base == 0)) + if (PUGI__UNLIKELY(static_cast(offset) >= (65535 << 7))) { compact_set_value(this, value); @@ -933,7 +927,28 @@ PUGI__NS_BEGIN } else { - _data = static_cast(remainder + 1); + uint16_t* base = reinterpret_cast(reinterpret_cast(this) - base_offset); + + if (PUGI__UNLIKELY(*base)) + { + ptrdiff_t remainder = offset - ((*base - 1) << 7); + + if (PUGI__UNLIKELY(static_cast(remainder) >= 254)) + { + compact_set_value(this, value); + + _data = 255; + } + else + { + _data = static_cast(remainder + 1); + } + } + else + { + *base = static_cast((offset >> 7) + 1); + _data = static_cast((offset & 127) + 1); + } } } else -- cgit v1.2.3 From fa8663c066e98170dd385bff7bc33e7cfeabfcc4 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 2 May 2015 15:40:30 -0700 Subject: Revise marker deletion strategy Instead of checking if the object being removed allocated a marker, mark the marker block as deleted immediately upon allocation. This simplifies the logic and prevents extra markers from being inserted if we allocate/deallocate the same node indefinitely. Also change marker pointer type to uint32_t*. --- src/pugixml.cpp | 51 ++++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index c98ee40..033bf13 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -473,7 +473,7 @@ PUGI__NS_BEGIN #ifdef PUGIXML_COMPACT char_t* compact_string_base; void* compact_shared_parent; - char* compact_page_marker; + uint32_t* compact_page_marker; #endif }; @@ -556,7 +556,16 @@ PUGI__NS_BEGIN assert(_root == page); // top page freed, just reset sizes - page->busy_size = page->freed_size = 0; + page->busy_size = 0; + page->freed_size = 0; + + #ifdef PUGIXML_COMPACT + // reset compact state to maximize efficiency + page->compact_string_base = 0; + page->compact_shared_parent = 0; + page->compact_page_marker = 0; + #endif + _busy_size = 0; } else @@ -696,7 +705,7 @@ PUGI__NS_BEGIN PUGI__STATIC_ASSERT(xml_memory_block_alignment == compact_alignment); PUGI__STATIC_ASSERT(sizeof(xml_memory_page) + xml_memory_page_size <= (1 << (16 + compact_alignment_log2))); - ptrdiff_t offset = (reinterpret_cast(this) - page->compact_page_marker); + ptrdiff_t offset = (reinterpret_cast(this) - reinterpret_cast(page->compact_page_marker)); assert(offset >= 0 && offset < 256 << compact_alignment_log2); _page = static_cast(offset >> compact_alignment_log2); @@ -725,11 +734,6 @@ PUGI__NS_BEGIN return const_cast(reinterpret_cast(page_marker - *reinterpret_cast(page_marker))); } - bool has_marker() const - { - return _page == sizeof(uint32_t) >> compact_alignment_log2; - } - private: unsigned char _page; unsigned char _flags; @@ -1122,13 +1126,17 @@ PUGI__NS_BEGIN if (!result) return 0; // adjust for marker - if (PUGI__UNLIKELY(static_cast(static_cast(result) - out_page->compact_page_marker) >= 256 * compact_alignment)) + if (PUGI__UNLIKELY(static_cast(static_cast(result) - reinterpret_cast(out_page->compact_page_marker)) >= 256 * compact_alignment)) { // insert new marker uint32_t* marker = static_cast(result); *marker = reinterpret_cast(marker) - reinterpret_cast(out_page); - out_page->compact_page_marker = reinterpret_cast(marker); + out_page->compact_page_marker = marker; + + // since we don't reuse the page space until we reallocate it, we can just pretend that we freed the marker block + // this will make sure deallocate_memory correctly tracks the size + out_page->freed_size += sizeof(uint32_t); return marker + 1; } @@ -1140,26 +1148,11 @@ PUGI__NS_BEGIN return result; } } - - template - inline void deallocate_object(xml_allocator& alloc, T* ptr, size_t size, xml_memory_page* page) - { - // this is very crude... we should be able to do better? - if (ptr->header.has_marker()) - page->compact_page_marker = 0; - - alloc.deallocate_memory(ptr, size + ptr->header.has_marker() * sizeof(uint32_t), page); - } #else inline void* allocate_object(xml_allocator& alloc, size_t size, xml_memory_page*& out_page) { return alloc.allocate_memory(size, out_page); } - - inline void deallocate_object(xml_allocator& alloc, void* ptr, size_t size, xml_memory_page* page) - { - alloc.deallocate_memory(ptr, size, page); - } #endif inline xml_attribute_struct* allocate_attribute(xml_allocator& alloc) @@ -1186,7 +1179,7 @@ PUGI__NS_BEGIN if (a->header & impl::xml_memory_page_value_allocated_mask) alloc.deallocate_string(a->value); - deallocate_object(alloc, a, sizeof(xml_attribute_struct), PUGI__GETPAGE(a)); + alloc.deallocate_memory(a, sizeof(xml_attribute_struct), PUGI__GETPAGE(a)); } inline void destroy_node(xml_node_struct* n, xml_allocator& alloc) @@ -1215,7 +1208,7 @@ PUGI__NS_BEGIN child = next; } - deallocate_object(alloc, n, sizeof(xml_node_struct), PUGI__GETPAGE(n)); + alloc.deallocate_memory(n, sizeof(xml_node_struct), PUGI__GETPAGE(n)); } inline void append_node(xml_node_struct* child, xml_node_struct* node) @@ -6725,8 +6718,8 @@ namespace pugi // setup first page marker #ifdef PUGIXML_COMPACT - page->compact_page_marker = reinterpret_cast(page) + sizeof(impl::xml_memory_page); - *reinterpret_cast(page->compact_page_marker) = sizeof(impl::xml_memory_page); + page->compact_page_marker = reinterpret_cast(reinterpret_cast(page) + sizeof(impl::xml_memory_page)); + *page->compact_page_marker = sizeof(impl::xml_memory_page); #endif // allocate new root -- cgit v1.2.3 From f8915c8eab0241d2a9621e28893518c010aa0cb5 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 2 May 2015 15:44:28 -0700 Subject: Minor refactoring --- src/pugixml.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 033bf13..1a7acb3 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -679,13 +679,13 @@ PUGI__NS_BEGIN // the last page is not deleted even if it's empty (see deallocate_memory) assert(_root->prev); + page->busy_size = size; + page->prev = _root->prev; page->next = _root; _root->prev->next = page; _root->prev = page; - - page->busy_size = size; } return reinterpret_cast(page) + sizeof(xml_memory_page); @@ -703,10 +703,9 @@ PUGI__NS_BEGIN compact_header(xml_memory_page* page, unsigned int flags) { PUGI__STATIC_ASSERT(xml_memory_block_alignment == compact_alignment); - PUGI__STATIC_ASSERT(sizeof(xml_memory_page) + xml_memory_page_size <= (1 << (16 + compact_alignment_log2))); ptrdiff_t offset = (reinterpret_cast(this) - reinterpret_cast(page->compact_page_marker)); - assert(offset >= 0 && offset < 256 << compact_alignment_log2); + assert(static_cast(offset) < 256 * compact_alignment); _page = static_cast(offset >> compact_alignment_log2); _flags = static_cast(flags); @@ -730,8 +729,9 @@ PUGI__NS_BEGIN xml_memory_page* get_page() const { const char* page_marker = reinterpret_cast(this) - (_page << compact_alignment_log2); + const char* page = page_marker - *reinterpret_cast(page_marker); - return const_cast(reinterpret_cast(page_marker - *reinterpret_cast(page_marker))); + return const_cast(reinterpret_cast(page)); } private: @@ -775,7 +775,7 @@ PUGI__NS_BEGIN // value is guaranteed to be compact-aligned; this is not // our decoding is based on this aligned to compact alignment downwards (see operator T*) // so for negative offsets (e.g. -3) we need to adjust the diff by compact_alignment - 1 to - // compensate for arithmetic shift behavior for negative values + // compensate for arithmetic shift rounding for negative values ptrdiff_t diff = reinterpret_cast(value) - reinterpret_cast(this); ptrdiff_t offset = ((diff + int(compact_alignment - 1)) >> compact_alignment_log2) - start; -- cgit v1.2.3 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 From f67e7619704ff362e5e93b3b205572457b5a2376 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 2 May 2015 16:41:21 -0700 Subject: Fix MSVC build --- src/pugixml.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 568eb17..1e49b8f 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -133,16 +133,16 @@ using std::memmove; #if !defined(_MSC_VER) || _MSC_VER >= 1600 # include #else +namespace pugi +{ # ifndef _UINTPTR_T_DEFINED -// No native uintptr_t in MSVC6 and in some WinCE versions -typedef size_t uintptr_t; -#define _UINTPTR_T_DEFINED + typedef size_t uintptr_t; # endif -PUGI__NS_BEGIN + typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; -PUGI__NS_END +} #endif // Memory allocation -- cgit v1.2.3 From b1965061afffaa7c7290dfdfc53b5bbf19d73c01 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sun, 3 May 2015 09:21:23 -0700 Subject: Fix MSVC warning --- src/pugixml.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 1e49b8f..7b19c2a 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -1129,7 +1129,7 @@ PUGI__NS_BEGIN // insert new marker uint32_t* marker = static_cast(result); - *marker = reinterpret_cast(marker) - reinterpret_cast(out_page); + *marker = static_cast(reinterpret_cast(marker) - reinterpret_cast(out_page)); out_page->compact_page_marker = marker; // since we don't reuse the page space until we reallocate it, we can just pretend that we freed the marker block -- cgit v1.2.3 From 9597265a122ce0ef8b2bb0099bb106ee85a74289 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sun, 3 May 2015 10:01:04 -0700 Subject: Cleanup before merge --- src/pugixml.cpp | 129 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 69 insertions(+), 60 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 7b19c2a..caf4ad3 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -430,11 +430,11 @@ PUGI__NS_BEGIN static const uintptr_t xml_memory_page_name_allocated_or_shared_mask = xml_memory_page_name_allocated_mask | xml_memory_page_contents_shared_mask; static const uintptr_t xml_memory_page_value_allocated_or_shared_mask = xml_memory_page_value_allocated_mask | xml_memory_page_contents_shared_mask; - #ifdef PUGIXML_COMPACT - #define PUGI__GETPAGE_IMPL(header) (header).get_page() - #else - #define PUGI__GETPAGE_IMPL(header) reinterpret_cast((header) & impl::xml_memory_page_pointer_mask) - #endif +#ifdef PUGIXML_COMPACT + #define PUGI__GETPAGE_IMPL(header) (header).get_page() +#else + #define PUGI__GETPAGE_IMPL(header) reinterpret_cast((header) & impl::xml_memory_page_pointer_mask) +#endif #define PUGI__GETPAGE(n) PUGI__GETPAGE_IMPL((n)->header) #define PUGI__NODETYPE(n) static_cast(((n)->header & impl::xml_memory_page_type_mask) + 1) @@ -539,6 +539,44 @@ PUGI__NS_BEGIN return buf; } + #ifdef PUGIXML_COMPACT + void* allocate_object(size_t size, xml_memory_page*& out_page) + { + void* result = allocate_memory(size + sizeof(uint32_t), out_page); + if (!result) return 0; + + // adjust for marker + ptrdiff_t offset = static_cast(result) - reinterpret_cast(out_page->compact_page_marker); + + if (PUGI__UNLIKELY(static_cast(offset) >= 256 * xml_memory_block_alignment)) + { + // insert new marker + uint32_t* marker = static_cast(result); + + *marker = static_cast(reinterpret_cast(marker) - reinterpret_cast(out_page)); + out_page->compact_page_marker = marker; + + // since we don't reuse the page space until we reallocate it, we can just pretend that we freed the marker block + // this will make sure deallocate_memory correctly tracks the size + out_page->freed_size += sizeof(uint32_t); + + return marker + 1; + } + else + { + // roll back uint32_t part + _busy_size -= sizeof(uint32_t); + + return result; + } + } + #else + void* allocate_object(size_t size, xml_memory_page*& out_page) + { + return allocate_memory(size, out_page); + } + #endif + void deallocate_memory(void* ptr, size_t size, xml_memory_page* page) { if (page == _root) page->busy_size = _busy_size; @@ -679,13 +717,13 @@ PUGI__NS_BEGIN // the last page is not deleted even if it's empty (see deallocate_memory) assert(_root->prev); - page->busy_size = size; - page->prev = _root->prev; page->next = _root; _root->prev->next = page; _root->prev = page; + + page->busy_size = size; } return reinterpret_cast(page) + sizeof(xml_memory_page); @@ -705,7 +743,7 @@ PUGI__NS_BEGIN PUGI__STATIC_ASSERT(xml_memory_block_alignment == compact_alignment); ptrdiff_t offset = (reinterpret_cast(this) - reinterpret_cast(page->compact_page_marker)); - assert(static_cast(offset) < 256 * compact_alignment); + assert(offset % compact_alignment == 0 && static_cast(offset) < 256 * compact_alignment); _page = static_cast(offset >> compact_alignment_log2); _flags = static_cast(flags); @@ -772,8 +810,8 @@ PUGI__NS_BEGIN { if (value) { - // value is guaranteed to be compact-aligned; this is not - // our decoding is based on this aligned to compact alignment downwards (see operator T*) + // value is guaranteed to be compact-aligned; 'this' is not + // our decoding is based on 'this' aligned to compact alignment downwards (see operator T*) // so for negative offsets (e.g. -3) we need to adjust the diff by compact_alignment - 1 to // compensate for arithmetic shift rounding for negative values ptrdiff_t diff = reinterpret_cast(value) - reinterpret_cast(this); @@ -834,8 +872,8 @@ PUGI__NS_BEGIN { if (value) { - // value is guaranteed to be compact-aligned; this is not - // our decoding is based on this aligned to compact alignment downwards (see operator T*) + // value is guaranteed to be compact-aligned; 'this' is not + // our decoding is based on 'this' aligned to compact alignment downwards (see operator T*) // so for negative offsets (e.g. -3) we need to adjust the diff by compact_alignment - 1 to // compensate for arithmetic shift behavior for negative values ptrdiff_t diff = reinterpret_cast(value) - reinterpret_cast(this); @@ -934,7 +972,7 @@ PUGI__NS_BEGIN { ptrdiff_t remainder = offset - ((*base - 1) << 7); - if (static_cast(remainder) < 254) + if (static_cast(remainder) <= 253) { _data = static_cast(remainder + 1); } @@ -1117,46 +1155,10 @@ PUGI__NS_END // Low-level DOM operations PUGI__NS_BEGIN -#ifdef PUGIXML_COMPACT - inline void* allocate_object(xml_allocator& alloc, size_t size, xml_memory_page*& out_page) - { - void* result = alloc.allocate_memory(size + sizeof(uint32_t), out_page); - if (!result) return 0; - - // adjust for marker - if (PUGI__UNLIKELY(static_cast(static_cast(result) - reinterpret_cast(out_page->compact_page_marker)) >= 256 * compact_alignment)) - { - // insert new marker - uint32_t* marker = static_cast(result); - - *marker = static_cast(reinterpret_cast(marker) - reinterpret_cast(out_page)); - out_page->compact_page_marker = marker; - - // since we don't reuse the page space until we reallocate it, we can just pretend that we freed the marker block - // this will make sure deallocate_memory correctly tracks the size - out_page->freed_size += sizeof(uint32_t); - - return marker + 1; - } - else - { - // roll back uint32_t part - alloc._busy_size -= sizeof(uint32_t); - - return result; - } - } -#else - inline void* allocate_object(xml_allocator& alloc, size_t size, xml_memory_page*& out_page) - { - return alloc.allocate_memory(size, out_page); - } -#endif - inline xml_attribute_struct* allocate_attribute(xml_allocator& alloc) { xml_memory_page* page; - void* memory = allocate_object(alloc, sizeof(xml_attribute_struct), page); + void* memory = alloc.allocate_object(sizeof(xml_attribute_struct), page); return new (memory) xml_attribute_struct(page); } @@ -1164,7 +1166,7 @@ PUGI__NS_BEGIN inline xml_node_struct* allocate_node(xml_allocator& alloc, xml_node_type type) { xml_memory_page* page; - void* memory = allocate_object(alloc, sizeof(xml_node_struct), page); + void* memory = alloc.allocate_object(sizeof(xml_node_struct), page); return new (memory) xml_node_struct(page, type); } @@ -6174,18 +6176,25 @@ namespace pugi // we can determine the offset reliably only if there is exactly once parse buffer if (!doc.buffer || doc.extra_buffers) return -1; - // document node always has an offset of 0 - if (_root == &doc) return 0; + switch (type()) + { + case node_document: + return 0; - // we need contents to be inside buffer and not shared - // if it's shared we don't know if this is the original node - if (_root->name && (_root->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0) - return _root->name - doc.buffer; + case node_element: + case node_declaration: + case node_pi: + return _root->name && (_root->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0 ? _root->name - doc.buffer : -1; - if (_root->value && (_root->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0) - return _root->value - doc.buffer; + case node_pcdata: + case node_cdata: + case node_comment: + case node_doctype: + return _root->value && (_root->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0 ? _root->value - doc.buffer : -1; - return -1; + default: + return -1; + } } #ifdef __BORLANDC__ -- cgit v1.2.3