Skip to content

Commit ac219c4

Browse files
committed
Avoid O(n^2) filterStmts
We were calling filterStmts for each group, but filterStmts recursively descends the whole statement tree. We only need to do it once at the top level.
1 parent 9d0c8d8 commit ac219c4

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

glean/db/Glean/Query/Opt.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ optimiseQuery :: FlatQuery -> U FlatQuery
162162
optimiseQuery query@(FlatQuery key maybeVal stmts) = do
163163
-- determine variables visible outside of any nested choice:
164164
modify $ \s -> s { optCurrentScope = queryScope query }
165-
stmts' <- optStmts stmts
165+
stmts' <- filterGroup =<< optStmts stmts
166166
FlatQuery
167167
<$> apply key
168168
<*> mapM apply maybeVal
@@ -275,11 +275,10 @@ optStmts :: FlatStatementGroup -> U FlatStatementGroup
275275
optStmts (FlatStatementGroup ord) = do
276276
notFalse <- and <$> mapM unifyOrdStmt ord
277277
ord' <- concatMap (mapM expandStmt) <$> mapM (mapM apply) ord
278-
ord'' <- filterOrdStmts ord'
279278
-- unify may fail, but apply may also leave behind a false statement:
280-
if notFalse && not (any isFalseOrdStmt ord'')
281-
then return (mkStatementGroup ord'')
282-
else return (mkStatementGroup (Ordered falseStmt : ord'' ))
279+
if notFalse && not (any isFalseOrdStmt ord')
280+
then return (mkStatementGroup ord')
281+
else return (mkStatementGroup (Ordered falseStmt : ord' ))
283282

284283
-- Look for the sentinel left by optStmts
285284
isFalseGroups :: FlatStatementGroup -> Bool

0 commit comments

Comments
 (0)