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

spring-projects-issues opened this issue Jul 30, 2018 · 2 comments


Copy link

@spring-projects-issues spring-projects-issues 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:


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

static {
    for (HttpMethod httpMethod : values()) {
        mappings.put(, 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:

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

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 30, 2018

Roman Stefankiv commented

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

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues 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
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants