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

Initialize pre-filled HashMaps with large enough capacity (e.g. in HttpMethod) [SPR-17105] #21642

Closed
spring-issuemaster opened this issue Jul 30, 2018 · 2 comments
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Jul 30, 2018

Roman Stefankiv opened SPR-17105 and commented

This is small enhancement but still, applicable.

Enum HttpMethod stores all its values internally in the hash map. It is instantiated with capacity 8 but default load factor:

GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE;

private static final Map<String, HttpMethod> mappings = new HashMap<>(8);

static {
    for (HttpMethod httpMethod : values()) {
        mappings.put(httpMethod.name(), httpMethod);
    }
}

Putting all 8 values in the map triggers map's resizing (when it's filled with 6 values), which could be avoided, changing load factor to 1.


Affects: 5.0.8

Reference URL: https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/http/HttpMethod.java#L38

Issue Links:

  • #21616 Spring-context optimization: LinkedMultiValueMap and ArrayList should be initialized with specified capacity for preventing collections from resizing
  • #21712 ClassUtils uses more memory than necessary

Referenced from: commits 91fa2ed, 4a147d2

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jul 30, 2018

Roman Stefankiv commented

I've created pull request which fixes that: #1908

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jul 30, 2018

Juergen Hoeller commented

Since we don't customize the load factor in other places either, I'll rather set the initial capacity to 16 here. This also potentially improves the lookup cost a bit, following general HashMap capacity recommendations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.