Read iterator pairs of std::string and std::vector as pointer ranges
Before C++20, a pair of iterators of a contiguous container cannot be told from any other random-access iterator, so json::parse(s.begin(), s.end()) on a std::string or std::vector<char> was read one character at a time, without the bulk scans of strings, numbers and indentation that the container itself gets (the container overload of input_adapter() already maps it to a pointer range). The iterators of std::string and of a std::vector of 1-byte integral elements are now matched exactly and mapped to the pointer range they cover; an empty range is not dereferenced. The character type stays the same, so the results and error positions do not change. parse(first, last) of a std::string gets 1.7-2.0x as fast in C++17 on an Apple M1 Max, accept(first, last) 2.7-3.1x, and from_cbor(first, last) of a std::vector 8-23% faster. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
3 files changed
+159
No files matched your search
@@ -1173,6 +1173,55 @@ struct iterator_input_adapter_factory<IteratorType, SentinelType, enable_if_t<is
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief whether @a IteratorType is an iterator of std::string or of a
|
||||||
|
std::vector of 1-byte elements
|
||||||
|
|
||||||
|
Their elements are contiguous, but before C++20 that cannot be detected from
|
||||||
|
the iterator type (std::contiguous_iterator), so an iterator pair of them would
|
||||||
|
be read one character at a time through the iterators. They are read as the
|
||||||
|
pointer range they are instead, as the containers themselves are (see the
|
||||||
|
container overload of input_adapter()). The types are matched exactly, so no
|
||||||
|
other iterator can be mistaken for one. (std::basic_string is only matched for
|
||||||
|
char: other character types need std::char_traits specializations that the
|
||||||
|
standard library need not have.)
|
||||||
|
*/
|
||||||
|
template < typename IteratorType, typename CharType, bool IsByte = sizeof(CharType) == 1 && std::is_integral<CharType>::value && !std::is_same<CharType, bool>::value >
|
||||||
|
struct is_std_contiguous_byte_iterator_impl : std::false_type {};
|
||||||
|
|
||||||
|
// (the containers are only named for byte types)
|
||||||
|
template<typename IteratorType, typename CharType>
|
||||||
|
struct is_std_contiguous_byte_iterator_impl<IteratorType, CharType, true>
|
||||||
|
: std::integral_constant < bool, !std::is_pointer<IteratorType>::value
|
||||||
|
&& (std::is_same<IteratorType, typename std::vector<CharType>::iterator>::value
|
||||||
|
|| std::is_same<IteratorType, typename std::vector<CharType>::const_iterator>::value
|
||||||
|
|| std::is_same<IteratorType, std::string::iterator>::value
|
||||||
|
|| std::is_same<IteratorType, std::string::const_iterator>::value) > {};
|
||||||
|
|
||||||
|
template<typename IteratorType>
|
||||||
|
struct is_std_contiguous_byte_iterator
|
||||||
|
: is_std_contiguous_byte_iterator_impl<IteratorType, typename std::remove_cv<typename std::iterator_traits<IteratorType>::value_type>::type> {};
|
||||||
|
|
||||||
|
template<typename IteratorType>
|
||||||
|
struct iterator_input_adapter_factory<IteratorType, IteratorType, enable_if_t<is_std_contiguous_byte_iterator<IteratorType>::value>>
|
||||||
|
{
|
||||||
|
using iterator_type = IteratorType;
|
||||||
|
using sentinel_type = IteratorType;
|
||||||
|
using char_type = typename std::remove_cv<typename std::iterator_traits<iterator_type>::value_type>::type;
|
||||||
|
using adapter_type = iterator_input_adapter<const char_type*>;
|
||||||
|
|
||||||
|
static adapter_type create(IteratorType first, IteratorType last)
|
||||||
|
{
|
||||||
|
if (first == last)
|
||||||
|
{
|
||||||
|
// (an empty range must not be dereferenced)
|
||||||
|
return adapter_type(nullptr, nullptr);
|
||||||
|
}
|
||||||
|
const char_type* const begin = std::addressof(*first);
|
||||||
|
return adapter_type(begin, begin + (last - first));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// General purpose iterator-based input (iterator+sentinel pair; SentinelType
|
// General purpose iterator-based input (iterator+sentinel pair; SentinelType
|
||||||
// defaults to IteratorType for the common same-type case, but may differ for
|
// defaults to IteratorType for the common same-type case, but may differ for
|
||||||
// C++20 ranges-style iterator+sentinel pairs). Only enable for types that can
|
// C++20 ranges-style iterator+sentinel pairs). Only enable for types that can
|
||||||
|
|||||||
Generated
+49
@@ -10282,6 +10282,55 @@ struct iterator_input_adapter_factory<IteratorType, SentinelType, enable_if_t<is
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief whether @a IteratorType is an iterator of std::string or of a
|
||||||
|
std::vector of 1-byte elements
|
||||||
|
|
||||||
|
Their elements are contiguous, but before C++20 that cannot be detected from
|
||||||
|
the iterator type (std::contiguous_iterator), so an iterator pair of them would
|
||||||
|
be read one character at a time through the iterators. They are read as the
|
||||||
|
pointer range they are instead, as the containers themselves are (see the
|
||||||
|
container overload of input_adapter()). The types are matched exactly, so no
|
||||||
|
other iterator can be mistaken for one. (std::basic_string is only matched for
|
||||||
|
char: other character types need std::char_traits specializations that the
|
||||||
|
standard library need not have.)
|
||||||
|
*/
|
||||||
|
template < typename IteratorType, typename CharType, bool IsByte = sizeof(CharType) == 1 && std::is_integral<CharType>::value && !std::is_same<CharType, bool>::value >
|
||||||
|
struct is_std_contiguous_byte_iterator_impl : std::false_type {};
|
||||||
|
|
||||||
|
// (the containers are only named for byte types)
|
||||||
|
template<typename IteratorType, typename CharType>
|
||||||
|
struct is_std_contiguous_byte_iterator_impl<IteratorType, CharType, true>
|
||||||
|
: std::integral_constant < bool, !std::is_pointer<IteratorType>::value
|
||||||
|
&& (std::is_same<IteratorType, typename std::vector<CharType>::iterator>::value
|
||||||
|
|| std::is_same<IteratorType, typename std::vector<CharType>::const_iterator>::value
|
||||||
|
|| std::is_same<IteratorType, std::string::iterator>::value
|
||||||
|
|| std::is_same<IteratorType, std::string::const_iterator>::value) > {};
|
||||||
|
|
||||||
|
template<typename IteratorType>
|
||||||
|
struct is_std_contiguous_byte_iterator
|
||||||
|
: is_std_contiguous_byte_iterator_impl<IteratorType, typename std::remove_cv<typename std::iterator_traits<IteratorType>::value_type>::type> {};
|
||||||
|
|
||||||
|
template<typename IteratorType>
|
||||||
|
struct iterator_input_adapter_factory<IteratorType, IteratorType, enable_if_t<is_std_contiguous_byte_iterator<IteratorType>::value>>
|
||||||
|
{
|
||||||
|
using iterator_type = IteratorType;
|
||||||
|
using sentinel_type = IteratorType;
|
||||||
|
using char_type = typename std::remove_cv<typename std::iterator_traits<iterator_type>::value_type>::type;
|
||||||
|
using adapter_type = iterator_input_adapter<const char_type*>;
|
||||||
|
|
||||||
|
static adapter_type create(IteratorType first, IteratorType last)
|
||||||
|
{
|
||||||
|
if (first == last)
|
||||||
|
{
|
||||||
|
// (an empty range must not be dereferenced)
|
||||||
|
return adapter_type(nullptr, nullptr);
|
||||||
|
}
|
||||||
|
const char_type* const begin = std::addressof(*first);
|
||||||
|
return adapter_type(begin, begin + (last - first));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// General purpose iterator-based input (iterator+sentinel pair; SentinelType
|
// General purpose iterator-based input (iterator+sentinel pair; SentinelType
|
||||||
// defaults to IteratorType for the common same-type case, but may differ for
|
// defaults to IteratorType for the common same-type case, but may differ for
|
||||||
// C++20 ranges-style iterator+sentinel pairs). Only enable for types that can
|
// C++20 ranges-style iterator+sentinel pairs). Only enable for types that can
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ using nlohmann::json;
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include <list>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <valarray>
|
#include <valarray>
|
||||||
@@ -1409,3 +1410,63 @@ TEST_CASE("indentation runs of every length around the 8-byte skip")
|
|||||||
CHECK(parse_result(indented("[\n~1]", std::string(17, ' ')), true) == "[1]");
|
CHECK(parse_result(indented("[\n~1]", std::string(17, ' ')), true) == "[1]");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !defined(JSON_NOEXCEPTION)
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
/// the value of parsing [first, last), or the message of the exception
|
||||||
|
template<typename Iterator>
|
||||||
|
std::string outcome_of(Iterator first, Iterator last)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return json::parse(first, last).dump();
|
||||||
|
}
|
||||||
|
catch (const json::exception& e)
|
||||||
|
{
|
||||||
|
return e.what();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// a std::string, std::vector<char>, <unsigned char> and <signed char> as
|
||||||
|
/// iterator pairs: the same as the byte path (std::list iterators)
|
||||||
|
void check_iterator_pairs(const std::string& text)
|
||||||
|
{
|
||||||
|
CAPTURE(text)
|
||||||
|
const std::list<char> bytes(text.begin(), text.end());
|
||||||
|
const std::string expected = outcome_of(bytes.begin(), bytes.end());
|
||||||
|
const bool expected_accept = json::accept(bytes.begin(), bytes.end());
|
||||||
|
|
||||||
|
std::string s = text;
|
||||||
|
CHECK(outcome_of(s.begin(), s.end()) == expected);
|
||||||
|
CHECK(outcome_of(s.cbegin(), s.cend()) == expected);
|
||||||
|
CHECK(json::accept(s.cbegin(), s.cend()) == expected_accept);
|
||||||
|
|
||||||
|
const std::vector<char> vc(text.begin(), text.end());
|
||||||
|
CHECK(outcome_of(vc.begin(), vc.end()) == expected);
|
||||||
|
const std::vector<unsigned char> vu(text.begin(), text.end());
|
||||||
|
CHECK(outcome_of(vu.begin(), vu.end()) == expected);
|
||||||
|
CHECK(json::accept(vu.begin(), vu.end()) == expected_accept);
|
||||||
|
const std::vector<signed char> vs(text.begin(), text.end());
|
||||||
|
CHECK(outcome_of(vs.begin(), vs.end()) == expected);
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST_CASE("deserialization from iterator pairs of std::string and std::vector")
|
||||||
|
{
|
||||||
|
// these are read as the pointer range they are, also before C++20
|
||||||
|
for (const char* text :
|
||||||
|
{
|
||||||
|
"", " ", "[1, 2.5, \"a\", true, null, {\"k\": [\"v\"]}]", "[1, 2", "{\"a\" 1}", "\"\xC3\xA4\"", "\"\xC3\"", "[1]x", "1e400"
|
||||||
|
})
|
||||||
|
{
|
||||||
|
check_iterator_pairs(text);
|
||||||
|
}
|
||||||
|
check_iterator_pairs(std::string("[1]" "\0" "x", 5));
|
||||||
|
|
||||||
|
// an empty range is not dereferenced
|
||||||
|
std::string empty;
|
||||||
|
CHECK_FALSE(json::accept(empty.begin(), empty.end()));
|
||||||
|
CHECK_THROWS_AS(json::parse(empty.begin(), empty.end()), json::parse_error&);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
Reference in new issue
Block a user