From f9f0f9df52afa0a5ac46e9dbbc3b5c6657d6e6ae Mon Sep 17 00:00:00 2001 From: Daniel Parker Date: Mon, 28 Oct 2024 17:27:09 -0400 Subject: [PATCH] Improve basic_json storage --- include/jsoncons/basic_json.hpp | 83 ++++++++------------------------- 1 file changed, 19 insertions(+), 64 deletions(-) diff --git a/include/jsoncons/basic_json.hpp b/include/jsoncons/basic_json.hpp index 8f0533e69..054711fce 100644 --- a/include/jsoncons/basic_json.hpp +++ b/include/jsoncons/basic_json.hpp @@ -641,17 +641,10 @@ namespace jsoncons { { ptr_ = heap_string_factory_type::create(data, length, null_type(), alloc); } - long_string_storage(const long_string_storage& other) - : storage_kind_(static_cast(json_storage_kind::long_str)), short_str_length_(0), tag_(other.tag_) - { - ptr_ = heap_string_factory_type::create(other.data(), other.length(), null_type(), - std::allocator_traits::select_on_container_copy_construction(other.get_allocator())); - } - long_string_storage(const long_string_storage& other, const Allocator& alloc) - : storage_kind_(static_cast(json_storage_kind::long_str)), short_str_length_(0), tag_(other.tag_) + long_string_storage(const long_string_storage& other) + : storage_kind_(static_cast(json_storage_kind::long_str)), short_str_length_(0), tag_(other.tag_), ptr_(other.ptr_) { - ptr_ = heap_string_factory_type::create(other.data(), other.length(), null_type(), alloc); } long_string_storage(long_string_storage&& other) noexcept @@ -760,16 +753,8 @@ namespace jsoncons { } byte_string_storage(const byte_string_storage& other) - : storage_kind_(other.storage_kind_), short_str_length_(0), tag_(other.tag_) + : storage_kind_(other.storage_kind_), short_str_length_(0), tag_(other.tag_), ptr_(other.ptr_) { - ptr_ = heap_string_factory_type::create(other.data(), other.length(), other.ext_tag(), - std::allocator_traits::select_on_container_copy_construction(other.get_allocator())); - } - - byte_string_storage(const byte_string_storage& other, const Allocator& alloc) - : storage_kind_(other.storage_kind_), short_str_length_(0), tag_(other.tag_) - { - ptr_ = heap_string_factory_type::create(other.data(), other.length(), other.ext_tag(), alloc); } byte_string_storage(byte_string_storage&& other) noexcept @@ -903,11 +888,10 @@ namespace jsoncons { } array_storage(const array_storage& other) - : storage_kind_(other.storage_kind_), short_str_length_(0), tag_(other.tag_), ptr_(nullptr) + : storage_kind_(other.storage_kind_), short_str_length_(0), tag_(other.tag_), ptr_(other.ptr_) { - create(std::allocator_traits::select_on_container_copy_construction(other.get_allocator()), *(other.ptr_)); } - + array_storage(array_storage&& other) noexcept : storage_kind_(other.storage_kind_), short_str_length_(0), tag_(other.tag_), ptr_(nullptr) @@ -920,12 +904,6 @@ namespace jsoncons { other.tag_ = semantic_tag::none; } - array_storage(const array_storage& other, const Allocator& alloc) - : storage_kind_(other.storage_kind_), short_str_length_(0), tag_(other.tag_), ptr_(nullptr) - { - create(allocator_type(alloc), *(other.ptr_)); - } - array_storage(array_storage&& other, const Allocator& alloc) : storage_kind_(other.storage_kind_), short_str_length_(0), tag_(other.tag_), ptr_(nullptr) { @@ -1039,15 +1017,8 @@ namespace jsoncons { } explicit object_storage(const object_storage& other) - : storage_kind_(other.storage_kind_), short_str_length_(0), tag_(other.tag_), ptr_(nullptr) - { - create(std::allocator_traits::select_on_container_copy_construction(other.get_allocator()), *(other.ptr_)); - } - - object_storage(const object_storage& other, const Allocator& alloc) - : storage_kind_(other.storage_kind_), short_str_length_(0), tag_(other.tag_), ptr_(nullptr) + : storage_kind_(other.storage_kind_), short_str_length_(0), tag_(other.tag_), ptr_(other.ptr_) { - create(allocator_type(alloc), *(other.ptr_)); } explicit object_storage(object_storage&& other) noexcept @@ -2065,33 +2036,9 @@ namespace jsoncons { template void swap_l_r(identity,identity,basic_json& other) { - TypeR tmpR(std::move(other.cast())); - - other.destroy(); - other.construct(std::move(cast())); - - destroy(); - construct(std::move(tmpR)); - } - - void swap_l_r(identity,identity,basic_json& other) noexcept - { - cast().swap(other.cast()); - } - - void swap_l_r(identity,identity,basic_json& other) noexcept - { - cast().swap(other.cast()); - } - - void swap_l_r(identity,identity,basic_json& other) noexcept - { - cast().swap(other.cast()); - } - - void swap_l_r(identity,identity,basic_json& other) noexcept - { - cast().swap(other.cast()); + TypeR temp{other.cast()}; + other.construct(cast()); + construct(temp); } template @@ -2178,11 +2125,19 @@ namespace jsoncons { switch (other.storage_kind()) { case json_storage_kind::long_str: - construct(other.cast(),alloc); + { + const auto& storage = other.cast(); + auto ptr = make_long_string(alloc, storage.data(), storage.length()); + construct(ptr, other.tag()); break; + } case json_storage_kind::byte_str: - construct(other.cast(),alloc); + { + const auto& storage = other.cast(); + auto ptr = make_byte_string(alloc, storage.data(), storage.length(), storage.ext_tag()); + construct(ptr, other.tag()); break; + } case json_storage_kind::array: { auto ptr = create_array(alloc, other.cast().value());