Skip to content

Commit

Permalink
Integer reader implementation optimization based on fast_float
Browse files Browse the repository at this point in the history
  • Loading branch information
eliaskosunen committed Jan 9, 2024
1 parent f26b24d commit 629c3c5
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 598 deletions.
19 changes: 5 additions & 14 deletions benchmark/runtime/common/bench_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,18 @@ template <typename T>
struct single_state {
single_state(scn::span<std::string> src) : source(src), it(source.begin())
{
values.reserve(2 << 12);
}

void reset_if_necessary()
{
if (it == source.end()) {
it = source.begin();
values.clear();
}
}

void push(T i)
{
values.push_back(i);
benchmark::DoNotOptimize(i);
}

static int64_t get_bytes_processed(benchmark::State& state)
Expand All @@ -88,29 +86,24 @@ struct single_state {

scn::span<std::string> source;
typename scn::span<std::string>::iterator it;

std::vector<T> values;
};

template <typename T>
struct repeated_state {
repeated_state(const std::string& src) : source(src), it(source.data())
{
values.reserve(2 << 12);
}
repeated_state(const std::string& src) : source(src), it(source.data()) {}

void reset()
{
it = source.data();
values.clear();
}

auto source_end_addr() const
{
return scn::detail::to_address(source.end());
}

void skip_classic_ascii_space() {
void skip_classic_ascii_space()
{
for (; is_classic_ascii_space(*it); ++it) {}
if (it == source_end_addr()) {
reset();
Expand All @@ -124,7 +117,7 @@ struct repeated_state {

void push(T i)
{
values.push_back(i);
benchmark::DoNotOptimize(i);
}

static int64_t get_bytes_processed(benchmark::State& state)
Expand All @@ -134,6 +127,4 @@ struct repeated_state {

const std::string& source;
const char* it;

std::vector<T> values;
};
2 changes: 1 addition & 1 deletion benchmark/runtime/float/repeated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static void scan_float_repeated_sstream(benchmark::State& state)

if (stream.eof()) {
stream = std::istringstream(s.source);
s.values.clear();
s.reset();
}
else if (stream.fail()) {
state.SkipWithError("Scan error");
Expand Down
30 changes: 29 additions & 1 deletion benchmark/runtime/integer/repeated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,34 @@ BENCHMARK_TEMPLATE(scan_int_repeated_scn_value, int);
BENCHMARK_TEMPLATE(scan_int_repeated_scn_value, long long);
BENCHMARK_TEMPLATE(scan_int_repeated_scn_value, unsigned);

template <typename Int>
static void scan_int_repeated_scn_decimal(benchmark::State& state)
{
repeated_state<Int> s{get_integer_string<Int>()};

for (auto _ : state) {
auto result = scn::scan<Int>(s.view(), "{:d}");

if (!result) {
if (result.error() == scn::scan_error::end_of_range) {
s.reset();
}
else {
state.SkipWithError("Scan error");
break;
}
}
else {
s.push(result->value());
s.it = scn::detail::to_address(result->range().begin());
}
}
state.SetBytesProcessed(s.get_bytes_processed(state));
}
BENCHMARK_TEMPLATE(scan_int_repeated_scn_decimal, int);
BENCHMARK_TEMPLATE(scan_int_repeated_scn_decimal, long long);
BENCHMARK_TEMPLATE(scan_int_repeated_scn_decimal, unsigned);

template <typename Int>
static void scan_int_repeated_scn_int(benchmark::State& state)
{
Expand Down Expand Up @@ -123,7 +151,7 @@ static void scan_int_repeated_sstream(benchmark::State& state)

if (stream.eof()) {
stream = std::istringstream(s.source);
s.values.clear();
s.reset();
}
else if (stream.fail()) {
state.SkipWithError("Scan error");
Expand Down
22 changes: 22 additions & 0 deletions benchmark/runtime/integer/single.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ BENCHMARK_TEMPLATE(scan_int_single_scn_value, int);
BENCHMARK_TEMPLATE(scan_int_single_scn_value, long long);
BENCHMARK_TEMPLATE(scan_int_single_scn_value, unsigned);

template <typename Int>
static void scan_int_single_scn_decimal(benchmark::State& state)
{
single_state<Int> s{get_integer_list<Int>()};

for (auto _ : state) {
s.reset_if_necessary();

if (auto result = scn::scan<Int>(*s.it, "{:d}"); !result) {
state.SkipWithError("Benchmark errored");
break;
}
else {
s.push(result->value());
}
}
state.SetBytesProcessed(s.get_bytes_processed(state));
}
BENCHMARK_TEMPLATE(scan_int_single_scn_decimal, int);
BENCHMARK_TEMPLATE(scan_int_single_scn_decimal, long long);
BENCHMARK_TEMPLATE(scan_int_single_scn_decimal, unsigned);

template <typename Int>
static void scan_int_single_scn_int(benchmark::State& state)
{
Expand Down
Loading

0 comments on commit 629c3c5

Please sign in to comment.