Skip to content

Commit ea14c7f

Browse files
Make XContentType.xContent() a getter
Seen this come up in some profiling, wasting some cycles. If we do a method per type here instead of a getter + field, we pay for a megamorphic callsite potentially. It's faster and uses less code anyway to just use a field + getter here.
1 parent f7ce959 commit ea14c7f

File tree

1 file changed

+15
-50
lines changed

1 file changed

+15
-50
lines changed

libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentType.java

+15-50
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public enum XContentType implements MediaType {
2424
/**
2525
* A JSON based content type.
2626
*/
27-
JSON(0) {
27+
JSON(0, JsonXContent.jsonXContent) {
2828
@Override
2929
public String mediaTypeWithoutParameters() {
3030
return "application/json";
@@ -40,11 +40,6 @@ public String queryParameter() {
4040
return "json";
4141
}
4242

43-
@Override
44-
public XContent xContent() {
45-
return JsonXContent.jsonXContent;
46-
}
47-
4843
@Override
4944
public Set<HeaderValue> headerValues() {
5045
return Set.of(new HeaderValue("application/json"), new HeaderValue("application/x-ndjson"), new HeaderValue("application/*"));
@@ -53,7 +48,7 @@ public Set<HeaderValue> headerValues() {
5348
/**
5449
* The jackson based smile binary format. Fast and compact binary format.
5550
*/
56-
SMILE(1) {
51+
SMILE(1, SmileXContent.smileXContent) {
5752
@Override
5853
public String mediaTypeWithoutParameters() {
5954
return "application/smile";
@@ -64,11 +59,6 @@ public String queryParameter() {
6459
return "smile";
6560
}
6661

67-
@Override
68-
public XContent xContent() {
69-
return SmileXContent.smileXContent;
70-
}
71-
7262
@Override
7363
public Set<HeaderValue> headerValues() {
7464
return Set.of(new HeaderValue("application/smile"));
@@ -77,7 +67,7 @@ public Set<HeaderValue> headerValues() {
7767
/**
7868
* A YAML based content type.
7969
*/
80-
YAML(2) {
70+
YAML(2, YamlXContent.yamlXContent) {
8171
@Override
8272
public String mediaTypeWithoutParameters() {
8373
return "application/yaml";
@@ -88,11 +78,6 @@ public String queryParameter() {
8878
return "yaml";
8979
}
9080

91-
@Override
92-
public XContent xContent() {
93-
return YamlXContent.yamlXContent;
94-
}
95-
9681
@Override
9782
public Set<HeaderValue> headerValues() {
9883
return Set.of(new HeaderValue("application/yaml"));
@@ -101,7 +86,7 @@ public Set<HeaderValue> headerValues() {
10186
/**
10287
* A CBOR based content type.
10388
*/
104-
CBOR(3) {
89+
CBOR(3, CborXContent.cborXContent) {
10590
@Override
10691
public String mediaTypeWithoutParameters() {
10792
return "application/cbor";
@@ -112,11 +97,6 @@ public String queryParameter() {
11297
return "cbor";
11398
}
11499

115-
@Override
116-
public XContent xContent() {
117-
return CborXContent.cborXContent;
118-
}
119-
120100
@Override
121101
public Set<HeaderValue> headerValues() {
122102
return Set.of(new HeaderValue("application/cbor"));
@@ -125,7 +105,7 @@ public Set<HeaderValue> headerValues() {
125105
/**
126106
* A versioned JSON based content type.
127107
*/
128-
VND_JSON(4) {
108+
VND_JSON(4, JsonXContent.jsonXContent) {
129109
@Override
130110
public String mediaTypeWithoutParameters() {
131111
return VENDOR_APPLICATION_PREFIX + "json";
@@ -136,11 +116,6 @@ public String queryParameter() {
136116
return "vnd_json";
137117
}
138118

139-
@Override
140-
public XContent xContent() {
141-
return JsonXContent.jsonXContent;
142-
}
143-
144119
@Override
145120
public Set<HeaderValue> headerValues() {
146121
return Set.of(
@@ -157,7 +132,7 @@ public XContentType canonical() {
157132
/**
158133
* Versioned jackson based smile binary format. Fast and compact binary format.
159134
*/
160-
VND_SMILE(5) {
135+
VND_SMILE(5, SmileXContent.smileXContent) {
161136
@Override
162137
public String mediaTypeWithoutParameters() {
163138
return VENDOR_APPLICATION_PREFIX + "smile";
@@ -168,11 +143,6 @@ public String queryParameter() {
168143
return "vnd_smile";
169144
}
170145

171-
@Override
172-
public XContent xContent() {
173-
return SmileXContent.smileXContent;
174-
}
175-
176146
@Override
177147
public Set<HeaderValue> headerValues() {
178148
return Set.of(new HeaderValue(VENDOR_APPLICATION_PREFIX + "smile", Map.of(COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN)));
@@ -186,7 +156,7 @@ public XContentType canonical() {
186156
/**
187157
* A Versioned YAML based content type.
188158
*/
189-
VND_YAML(6) {
159+
VND_YAML(6, YamlXContent.yamlXContent) {
190160
@Override
191161
public String mediaTypeWithoutParameters() {
192162
return VENDOR_APPLICATION_PREFIX + "yaml";
@@ -197,11 +167,6 @@ public String queryParameter() {
197167
return "vnd_yaml";
198168
}
199169

200-
@Override
201-
public XContent xContent() {
202-
return YamlXContent.yamlXContent;
203-
}
204-
205170
@Override
206171
public Set<HeaderValue> headerValues() {
207172
return Set.of(new HeaderValue(VENDOR_APPLICATION_PREFIX + "yaml", Map.of(COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN)));
@@ -215,7 +180,7 @@ public XContentType canonical() {
215180
/**
216181
* A Versioned CBOR based content type.
217182
*/
218-
VND_CBOR(7) {
183+
VND_CBOR(7, CborXContent.cborXContent) {
219184
@Override
220185
public String mediaTypeWithoutParameters() {
221186
return VENDOR_APPLICATION_PREFIX + "cbor";
@@ -226,11 +191,6 @@ public String queryParameter() {
226191
return "vnd_cbor";
227192
}
228193

229-
@Override
230-
public XContent xContent() {
231-
return CborXContent.cborXContent;
232-
}
233-
234194
@Override
235195
public Set<HeaderValue> headerValues() {
236196
return Set.of(new HeaderValue(VENDOR_APPLICATION_PREFIX + "cbor", Map.of(COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN)));
@@ -275,8 +235,11 @@ public static XContentType fromMediaType(String mediaTypeHeaderValue) throws Ill
275235

276236
private final int index;
277237

278-
XContentType(int index) {
238+
private final XContent xContent;
239+
240+
XContentType(int index, XContent xContent) {
279241
this.index = index;
242+
this.xContent = xContent;
280243
}
281244

282245
public static Byte parseVersion(String mediaType) {
@@ -296,7 +259,9 @@ public String mediaType() {
296259
return mediaTypeWithoutParameters();
297260
}
298261

299-
public abstract XContent xContent();
262+
public final XContent xContent() {
263+
return xContent;
264+
}
300265

301266
public abstract String mediaTypeWithoutParameters();
302267

0 commit comments

Comments
 (0)