After looking at:

I think an easy optimisation exists UrlPathHelper.getLookupPathForRequest(HttpServletRequest).
I notice that it calls getPathWithinServletMapping which calls getPathWithinApplication, later it decides it is not happy with the result and so calls getPathWithinApplication again.
I suspect it should be easy enough to change getLookupPathForRequest to first call getPathWithinApplication then pass that into getPathWithinServletMapping, if that doesn't return a satisfactory result return the already computed value of: getPathWithinApplication