dubbo-hessian-lite icon indicating copy to clipboard operation
dubbo-hessian-lite copied to clipboard

put a length param into this createList method so that the collection will not resize

Open justdoit1995 opened this issue 2 years ago • 0 comments

this class: com.alibaba.com.caucho.hessian.io.CollectionDeserializer.class

CollectionDeserializer.readLengthList.createList()

you can put a length param into this createList method and initialize the collection with length so that the collection will not resize it will avoid the resize of the collection and save cpu resources

before:

private Collection createList()
            throws IOException {
        Collection list = null;

        if (_type == null)
            list = new ArrayList();
        else if (!_type.isInterface()) {
            try {
                list = (Collection) _type.newInstance();
            } catch (Exception e) {
            }
        }

        if (list != null) {
        } else if (SortedSet.class.isAssignableFrom(_type))
            list = new TreeSet();
        else if (Set.class.isAssignableFrom(_type))
            list = new HashSet();
        else if (List.class.isAssignableFrom(_type))
            list = new ArrayList();
        else if (Collection.class.isAssignableFrom(_type))
            list = new ArrayList();
        else {
            try {
                list = (Collection) _type.newInstance();
            } catch (Exception e) {
                throw new IOExceptionWrapper(e);
            }
        }

        return list;
    }


after:

private Collection createList(int length)
            throws IOException {
        Collection list = null;
        if (_type == null)
            list = new ArrayList();
        else if (!_type.isInterface()) {
            try {
                list = (Collection) _type.newInstance();
            } catch (Exception e) {
            }
        }

        if (list != null) {
        } else if (SortedSet.class.isAssignableFrom(_type))
            list = new TreeSet(length);
        else if (Set.class.isAssignableFrom(_type))
            list = new HashSet(length);
        else if (List.class.isAssignableFrom(_type))
            list = new ArrayList(length);
        else if (Collection.class.isAssignableFrom(_type))
            list = new ArrayList(length);
        else {
            try {
                list = (Collection) _type.newInstance();
            } catch (Exception e) {
                throw new IOExceptionWrapper(e);
            }
        }

        return list;
    }

justdoit1995 avatar Sep 07 '22 13:09 justdoit1995