Skip to content

Auto-rewrite associative DSL expression trees #7844

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

Open
wants to merge 2 commits into
base: master
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
64 changes: 0 additions & 64 deletions lib/base/debuginfo.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "base/debuginfo.hpp"
#include "base/convert.hpp"
#include <fstream>

using namespace icinga;

Expand Down Expand Up @@ -33,65 +31,3 @@ DebugInfo icinga::DebugInfoRange(const DebugInfo& start, const DebugInfo& end)
result.LastColumn = end.LastColumn;
return result;
}

#define EXTRA_LINES 2

void icinga::ShowCodeLocation(std::ostream& out, const DebugInfo& di, bool verbose)
{
if (di.Path.IsEmpty())
return;

out << "Location: " << di;

std::ifstream ifs;
ifs.open(di.Path.CStr(), std::ifstream::in);

int lineno = 0;
char line[1024];

while (ifs.good() && lineno <= di.LastLine + EXTRA_LINES) {
if (lineno == 0)
out << "\n";

lineno++;

ifs.getline(line, sizeof(line));

for (int i = 0; line[i]; i++)
if (line[i] == '\t')
line[i] = ' ';

int extra_lines = verbose ? EXTRA_LINES : 0;

if (lineno < di.FirstLine - extra_lines || lineno > di.LastLine + extra_lines)
continue;

String pathInfo = di.Path + "(" + Convert::ToString(lineno) + "): ";
out << pathInfo;
out << line << "\n";

if (lineno >= di.FirstLine && lineno <= di.LastLine) {
int start, end;

start = 0;
end = strlen(line);

if (lineno == di.FirstLine)
start = di.FirstColumn - 1;

if (lineno == di.LastLine)
end = di.LastColumn;

if (start < 0) {
end -= start;
start = 0;
}

out << String(pathInfo.GetLength(), ' ');
out << String(start, ' ');
out << String(end - start, '^');

out << "\n";
}
}
}
63 changes: 62 additions & 1 deletion lib/base/debuginfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#ifndef DEBUGINFO_H
#define DEBUGINFO_H

#include "base/convert.hpp"
#include "base/i2-base.hpp"
#include "base/string.hpp"
#include <fstream>

namespace icinga
{
Expand All @@ -29,7 +31,66 @@ std::ostream& operator<<(std::ostream& out, const DebugInfo& val);

DebugInfo DebugInfoRange(const DebugInfo& start, const DebugInfo& end);

void ShowCodeLocation(std::ostream& out, const DebugInfo& di, bool verbose = true);
template<class T, unsigned long ExtraLines = 2>
void ShowCodeLocation(T& out, const DebugInfo& di, bool verbose = true)
{
if (di.Path.IsEmpty())
return;

out << "Location: " << di;

std::ifstream ifs;
ifs.open(di.Path.CStr(), std::ifstream::in);

int lineno = 0;
char line[1024];

while (ifs.good() && lineno <= di.LastLine + ExtraLines) {
if (lineno == 0)
out << "\n";

lineno++;

ifs.getline(line, sizeof(line));

for (int i = 0; line[i]; i++)
if (line[i] == '\t')
line[i] = ' ';

int extra_lines = verbose ? ExtraLines : 0;

if (lineno < di.FirstLine - extra_lines || lineno > di.LastLine + extra_lines)
continue;

String pathInfo = di.Path + "(" + Convert::ToString(lineno) + "): ";
out << pathInfo;
out << line << "\n";

if (lineno >= di.FirstLine && lineno <= di.LastLine) {
int start, end;

start = 0;
end = strlen(line);

if (lineno == di.FirstLine)
start = di.FirstColumn - 1;

if (lineno == di.LastLine)
end = di.LastColumn;

if (start < 0) {
end -= start;
start = 0;
}

out << String(pathInfo.GetLength(), ' ');
out << String(start, ' ');
out << String(end - start, '^');

out << "\n";
}
}
}

}

Expand Down
3 changes: 3 additions & 0 deletions lib/config/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
#include "base/defer.hpp"
#include <boost/exception_ptr.hpp>
#include <boost/exception/errinfo_nested_exception.hpp>
#include <cmath>

