Skip to content

Commit 492f41d

Browse files
hvitvedaibaars
authored andcommitted
Fix performance
1 parent 0ccca47 commit 492f41d

3 files changed

Lines changed: 24 additions & 16 deletions

File tree

ql/src/codeql_ruby/ast/Module.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class Module extends TModule {
2020
/** Gets an `include`d module. */
2121
Module getAnIncludedModule() { result = getAnIncludedModule(this) }
2222

23+
/** Holds if this module is a class. */
24+
pragma[noinline]
25+
predicate isClass() { this.getADeclaration() instanceof ClassDeclaration }
26+
2327
/** Gets a textual representation of this module. */
2428
string toString() {
2529
this = TResolved(result)

ql/src/codeql_ruby/controlflow/CfgNodes.qll

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,9 @@ abstract private class ExprChildMapping extends Expr {
123123
*/
124124
abstract predicate relevantChild(Expr child);
125125

126-
private AstNode getAChildStar() {
127-
result = this
128-
or
129-
result.getParent() = this.getAChildStar()
130-
}
131-
132126
pragma[noinline]
133127
private BasicBlock getABasicBlockInScope() {
134-
result.getANode() = TAstCfgNode(this.getAChildStar(), _)
128+
result.getANode() = TAstCfgNode(this.getAChild*(), _)
135129
}
136130

137131
pragma[nomagic]

ql/src/codeql_ruby/dataflow/internal/DataFlowDispatch.qll

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,10 @@ private DataFlow::LocalSourceNode trackInstance(Module tp, TypeTracker t) {
147147
)
148148
or
149149
// `self` in singleton method
150-
exists(Self self, MethodBase enclosing, DataFlow::Node objectNode |
150+
exists(Self self, MethodBase enclosing |
151151
self = result.asExpr().getExpr() and
152-
singletonMethod(enclosing, objectNode.asExpr().getExpr()) and
152+
flowsToSingletonMethodObject(trackInstance(tp), enclosing) and
153153
enclosing = self.getEnclosingMethod() and
154-
trackInstance(tp).flowsTo(objectNode) and
155154
not self.getEnclosingModule().getEnclosingMethod() = enclosing
156155
)
157156
or
@@ -166,9 +165,7 @@ private DataFlow::LocalSourceNode trackInstance(Module tp, TypeTracker t) {
166165
// a module or class
167166
exists(Module m |
168167
result = trackModule(m) and
169-
if m.getADeclaration() instanceof ClassDeclaration
170-
then tp = TResolved("Class")
171-
else tp = TResolved("Module")
168+
if m.isClass() then tp = TResolved("Class") else tp = TResolved("Module")
172169
)
173170
)
174171
or
@@ -197,13 +194,26 @@ private predicate singletonMethod(MethodBase method, Expr object) {
197194
)
198195
}
199196

197+
pragma[nomagic]
198+
private predicate flowsToSingletonMethodObject(DataFlow::LocalSourceNode nodeFrom, MethodBase method) {
199+
exists(DataFlow::LocalSourceNode nodeTo |
200+
nodeFrom.flowsTo(nodeTo) and
201+
singletonMethod(method, nodeTo.asExpr().getExpr())
202+
)
203+
}
204+
205+
pragma[nomagic]
206+
private predicate moduleFlowsToSingletonMethodObject(Module m, MethodBase method) {
207+
flowsToSingletonMethodObject(trackModule(m), method)
208+
}
209+
200210
pragma[nomagic]
201211
private DataFlow::LocalSourceNode trackSingletonMethod0(MethodBase method, TypeTracker t) {
202212
t.start() and
203-
exists(DataFlow::Node nodeTo | singletonMethod(method, nodeTo.asExpr().getExpr()) |
204-
result.flowsTo(nodeTo)
213+
(
214+
flowsToSingletonMethodObject(result, method)
205215
or
206-
exists(Module m | result = trackModule(m) and trackModule(m).flowsTo(nodeTo))
216+
exists(Module m | result = trackModule(m) and moduleFlowsToSingletonMethodObject(m, method))
207217
)
208218
or
209219
exists(TypeTracker t2 | result = trackSingletonMethod0(method, t2).track(t2, t))

0 commit comments

Comments
 (0)