-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collection.toArray().hint
54 lines (48 loc) · 1.99 KB
/
Collection.toArray().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
// performance: https://shipilev.net/blog/2016/arrays-wisdom-ancients/#_conclusion
<!description="Use Colleciton.toArray(new Obj[0]) instead of Colleciton.toArray(new Obj[size]).">
/*
Benchmark (size) Mode Cnt Score Error Units
ToArrayJMH.toNew 8 avgt 5 23.899 ± 0.426 ns/op
ToArrayJMH.toNew 64 avgt 5 70.554 ± 0.787 ns/op
ToArrayJMH.toNew 1024 avgt 5 863.436 ± 9.239 ns/op
ToArrayJMH.toSize 8 avgt 5 27.440 ± 0.290 ns/op
ToArrayJMH.toSize 64 avgt 5 114.091 ± 0.794 ns/op
ToArrayJMH.toSize 1024 avgt 5 1574.972 ± 7.812 ns/op
ToArrayJMH.toZero 8 avgt 5 23.498 ± 0.263 ns/op
ToArrayJMH.toZero 64 avgt 5 70.641 ± 0.662 ns/op
ToArrayJMH.toZero 1024 avgt 5 860.637 ± 4.714 ns/op
*/
"Array conversion can be optimized by allowing the JVM to create the array.":
$T[] $array = new $T[$c.size()];
$a$ $b$ = $c.toArray($array);
$more$ :: $c instanceof java.util.Collection && !referencedIn($array, $more$)
=>
$a$ $b$ = $c.toArray($T[]::new);
$more$ :: sourceVersionGE(11)
=>
$a$ $b$ = $c.toArray(new $T[0]);
$more$ :: otherwise
;;
"Array conversion can be optimized by allowing the JVM to create the array.":
$T[] $array = new $T[$c.size()];
return $c.toArray($array); :: $c instanceof java.util.Collection
=>
return $c.toArray($T[]::new); :: sourceVersionGE(11)
=>
return $c.toArray(new $T[0]); :: otherwise
;;
// https://github.com/apache/netbeans/pull/3166 (12.6+)
/*
"Array conversion can be optimized by allowing the JVM to create the array.":
$c.toArray(new $t[$c.size()]) :: $c instanceof java.util.Collection
=>
$c.toArray($t[]::new) //:: sourceVersionGE(SourceVersion.RELEASE_11)
=>
$c.toArray(new $t[0])
;;
// 11+ best practise
$c.toArray(new $t[0]) :: $c instanceof java.util.Collection //:: sourceVersionGE(SourceVersion.RELEASE_11)
=>
$c.toArray($t[]::new) //:: sourceVersionGE(SourceVersion.RELEASE_11)
;;
*/