From e82cacfdb6ce69291c0369f22291a917248e06b0 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Wed, 27 May 2020 09:53:13 +0200 Subject: [PATCH] Jetty 9.4.x single context optimisation (#4909) * Optimisation for single context It is a frequent deployment mode to have only a single context. In that case, the ContextHandlerCollection can bypass a bit of looping/matching/selecting and just call the single context, which it works out itself anyway if the request applies to it. Signed-off-by: Greg Wilkins * Optimisation for single context updates from review Signed-off-by: Greg Wilkins --- .../handler/ContextHandlerCollection.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandlerCollection.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandlerCollection.java index adc32da6368..e2e57efc8c9 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandlerCollection.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandlerCollection.java @@ -176,11 +176,23 @@ public class ContextHandlerCollection extends HandlerCollection @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - Handlers handlers = _handlers.get(); - if (handlers == null) + Mapping mapping = (Mapping)_handlers.get(); + + // Handle no contexts + if (mapping == null) + return; + Handler[] handlers = mapping.getHandlers(); + if (handlers == null || handlers.length == 0) return; - Mapping mapping = (Mapping)handlers; + // handle only a single context. + if (handlers.length == 1) + { + handlers[0].handle(target, baseRequest, request, response); + return; + } + + // handle async dispatch to specific context HttpChannelState async = baseRequest.getHttpChannelState(); if (async.isAsync()) { @@ -197,6 +209,7 @@ public class ContextHandlerCollection extends HandlerCollection } } + // handle many contexts if (target.startsWith("/")) { Trie> pathBranches = mapping._pathBranches; @@ -229,11 +242,9 @@ public class ContextHandlerCollection extends HandlerCollection } else { - if (mapping.getHandlers() == null) - return; - for (int i = 0; i < mapping.getHandlers().length; i++) + for (Handler handler : handlers) { - mapping.getHandlers()[i].handle(target, baseRequest, request, response); + handler.handle(target, baseRequest, request, response); if (baseRequest.isHandled()) return; }