using namespace icinga;

boost::signals2::signal<void (ScriptFrame&, ScriptError *ex, const DebugInfo&)> Expression::OnBreakpoint;
boost::thread_specific_ptr<bool> l_InBreakpointHandler;

const long double AssociativeExpression::m_Log2 = logl(2);

Expression::~Expression()
{ }

Expand Down
112 changes: 106 additions & 6 deletions lib/config/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@
#include "base/dictionary.hpp"
#include "base/function.hpp"
#include "base/exception.hpp"
#include "base/logger.hpp"
#include "base/scriptframe.hpp"
#include "base/shared-object.hpp"
#include "base/convert.hpp"
#include <cmath>
#include <cstdint>
#include <iterator>
#include <map>
#include <memory>
#include <utility>
#include <vector>

namespace icinga
{
Expand Down Expand Up @@ -562,23 +569,116 @@ class NotInExpression final : public BinaryExpression
ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class LogicalAndExpression final : public BinaryExpression
class AssociativeExpression : public BinaryExpression
{
public:
using BinaryExpression::BinaryExpression;

protected:
template<class Branch>
void RebalanceLeaves()
{
uintmax_t count = 0, depth = 0, depthBuf = 0;
StatLeaves<Branch>(count, depth, depthBuf);

if (depth > logl(count) / m_Log2 * 2) {
{
Log msg (LogDebug, "config");

msg << "Detected associative expression tree with " << count << " leaves, but "
<< depth << " > log2(" << count << ") * 2 nesting levels. Rebalancing to log2(" << count << ") nesting levels.\n";

ShowCodeLocation(msg, m_DebugInfo);
}

std::vector<std::unique_ptr<Expression>> leaves;
leaves.reserve(count);
HarvestLeaves<Branch>(leaves);

auto split (leaves.begin() + leaves.size() / 2u);
m_Operand1 = AssembleLeaves<Branch>(leaves.begin(), split);
m_Operand2 = AssembleLeaves<Branch>(split, leaves.end());
}
}

private:
static const long double m_Log2;

template<class Branch>
void StatLeaves(uintmax_t& count, uintmax_t& depth, uintmax_t& depthBuf)
{
++depthBuf;

if (depth < depthBuf) {
depth = depthBuf;
}

for (auto op : {m_Operand1.get(), m_Operand2.get()}) {
auto branch (dynamic_cast<Branch*>(op));

if (branch == nullptr) {
count += 1u;
} else {
branch->template StatLeaves<Branch>(count, depth, depthBuf);
}
}

--depthBuf;
}

template<class Branch>
void HarvestLeaves(std::vector<std::unique_ptr<Expression>>& leaves)
{
for (auto op : {&m_Operand1, &m_Operand2}) {
auto branch (dynamic_cast<Branch*>(op->get()));

if (branch == nullptr) {
leaves.emplace_back(std::move(*op));
} else {
branch->template HarvestLeaves<Branch>(leaves);
}
}
}

template<class Branch, class Iter>
std::unique_ptr<Expression> AssembleLeaves(Iter begin, Iter end)
{
auto distance (std::distance(begin, end));

if (distance < 2u) {
return std::move(*begin);
} else {
auto split (begin + distance / 2u);

return std::unique_ptr<Branch>(new Branch(
AssembleLeaves<Branch, Iter>(std::move(begin), split),
AssembleLeaves<Branch, Iter>(split, std::move(end))
));
}
}
};

class LogicalAndExpression final : public AssociativeExpression
{
public:
LogicalAndExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ }
: AssociativeExpression(std::move(operand1), std::move(operand2), debugInfo)
{
RebalanceLeaves<LogicalAndExpression>();
}

protected:
ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class LogicalOrExpression final : public BinaryExpression
class LogicalOrExpression final : public AssociativeExpression
{
public:
LogicalOrExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ }
: AssociativeExpression(std::move(operand1), std::move(operand2), debugInfo)
{
RebalanceLeaves<LogicalOrExpression>();
}

protected:
ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
Expand Down
Loading