Skip to content

Commit 3d0e3aa

Browse files
committed
Java: Fix a number of performance issues when toString is cached.
1 parent 8a8b795 commit 3d0e3aa

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

java/ql/src/Violations of Best Practice/Boxed Types/BoxedVariable.ql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ predicate notDeliberatelyBoxed(LocalBoxedVar v) {
3939
)
4040
}
4141

42+
pragma[nomagic]
43+
int callableGetNumberOfParameters(Callable c) { result = c.getNumberOfParameters() }
44+
4245
/**
4346
* Replacing the type of a boxed variable with the corresponding primitive type may affect
4447
* overload resolution. If this is the case then the boxing is most likely intentional and
@@ -52,7 +55,7 @@ predicate affectsOverload(LocalBoxedVar v) {
5255
c1.getParameterType(i) instanceof RefType and
5356
c2.getParameterType(i) instanceof PrimitiveType and
5457
c1.getName() = c2.getName() and
55-
c1.getNumberOfParameters() = c2.getNumberOfParameters()
58+
callableGetNumberOfParameters(c1) = callableGetNumberOfParameters(c2)
5659
)
5760
}
5861

java/ql/src/semmle/code/Location.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class Top extends @top {
8484
int getNumberOfCommentLines() { numlines(this, _, _, result) }
8585

8686
/** Gets a textual representation of this element. */
87+
cached
8788
string toString() { hasName(this, result) }
8889
}
8990

java/ql/src/semmle/code/java/deadcode/DeadCode.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ class RootdefCallable extends Callable {
301301
}
302302
}
303303

304-
pragma[noinline]
304+
pragma[nomagic]
305305
private predicate overrideAccess(Callable c, int i) {
306306
exists(Method m | m.overridesOrInstantiates+(c) | exists(m.getParameter(i).getAnAccess()))
307307
}

java/ql/src/semmle/code/java/frameworks/gwt/GWT.qll

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,29 @@ class ClientSideGwtCompilationUnit extends GwtCompilationUnit {
6161
}
6262
}
6363

64-
/** Auxiliary predicate: `jsni` is a JSNI comment associated with method `m`. */
65-
private predicate jsniComment(Javadoc jsni, Method m) {
64+
private predicate jsni(Javadoc jsni, File file, int startline) {
6665
// The comment must start with `-{` ...
6766
jsni.getChild(0).getText().matches("-{%") and
6867
// ... and it must end with `}-`.
6968
jsni.getChild(jsni.getNumChild() - 1).getText().matches("%}-") and
70-
// The associated callable must be marked as `native` ...
69+
file = jsni.getFile() and
70+
startline = jsni.getLocation().getStartLine()
71+
}
72+
73+
private predicate nativeMethodLines(Method m, File file, int line) {
7174
m.isNative() and
72-
// ... and the comment has to be contained in `m`.
73-
jsni.getFile() = m.getFile() and
74-
jsni.getLocation().getStartLine() in [m.getLocation().getStartLine() .. m
75-
.getLocation()
76-
.getEndLine()]
75+
file = m.getFile() and
76+
line in [m.getLocation().getStartLine() .. m.getLocation().getEndLine()]
77+
}
78+
79+
/** Auxiliary predicate: `jsni` is a JSNI comment associated with method `m`. */
80+
private predicate jsniComment(Javadoc jsni, Method m) {
81+
exists(File file, int line |
82+
jsni(jsni, file, line) and
83+
// The associated callable must be marked as `native`
84+
// and the comment has to be contained in `m`.
85+
nativeMethodLines(m, file, line)
86+
)
7787
}
7888

7989
/**

0 commit comments

Comments
 (0)