From 07f3ce053d947adadf80a20e8acf8caffca8b6cd Mon Sep 17 00:00:00 2001 From: Harry Chan Date: Wed, 23 Jan 2019 17:30:40 +1100 Subject: [PATCH] Performance: replace toArray(new T[size]) with toArray(new T[0]) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- CHANGES.md | 1 + .../platform/src/com/sun/jna/platform/RasterRangesUtils.java | 4 ++-- .../platform/src/com/sun/jna/platform/win32/Advapi32Util.java | 4 ++-- .../platform/src/com/sun/jna/platform/win32/DdemlUtil.java | 2 +- .../platform/src/com/sun/jna/platform/win32/Kernel32Util.java | 2 +- .../platform/src/com/sun/jna/platform/win32/Netapi32Util.java | 2 +- .../test/com/sun/jna/platform/win32/Netapi32Test.java | 2 +- src/com/sun/jna/CallbackReference.java | 2 +- src/com/sun/jna/NativeLibrary.java | 2 +- src/com/sun/jna/Pointer.java | 4 ++-- 10 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 4cdf34c6d6..4ab7bcb770 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 --------- diff --git a/contrib/platform/src/com/sun/jna/platform/RasterRangesUtils.java b/contrib/platform/src/com/sun/jna/platform/RasterRangesUtils.java index a513dc4224..8862688f10 100644 --- a/contrib/platform/src/com/sun/jna/platform/RasterRangesUtils.java +++ b/contrib/platform/src/com/sun/jna/platform/RasterRangesUtils.java @@ -243,8 +243,8 @@ public static boolean outputOccupiedRanges(int[] pixels, int w, int h, int occup private static Set mergeRects(Set prev, Set current) { Set unmerged = new HashSet(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) { diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java b/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java index b4b41ff681..3bbe7c10d7 100755 --- a/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java @@ -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]); } /** @@ -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; diff --git a/contrib/platform/src/com/sun/jna/platform/win32/DdemlUtil.java b/contrib/platform/src/com/sun/jna/platform/win32/DdemlUtil.java index cd7570f7b2..ff6fa66b6d 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/DdemlUtil.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/DdemlUtil.java @@ -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]); } diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java b/contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java index ad4759364e..991e60be2c 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java @@ -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]); } /** diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java b/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java index d93e63ef43..076c01bc9f 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java @@ -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 trusts = new ArrayList(domainTrustCount.getValue()); for(DS_DOMAIN_TRUSTS domainTrust : domainTrusts) { DomainTrust t = new DomainTrust(); diff --git a/contrib/platform/test/com/sun/jna/platform/win32/Netapi32Test.java b/contrib/platform/test/com/sun/jna/platform/win32/Netapi32Test.java index df8930c5b1..cd445aa369 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/Netapi32Test.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/Netapi32Test.java @@ -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); diff --git a/src/com/sun/jna/CallbackReference.java b/src/com/sun/jna/CallbackReference.java index 8fcb34773d..0cecb9d9c4 100644 --- a/src/com/sun/jna/CallbackReference.java +++ b/src/com/sun/jna/CallbackReference.java @@ -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]); } diff --git a/src/com/sun/jna/NativeLibrary.java b/src/com/sun/jna/NativeLibrary.java index 4573361bcb..7e4ec3d28f 100644 --- a/src/com/sun/jna/NativeLibrary.java +++ b/src/com/sun/jna/NativeLibrary.java @@ -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++) { diff --git a/src/com/sun/jna/Pointer.java b/src/com/sun/jna/Pointer.java index 622df2e8c6..d382e5fae2 100644 --- a/src/com/sun/jna/Pointer.java +++ b/src/com/sun/jna/Pointer.java @@ -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. */ @@ -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]); } //////////////////////////////////////////////////////////////////////////