Skip to content

Commit ff99439

Browse files
committed
micro optimize the hot loops by adding special cases and removing streams
1 parent fe551f1 commit ff99439

File tree

1 file changed

+59
-14
lines changed

1 file changed

+59
-14
lines changed

javascript/extractor/src/com/semmle/js/extractor/CFGExtractor.java

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@
122122
import java.util.Map;
123123
import java.util.Set;
124124
import java.util.Stack;
125-
import java.util.stream.Collectors;
126-
import java.util.stream.Stream;
127125

128126
/**
129127
* Extractor for intra-procedural expression-level control flow graphs.
@@ -187,21 +185,55 @@ public CFGExtractor(ASTExtractor astExtractor) {
187185
}
188186

189187
private static Collection<Node> union(Node x, Node y) {
190-
return union(Collections.singleton(x), Collections.singleton(y));
188+
if (x == y) {
189+
if (x == null) {
190+
return Collections.emptySet();
191+
} else {
192+
return Collections.singleton(x);
193+
}
194+
}
195+
196+
if (x == null) {
197+
return Collections.singleton(y);
198+
}
199+
if (y == null) {
200+
return Collections.singleton(x);
201+
}
202+
203+
return Arrays.asList(x, y);
191204
}
192205

193206
private static Collection<Node> union(Collection<Node> xs, Node y) {
194-
return union(xs, Collections.singleton(y));
207+
if (y == null) {
208+
return xs;
209+
}
210+
if (xs == null || xs.isEmpty()) {
211+
return Collections.singleton(y);
212+
}
213+
if (xs.contains(y)) {
214+
return xs;
215+
}
216+
217+
return nonNullUnion(xs, Collections.singleton(y));
195218
}
196219

197220
private static Collection<Node> union(Node x, Collection<Node> ys) {
198-
return union(Collections.singleton(x), ys);
221+
if (x == null) {
222+
return ys;
223+
}
224+
if (ys == null || ys.isEmpty()) {
225+
return Collections.singleton(x);
226+
}
227+
if (ys.contains(x)) {
228+
return ys;
229+
}
230+
231+
return nonNullUnion(Collections.singleton(x), ys);
199232
}
200233

201234
/**
202235
* Creates an order preserving concatenation of the nodes in `xs` and `ys` without duplicates.
203236
*/
204-
@SuppressWarnings("unchecked")
205237
private static Collection<Node> union(Collection<Node> xs, Collection<Node> ys) {
206238
if (xs == null || xs.size() == 0) {
207239
return ys;
@@ -210,8 +242,21 @@ private static Collection<Node> union(Collection<Node> xs, Collection<Node> ys)
210242
return xs;
211243
}
212244

213-
Set<Node> set = new HashSet<>();
214-
return Stream.concat(xs.stream(), ys.stream()).filter(set::add).collect(Collectors.toList());
245+
return nonNullUnion(xs, ys);
246+
}
247+
248+
/**
249+
* Creates an order preserving concatenation of the nodes in `xs` and `ys` without duplicates.
250+
* Where `xs` and `ys` have non null values, and are non-empty.
251+
*/
252+
private static Collection<Node> nonNullUnion(Collection<Node> xs, Collection<Node> ys) {
253+
List<Node> result = new ArrayList<>(xs);
254+
for (Node y : ys) {
255+
if (!result.contains(y)) {
256+
result.add(y);
257+
}
258+
}
259+
return result;
215260
}
216261

217262
/**
@@ -1167,12 +1212,12 @@ private List<Node> getDecoratorsOfClass(AClass ac) {
11671212
instanceDecorators.addAll(decorators);
11681213
}
11691214
}
1170-
return Arrays.asList(
1171-
instanceDecorators,
1172-
staticDecorators,
1173-
constructorParameterDecorators,
1174-
classDecorators
1175-
).stream().flatMap(list -> list.stream()).collect(Collectors.toList());
1215+
List<Node> result = new ArrayList<>();
1216+
result.addAll(instanceDecorators);
1217+
result.addAll(staticDecorators);
1218+
result.addAll(constructorParameterDecorators);
1219+
result.addAll(classDecorators);
1220+
return result;
11761221
}
11771222

11781223
@Override

0 commit comments

Comments
 (0)