Bug 65208 - Multi-threaded loading of servlets
Summary: Multi-threaded loading of servlets
Status: NEW
Alias: None
Product: Tomcat 9
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 9.0.40
Hardware: PC Linux
: P2 enhancement (vote)
Target Milestone: -----
Assignee: Tomcat Developers Mailing List
Depends on:
Reported: 2021-03-29 11:15 UTC by Andreas Bergander
Modified: 2021-03-29 11:15 UTC (History)
0 users

Implementation of a context which loads servlets in parallel (4.86 KB, text/plain)
2021-03-29 11:15 UTC, Andreas Bergander

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Bergander 2021-03-29 11:15:42 UTC
Created attachment 37788 [details]
Implementation of a context which loads servlets in parallel

I'm not sure if this is something that you would like to add to Tomcat, but for us it made a huge difference on the startup time to have the servlets load using multiple threads instead of single threaded.

We have a lot of dynamic jsp files in our database (sort of a CMS system). So to avoid adding time on the first request to those files we register them as servlets using  ServletContext.addJspFile(...) to pre-compile them. But since we have thousands of those it takes very long time to compile them using the single threaded approach currently implemented in Tomcat. Our server has many cores that is just idling during this time, so I wanted to put them in use.

What I did was I override the loadOnStartup method in StandardContext and added the possibility to use Tomcat's executor service to parallelize the loading of servlets. I don't think this breaks any rules, since the JSP spec says "The container may choose the order of loading of servlets with the same load-on-startup value".

So I thought I would share the code (see attached file) and some benchmarks with you so that you can decide if this is something for you or not. 

Development environment, compiling 200 jsp files:
Single threaded: 70 seconds
Using 8 CPU cores: 30 seconds

Production environment, compiling 12.000 jsp files:
Single threaded: 17 minutes
Using 96 CPU cores: 4 minutes