Skip to content

Commit

Permalink
Enable clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
tfc committed Oct 25, 2023
1 parent 731ef20 commit 5d43a9a
Show file tree
Hide file tree
Showing 7 changed files with 911 additions and 885 deletions.
136 changes: 70 additions & 66 deletions benchmark/main.cpp
Original file line number Diff line number Diff line change
@@ -1,108 +1,112 @@
#include <cassert>
#include <sstream>

#include <attoparsecpp/parser.hpp>
#include <attoparsecpp/math_expression.hpp>
#include <attoparsecpp/parser.hpp>

#include <benchmark/benchmark.h>


static constexpr size_t max_range {10000000};
#define BENCH_COMPLX(x) BENCHMARK(x)->RangeMultiplier(10)->Range(10, max_range)->Complexity(benchmark::oN)
static constexpr size_t max_range{10000000};
#define BENCH_COMPLX(x) \
BENCHMARK(x) \
->RangeMultiplier(10) \
->Range(10, max_range) \
->Complexity(benchmark::oN)

using namespace apl;

std::string self_concat(const char *s, size_t times) {
std::ostringstream ss;
while (times--) { ss << s; }
return ss.str();
std::ostringstream ss;
while (times--) {
ss << s;
}
return ss.str();
}

static void measure_word_parsing(benchmark::State &state) {
const size_t size {static_cast<size_t>(state.range(0))};
const auto p {many(noneOf(' '))};
const std::string s {self_concat("a", size)};

for (auto _ : state) {
auto r {parse_result(p, s)->data()};
benchmark::DoNotOptimize(r);
}
state.SetComplexityN(state.range(0));
const size_t size{static_cast<size_t>(state.range(0))};
const auto p{many(noneOf(' '))};
const std::string s{self_concat("a", size)};

for (auto _ : state) {
auto r{parse_result(p, s)->data()};
benchmark::DoNotOptimize(r);
}
state.SetComplexityN(state.range(0));
}

BENCH_COMPLX(measure_word_parsing);

static void measure_vector_filling(benchmark::State &state) {
const size_t size {static_cast<size_t>(state.range(0))};
const auto p {manyV(token(integer), false, size)};
const std::string s {self_concat("1 ", size)};

for (auto _ : state) {
auto r {parse_result(p, s)};
auto res {r->data()};
benchmark::DoNotOptimize(res);
assert(r->size() == size);
}
state.SetComplexityN(state.range(0));
const size_t size{static_cast<size_t>(state.range(0))};
const auto p{manyV(token(integer), false, size)};
const std::string s{self_concat("1 ", size)};

for (auto _ : state) {
auto r{parse_result(p, s)};
auto res{r->data()};
benchmark::DoNotOptimize(res);
assert(r->size() == size);
}
state.SetComplexityN(state.range(0));
}

BENCH_COMPLX(measure_vector_filling);

static auto csv_line(size_t reserve_items = 0) {
return [reserve_items] (str_pos pos) {
const auto comma_whitespace {prefixed(oneOf(','), many(oneOf(' ')))};
return sep_by1(integer, comma_whitespace, reserve_items)(pos);
};
return [reserve_items](str_pos pos) {
const auto comma_whitespace{prefixed(oneOf(','), many(oneOf(' ')))};
return sep_by1(integer, comma_whitespace, reserve_items)(pos);
};
}

static void csv_vector_of_ints(benchmark::State &state) {
const size_t size {static_cast<size_t>(state.range(0))};
assert(size > 0);
const std::string s {std::string{"1"} + self_concat(", 1", size - 1)};
const auto p {csv_line(size)};

for (auto _ : state) {
const auto r {parse_result(p, s)};
auto res {r->data()};
benchmark::DoNotOptimize(res);
assert(r->size() == size);
}
state.SetComplexityN(state.range(0));
const size_t size{static_cast<size_t>(state.range(0))};
assert(size > 0);
const std::string s{std::string{"1"} + self_concat(", 1", size - 1)};
const auto p{csv_line(size)};

for (auto _ : state) {
const auto r{parse_result(p, s)};
auto res{r->data()};
benchmark::DoNotOptimize(res);
assert(r->size() == size);
}
state.SetComplexityN(state.range(0));
}

