Given the latest issues reported against MediaType / MimeTypeUtils and the internal LRU cache implementation (see #24767), we should benchmark MediaType.parseMediaType and MimeTypeUtils.parseMimeType to find performance opporuntinies.

Comment From: radhakrishnanch

I am facing a similar issue #25043. Can you please help me. Is there any way to disable MimeType caching?

Comment From: bclozel

I've found out that the LRU cache in MimeTypeUtils is doing its job well and that the next optimization opportunity was in MediaType itself.

In MediaType.parseMediaType, we're first getting the cached MimeType instance from the LRU cache. Then, we're using the new MimeType(type, subtype, parameters) constructor which skips the parsing but is still performing all MimeType checks on tokens and parameters.

Adding a new copy constructor skipping the duplicate MIME type checks yields interesting performance improvements (approx. +400% throughput, -40% allocation/call).

Benchmark                                      (customTypesCount)  (requestedTypeCount)   Mode  Cnt        Score        Error  Units
MediaTypeBenchmark.parseMediaTypes                             40                    10  thrpt    5  1028617.327 ±   7061.771  ops/s
MediaTypeBenchmark.parseMediaTypesOptimized                    40                    10  thrpt    5  4444368.816 ± 119817.630  ops/s