From 943a054b611ad391dfa555617f72a083efd2ad4e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 19 Oct 2016 14:10:34 +0100 Subject: [PATCH] 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 --- .../boot/web/support/ErrorPageFilter.java | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/web/support/ErrorPageFilter.java b/spring-boot/src/main/java/org/springframework/boot/web/support/ErrorPageFilter.java index 877673b493d..484bc42d08f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/support/ErrorPageFilter.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/support/ErrorPageFilter.java @@ -83,8 +83,6 @@ public class ErrorPageFilter implements Filter, ErrorPageRegistry { private final Map, String> exceptions = new HashMap, String>(); - private final Map, Class> subtypes = new HashMap, Class>(); - private final OncePerRequestFilter delegate = new OncePerRequestFilter() { @Override @@ -217,19 +215,12 @@ public class ErrorPageFilter implements Filter, ErrorPageRegistry { } private String getErrorPath(Class type) { - if (this.exceptions.containsKey(type)) { - return this.exceptions.get(type); - } - if (this.subtypes.containsKey(type)) { - return this.exceptions.get(this.subtypes.get(type)); - } - Class subtype = type; - while (subtype != Object.class) { - subtype = subtype.getSuperclass(); - if (this.exceptions.containsKey(subtype)) { - this.subtypes.put(subtype, type); - return this.exceptions.get(subtype); + while (type != Object.class) { + String path = this.exceptions.get(type); + if (path != null) { + return path; } + type = type.getSuperclass(); } return this.global; }