mirror of
https://github.com/github/codeql.git
synced 2026-04-27 09:45:15 +02:00
@@ -97,28 +97,28 @@ In the following example, we explore some lookups on two ``Element``\ s:
|
||||
.. code-block:: ql
|
||||
|
||||
predicate similar(Element e1, Element e2) {
|
||||
e1.getName() = e2.getName() and
|
||||
e1.getFile() = e2.getFile() and
|
||||
e1.getLocation().getStartLine() = e2.getLocation().getStartLine()
|
||||
e1.getName() = e2.getName() and
|
||||
e1.getFile() = e2.getFile() and
|
||||
e1.getLocation().getStartLine() = e2.getLocation().getStartLine()
|
||||
}
|
||||
|
||||
Going from ``Element -> File`` and ``Element -> Location -> StartLine`` is linear--that is, there is only one ``File``, ``Location``, etc. for each ``Element``.
|
||||
|
||||
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:
|
||||
However, as written it is difficult for the optimizer to pick out the best ordering. Joining first and then doing the linear lookups later would likely result in poor performance. Generally, we want to do the quick, linear parts first, and then join on the resultant larger tables. We can initiate this kind of ordering by splitting the above predicate as follows:
|
||||
|
||||
.. code-block:: ql
|
||||
|
||||
predicate locInfo(Element e, string name, File f, int startLine) {
|
||||
name = e.getName() and
|
||||
f = e.getFile() and
|
||||
startLine = e.getLocation().getStartLine()
|
||||
name = e.getName() and
|
||||
f = e.getFile() and
|
||||
startLine = e.getLocation().getStartLine()
|
||||
}
|
||||
|
||||
|
||||
predicate sameLoc(Element e1, Element e2) {
|
||||
exists(string name, File f, int startLine |
|
||||
locInfo(e1, name, f, startLine) and
|
||||
locInfo(e2, name, f, startLine)
|
||||
)
|
||||
exists(string name, File f, int startLine |
|
||||
locInfo(e1, name, f, startLine) and
|
||||
locInfo(e2, name, f, startLine)
|
||||
)
|
||||
}
|
||||
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user