Optimize compact_string

First assignment uses a fast path; second assignment uses a specialized
path as well.
This commit is contained in:
Arseny Kapoulkine 2015-05-02 14:52:27 -07:00
parent 19d43d39fc
commit 613301ce51

View File

@ -527,7 +527,8 @@ PUGI__NS_BEGIN
void* allocate_memory(size_t size, xml_memory_page*& out_page) 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<char*>(_root) + sizeof(xml_memory_page) + _busy_size; void* buf = reinterpret_cast<char*>(_root) + sizeof(xml_memory_page) + _busy_size;
@ -916,16 +917,23 @@ PUGI__NS_BEGIN
if (PUGI__UNLIKELY(page->compact_string_base == 0)) if (PUGI__UNLIKELY(page->compact_string_base == 0))
page->compact_string_base = value; page->compact_string_base = value;
uint16_t* base = reinterpret_cast<uint16_t*>(reinterpret_cast<char*>(this) - base_offset);
ptrdiff_t offset = value - page->compact_string_base; ptrdiff_t offset = value - page->compact_string_base;
if (*base == 0) if (PUGI__UNLIKELY(static_cast<uintptr_t>(offset) >= (65535 << 7)))
*base = static_cast<uint16_t>(offset >> 7) + 1; {
compact_set_value<header_offset>(this, value);
_data = 255;
}
else
{
uint16_t* base = reinterpret_cast<uint16_t*>(reinterpret_cast<char*>(this) - base_offset);
if (PUGI__UNLIKELY(*base))
{
ptrdiff_t remainder = offset - ((*base - 1) << 7); ptrdiff_t remainder = offset - ((*base - 1) << 7);
if (PUGI__UNLIKELY(static_cast<uintptr_t>(remainder) >= 254 || *base == 0)) if (PUGI__UNLIKELY(static_cast<uintptr_t>(remainder) >= 254))
{ {
compact_set_value<header_offset>(this, value); compact_set_value<header_offset>(this, value);
@ -937,6 +945,13 @@ PUGI__NS_BEGIN
} }
} }
else else
{
*base = static_cast<uint16_t>((offset >> 7) + 1);
_data = static_cast<unsigned char>((offset & 127) + 1);
}
}
}
else
{ {
_data = 0; _data = 0;
} }