Skip to content

Commit 8e6e6d3

Browse files
author
james
committed
docs: move folding predicates topic
(cherry picked from commit fe18c18)
1 parent 2900dce commit 8e6e6d3

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

docs/language/learn-ql/advanced/advanced-ql.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@ Topics on advanced uses of QL. These topics assume that you are familiar with QL
1313
- :doc:`Semantics of abstract classes <abstract-classes>`
1414
- :doc:`Choosing appropriate ways to constrain types <constraining-types>`
1515
- :doc:`Determining the most specific types of a variable <determining-specific-types-variables>`
16-
- :doc:`Folding predicates <folding-predicates>`
1716
- :doc:`Understanding the difference between != and not(=) <equivalence>`
1817
- :doc:`Monotonic aggregates in QL <monotonic-aggregates>`

docs/language/learn-ql/advanced/folding-predicates.rst

Lines changed: 0 additions & 36 deletions
This file was deleted.

docs/language/learn-ql/writing-queries/debugging-queries.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,48 @@ That is, you should define a *base case* that allows the predicate to *bottom ou
8181
The query optimizer has special data structures for dealing with `transitive closures <https://help.semmle.com/QL/ql-handbook/recursion.html#transitive-closures>`__.
8282
If possible, use a transitive closure over a simple recursive predicate, as it is likely to be computed faster.
8383

84+
Folding predicates
85+
~~~~~~~~~~~~~~~~~~
86+
87+
Sometimes you can assist the query optimizer by "folding" parts of large predicates out into smaller predicates.
88+
89+
The general principle is to split off chunks of work that are:
90+
91+
- **linear**, so that there is not too much branching.
92+
- **tightly bound**, so that the chunks join with each other on as many variables as possible.
93+
94+
95+
In the following example, we explore some lookups on two ``Element``\ s:
96+
97+
.. code-block:: ql
98+
99+
predicate similar(Element e1, Element e2) {
100+
e1.getName() = e2.getName() and
101+
e1.getFile() = e2.getFile() and
102+
e1.getLocation().getStartLine() = e2.getLocation().getStartLine()
103+
}
104+
105+
Going from ``Element -> File`` and ``Element -> Location -> StartLine`` is linear--that is, there is only one ``File``, ``Location``, etc. for each ``Element``.
106+
107+
However, as written it is difficult for the optimizer to pick out the best ordering. Generally, we want to do the quick, linear parts first, and then join on the resultant larger tables. Joining first and then doing the linear lookups later would likely result in poor performance. We can initiate this kind of ordering by splitting the above predicate as follows:
108+
109+
.. code-block:: ql
110+
111+
predicate locInfo(Element e, string name, File f, int startLine) {
112+
name = e.getName() and
113+
f = e.getFile() and
114+
startLine = e.getLocation().getStartLine()
115+
}
116+
117+
predicate sameLoc(Element e1, Element e2) {
118+
exists(string name, File f, int startLine |
119+
locInfo(e1, name, f, startLine) and
120+
locInfo(e2, name, f, startLine)
121+
)
122+
}
123+
124+
Now the structure we want is clearer. We've separated out the easy part into its own predicate ``locInfo``, and the main predicate ``sameLoc`` is just a larger join.
125+
84126
Further information
85127
-------------------
86128

0 commit comments

Comments
 (0)