Optimize toArray (#484)

replace toArray(new T[size]) with toArray(new T[0]) for better performance

https://shipilev.net/blog/2016/arrays-wisdom-ancients/#_conclusion
This commit is contained in:
Harry Chan 2019-01-29 13:42:02 +11:00 committed by hengyunabc
parent efb835823e
commit 4dba865995
5 changed files with 5 additions and 5 deletions

View File

@ -129,7 +129,7 @@ public class ThreadCommand extends AnnotatedCommand {
private void processTopBusyThreads(CommandProcess process) {
Map<Long, Long> topNThreads = ThreadUtil.getTopNThreads(sampleInterval, topNBusy);
Long[] tids = topNThreads.keySet().toArray(new Long[topNThreads.keySet().size()]);
Long[] tids = topNThreads.keySet().toArray(new Long[0]);
ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(ArrayUtils.toPrimitive(tids), true, true);
if (threadInfos == null) {
process.write("thread do not exist! id: " + id + "\n");

View File

@ -236,7 +236,7 @@ public class FeatureCodec {
}
return segmentArrayList.toArray(new String[segmentArrayList.size()]);
return segmentArrayList.toArray(new String[0]);
}

View File

@ -538,7 +538,7 @@ public abstract class StringUtils {
public static String[] toStringArray(Collection<String> collection) {
return collection == null?null:(String[])collection.toArray(new String[collection.size()]);
return collection == null?null:(String[])collection.toArray(new String[0]);
}
public static String[] split(String toSplit, String delimiter) {

View File

@ -160,6 +160,6 @@ public class TypeRenderUtils {
types.add(annotation.annotationType());
}
}
return StringUtils.concat(",", types.toArray(new Class<?>[types.size()]));
return StringUtils.concat(",", types.toArray(new Class<?>[0]));
}
}

View File

@ -238,7 +238,7 @@ public class FieldUtils {
*/
public static Field[] getAllFields(final Class<?> cls) {
final List<Field> allFieldsList = getAllFieldsList(cls);
return allFieldsList.toArray(new Field[allFieldsList.size()]);
return allFieldsList.toArray(new Field[0]);
}
/**