Skip to content

Commit 7031391

Browse files
committed
More performance fixes.
1 parent d2113f1 commit 7031391

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

python/ql/src/semmle/python/Exprs.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ class FloatLiteral extends Num {
399399

400400
FloatLiteral() {
401401
not this instanceof ImaginaryLiteral and
402-
exists(string n | n = this.getN() | n.charAt(_) = "." or n.charAt(_) = "e" or n.charAt(_) = "E")
402+
this.getN().regexpMatch(".*[.eE].*")
403403
}
404404

405405
float getValue() {
@@ -427,15 +427,15 @@ class FloatLiteral extends Num {
427427

428428
/** An imaginary numeric constant, such as `3j` */
429429
class ImaginaryLiteral extends Num {
430+
private float value;
430431

431432
ImaginaryLiteral() {
432-
exists(string n | n = this.getN() | n.charAt(_) = "j")
433+
value = this.getN().regexpCapture("(.+)j.*", 1).toFloat()
433434
}
434435

435436
/** Gets the value of this constant as a floating point value */
436437
float getValue() {
437-
exists(string s, int j | s = this.getN() and s.charAt(j) = "j" |
438-
result = s.prefix(j).toFloat())
438+
result = value
439439
}
440440

441441
override string toString() {

python/ql/src/semmle/python/dataflow/SsaDefinitions.qll

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,13 @@ class NonLocalVariable extends PythonSsaSourceVariable {
146146
this.(LocalVariable).getScope().getEntryNode() = result
147147
}
148148

149+
pragma [noinline]
150+
Scope scope_as_local_variable() {
151+
result = this.(LocalVariable).getScope()
152+
}
153+
149154
override CallNode redefinedAtCallSite() {
150-
result.getScope().getScope*() = this.(LocalVariable).getScope()
155+
result.getScope().getScope*() = this.scope_as_local_variable()
151156
}
152157

153158
}
@@ -173,7 +178,7 @@ class ClassLocalVariable extends PythonSsaSourceVariable {
173178
class BuiltinVariable extends PythonSsaSourceVariable {
174179

175180
BuiltinVariable() {
176-
this instanceof GlobalVariable and
181+
this instanceof GlobalVariable and
177182
not exists(this.(Variable).getAStore()) and
178183
not this.(Variable).getId() = "__name__" and
179184
not this.(Variable).getId() = "__package__" and
@@ -207,13 +212,21 @@ class ModuleVariable extends PythonSsaSourceVariable {
207212
)
208213
}
209214

210-
override ControlFlowNode getAnImplicitUse() {
215+
pragma [noinline]
216+
CallNode global_variable_callnode() {
217+
result.getScope() = this.(GlobalVariable).getScope()
218+
}
219+
220+
pragma[noinline]
221+
ImportMemberNode global_variable_import() {
211222
result.getScope() = this.(GlobalVariable).getScope() and
212-
(
213-
result instanceof CallNode
214-
or
215-
import_from_dot_in_init(result.(ImportMemberNode).getModule(this.getName()))
216-
)
223+
import_from_dot_in_init(result.(ImportMemberNode).getModule(this.getName()))
224+
}
225+
226+
override ControlFlowNode getAnImplicitUse() {
227+
result = global_variable_callnode()
228+
or
229+
result = global_variable_import()
217230
or
218231
exists(ImportTimeScope scope |
219232
scope.entryEdge(result, _) |
@@ -292,8 +305,13 @@ class EscapingGlobalVariable extends ModuleVariable {
292305
result = this.innerScope().getEntryNode()
293306
}
294307

308+
pragma [noinline]
309+
Scope scope_as_global_variable() {
310+
result = this.(GlobalVariable).getScope()
311+
}
312+
295313
override CallNode redefinedAtCallSite() {
296-
result.(CallNode).getScope().getScope*() = this.(GlobalVariable).getScope()
314+
result.(CallNode).getScope().getScope*() = this.scope_as_global_variable()
297315
}
298316

299317
}
@@ -324,8 +342,13 @@ class SpecialSsaSourceVariable extends PythonSsaSourceVariable {
324342
this.getScope().getEntryNode() = result
325343
}
326344

345+
pragma [noinline]
346+
Scope scope_as_global_variable() {
347+
result = this.(GlobalVariable).getScope()
348+
}
349+
327350
override CallNode redefinedAtCallSite() {
328-
result.(CallNode).getScope().getScope*() = this.(GlobalVariable).getScope()
351+
result.(CallNode).getScope().getScope*() = this.scope_as_global_variable()
329352
}
330353

331354
}

0 commit comments

Comments
 (0)