13namespace CppClingo::Ground {
34 explicit ProfileIndent(
size_t level,
size_t width = 2) : level{level}, width{width} {}
38 for (
size_t i = 0; i < indent.level * indent.width; ++i) {
87 [[nodiscard]]
auto score() const ->
double {
106 std::variant<std::pair<std::string_view, bool>, std::pair<ProfileStats const *, ProfileType>>,
size_t)>;
123 do_print(out, indent, detail, type);
130 [[nodiscard]]
virtual auto score(
ProfileType type)
const ->
double {
return do_score(type); }
142 void accept(
Visitor const &visit,
size_t depth)
const { do_accept(visit, depth); }
146 [[nodiscard]]
virtual auto do_equal(
ProfileNode const &node)
const ->
bool = 0;
147 [[nodiscard]]
virtual auto do_score(
ProfileType type)
const ->
double = 0;
149 virtual void do_begin_step() = 0;
150 virtual void do_end_step() = 0;
151 virtual void do_accept(
Visitor const &visit,
size_t depth)
const = 0;
160 template <
class T>
auto add_child(std::unique_ptr<T> child) -> T & {
161 assert(child !=
nullptr);
162 auto *ret = child.get();
163 auto *ins = do_add_child(std::move(child));
164 assert(
dynamic_cast<T *
>(ins) !=
nullptr);
165 return ins !=
nullptr ? *
static_cast<T *
>(ins) : *ret;
170 virtual auto do_add_child(std::unique_ptr<ProfileNode> child) ->
ProfileNode * = 0;
181 auto do_add_child(std::unique_ptr<ProfileNode> child) ->
ProfileNode *
override {
182 for (
auto const &x : children_) {
183 if (x->equal(*child)) {
187 children_.emplace_back(std::move(child));
188 return children_.back().get();
193 out << indent << (nested_ ?
"[" :
"") << expr_ << (nested_ ?
"]" :
"") <<
"\n";
194 if (detail == ProfileDetail::detailed) {
195 for (
auto const &child : children_) {
196 child->print(out, indent + 1, detail, type);
199 auto stats = ProfileStats{};
201 stats.print(out, indent + 1);
206 [[nodiscard]]
auto do_equal(ProfileNode
const &node)
const ->
bool override {
208 if constexpr (
requires { expr_.get(); }) {
209 return other !=
nullptr && nested_ == other->nested_ && expr_.get() == other->expr_.get();
211 return other !=
nullptr && nested_ == other->nested_ && expr_ == other->expr_;
216 [[nodiscard]]
auto do_score(
ProfileType type)
const ->
double override {
217 return !nested_ ? std::accumulate(children_.begin(), children_.end(), 0.0,
218 [=](
double sum,
auto const &child) { return sum + child->score(type); })
223 void do_begin_step()
override {
224 for (
auto &child : children_) {
230 void do_end_step()
override {
231 for (
auto &child : children_) {
237 void do_combine(ProfileStats &stats,
ProfileType type,
bool nested)
const override {
238 for (
auto const &child : children_) {
239 child->combine(stats, type, nested || nested_);
244 void do_accept(
Visitor const &visit,
size_t depth)
const override {
245 std::ostringstream oss;
247 visit(std::make_pair(oss.view(), nested_), depth);
248 for (
auto const &child : children_) {
249 child->accept(visit, depth + 1);
256 std::vector<std::unique_ptr<ProfileNode>> children_;
272 std::ignore = detail;
276 [[nodiscard]]
auto do_equal(
ProfileNode const &node)
const ->
bool override {
277 auto const *other =
dynamic_cast<ProfileData const *
>(&node);
278 return other !=
nullptr;
281 [[nodiscard]]
auto do_score(
ProfileType type)
const ->
double override {
285 void do_begin_step()
override { step_.
reset(); }
287 void do_end_step()
override { accu_.
accumulate(step_); }
289 void do_combine(ProfileStats &stats,
ProfileType type,
bool nested)
const override {
291 stats.accumulate(step_, nested);
293 stats.accumulate(accu_, nested);
298 void do_accept(
Visitor const &visit,
size_t depth)
const override {
An instantiator implementing the basic grounding algorithm.
Definition instantiator.hh:143
This profile node holds the profiling data for a single step and accumulated data.
Definition profile.hh:262
ProfileData()=default
Construct the profile data.
A profile node that holds a printable expression and children.
Definition profile.hh:174
ProfileNodeExpression(T expr, bool nested=false)
Construct the profile node with the given expression.
Definition profile.hh:177
Profile node that can hold children.
Definition profile.hh:155
auto add_child(std::unique_ptr< T > child) -> T &
Add a child profile node.
Definition profile.hh:160
Base class for profiling data.
Definition profile.hh:102
ProfileNode()=default
The default constructor.
void print(std::ostream &out, ProfileIndent indent, ProfileDetail detail, ProfileType type) const
Print the profiling data to the given output stream.
Definition profile.hh:122
virtual auto score(ProfileType type) const -> double
Get a score for sorting profile nodes.
Definition profile.hh:130
std::function< void(std::variant< std::pair< std::string_view, bool >, std::pair< ProfileStats const *, ProfileType > >, size_t)> Visitor
The type of visitor function to use for visiting profile nodes.
Definition profile.hh:106
virtual void combine(ProfileStats &stats, ProfileType type, bool nested) const
Combine stats below this node.
Definition profile.hh:139
ProfileNode(ProfileNode const &other)=delete
Delete the copy constructor.
virtual ~ProfileNode()=default
Destructor.
void accept(Visitor const &visit, size_t depth) const
Visit the profile node with the given visitor function.
Definition profile.hh:142
virtual void end_step()
Accumulate the per step stats into the accumulated stats.
Definition profile.hh:136
virtual void begin_step()
Reset the per step statistics.
Definition profile.hh:133
auto equal(ProfileNode const &node) const -> bool
Compare this profile node with another for equality.
Definition profile.hh:127
auto operator=(ProfileNode const &other) -> ProfileNode &=delete
Delete assignment operator.
@ sum
The #count function.
void visit(Visitor const &fun, Node const &node)
Visit the given node with the visitor.
Definition ast.hh:665
ProfileType
Whether to process per step or accumulated profiling data.
Definition profile.hh:19
ProfileDetail
Whether to print profile data into a compact or detailed form.
Definition profile.hh:25
@ accu
Profile data accumulated over all steps.
@ step
Profile data for a single step.
Helper to print indentation.
Definition profile.hh:31
ProfileIndent(size_t level, size_t width=2)
Construct the indent with the given amount.
Definition profile.hh:34
friend auto operator+(ProfileIndent const &indent, size_t add) -> ProfileIndent
Add indentation to the given indent.
Definition profile.hh:45
friend auto operator<<(std::ostream &out, ProfileIndent const &indent) -> std::ostream &
Print the given indentation to the given output stream.
Definition profile.hh:37
friend auto operator+=(ProfileIndent &indent, size_t add) -> ProfileIndent &
Add indentation to the current indent (inplace).
Definition profile.hh:50
The profiling data.
Definition profile.hh:63
uint64_t time_instantiate
The time in nanoseconds spent instantiating.
Definition profile.hh:96
void print(std::ostream &out, ProfileIndent indent) const
Print the profiling data to the given output stream.
void accumulate(ProfileStats const &stats, bool nested=false)
Accumulate the given step stats into this stats object.
Definition profile.hh:74
uint64_t matches
The number of matches produced by the instantiator.
Definition profile.hh:92
void reset()
Reset the profiling data to zero.
Definition profile.hh:68
uint64_t instances
The number of instances produced by the instantiator.
Definition profile.hh:94
uint64_t time_propagate
The time in nanoseconds spent propagating.
Definition profile.hh:98
auto score() const -> double
Compute a score for sorting profile nodes based on time.
Definition profile.hh:87
ProfileStats()=default
The default constructor.