Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
grisumbras committed Jan 27, 2025
1 parent 813795d commit d5c2a6f
Showing 1 changed file with 136 additions and 77 deletions.
213 changes: 136 additions & 77 deletions bench/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ find_supported_file(file_item const& fi)
return result;
}

template< class F >
auto
with_file(int file_index, F&& f)
-> decltype(static_cast<F&&>(f)( mp11::mp_int<-1>() ))
{
if(file_index < 0)
return static_cast<F&&>(f)( mp11::mp_int<-1>() );
return mp11::mp_with_index<supported_file_count>(file_index, f);
}

struct convert_from_value
{
value& jv;
Expand Down Expand Up @@ -189,6 +199,9 @@ struct data_constructor
std::unique_ptr<void, delete_data>& data;
value const& jv;

void operator()( mp11::mp_int<-1> ) const noexcept
{}

template< class I >
void operator()(I) const
{
Expand All @@ -203,9 +216,7 @@ std::unique_ptr<void, delete_data>
construct_data(int file_index, value const& jv)
{
std::unique_ptr<void, delete_data> result( nullptr, {file_index} );
if( file_index >= 0 )
mp11::mp_with_index<supported_file_count>(
file_index, data_constructor{result, jv} );
with_file(file_index, data_constructor{result, jv});
return result;
}

Expand All @@ -214,6 +225,9 @@ struct convert_to_value
std::unique_ptr<void, delete_data>& data;
value& jv;

void operator()( mp11::mp_int<-1> ) const noexcept
{}

template< class I >
void operator()(I) const
{
Expand Down Expand Up @@ -327,15 +341,10 @@ class any_impl
clock_type::duration
bench(string_view verb, file_item const& fi, std::size_t repeat) const
{
if( with_conversion_ )
{
if( ( file_index_ = find_supported_file(fi) ) < 0 )
return skip();
}
else
{
if( !with_conversion_ )
file_index_ = -1;
}
else if( ( file_index_ = find_supported_file(fi) ) < 0 )
return skip();

if(verb == "Parse")
{
Expand Down Expand Up @@ -526,6 +535,70 @@ class boost_impl : public any_impl
{
bool is_pool_;

struct do_serialize_string
{
value& jv;
std::size_t& repeat;

clock_type::duration
operator()( mp11::mp_int<-1> ) const
{
auto const start = clock_type::now();
serializer sr;
string out;
out.reserve(512);
while(repeat--)
{
sr.reset(&jv);
out.clear();
for(;;)
{
out.grow(sr.read(
out.end(),
out.capacity() -
out.size()).size());
if(sr.done())
break;
out.reserve(
out.capacity() + 1);
}
}

return clock_type::now() - start;
}

template< class I >
clock_type::duration
operator()(I) const
{
auto data = construct_data(I::value, jv);
auto const start = clock_type::now();
serializer sr;
string out;
out.reserve(512);
while(repeat--)
{
convert_to_value{data, jv}(I());

sr.reset(&jv);
out.clear();
for(;;)
{
out.grow(sr.read(
out.end(),
out.capacity() -
out.size()).size());
if(sr.done())
break;
out.reserve(
out.capacity() + 1);
}
}

return clock_type::now() - start;
}
};

public:
boost_impl(
bool is_pool,
Expand All @@ -537,27 +610,62 @@ class boost_impl : public any_impl
, is_pool_(is_pool)
{}

clock_type::duration
parse_string(file_item const& fi, std::size_t repeat) const override
struct do_parse_string
{
auto const start = clock_type::now();
parser p( {}, get_parse_options() );
while(repeat--)
std::size_t repeat;
parse_options opts;
string_view text;
bool is_pool;

clock_type::duration
operator()( mp11::mp_int<-1> )
{
monotonic_resource mr;
storage_ptr sp;
if( is_pool_ )
sp = &mr;
p.reset( std::move(sp) );
auto const start = clock_type::now();
parser p({}, opts);
while(repeat--)
{
monotonic_resource mr;
storage_ptr sp;
if(is_pool)
sp = &mr;
p.reset( std::move(sp) );

p.write( text.data(), text.size() );
auto jv = p.release();
}
return clock_type::now() - start;
}

p.write( fi.text.data(), fi.text.size() );
auto jv = p.release();
template< class I >
clock_type::duration
operator()(I)
{
auto const start = clock_type::now();
parser p({}, opts);
while(repeat--)
{
monotonic_resource mr;
storage_ptr sp;
if(is_pool)
sp = &mr;
p.reset( std::move(sp) );

if( file_index() >= 0 )
mp11::mp_with_index<supported_file_count>(
file_index(), convert_from_value{jv});
p.write( text.data(), text.size() );
auto jv = p.release();

using data_type = typename supported_file_at<I>::type;
auto data = value_to<data_type>(jv);
}
return clock_type::now() - start;
}
return clock_type::now() - start;
};

clock_type::duration
parse_string(file_item const& fi, std::size_t repeat) const override
{
return with_file(
file_index(),
do_parse_string{repeat, get_parse_options(), fi.text, is_pool_});
}

clock_type::duration
Expand Down Expand Up @@ -609,56 +717,7 @@ class boost_impl : public any_impl
if( is_pool_ )
sp = &mr;
value jv = json::parse( fi.text, std::move(sp) );

auto data = construct_data(file_index(), jv);

auto const start = clock_type::now();
serializer sr;
string out;
out.reserve(512);
if( file_index() >= 0 )
{
while(repeat--)
{
mp11::mp_with_index<supported_file_count>(
file_index(), convert_to_value{data, jv} );

sr.reset(&jv);
out.clear();
for(;;)
{
out.grow(sr.read(
out.end(),
out.capacity() -
out.size()).size());
if(sr.done())
break;
out.reserve(
out.capacity() + 1);
}
}
}
else
{
while(repeat--)
{
sr.reset(&jv);
out.clear();
for(;;)
{
out.grow(sr.read(
out.end(),
out.capacity() -
out.size()).size());
if(sr.done())
break;
out.reserve(
out.capacity() + 1);
}
}
}

return clock_type::now() - start;
return with_file(file_index(), do_serialize_string{jv, repeat});
}
};

Expand Down

0 comments on commit d5c2a6f

Please sign in to comment.