BENCH_COMPLX(csv_vector_of_ints);

static void sum_of_ints(benchmark::State &state) {
const auto size {static_cast<int>(state.range(0))};
assert(size > 0);
const std::string s {std::string{"1 "} + self_concat("+ 1", size - 1)};

for (auto _ : state) {
const auto r {parse_result(expr, s)};
bool res {*r == size};
benchmark::DoNotOptimize(res);
assert(res);
}
state.SetComplexityN(state.range(0));
const auto size{static_cast<int>(state.range(0))};
assert(size > 0);
const std::string s{std::string{"1 "} + self_concat("+ 1", size - 1)};

for (auto _ : state) {
const auto r{parse_result(expr, s)};
bool res{*r == size};
benchmark::DoNotOptimize(res);
assert(res);
}
state.SetComplexityN(state.range(0));
}

BENCH_COMPLX(sum_of_ints);

static void product_of_ints(benchmark::State &state) {
const size_t size {static_cast<size_t>(state.range(0))};
assert(size > 0);
const std::string s {std::string{"1 "} + self_concat("* 1", size - 1)};

for (auto _ : state) {
const auto r {parse_result(expr, s)};
bool res {r == 1};
benchmark::DoNotOptimize(res);
assert(res);
}
state.SetComplexityN(state.range(0));
const size_t size{static_cast<size_t>(state.range(0))};
assert(size > 0);
const std::string s{std::string{"1 "} + self_concat("* 1", size - 1)};

for (auto _ : state) {
const auto r{parse_result(expr, s)};
bool res{r == 1};
benchmark::DoNotOptimize(res);
assert(res);
}
state.SetComplexityN(state.range(0));
}

BENCH_COMPLX(product_of_ints);


BENCHMARK_MAIN();
3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@
pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
src = ./.;
hooks = {
clang-format.enable = true;
deadnix.enable = true;
nixpkgs-fmt.enable = true;
statix.enable = true;
deadnix.enable = true;
};
};
};
Expand Down
58 changes: 35 additions & 23 deletions include/attoparsecpp/math_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,54 @@

namespace apl {

static parser<int(*)(int, int)> add_op(str_pos &p)
{
if (p.at_end()) { return {}; }

switch (*p) {
case '+': p.next(); return {[](int a, int b) { return a + b; }};
case '-': p.next(); return {[](int a, int b) { return a - b; }};
default: return {};
}
static parser<int (*)(int, int)> add_op(str_pos &p) {
if (p.at_end()) {
return {};
}

switch (*p) {
case '+':
p.next();
return {[](int a, int b) { return a + b; }};
case '-':
p.next();
return {[](int a, int b) { return a - b; }};
default:
return {};
}
}

static parser<int(*)(int, int)> mul_op(str_pos &p)
{
if (p.at_end()) { return {}; }
static parser<int (*)(int, int)> mul_op(str_pos &p) {
if (p.at_end()) {
return {};
}

switch (*p) {
case '*': p.next(); return {[](int a, int b) { return a * b; }};
case '/': p.next(); return {[](int a, int b) { return a / b; }};
default: return {};
}
switch (*p) {
case '*':
p.next();
return {[](int a, int b) { return a * b; }};
case '/':
p.next();
return {[](int a, int b) { return a / b; }};
default:
return {};
}
}

static parser<int> expr( str_pos &p);
static parser<int> term( str_pos &p);
static parser<int> expr(str_pos &p);
static parser<int> term(str_pos &p);
static parser<int> factor(str_pos &p);

static parser<int> expr(str_pos &p) {
return chainl1(token(term), token(add_op))(p);
return chainl1(token(term), token(add_op))(p);
};

static parser<int> term(str_pos &p) {
return chainl1(token(factor), token(mul_op))(p);
return chainl1(token(factor), token(mul_op))(p);
};

static parser<int> factor(str_pos &p) {
return choice(base_integer(10), clasped(oneOf('('), oneOf(')'), expr))(p);
return choice(base_integer(10), clasped(oneOf('('), oneOf(')'), expr))(p);
}

}
} // namespace apl
Loading

0 comments on commit 5d43a9a

Please sign in to comment.