-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamPerformance.hint
459 lines (365 loc) · 16 KB
/
StreamPerformance.hint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<!description="Improve stream call chains and other java.util.stream.Stream related inspections.">
// Stream.empty uses a constant internally + is more descriptive
java.util.stream.Stream.of() =>
java.util.stream.Stream.empty()
;;
// Arrays.asList().stream() -> Arrays.stream() or Stream.of()
java.util.Arrays.asList($l, $rest$).stream() =>
java.util.stream.Stream.of($l, $rest$)
;;
// Collections.singleton().stream() -> Stream.of()
java.util.Collections.singleton($s).stream() =>
java.util.stream.Stream.of($s)
;;
java.util.Collections.singletonList($s).stream() =>
java.util.stream.Stream.of($s)
;;
// Collections.emptyCol().stream() -> Stream.empty()
java.util.Collections.emptyList().stream() =>
java.util.stream.Stream.empty()
;;
java.util.Collections.emptySet().stream() =>
java.util.stream.Stream.empty()
;;
java.util.Collections.emptySortedSet().stream() =>
java.util.stream.Stream.empty()
;;
java.util.Collections.emptyNavigableSet().stream() =>
java.util.stream.Stream.empty()
;;
// other empty immutables -> Stream.empty()
java.util.List.of().stream() =>
java.util.stream.Stream.empty()
;;
java.util.Set.of().stream() =>
java.util.stream.Stream.empty()
;;
// 11
"String::lines stream can be used.":
$str.split("\n") :: $str instanceof java.lang.String && sourceVersionGE(11) =>
$str.lines().toArray(String[]::new)
;;
"String::lines stream can be used.":
$str.split("\\R") :: $str instanceof java.lang.String && sourceVersionGE(11) =>
$str.lines().toArray(String[]::new)
;;
// 9/16+
"Stream::toList can be used.":
java.util.List.of($stream.toArray($T[]::new)) :: $stream instanceof java.util.stream.Stream && sourceVersionGE(9) =>
$stream.toList(); :: sourceVersionGE(16) =>
$stream.collect(java.util.stream.Collectors.toList()) :: otherwise
;;
"Stream::toList can be used.":
java.util.Arrays.asList($stream.toArray($T[]::new)) :: $stream instanceof java.util.stream.Stream && sourceVersionGE(9) =>
$stream.toList(); :: sourceVersionGE(16) =>
$stream.collect(java.util.stream.Collectors.toList()) :: otherwise
;;
"Stream::toArray can be used.":
$stream.toList().toArray($T[]::new) :: $stream instanceof java.util.stream.Stream =>
$stream.toArray($T[]::new)
;;
// shorter and faster
// https://bugs.openjdk.java.net/browse/JDK-8265237
"String::join can be used.":
java.util.stream.Stream.of($e, $rest$).collect(java.util.stream.Collectors.joining($d)) :: $d instanceof CharSequence =>
String.join($d, $e, $rest$)
;;
"String::join can be used.":
java.util.Arrays.stream($e).collect(java.util.stream.Collectors.joining($d)) :: $d instanceof CharSequence =>
String.join($d, $e)
;;
"String::join can be used.":
java.util.List.of($e, $rest$).stream().collect(java.util.stream.Collectors.joining($d)) :: $d instanceof CharSequence =>
String.join($d, $e, $rest$)
;;
"String::join can be used.":
$col.stream().collect(java.util.stream.Collectors.joining($d)) :: $col instanceof java.util.Collection && $d instanceof CharSequence =>
String.join($d, $col)
;;
// todo: doesn't work
// map primitve to object -> boxed()
/*
$stream.mapToObj(($x) -> $x) :: $stream instanceof java.util.stream.IntStream =>
$stream.boxed()
;;
$stream.mapToObj(($x) -> $x) :: $stream instanceof java.util.stream.LongStream =>
$stream.boxed()
;;
$stream.mapToObj(($x) -> $x) :: $stream instanceof java.util.stream.DoubleStream =>
$stream.boxed()
;;
// use peek if possible
$stream.map(($x) -> {
$body$;
return $x;
}) :: $stream instanceof java.util.stream.Stream =>
$stream.peek($x -> {
$body$;
})
;;
*/
// filter+findAny is present -> anyMatch
$stream.filter($lambda).findAny().isPresent() :: $stream instanceof java.util.stream.Stream =>
$stream.anyMatch($lambda)
;;
!$stream.filter($lambda).findAny().isEmpty() :: $stream instanceof java.util.stream.Stream =>
$stream.anyMatch($lambda)
;;
// filter+findAny is empty -> noneMatch
$stream.filter($lambda).findAny().isEmpty() :: $stream instanceof java.util.stream.Stream =>
$stream.noneMatch($lambda)
;;
!$stream.filter($lambda).findAny().isPresent() :: $stream instanceof java.util.stream.Stream =>
$stream.noneMatch($lambda)
;;
// !anyMatch -> noneMatch
!$stream.anyMatch($lambda) :: $stream instanceof java.util.stream.Stream =>
$stream.noneMatch($lambda)
;;
// !noneMatch -> anyMatch
!$stream.noneMatch($lambda) :: $stream instanceof java.util.stream.Stream =>
$stream.anyMatch($lambda)
;;
"use min() instead of sorted().findFirst()":
$stream.sorted($comp).findFirst() :: $stream instanceof java.util.stream.Stream && $comp instanceof java.util.Comparator =>
$stream.min($comp)
;;
"use min() instead of sorted().findFirst()":
$stream.sorted().findFirst() :: $stream instanceof java.util.stream.Stream =>
$stream.min(java.util.Comparator.naturalOrder())
;;
// min -> max + invert order
$stream.min(java.util.Comparator.reverseOrder()) :: $stream instanceof java.util.stream.Stream =>
$stream.max(java.util.Comparator.naturalOrder())
;;
// max -> min + invert order
$stream.max(java.util.Comparator.reverseOrder()) :: $stream instanceof java.util.stream.Stream =>
$stream.min(java.util.Comparator.naturalOrder())
;;
// step order
"move sorted() before distinct()":
$stream.distinct().sorted($comp$) :: $stream instanceof java.util.stream.Stream =>
$stream.sorted($comp$).distinct()
;;
"move filter() before distinct()":
$stream.distinct().filter($filter) :: $stream instanceof java.util.stream.Stream =>
$stream.filter($filter).distinct()
;;
"move filter() before sorted()":
$stream.sorted($comp$).filter($filter) :: $stream instanceof java.util.stream.Stream =>
$stream.filter($filter).sorted($comp$)
;;
// redundant steps
"remove redundant sorted() step before findAny()":
$stream.sorted($comp$).findAny() :: $stream instanceof java.util.stream.Stream =>
$stream.findAny()
;;
"remove redundant distinct() step before findAny()":
$stream.distinct().findAny() :: $stream instanceof java.util.stream.Stream =>
$stream.findAny()
;;
"remove redundant distinct() step before findFirst()":
$stream.distinct().findFirst() :: $stream instanceof java.util.stream.Stream =>
$stream.findFirst()
;;
"remove redundant sorted() step before anyMatch()":
$stream.sorted($comp$).anyMatch($pred) :: $stream instanceof java.util.stream.Stream =>
$stream.anyMatch($pred)
;;
"remove redundant distinct() step before anyMatch()":
$stream.distinct().anyMatch($pred)) :: $stream instanceof java.util.stream.Stream =>
$stream.anyMatch($pred))
;;
"remove redundant sorted() step before noneMatch()":
$stream.sorted($comp$).noneMatch($pred) :: $stream instanceof java.util.stream.Stream =>
$stream.noneMatch($pred)
;;
"remove redundant distinct() step before noneMatch()":
$stream.distinct().noneMatch($pred) :: $stream instanceof java.util.stream.Stream =>
$stream.noneMatch($pred)
;;
"remove redundant sorted() step before allMatch()":
$stream.sorted($comp$).allMatch($pred) :: $stream instanceof java.util.stream.Stream =>
$stream.allMatch($pred)
;;
"remove redundant distinct() step before allMatch()":
$stream.distinct().allMatch($pred) :: $stream instanceof java.util.stream.Stream =>
$stream.allMatch($pred)
;;
// collection.stream().forEach() -> collection.forEach()
"remove redundant stream() before forEach().":
$col.stream().forEach($con) :: $col instanceof java.util.Collection && $con instanceof java.util.function.Consumer =>
$col.forEach($con)
;;
"remove redundant toList() before forEach().":
$stream.toList().forEach($con) :: $stream instanceof java.util.stream.Stream && $con instanceof java.util.function.Consumer =>
$stream.forEach($con)
;;
"can be simplified to iterable.forEach(..)":
$iterable.iterator().forEachRemaining($con); :: $iterable instanceof java.lang.Iterable && $con instanceof java.util.function.Consumer =>
$iterable.forEach($con);
;;
"could potentially use forEach() instead of intermediate Collection if all elements are traversed.":
for ($E $elem : $stream.toList()) {
$body$;
}
:: $stream instanceof java.util.stream.Stream =>
$stream.forEach($elem -> {
$body$;
});
;;
"could potentially use forEach() instead of intermediate Collection if all elements are traversed.":
java.util.List<$E> $list = $stream.toList();
for ($E $elem : $list) {
$body$;
}
:: $stream instanceof java.util.stream.Stream =>
$stream.forEach($elem -> {
$body$;
});
;;
// doesn't work as fix. !containsAny("return;") doesn't work either
/*
=>
for ($E $elem : (Iterable<$E>) $stream::iterator) {
$body$;
}
*/
// use copy constructors
// collection.stream().collect(toList/toSet) -> new CollectionType<>(collection)
$col.stream().collect(java.util.stream.Collectors.toList()) :: $col instanceof java.util.Collection =>
new java.util.ArrayList<>($col)
;;
$col.stream().toList() :: $col instanceof java.util.Collection =>
new java.util.ArrayList<>($col)
;;
$col.stream().collect(java.util.stream.Collectors.toSet()) :: $col instanceof java.util.Collection =>
new java.util.HashSet<>($col)
;;
// collection.stream().toArray() -> collection.toArray()
$col.stream().toArray($gen$) :: $col instanceof java.util.Collection =>
$col.toArray($gen$)
;;
// Collections know best how to determine their element count
$col.stream().count() :: $col instanceof java.util.Collection =>
$col.size()
;;
// same here for collections
$stream.flatMap(java.util.Collection::stream).count() :: $stream instanceof java.util.stream.Stream =>
$stream.mapToLong(java.util.Collection::size).sum()
;;
// and map
$stream.map(java.util.Map::values).flatMap(java.util.Collection::stream).count() :: $stream instanceof java.util.stream.Stream =>
$stream.mapToLong(java.util.Map::size).sum()
;;
$stream.map(java.util.Map::values).mapToLong(java.util.Collection::size) :: $stream instanceof java.util.stream.Stream =>
$stream.mapToLong(java.util.Map::size)
;;
// unnessesary collection step
$stream.toList().size() :: $stream instanceof java.util.stream.Stream =>
$stream.count()
;;
// simplify collectors
// toList() shortcut 16+
"could be potentially changed to stream.toList(), if unmodifiable.":
$stream.collect(java.util.stream.Collectors.toList()) :: $stream instanceof java.util.stream.Stream && sourceVersionGE(16) =>
$stream.toList()
;;
$stream.collect(java.util.stream.Collectors.toUnmodifiableList()) :: $stream instanceof java.util.stream.Stream && sourceVersionGE(16) =>
$stream.toList()
;;
// default impls
java.util.stream.Collectors.toCollection(java.util.ArrayList::new) =>
java.util.stream.Collectors.toList()
;;
java.util.stream.Collectors.toCollection(java.util.HashSet::new) =>
java.util.stream.Collectors.toSet()
;;
java.util.stream.Collectors.toMap($keymapper, $valuemapper, $merger, java.util.HashMap::new) =>
java.util.stream.Collectors.toMap($keymapper, $valuemapper, $merger)
;;
java.util.stream.Collectors.toConcurrentMap($keymapper, $valuemapper, $merger, java.util.concurrent.ConcurrentHashMap::new) =>
java.util.stream.Collectors.toConcurrentMap($keymapper, $valuemapper, $merger)
;;
// redundant distinct() or sorted() before toSet()
"distinct() is redundant before Set collectors.":
$stream.distinct().collect(java.util.stream.Collectors.toSet()) :: $stream instanceof java.util.stream.Stream =>
$stream.collect(java.util.stream.Collectors.toSet())
;;
"distinct() is redundant before Set collectors.":
$stream.distinct().collect(java.util.stream.Collectors.toUnmodifiableSet()) :: $stream instanceof java.util.stream.Stream =>
$stream.collect(java.util.stream.Collectors.toUnmodifiableSet())
;;
"sorted() is redundant before unordered Set collectors.":
$stream.sorted($comp$).collect(java.util.stream.Collectors.toSet()) :: $stream instanceof java.util.stream.Stream =>
$stream.collect(java.util.stream.Collectors.toSet())
;;
"sorted() is redundant before unordered Set collectors.":
$stream.sorted($comp$).collect(java.util.stream.Collectors.toUnmodifiableSet()) :: $stream instanceof java.util.stream.Stream =>
$stream.collect(java.util.stream.Collectors.toUnmodifiableSet())
;;
// redundant sort before toMap()
"sorted() is redundant before unordered Map collectors.":
$stream.sorted($comp$).collect(java.util.stream.Collectors.toMap($keymapper, $valuemapper)) :: $stream instanceof java.util.stream.Stream =>
$stream.collect(java.util.stream.Collectors.toMap($keymapper, $valuemapper))
;;
"sorted() is redundant before unordered Map collectors.":
$stream.sorted($comp$).collect(java.util.stream.Collectors.toConcurrentMap($keymapper, $valuemapper)) :: $stream instanceof java.util.stream.Stream =>
$stream.collect(java.util.stream.Collectors.toConcurrentMap($keymapper, $valuemapper))
;;
"sorted() is redundant before unordered Map collectors.":
$stream.sorted($comp$).collect(java.util.stream.Collectors.toUnmodifiableMap($keymapper, $valuemapper)) :: $stream instanceof java.util.stream.Stream =>
$stream.collect(java.util.stream.Collectors.toUnmodifiableMap($keymapper, $valuemapper))
;;
"sorted() could be potentially removed if the merge function does not require ordering, the resulting map is unordered.":
$stream.sorted($comp$).collect(java.util.stream.Collectors.toMap($keymapper, $valuemapper, $merger)) :: $stream instanceof java.util.stream.Stream =>
$stream.collect(java.util.stream.Collectors.toMap($keymapper, $valuemapper, $merger))
;;
"sorted() could be potentially removed if the merge function does not require ordering, the resulting map is unordered.":
$stream.sorted($comp$).collect(java.util.stream.Collectors.toConcurrentMap($keymapper, $valuemapper, $merger)) :: $stream instanceof java.util.stream.Stream =>
$stream.collect(java.util.stream.Collectors.toConcurrentMap($keymapper, $valuemapper, $merger))
;;
"sorted() could be potentially removed if the merge function does not require ordering, the resulting map is unordered.":
$stream.sorted($comp$).collect(java.util.stream.Collectors.toUnmodifiableMap($keymapper, $valuemapper, $merger)) :: $stream instanceof java.util.stream.Stream =>
$stream.collect(java.util.stream.Collectors.toUnmodifiableMap($keymapper, $valuemapper, $merger))
;;
// stream.collect(counting()) -> stream.count()
$stream.collect(java.util.stream.Collectors.counting()) :: $stream instanceof java.util.stream.Stream =>
$stream.count()
;;
// stream.collect(maxBy()) -> stream.max()
$stream.collect(java.util.stream.Collectors.maxBy($comp)) :: $stream instanceof java.util.stream.Stream && $comp instanceof java.util.Comparator =>
$stream.max($comp)
;;
// stream.collect(minBy()) -> stream.min()
$stream.collect(java.util.stream.Collectors.minBy($comp)) :: $stream instanceof java.util.stream.Stream && $comp instanceof java.util.Comparator =>
$stream.min($comp)
;;
// stream.collect(mapping()) -> stream.map().collect()
$stream.collect(java.util.stream.Collectors.mapping($f, $col)) :: $stream instanceof java.util.stream.Stream && $f instanceof java.util.function.Function && $col instanceof java.util.stream.Collector =>
$stream.map($f).collect($col)
;;
// stream.collect(reducing()) -> stream.reduce()
$stream.collect(java.util.stream.Collectors.reducing($params$)) :: $stream instanceof java.util.stream.Stream =>
$stream.reduce($params$)
;;
// stream.collect(summingInt()) -> stream.mapToInt().sum()
$stream.collect(java.util.stream.Collectors.summingInt($f)) :: $stream instanceof java.util.stream.Stream && $f instanceof java.util.function.ToIntFunction =>
$stream.mapToInt($f).sum()
;;
$stream.collect(java.util.stream.Collectors.summingLong($f)) :: $stream instanceof java.util.stream.Stream && $f instanceof java.util.function.ToLongFunction =>
$stream.mapToLong($f).sum()
;;
$stream.collect(java.util.stream.Collectors.summingDouble($f)) :: $stream instanceof java.util.stream.Stream && $f instanceof java.util.function.ToDoubleFunction =>
$stream.mapToDouble($f).sum()
;;
// same for summarizing stats
$stream.collect(java.util.stream.Collectors.summarizingInt($f)) :: $stream instanceof java.util.stream.Stream && $f instanceof java.util.function.ToIntFunction =>
$stream.mapToInt($f).summaryStatistics()
;;
$stream.collect(java.util.stream.Collectors.summarizingLong($f)) :: $stream instanceof java.util.stream.Stream && $f instanceof java.util.function.ToLongFunction =>
$stream.mapToLong($f).summaryStatistics()
;;
$stream.collect(java.util.stream.Collectors.summarizingDouble($f)) :: $stream instanceof java.util.stream.Stream && $f instanceof java.util.function.ToDoubleFunction =>
$stream.mapToDouble($f).summaryStatistics()
;;