Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
justdoit1995 opened this issue Sep 7, 2022 · 0 comments

Comments

@justdoit1995
Copy link

justdoit1995 commented Sep 7, 2022

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;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant