Skip to content

Commit 943a054

Browse files
committed
Remove ineffective, premature optimisation from ErrorPageFilter
ErrorPageFilter contained an optimisation for looking up the path of an error page by exception type. For cases where there was no mapping for the type of the exception that was thrown but there was a mapping for one of its super classes, it was intended to speed up the lookup. Unfortunately, there was a bug in the implementation which meant that the optimisation had no effect. Analysis with JMH reveals that for an Exception with a deep type hierarchy, such as Spring Framework's UnsatisfiedDependencyException, and an error page mapping for Exception, searching up the hierarchy until a mapping is found takes 0.0000001s. With the same mapping, a lookup for Exception takes 0.00000001s, i.e. it's 10x faster. The optimisation, when correctly implemented, brings the time for UnsatisfiedDependencyException down to 0.00000001s and into line with a lookup for Exception. However, the amount of time involved is so small compared to the overall time spent processing a request that the added complexity of the optimisation is not justified. Closes gh-7010
1 parent 02e89ac commit 943a054

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

spring-boot/src/main/java/org/springframework/boot/web/support/ErrorPageFilter.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ public class ErrorPageFilter implements Filter, ErrorPageRegistry {
8383

8484
private final Map<Class<?>, String> exceptions = new HashMap<Class<?>, String>();
8585

86-
private final Map<Class<?>, Class<?>> subtypes = new HashMap<Class<?>, Class<?>>();
87-
8886
private final OncePerRequestFilter delegate = new OncePerRequestFilter() {
8987

9088
@Override
@@ -217,19 +215,12 @@ private String getErrorPath(Map<Integer, String> map, Integer status) {
217215
}
218216

219217
private String getErrorPath(Class<?> type) {
220-
if (this.exceptions.containsKey(type)) {
221-
return this.exceptions.get(type);
222-
}
223-
if (this.subtypes.containsKey(type)) {
224-
return this.exceptions.get(this.subtypes.get(type));
225-
}
226-
Class<?> subtype = type;
227-
while (subtype != Object.class) {
228-
subtype = subtype.getSuperclass();
229-
if (this.exceptions.containsKey(subtype)) {
230-
this.subtypes.put(subtype, type);
231-
return this.exceptions.get(subtype);
218+
while (type != Object.class) {
219+
String path = this.exceptions.get(type);
220+
if (path != null) {
221+
return path;
232222
}
223+
type = type.getSuperclass();
233224
}
234225
return this.global;
235226
}

0 commit comments

Comments
 (0)