Skip to content

Avoid MimeType garbage creation #22340

Closed
Closed

Description

@bclozel

While profiling Spring web applications (MVC and WebFlux), it seems that MimeType (through MediaType) is creating a significant amount of garbage that could be avoided.

  • Caching the toString() result of MimeType instances, since it cannot change and this method is called many times on hot paths
  • MediaType.parseMediaType is called multiple times, for each request; this could be backed by a simple LRU cache implementation to avoid re-parsing known media types
  • MimeTypeUtils.parseMimeType parsing code can be improved; a draft implementation shows +40% throughput and -10% allocation.
  • MimeTypeUtils and MediaType both have static sections where well-known types are parsed; changing those from APPLICATION_JSON = MimeType.valueOf(APPLICATION_JSON_VALUE); to APPLICATION_JSON = new MimeType("application", "json"); leads to a bit of duplication but consistently reduces garbage and CPU usage at startup time (approx. 10ms).

Activity

  1. added
    in: web
    type: enhancement
    on Feb 4, 2019
  2. added this to the 5.2 M1 milestone on Feb 4, 2019
  3. self-assigned this
    on Feb 4, 2019
  4. added a commit that references this issue on Feb 5, 2019
  5. added a commit that references this issue on Feb 11, 2019
  6. added a commit that references this issue on Sep 17, 2026
    ba8849d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions