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

Avoid MimeType garbage creation #22340

Closed
3 of 4 tasks
bclozel opened this issue Feb 4, 2019 · 0 comments
Closed
3 of 4 tasks

Avoid MimeType garbage creation #22340

bclozel opened this issue Feb 4, 2019 · 0 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@bclozel
Copy link
Member

bclozel commented Feb 4, 2019

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).
@bclozel bclozel added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement labels Feb 4, 2019
@bclozel bclozel added this to the 5.2 M1 milestone Feb 4, 2019
@bclozel bclozel self-assigned this Feb 4, 2019
@bclozel bclozel closed this as completed in ba8849d Feb 5, 2019
bclozel added a commit that referenced this issue Feb 5, 2019
bclozel added a commit that referenced this issue Feb 11, 2019
bclozel added a commit that referenced this issue Jun 28, 2019
As of gh-22340, `MimeTypeUtils` has a built-in LRU cache implementation
for caching parsed MIME types and avoiding excessive garbage creation at
runtime.
This implementation, when hit with highly concurrent reads on the same
media type (the cache key), can create multiple keys for the same MIME
type string. This duplication leads to the cache filling up and evicting
entries. When the cache fetches a duplicate key, it is then not
associated with a value and the cache can return a `null` value, which
is forbidden by the API contract.

This commit adds another cache check within the write lock: this avoids
creating duplicate entries in the cache and `null` return values.

Fixes gh-23211
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant