Skip to content

Commit 951df38

Browse files
authored
Merge pull request #6829 from hvitved/csharp/gvn-to-string-concat-range
C#: Speedup GVN string `concat`s by pulling ranges into separate predicates
2 parents 06e59f3 + 764a987 commit 951df38

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

csharp/ql/lib/semmle/code/csharp/Implements.qll

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,15 @@ private module Gvn {
398398
)
399399
}
400400

401+
pragma[noinline]
402+
private predicate toStringPart(int i, int j) {
403+
exists(Unification::GenericType t, int children |
404+
t = this.getConstructedGenericDeclaringTypeAt(i) and
405+
children = t.getNumberOfArgumentsSelf() and
406+
if children = 0 then j = 0 else j in [0 .. 2 * children]
407+
)
408+
}
409+
401410
language[monotonicAggregates]
402411
string toString() {
403412
this.isFullyConstructed() and
@@ -406,11 +415,7 @@ private module Gvn {
406415
or
407416
result =
408417
strictconcat(int i, int j |
409-
exists(Unification::GenericType t, int children |
410-
t = this.getConstructedGenericDeclaringTypeAt(i) and
411-
children = t.getNumberOfArgumentsSelf() and
412-
if children = 0 then j = 0 else j in [0 .. 2 * children]
413-
)
418+
this.toStringPart(i, j)
414419
|
415420
this.toStringConstructedPart(i, j) order by i desc, j
416421
)

csharp/ql/lib/semmle/code/csharp/Unification.qll

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,20 +277,27 @@ module Gvn {
277277
)
278278
}
279279

280+
pragma[noinline]
281+
private predicate toStringPart(int i, int j) {
282+
exists(int offset |
283+
exists(GenericType t, int children |
284+
t = this.getConstructedGenericDeclaringTypeAt(i) and
285+
children = t.getNumberOfArgumentsSelf() and
286+
(if this.isDeclaringTypeAt(i) then offset = 1 else offset = 0) and
287+
if children = 0 then j in [0 .. offset] else j in [0 .. 2 * children + offset]
288+
)
289+
)
290+
}
291+
280292
language[monotonicAggregates]
281293
string toString() {
282294
this.isFullyConstructed() and
283295
exists(CompoundTypeKind k | k = this.getKind() |
284296
result = k.toStringBuiltin(this.getArg(0).toString())
285297
or
286298
result =
287-
strictconcat(int i, int j, int offset |
288-
exists(GenericType t, int children |
289-
t = this.getConstructedGenericDeclaringTypeAt(i) and
290-
children = t.getNumberOfArgumentsSelf() and
291-
(if this.isDeclaringTypeAt(i) then offset = 1 else offset = 0) and
292-
if children = 0 then j in [0 .. offset] else j in [0 .. 2 * children + offset]
293-
)
299+
strictconcat(int i, int j |
300+
toStringPart(i, j)
294301
|
295302
this.toStringConstructedPart(i, j) order by i desc, j
296303
)

0 commit comments

Comments
 (0)