Skip to content

Commit

Permalink
Performance: replace toArray(new T[size]) with toArray(new T[0])
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Harry Chan authored and matthiasblaesing committed Jan 25, 2019
1 parent 904a71e commit 07f3ce0
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Features
--------
* [#1058](https://github.com/java-native-access/jna/pull/1058): Add selectable timeout to stopService() and improve timeout handling - [@keithharp](https://github.com/keithharp).
* [#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).
* [#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).

Bug Fixes
---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ public static boolean outputOccupiedRanges(int[] pixels, int w, int h, int occup
private static Set<Rectangle> mergeRects(Set<Rectangle> prev, Set<Rectangle> current) {
Set<Rectangle> unmerged = new HashSet<Rectangle>(prev);
if (!prev.isEmpty() && !current.isEmpty()) {
Rectangle[] pr = prev.toArray(new Rectangle[prev.size()]);
Rectangle[] cr = current.toArray(new Rectangle[current.size()]);
Rectangle[] pr = prev.toArray(new Rectangle[0]);
Rectangle[] cr = current.toArray(new Rectangle[0]);
int ipr = 0;
int icr = 0;
while (ipr < pr.length && icr < cr.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ public static String[] registryGetStringArray(HKEY hKey, String value) {
result.add(s);
}
}
return result.toArray(new String[result.size()]);
return result.toArray(new String[0]);
}

/**
Expand Down Expand Up @@ -2632,7 +2632,7 @@ public static ACE_HEADER[] getFileSecurity(String fileName,
result.add(aceStructure);
}
}
return result.toArray(new ACE_HEADER[result.size()]);
return result.toArray(new ACE_HEADER[0]);
}

return aceStructures;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ private Ddeml.HSZPAIR[] onWildconnect(int transactionType, HSZ topic, HSZ servic
for(WildconnectHandler handler: wildconnectHandler) {
hszpairs.addAll(handler.onWildconnect(transactionType, topic, service, convcontext, sameInstance));
}
return hszpairs.toArray(new HSZPAIR[hszpairs.size()]);
return hszpairs.toArray(new HSZPAIR[0]);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ public static final WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] getLogicalProce
int returnedStructCount = bufferSize.getValue().intValue()
/ sizePerStruct;
return (WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]) firstInformation
.toArray(new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[returnedStructCount]);
.toArray(new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[0]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ public static DomainTrust[] getDomainTrusts(String serverName) {
}
try {
DS_DOMAIN_TRUSTS domainTrustRefs = new DS_DOMAIN_TRUSTS(domainsPointerRef.getValue());
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[domainTrustCount.getValue()]);
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[0]);
ArrayList<DomainTrust> trusts = new ArrayList<DomainTrust>(domainTrustCount.getValue());
for(DS_DOMAIN_TRUSTS domainTrust : domainTrusts) {
DomainTrust t = new DomainTrust();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public void testDsEnumerateDomainTrusts() {
assertTrue(domainTrustCount.getValue() >= 0);

DS_DOMAIN_TRUSTS domainTrustRefs = new DS_DOMAIN_TRUSTS(domainsPointerRef.getValue());
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[domainTrustCount.getValue()]);
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[0]);

for(DS_DOMAIN_TRUSTS trust : domainTrusts) {
assertTrue(trust.DnsDomainName.length() > 0);
Expand Down
2 changes: 1 addition & 1 deletion src/com/sun/jna/CallbackReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private static Method getCallbackMethod(Class<?> cls) {
}
}

Method[] methods = pmethods.toArray(new Method[pmethods.size()]);
Method[] methods = pmethods.toArray(new Method[0]);
if (methods.length == 1) {
return checkMethod(methods[0]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/sun/jna/NativeLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ static double parseVersion(String ver) {
}
ldPaths.add(0, paths[i]);
}
paths = ldPaths.toArray(new String[ldPaths.size()]);
paths = ldPaths.toArray(new String[0]);
}

for (int i=0;i < paths.length;i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/com/sun/jna/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ public Pointer[] getPointerArray(long offset) {
addOffset += Native.POINTER_SIZE;
p = getPointer(offset + addOffset);
}
return array.toArray(new Pointer[array.size()]);
return array.toArray(new Pointer[0]);
}

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

//////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 07f3ce0

Please sign in to comment.