Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch: optimize selects by extracting exclusive branches #205

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/ctll/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ template <typename T> struct item_matcher {
static constexpr auto check(...) { return std::false_type{}; }
static constexpr auto select(T) { return not_selected{}; }
template <typename Y> static constexpr auto select(Y) { return wrapper<Y>{}; }
static constexpr auto pick(std::true_type) { return wrapper<Y>{}; };
static constexpr auto pick(std::false_type) { return not_selected{}; };
};

template <typename T, typename... Ts> constexpr bool exists_in(T, list<Ts...>) noexcept {
Expand Down
1 change: 1 addition & 0 deletions include/ctre/atoms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct any { };
// actual AST of regexp
template <auto... Str> struct string { };
template <typename... Opts> struct select { };
template <typename... Opts> struct non_exclusive_select { };
template <typename... Content> struct sequence { };
struct empty { };

Expand Down
125 changes: 122 additions & 3 deletions include/ctre/evaluation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,116 @@ constexpr CTRE_FORCE_INLINE R evaluate(const Iterator begin, Iterator current, c
return evaluate(begin, result.position, last, consumed_something(f, sizeof...(String) > 0), captures, ctll::list<Tail...>());
}

template <typename T, typename... Ts> constexpr auto collides_with(T, ctll::list<Ts...>) noexcept {
return decltype((ctll::list<>{} + ... + ctll::item_matcher<Ts>::pick(
typename std::conditional<collides(calculate_first(sequence<T>{}), calculate_first(sequence<Ts>{})), std::true_type, std::false_type>::type{}
))){};
}

template <typename T, typename... Ts> constexpr auto collides_with_negated(T, ctll::list<Ts...>) noexcept {
return decltype((ctll::list<>{} + ... + ctll::item_matcher<Ts>::pick(
typename std::conditional<collides(ctre::negative_set<decltype(calculate_first(sequence<T>{}))>{}, calculate_first(sequence<Ts>{})), std::true_type, std::false_type>::type{}
))){};
}

template <typename T, typename... Ts> constexpr auto mutually_exclusive_with(T, ctll::list<Ts...>) noexcept {
return decltype((ctll::list<>{} + ... + ctll::item_matcher<Ts>::pick(
typename std::conditional<!collides(calculate_first(sequence<T>{}), calculate_first(sequence<Ts>{})), std::true_type, std::false_type>::type{}
))){};
}

namespace detail {
template <typename... Content>
constexpr auto transform_into_set(ctll::list<Content...>) {
return ctre::set<decltype(Content{})...>{};
//return ctre::set<decltype(transform_into_set(Content{}))... > {};
}

template <typename T>
constexpr auto transform_into_set(T) {
return T{};
}

template<>
constexpr auto transform_into_set(can_be_anything) {
return ctre::char_range<std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max()>{};
}

template <typename... Content>
constexpr auto transform_into_select(ctll::list<Content...>) {
return ctre::select<decltype(Content{})...>{};
}
}

// matching select in patterns
template <typename R, typename Iterator, typename EndIterator, typename HeadOptions, typename... TailOptions, typename... Tail>
constexpr CTRE_FORCE_INLINE R evaluate(const Iterator begin, Iterator current, const EndIterator last, const flags & f, R captures, ctll::list<select<HeadOptions, TailOptions...>, Tail...>) noexcept {
if (auto r = evaluate(begin, current, last, f, captures, ctll::list<HeadOptions, Tail...>())) {
return r;
if constexpr (sizeof...(TailOptions) > 1) {
constexpr auto collision_list = collides_with(sequence<HeadOptions, Tail...>{}, ctll::list<
decltype(sequence<TailOptions, Tail...>{})...
>{});

if constexpr (ctll::empty(collision_list)) {
using set_type = decltype(detail::transform_into_set(calculate_first(sequence<HeadOptions, Tail...>{})));
if constexpr (::std::is_same_v<set<>, set_type>) {
//fail handle as normal
if (auto r = evaluate(begin, current, last, f, captures, ctll::list<HeadOptions, Tail...>())) {
return r;
} else {
return evaluate(begin, current, last, f, captures, ctll::list<select<TailOptions...>, Tail...>());
}
} else {
//ok optimize into exclusive select
if (auto r = evaluate(begin, current, last, f, captures, ctll::list<set_type, end_cycle_mark>{})) {
return evaluate(begin, current, last, f, captures, ctll::list<HeadOptions, Tail...>{});
} else {
return evaluate(begin, current, last, f, captures, ctll::list<select<TailOptions...>, Tail...>());
}
}

return evaluate(begin, current, last, f, captures, ctll::list<HeadOptions, Tail...>());
} else if constexpr (ctll::size(collision_list) == sizeof...(TailOptions)) {
//continue as normal...we collided with everything
if (auto r = evaluate(begin, current, last, f, captures, ctll::list<HeadOptions, Tail...>())) {
return r;
} else {
return evaluate(begin, current, last, f, captures, ctll::list<select<TailOptions...>, Tail...>());
}
} else {
//we collided with some things, but not others, bifricate on the first character we see
//may be less computationally expensive to do a set subtraction from the complete list with the collision list, because we're evaluating collisions again
constexpr auto negated_collision_list = collides_with_negated(sequence<HeadOptions, Tail...>{}, ctll::list<
decltype(sequence<TailOptions, Tail...>{})...
>{});

using set_type = decltype(detail::transform_into_set(calculate_first(sequence<HeadOptions, Tail...>{})));
if constexpr (::std::is_same_v<set<>, set_type>) {
//fail
if (auto r = evaluate(begin, current, last, f, captures, ctll::list<HeadOptions, Tail...>())) {
return r;
} else {
return evaluate(begin, current, last, f, captures, ctll::list<non_exclusive_select<TailOptions...>, Tail...>());
}
} else {
//ok optimize into a split
if (auto r = evaluate(begin, current, last, f, captures, ctll::list<set_type, end_cycle_mark>{})) {
if (auto r2 = evaluate(begin, current, last, f, captures, ctll::list<HeadOptions, Tail...>{})) {
return r2;
} else {
return evaluate(begin, current, last, f, captures, detail::transform_into_select(collision_list));
}
} else {
return evaluate(begin, current, last, f, captures, detail::transform_into_select(negated_collision_list));
}
}
}
} else {
return evaluate(begin, current, last, f, captures, ctll::list<select<TailOptions...>, Tail...>());
//simple case, too few branches to pick handle as normal
if (auto r = evaluate(begin, current, last, f, captures, ctll::list<HeadOptions, Tail...>())) {
return r;
} else {
return evaluate(begin, current, last, f, captures, ctll::list<select<TailOptions...>, Tail...>());
}
}
}

Expand All @@ -151,6 +254,22 @@ constexpr CTRE_FORCE_INLINE R evaluate(const Iterator, Iterator, const EndIterat
return not_matched;
}

// non exclusive select (assume collisions)
template <typename R, typename Iterator, typename EndIterator, typename HeadOptions, typename... TailOptions, typename... Tail>
constexpr CTRE_FORCE_INLINE R evaluate(const Iterator begin, Iterator current, const EndIterator last, const flags & f, R captures, ctll::list<non_exclusive_select<HeadOptions, TailOptions...>, Tail...>) noexcept {
if (auto r = evaluate(begin, current, last, f, captures, ctll::list<HeadOptions, Tail...>())) {
return r;
} else {
return evaluate(begin, current, last, f, captures, ctll::list<non_exclusive_select<TailOptions...>, Tail...>());
}
}

template <typename R, typename Iterator, typename EndIterator, typename... Tail>
constexpr CTRE_FORCE_INLINE R evaluate(const Iterator, Iterator, const EndIterator, flags, R, ctll::list<non_exclusive_select<>, Tail...>) noexcept {
// no previous option was matched => REJECT
return not_matched;
}

// matching sequence in patterns
template <typename R, typename Iterator, typename EndIterator, typename HeadContent, typename... TailContent, typename... Tail>
constexpr CTRE_FORCE_INLINE R evaluate(const Iterator begin, Iterator current, const EndIterator last, const flags & f, R captures, ctll::list<sequence<HeadContent, TailContent...>, Tail...>) noexcept {
Expand Down