Skip to content

Avoid MimeType garbage creation #22340

Closed
Closed
@bclozel

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).

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions