Skip to content

Commit 07f3ce0

Browse files
Harry Chanmatthiasblaesing
authored andcommitted
Performance: replace toArray(new T[size]) with toArray(new T[0])
Analysis by Aleksey Shipilёv: https://shipilev.net/blog/2016/arrays-wisdom-ancients/#_conclusin Bottom line: toArray(new T[0]) seems faster, safer, and contractually cleaner, and therefore should be the default choice now. Future VM optimizations may close this performance gap for toArray(new T[size]), rendering the current "believed to be optimal" usages on par with an actually optimal one. Further improvements in toArray APIs would follow the same logic as toArray(new T[0]) — the collection itself should create the appropriate storage.
1 parent 904a71e commit 07f3ce0

10 files changed

Lines changed: 13 additions & 12 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Features
99
--------
1010
* [#1058](https://github.com/java-native-access/jna/pull/1058): Add selectable timeout to stopService() and improve timeout handling - [@keithharp](https://github.com/keithharp).
1111
* [#1050](https://github.com/java-native-access/jna/pull/1050): Add `c.s.j.p.win32.VersionHelpers` and supporting functions - [@dbwiddis](https://github.com/dbwiddis).
12+
* [#1061](https://github.com/java-native-access/jna/pull/1061): replace toArray(new T[size]) with toArray(new T[0]) for better performance - [@hc-codersatlas](https://github.com/hc-codersatlas).
1213

1314
Bug Fixes
1415
---------

contrib/platform/src/com/sun/jna/platform/RasterRangesUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ public static boolean outputOccupiedRanges(int[] pixels, int w, int h, int occup
243243
private static Set<Rectangle> mergeRects(Set<Rectangle> prev, Set<Rectangle> current) {
244244
Set<Rectangle> unmerged = new HashSet<Rectangle>(prev);
245245
if (!prev.isEmpty() && !current.isEmpty()) {
246-
Rectangle[] pr = prev.toArray(new Rectangle[prev.size()]);
247-
Rectangle[] cr = current.toArray(new Rectangle[current.size()]);
246+
Rectangle[] pr = prev.toArray(new Rectangle[0]);
247+
Rectangle[] cr = current.toArray(new Rectangle[0]);
248248
int ipr = 0;
249249
int icr = 0;
250250
while (ipr < pr.length && icr < cr.length) {

contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ public static String[] registryGetStringArray(HKEY hKey, String value) {
946946
result.add(s);
947947
}
948948
}
949-
return result.toArray(new String[result.size()]);
949+
return result.toArray(new String[0]);
950950
}
951951

952952
/**
@@ -2632,7 +2632,7 @@ public static ACE_HEADER[] getFileSecurity(String fileName,
26322632
result.add(aceStructure);
26332633
}
26342634
}
2635-
return result.toArray(new ACE_HEADER[result.size()]);
2635+
return result.toArray(new ACE_HEADER[0]);
26362636
}
26372637

26382638
return aceStructures;

contrib/platform/src/com/sun/jna/platform/win32/DdemlUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,7 @@ private Ddeml.HSZPAIR[] onWildconnect(int transactionType, HSZ topic, HSZ servic
17511751
for(WildconnectHandler handler: wildconnectHandler) {
17521752
hszpairs.addAll(handler.onWildconnect(transactionType, topic, service, convcontext, sameInstance));
17531753
}
1754-
return hszpairs.toArray(new HSZPAIR[hszpairs.size()]);
1754+
return hszpairs.toArray(new HSZPAIR[0]);
17551755
}
17561756

17571757

contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ public static final WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] getLogicalProce
674674
int returnedStructCount = bufferSize.getValue().intValue()
675675
/ sizePerStruct;
676676
return (WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]) firstInformation
677-
.toArray(new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[returnedStructCount]);
677+
.toArray(new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[0]);
678678
}
679679

680680
/**

contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ public static DomainTrust[] getDomainTrusts(String serverName) {
636636
}
637637
try {
638638
DS_DOMAIN_TRUSTS domainTrustRefs = new DS_DOMAIN_TRUSTS(domainsPointerRef.getValue());
639-
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[domainTrustCount.getValue()]);
639+
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[0]);
640640
ArrayList<DomainTrust> trusts = new ArrayList<DomainTrust>(domainTrustCount.getValue());
641641
for(DS_DOMAIN_TRUSTS domainTrust : domainTrusts) {
642642
DomainTrust t = new DomainTrust();

contrib/platform/test/com/sun/jna/platform/win32/Netapi32Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public void testDsEnumerateDomainTrusts() {
247247
assertTrue(domainTrustCount.getValue() >= 0);
248248

249249
DS_DOMAIN_TRUSTS domainTrustRefs = new DS_DOMAIN_TRUSTS(domainsPointerRef.getValue());
250-
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[domainTrustCount.getValue()]);
250+
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[0]);
251251

252252
for(DS_DOMAIN_TRUSTS trust : domainTrusts) {
253253
assertTrue(trust.DnsDomainName.length() > 0);

src/com/sun/jna/CallbackReference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ private static Method getCallbackMethod(Class<?> cls) {
346346
}
347347
}
348348

349-
Method[] methods = pmethods.toArray(new Method[pmethods.size()]);
349+
Method[] methods = pmethods.toArray(new Method[0]);
350350
if (methods.length == 1) {
351351
return checkMethod(methods[0]);
352352
}

src/com/sun/jna/NativeLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ static double parseVersion(String ver) {
941941
}
942942
ldPaths.add(0, paths[i]);
943943
}
944-
paths = ldPaths.toArray(new String[ldPaths.size()]);
944+
paths = ldPaths.toArray(new String[0]);
945945
}
946946

947947
for (int i=0;i < paths.length;i++) {

src/com/sun/jna/Pointer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ public Pointer[] getPointerArray(long offset) {
755755
addOffset += Native.POINTER_SIZE;
756756
p = getPointer(offset + addOffset);
757757
}
758-
return array.toArray(new Pointer[array.size()]);
758+
return array.toArray(new Pointer[0]);
759759
}
760760

761761
/** Returns an array of {@link Pointer} of the requested size. */
@@ -837,7 +837,7 @@ public String[] getStringArray(long offset, int length, String encoding) {
837837
addOffset += Native.POINTER_SIZE;
838838
}
839839
}
840-
return strings.toArray(new String[strings.size()]);
840+
return strings.toArray(new String[0]);
841841
}
842842

843843
//////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)