From aa31d0a0fbfa6864ec5f85e21a2b7b39ecd20c7f Mon Sep 17 00:00:00 2001 From: Henri DF Date: Wed, 16 Mar 2016 13:25:50 -0700 Subject: [PATCH] Optimization: don't nest at every boolean op --- userspace/digwatch/lua/rule_loader.lua | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/userspace/digwatch/lua/rule_loader.lua b/userspace/digwatch/lua/rule_loader.lua index aa00596d..d5fc2619 100644 --- a/userspace/digwatch/lua/rule_loader.lua +++ b/userspace/digwatch/lua/rule_loader.lua @@ -36,15 +36,25 @@ end --[[ Take a filter AST and set it up in the libsinsp runtime, using the filter API. --]] -local function install_filter(node) +local function install_filter(node, parent_bool_op) local t = node.type if t == "BinaryBoolOp" then - filter.nest() --io.write("(") - install_filter(node.left) - filter.bool_op(node.operator) --io.write(" "..node.operator.." ") - install_filter(node.right) - filter.unnest() --io.write(")") + + -- "nesting" (the runtime equivalent of placing parens in syntax) is + -- never necessary when we have identical successive operators. so we + -- avoid it as a runtime performance optimization. + if (not(node.operator == parent_bool_op)) then + filter.nest() -- io.write("(") + end + + install_filter(node.left, node.operator) + filter.bool_op(node.operator) -- io.write(" "..node.operator.." ") + install_filter(node.right, node.operator) + + if (not (node.operator == parent_bool_op)) then + filter.unnest() -- io.write(")") + end elseif t == "UnaryBoolOp" then filter.nest() --io.write("(") @@ -135,6 +145,7 @@ end function on_done() install_filter(state.filter_ast) + io.flush() end evt = nil