Optimize MediaType parsing
Prior to this commit, `MediaType.parseMediaType` would already rely on the internal LRU cache in `MimeTypeUtils` for better performance. With that optimization, the parsing of raw media types is skipped for cached elements. But still, `MediaType.parseMediaType` would first get a cached `MimeType` instance from that cache and then instantiate a `new MediaType(type, subtype, parameters)`. This constructor not only replays the `MimeType` checks on type/subtyme tokens and parameters, but it also performs `MediaType`-specific checks on parameters. Such checks are not required, as we're using an existing `MimeType` instance in the first place. This commit adds a new protected copy constructor (skipping checks) in `MimeType` and uses it in `MediaType.parseMediaType` as a result. This yields interesting performance improvements, with +400% throughput and -40% allocation/call in benchmarks. This commit also introduces a new JMH benchmark for future optimization work. Closes gh-24769
This commit is contained in:
@@ -479,6 +479,11 @@ public class MediaType extends MimeType implements Serializable {
|
||||
super(type, subtype, parameters);
|
||||
}
|
||||
|
||||
public MediaType(MimeType mimeType) {
|
||||
super(mimeType);
|
||||
this.getParameters().forEach(this::checkParameters);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void checkParameters(String attribute, String value) {
|
||||
@@ -587,7 +592,8 @@ public class MediaType extends MimeType implements Serializable {
|
||||
throw new InvalidMediaTypeException(ex);
|
||||
}
|
||||
try {
|
||||
return new MediaType(type.getType(), type.getSubtype(), type.getParameters());
|
||||
//return new MediaType(type.getType(), type.getSubtype(), type.getParameters());
|
||||
return new MediaType(type);
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
throw new InvalidMediaTypeException(mediaType, ex.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user