zed icon indicating copy to clipboard operation
zed copied to clipboard

streamline PEG grammar

Open mccanne opened this issue 3 years ago • 1 comments

There are many PEG rules in the grammar that can be rewritten to reduce backtracking overhead. This perf issue became more apparent with the introduction of "over expressions" where sequential dataflow can appear inside of complex expressions. This can cause worst-case exponential running times.

To remedy this PEG ruler like this:

ConditionalExpr
  = condition:LogicalOrExpr __ "?" __ thenClause:Expr __ ":" __ elseClause:Expr {
      RETURN(MAP("kind": "Conditional", "cond": condition, "then": thenClause, "else": elseClause))
    }
  / LogicalOrExpr

should be rewritten as

ConditionalExpr
  = condition:LogicalOrExpr opt:(__ "?" __ Expr __ ":" __ Expr)? {
      VAR(p) = MAP("kind": "Conditional", "cond": condition)
      if ISNOTNULL(opt) {
        p["then"] = ASSERT_ARRAY(opt)[3]
        p["else"] = ASSERT_ARRAY(opt)[7]
      }
      RETURN(p)
    }

to reduce the out-degree of nodes in the PEG search tree.

There are many opportunities throughout the grammar to rewrite rules like this and perhaps put in some & constraints at the start of rules to short-circuit searches.

mccanne avatar Apr 06 '22 13:04 mccanne

Some of this has been fixed, but still work to do.

philrz avatar Sep 21 '22 18:09 philrz