Re: [tomcat] 02/02: Avoid waste of resources due to reconstruction of objects

2020-05-14 Thread Mark Thomas
On 13/05/2020 23:57, Rémy Maucherat wrote:
> On Thu, May 14, 2020 at 12:20 AM Mark Thomas  > wrote:
> 
> On 13/05/2020 19:23, Rémy Maucherat wrote:
> > On Mon, May 4, 2020 at 4:43 PM  
> > >> wrote:
> >
> >     This is an automated email from the ASF dual-hosted git
> repository.
> >
> >     markt pushed a commit to branch master
> >     in repository https://gitbox.apache.org/repos/asf/tomcat.git
> >
> >     commit 1719b71374d57d59bdcd99537bf13348cdaf87c7
> >     Author: KangZhiDong    >>
> >     AuthorDate: Sat Apr 25 01:30:47 2020 +0800
> >
> >         Avoid waste of resources due to reconstruction of objects
> >     ---
> >      .../apache/catalina/core/ApplicationContext.java   |  2 +-
> >      .../apache/catalina/ha/tcp/ReplicationValve.java   |  2 +-
> >      .../catalina/session/PersistentManagerBase.java    |  2 +-
> >      .../catalina/valves/rewrite/RewriteCond.java       |  2 +-
> >      .../catalina/valves/rewrite/RewriteRule.java       |  4 ++--
> >      .../catalina/valves/rewrite/RewriteValve.java      |  2 +-
> >      java/org/apache/juli/ClassLoaderLogManager.java    |  2 +-
> >      test/org/apache/catalina/valves/Benchmarks.java    | 26
> >     +++---
> >      8 files changed, 21 insertions(+), 21 deletions(-)
> >
> >
> > https://bz.apache.org/bugzilla/show_bug.cgi?id=64432
> > Ok, so this looked like very fishy savings. It turns out each object
> > instance may want to have its own thread local. IMO that's the
> case for
> > nearly all the classes above, and either way it's probably not a good
> > idea to take any chances.
> > I think this should be reverted.
> 
> I went through each of these in turn and checked that a static
> ThreadLocal was safe to use. What do you think I missed?
> 
> 
> I used the testcases and verified the patch from the BZ:
> https://bz.apache.org/bugzilla/attachment.cgi?id=37241
> The static thread local would mean all rules are sharing the pattern of
> the first rule.
> 
> So overall: this optimization using static thread locals sounds
> dangerous as it could lead to hard to find bugs later on as soon as
> there are two or more instances of an object. So I'd prefer passing on
> it, even if it would be acceptable in some cases.

Fair enough. If I missed something in one place, it is possible I missed
it elsewhere. I'll revert.

Mark

-
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org



Re: [tomcat] 02/02: Avoid waste of resources due to reconstruction of objects

2020-05-13 Thread Rémy Maucherat
On Thu, May 14, 2020 at 12:20 AM Mark Thomas  wrote:

> On 13/05/2020 19:23, Rémy Maucherat wrote:
> > On Mon, May 4, 2020 at 4:43 PM  > > wrote:
> >
> > This is an automated email from the ASF dual-hosted git repository.
> >
> > markt pushed a commit to branch master
> > in repository https://gitbox.apache.org/repos/asf/tomcat.git
> >
> > commit 1719b71374d57d59bdcd99537bf13348cdaf87c7
> > Author: KangZhiDong mailto:world...@gmail.com>>
> > AuthorDate: Sat Apr 25 01:30:47 2020 +0800
> >
> > Avoid waste of resources due to reconstruction of objects
> > ---
> >  .../apache/catalina/core/ApplicationContext.java   |  2 +-
> >  .../apache/catalina/ha/tcp/ReplicationValve.java   |  2 +-
> >  .../catalina/session/PersistentManagerBase.java|  2 +-
> >  .../catalina/valves/rewrite/RewriteCond.java   |  2 +-
> >  .../catalina/valves/rewrite/RewriteRule.java   |  4 ++--
> >  .../catalina/valves/rewrite/RewriteValve.java  |  2 +-
> >  java/org/apache/juli/ClassLoaderLogManager.java|  2 +-
> >  test/org/apache/catalina/valves/Benchmarks.java| 26
> > +++---
> >  8 files changed, 21 insertions(+), 21 deletions(-)
> >
> >
> > https://bz.apache.org/bugzilla/show_bug.cgi?id=64432
> > Ok, so this looked like very fishy savings. It turns out each object
> > instance may want to have its own thread local. IMO that's the case for
> > nearly all the classes above, and either way it's probably not a good
> > idea to take any chances.
> > I think this should be reverted.
>
> I went through each of these in turn and checked that a static
> ThreadLocal was safe to use. What do you think I missed?
>

I used the testcases and verified the patch from the BZ:
https://bz.apache.org/bugzilla/attachment.cgi?id=37241
The static thread local would mean all rules are sharing the pattern of the
first rule.

So overall: this optimization using static thread locals sounds dangerous
as it could lead to hard to find bugs later on as soon as there are two or
more instances of an object. So I'd prefer passing on it, even if it would
be acceptable in some cases.

Rémy


>
> Mark
>
> -
> To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: dev-h...@tomcat.apache.org
>
>


Re: [tomcat] 02/02: Avoid waste of resources due to reconstruction of objects

2020-05-13 Thread Mark Thomas
On 13/05/2020 19:23, Rémy Maucherat wrote:
> On Mon, May 4, 2020 at 4:43 PM  > wrote:
> 
> This is an automated email from the ASF dual-hosted git repository.
> 
> markt pushed a commit to branch master
> in repository https://gitbox.apache.org/repos/asf/tomcat.git
> 
> commit 1719b71374d57d59bdcd99537bf13348cdaf87c7
> Author: KangZhiDong mailto:world...@gmail.com>>
> AuthorDate: Sat Apr 25 01:30:47 2020 +0800
> 
>     Avoid waste of resources due to reconstruction of objects
> ---
>  .../apache/catalina/core/ApplicationContext.java   |  2 +-
>  .../apache/catalina/ha/tcp/ReplicationValve.java   |  2 +-
>  .../catalina/session/PersistentManagerBase.java    |  2 +-
>  .../catalina/valves/rewrite/RewriteCond.java       |  2 +-
>  .../catalina/valves/rewrite/RewriteRule.java       |  4 ++--
>  .../catalina/valves/rewrite/RewriteValve.java      |  2 +-
>  java/org/apache/juli/ClassLoaderLogManager.java    |  2 +-
>  test/org/apache/catalina/valves/Benchmarks.java    | 26
> +++---
>  8 files changed, 21 insertions(+), 21 deletions(-)
> 
> 
> https://bz.apache.org/bugzilla/show_bug.cgi?id=64432
> Ok, so this looked like very fishy savings. It turns out each object
> instance may want to have its own thread local. IMO that's the case for
> nearly all the classes above, and either way it's probably not a good
> idea to take any chances.
> I think this should be reverted.

I went through each of these in turn and checked that a static
ThreadLocal was safe to use. What do you think I missed?

Mark

-
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org



Re: [tomcat] 02/02: Avoid waste of resources due to reconstruction of objects

2020-05-13 Thread Rémy Maucherat
On Mon, May 4, 2020 at 4:43 PM  wrote:

> This is an automated email from the ASF dual-hosted git repository.
>
> markt pushed a commit to branch master
> in repository https://gitbox.apache.org/repos/asf/tomcat.git
>
> commit 1719b71374d57d59bdcd99537bf13348cdaf87c7
> Author: KangZhiDong 
> AuthorDate: Sat Apr 25 01:30:47 2020 +0800
>
> Avoid waste of resources due to reconstruction of objects
> ---
>  .../apache/catalina/core/ApplicationContext.java   |  2 +-
>  .../apache/catalina/ha/tcp/ReplicationValve.java   |  2 +-
>  .../catalina/session/PersistentManagerBase.java|  2 +-
>  .../catalina/valves/rewrite/RewriteCond.java   |  2 +-
>  .../catalina/valves/rewrite/RewriteRule.java   |  4 ++--
>  .../catalina/valves/rewrite/RewriteValve.java  |  2 +-
>  java/org/apache/juli/ClassLoaderLogManager.java|  2 +-
>  test/org/apache/catalina/valves/Benchmarks.java| 26
> +++---
>  8 files changed, 21 insertions(+), 21 deletions(-)
>

https://bz.apache.org/bugzilla/show_bug.cgi?id=64432
Ok, so this looked like very fishy savings. It turns out each object
instance may want to have its own thread local. IMO that's the case for
nearly all the classes above, and either way it's probably not a good idea
to take any chances.
I think this should be reverted.

Rémy


>
> diff --git a/java/org/apache/catalina/core/ApplicationContext.java
> b/java/org/apache/catalina/core/ApplicationContext.java
> index 61981e5..004055a 100644
> --- a/java/org/apache/catalina/core/ApplicationContext.java
> +++ b/java/org/apache/catalina/core/ApplicationContext.java
> @@ -171,7 +171,7 @@ public class ApplicationContext implements
> ServletContext {
>  /**
>   * Thread local data used during request dispatch.
>   */
> -private final ThreadLocal dispatchData = new
> ThreadLocal<>();
> +private static final ThreadLocal dispatchData = new
> ThreadLocal<>();
>
>
>  /**
> diff --git a/java/org/apache/catalina/ha/tcp/ReplicationValve.java
> b/java/org/apache/catalina/ha/tcp/ReplicationValve.java
> index 144dbbb..8cd73b9 100644
> --- a/java/org/apache/catalina/ha/tcp/ReplicationValve.java
> +++ b/java/org/apache/catalina/ha/tcp/ReplicationValve.java
> @@ -85,7 +85,7 @@ public class ReplicationValve
>  /**
>   * crossContext session container
>   */
> -protected final ThreadLocal>
> crossContextSessions =
> +protected static final ThreadLocal>
> crossContextSessions =
>  new ThreadLocal<>() ;
>
>  /**
> diff --git a/java/org/apache/catalina/session/PersistentManagerBase.java
> b/java/org/apache/catalina/session/PersistentManagerBase.java
> index 9f52c44..1fd08be 100644
> --- a/java/org/apache/catalina/session/PersistentManagerBase.java
> +++ b/java/org/apache/catalina/session/PersistentManagerBase.java
> @@ -188,7 +188,7 @@ public abstract class PersistentManagerBase extends
> ManagerBase
>   * Session that is currently getting swapped in to prevent loading it
> more
>   * than once concurrently
>   */
> -private final ThreadLocal sessionToSwapIn = new
> ThreadLocal<>();
> +private static final ThreadLocal sessionToSwapIn = new
> ThreadLocal<>();
>
>
>  // -
> Properties
> diff --git a/java/org/apache/catalina/valves/rewrite/RewriteCond.java
> b/java/org/apache/catalina/valves/rewrite/RewriteCond.java
> index 47a904c..a980d81 100644
> --- a/java/org/apache/catalina/valves/rewrite/RewriteCond.java
> +++ b/java/org/apache/catalina/valves/rewrite/RewriteCond.java
> @@ -28,7 +28,7 @@ public class RewriteCond {
>
>  public static class PatternCondition extends Condition {
>  public Pattern pattern;
> -private ThreadLocal matcher = new ThreadLocal<>();
> +private static ThreadLocal matcher = new ThreadLocal<>();
>
>  @Override
>  public boolean evaluate(String value, Resolver resolver) {
> diff --git a/java/org/apache/catalina/valves/rewrite/RewriteRule.java
> b/java/org/apache/catalina/valves/rewrite/RewriteRule.java
> index 833a12c..0aa96de 100644
> --- a/java/org/apache/catalina/valves/rewrite/RewriteRule.java
> +++ b/java/org/apache/catalina/valves/rewrite/RewriteRule.java
> @@ -26,7 +26,7 @@ public class RewriteRule {
>
>  protected RewriteCond[] conditions = new RewriteCond[0];
>
> -protected ThreadLocal pattern = new ThreadLocal<>();
> +protected static ThreadLocal pattern = new ThreadLocal<>();
>  protected Substitution substitution = null;
>
>  protected String patternString = null;
> @@ -186,7 +186,7 @@ public class RewriteRule {
>  protected boolean cookieSecure = false;
>  protected boolean cookieHttpOnly = false;
>  protected Substitution cookieSubstitution = null;
> -protected ThreadLocal cookieResult = new ThreadLocal<>();
> +protected static ThreadLocal cookieResult = new
> ThreadLocal<>();
>
>  /**
>   *  This forces a request attribute named VAR to be set to the 

[tomcat] 02/02: Avoid waste of resources due to reconstruction of objects

2020-05-04 Thread markt
This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 2b628e8dc9acbab42ad8af068155f660a29c2d91
Author: KangZhiDong 
AuthorDate: Sat Apr 25 01:30:47 2020 +0800

Avoid waste of resources due to reconstruction of objects
---
 .../apache/catalina/core/ApplicationContext.java   |  2 +-
 .../apache/catalina/ha/tcp/ReplicationValve.java   |  2 +-
 .../catalina/session/PersistentManagerBase.java|  2 +-
 .../catalina/valves/rewrite/RewriteCond.java   |  2 +-
 .../catalina/valves/rewrite/RewriteRule.java   |  4 ++--
 .../catalina/valves/rewrite/RewriteValve.java  |  2 +-
 java/org/apache/juli/ClassLoaderLogManager.java|  2 +-
 test/org/apache/catalina/valves/Benchmarks.java| 26 +++---
 8 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/java/org/apache/catalina/core/ApplicationContext.java 
b/java/org/apache/catalina/core/ApplicationContext.java
index b6cd374..90f32b2 100644
--- a/java/org/apache/catalina/core/ApplicationContext.java
+++ b/java/org/apache/catalina/core/ApplicationContext.java
@@ -186,7 +186,7 @@ public class ApplicationContext implements ServletContext {
 /**
  * Thread local data used during request dispatch.
  */
-private final ThreadLocal dispatchData = new ThreadLocal<>();
+private static final ThreadLocal dispatchData = new 
ThreadLocal<>();
 
 
 /**
diff --git a/java/org/apache/catalina/ha/tcp/ReplicationValve.java 
b/java/org/apache/catalina/ha/tcp/ReplicationValve.java
index 6eed956..666e29d 100644
--- a/java/org/apache/catalina/ha/tcp/ReplicationValve.java
+++ b/java/org/apache/catalina/ha/tcp/ReplicationValve.java
@@ -85,7 +85,7 @@ public class ReplicationValve
 /**
  * crossContext session container
  */
-protected final ThreadLocal> crossContextSessions =
+protected static final ThreadLocal> 
crossContextSessions =
 new ThreadLocal<>() ;
 
 /**
diff --git a/java/org/apache/catalina/session/PersistentManagerBase.java 
b/java/org/apache/catalina/session/PersistentManagerBase.java
index 9f52c44..1fd08be 100644
--- a/java/org/apache/catalina/session/PersistentManagerBase.java
+++ b/java/org/apache/catalina/session/PersistentManagerBase.java
@@ -188,7 +188,7 @@ public abstract class PersistentManagerBase extends 
ManagerBase
  * Session that is currently getting swapped in to prevent loading it more
  * than once concurrently
  */
-private final ThreadLocal sessionToSwapIn = new ThreadLocal<>();
+private static final ThreadLocal sessionToSwapIn = new 
ThreadLocal<>();
 
 
 // - Properties
diff --git a/java/org/apache/catalina/valves/rewrite/RewriteCond.java 
b/java/org/apache/catalina/valves/rewrite/RewriteCond.java
index 47a904c..a980d81 100644
--- a/java/org/apache/catalina/valves/rewrite/RewriteCond.java
+++ b/java/org/apache/catalina/valves/rewrite/RewriteCond.java
@@ -28,7 +28,7 @@ public class RewriteCond {
 
 public static class PatternCondition extends Condition {
 public Pattern pattern;
-private ThreadLocal matcher = new ThreadLocal<>();
+private static ThreadLocal matcher = new ThreadLocal<>();
 
 @Override
 public boolean evaluate(String value, Resolver resolver) {
diff --git a/java/org/apache/catalina/valves/rewrite/RewriteRule.java 
b/java/org/apache/catalina/valves/rewrite/RewriteRule.java
index 833a12c..0aa96de 100644
--- a/java/org/apache/catalina/valves/rewrite/RewriteRule.java
+++ b/java/org/apache/catalina/valves/rewrite/RewriteRule.java
@@ -26,7 +26,7 @@ public class RewriteRule {
 
 protected RewriteCond[] conditions = new RewriteCond[0];
 
-protected ThreadLocal pattern = new ThreadLocal<>();
+protected static ThreadLocal pattern = new ThreadLocal<>();
 protected Substitution substitution = null;
 
 protected String patternString = null;
@@ -186,7 +186,7 @@ public class RewriteRule {
 protected boolean cookieSecure = false;
 protected boolean cookieHttpOnly = false;
 protected Substitution cookieSubstitution = null;
-protected ThreadLocal cookieResult = new ThreadLocal<>();
+protected static ThreadLocal cookieResult = new ThreadLocal<>();
 
 /**
  *  This forces a request attribute named VAR to be set to the value VAL,
diff --git a/java/org/apache/catalina/valves/rewrite/RewriteValve.java 
b/java/org/apache/catalina/valves/rewrite/RewriteValve.java
index 78cbe7c..c44f798 100644
--- a/java/org/apache/catalina/valves/rewrite/RewriteValve.java
+++ b/java/org/apache/catalina/valves/rewrite/RewriteValve.java
@@ -79,7 +79,7 @@ public class RewriteValve extends ValveBase {
 /**
  * If rewriting occurs, the whole request will be processed again.
  */
-protected ThreadLocal invoked = new ThreadLocal<>();
+protected static ThreadLocal invoked = new 

[tomcat] 02/02: Avoid waste of resources due to reconstruction of objects

2020-05-04 Thread markt
This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit c4b71e31f3183ce3f4b8e86bd2cef49393a2a7e5
Author: KangZhiDong 
AuthorDate: Sat Apr 25 01:30:47 2020 +0800

Avoid waste of resources due to reconstruction of objects
---
 .../apache/catalina/core/ApplicationContext.java   |  2 +-
 .../apache/catalina/ha/tcp/ReplicationValve.java   |  2 +-
 .../catalina/session/PersistentManagerBase.java|  2 +-
 .../catalina/valves/rewrite/RewriteCond.java   |  2 +-
 .../catalina/valves/rewrite/RewriteRule.java   |  4 ++--
 .../catalina/valves/rewrite/RewriteValve.java  |  2 +-
 java/org/apache/juli/ClassLoaderLogManager.java|  2 +-
 test/org/apache/catalina/valves/Benchmarks.java| 26 +++---
 8 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/java/org/apache/catalina/core/ApplicationContext.java 
b/java/org/apache/catalina/core/ApplicationContext.java
index 7e6eed6..1e29076 100644
--- a/java/org/apache/catalina/core/ApplicationContext.java
+++ b/java/org/apache/catalina/core/ApplicationContext.java
@@ -186,7 +186,7 @@ public class ApplicationContext implements ServletContext {
 /**
  * Thread local data used during request dispatch.
  */
-private final ThreadLocal dispatchData = new ThreadLocal<>();
+private static final ThreadLocal dispatchData = new 
ThreadLocal<>();
 
 
 /**
diff --git a/java/org/apache/catalina/ha/tcp/ReplicationValve.java 
b/java/org/apache/catalina/ha/tcp/ReplicationValve.java
index 6eed956..666e29d 100644
--- a/java/org/apache/catalina/ha/tcp/ReplicationValve.java
+++ b/java/org/apache/catalina/ha/tcp/ReplicationValve.java
@@ -85,7 +85,7 @@ public class ReplicationValve
 /**
  * crossContext session container
  */
-protected final ThreadLocal> crossContextSessions =
+protected static final ThreadLocal> 
crossContextSessions =
 new ThreadLocal<>() ;
 
 /**
diff --git a/java/org/apache/catalina/session/PersistentManagerBase.java 
b/java/org/apache/catalina/session/PersistentManagerBase.java
index 9f52c44..1fd08be 100644
--- a/java/org/apache/catalina/session/PersistentManagerBase.java
+++ b/java/org/apache/catalina/session/PersistentManagerBase.java
@@ -188,7 +188,7 @@ public abstract class PersistentManagerBase extends 
ManagerBase
  * Session that is currently getting swapped in to prevent loading it more
  * than once concurrently
  */
-private final ThreadLocal sessionToSwapIn = new ThreadLocal<>();
+private static final ThreadLocal sessionToSwapIn = new 
ThreadLocal<>();
 
 
 // - Properties
diff --git a/java/org/apache/catalina/valves/rewrite/RewriteCond.java 
b/java/org/apache/catalina/valves/rewrite/RewriteCond.java
index 47a904c..a980d81 100644
--- a/java/org/apache/catalina/valves/rewrite/RewriteCond.java
+++ b/java/org/apache/catalina/valves/rewrite/RewriteCond.java
@@ -28,7 +28,7 @@ public class RewriteCond {
 
 public static class PatternCondition extends Condition {
 public Pattern pattern;
-private ThreadLocal matcher = new ThreadLocal<>();
+private static ThreadLocal matcher = new ThreadLocal<>();
 
 @Override
 public boolean evaluate(String value, Resolver resolver) {
diff --git a/java/org/apache/catalina/valves/rewrite/RewriteRule.java 
b/java/org/apache/catalina/valves/rewrite/RewriteRule.java
index 833a12c..0aa96de 100644
--- a/java/org/apache/catalina/valves/rewrite/RewriteRule.java
+++ b/java/org/apache/catalina/valves/rewrite/RewriteRule.java
@@ -26,7 +26,7 @@ public class RewriteRule {
 
 protected RewriteCond[] conditions = new RewriteCond[0];
 
-protected ThreadLocal pattern = new ThreadLocal<>();
+protected static ThreadLocal pattern = new ThreadLocal<>();
 protected Substitution substitution = null;
 
 protected String patternString = null;
@@ -186,7 +186,7 @@ public class RewriteRule {
 protected boolean cookieSecure = false;
 protected boolean cookieHttpOnly = false;
 protected Substitution cookieSubstitution = null;
-protected ThreadLocal cookieResult = new ThreadLocal<>();
+protected static ThreadLocal cookieResult = new ThreadLocal<>();
 
 /**
  *  This forces a request attribute named VAR to be set to the value VAL,
diff --git a/java/org/apache/catalina/valves/rewrite/RewriteValve.java 
b/java/org/apache/catalina/valves/rewrite/RewriteValve.java
index 70d204b..273fe80 100644
--- a/java/org/apache/catalina/valves/rewrite/RewriteValve.java
+++ b/java/org/apache/catalina/valves/rewrite/RewriteValve.java
@@ -77,7 +77,7 @@ public class RewriteValve extends ValveBase {
 /**
  * If rewriting occurs, the whole request will be processed again.
  */
-protected ThreadLocal invoked = new ThreadLocal<>();
+protected static ThreadLocal invoked = new 

[tomcat] 02/02: Avoid waste of resources due to reconstruction of objects

2020-05-04 Thread markt
This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 1719b71374d57d59bdcd99537bf13348cdaf87c7
Author: KangZhiDong 
AuthorDate: Sat Apr 25 01:30:47 2020 +0800

Avoid waste of resources due to reconstruction of objects
---
 .../apache/catalina/core/ApplicationContext.java   |  2 +-
 .../apache/catalina/ha/tcp/ReplicationValve.java   |  2 +-
 .../catalina/session/PersistentManagerBase.java|  2 +-
 .../catalina/valves/rewrite/RewriteCond.java   |  2 +-
 .../catalina/valves/rewrite/RewriteRule.java   |  4 ++--
 .../catalina/valves/rewrite/RewriteValve.java  |  2 +-
 java/org/apache/juli/ClassLoaderLogManager.java|  2 +-
 test/org/apache/catalina/valves/Benchmarks.java| 26 +++---
 8 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/java/org/apache/catalina/core/ApplicationContext.java 
b/java/org/apache/catalina/core/ApplicationContext.java
index 61981e5..004055a 100644
--- a/java/org/apache/catalina/core/ApplicationContext.java
+++ b/java/org/apache/catalina/core/ApplicationContext.java
@@ -171,7 +171,7 @@ public class ApplicationContext implements ServletContext {
 /**
  * Thread local data used during request dispatch.
  */
-private final ThreadLocal dispatchData = new ThreadLocal<>();
+private static final ThreadLocal dispatchData = new 
ThreadLocal<>();
 
 
 /**
diff --git a/java/org/apache/catalina/ha/tcp/ReplicationValve.java 
b/java/org/apache/catalina/ha/tcp/ReplicationValve.java
index 144dbbb..8cd73b9 100644
--- a/java/org/apache/catalina/ha/tcp/ReplicationValve.java
+++ b/java/org/apache/catalina/ha/tcp/ReplicationValve.java
@@ -85,7 +85,7 @@ public class ReplicationValve
 /**
  * crossContext session container
  */
-protected final ThreadLocal> crossContextSessions =
+protected static final ThreadLocal> 
crossContextSessions =
 new ThreadLocal<>() ;
 
 /**
diff --git a/java/org/apache/catalina/session/PersistentManagerBase.java 
b/java/org/apache/catalina/session/PersistentManagerBase.java
index 9f52c44..1fd08be 100644
--- a/java/org/apache/catalina/session/PersistentManagerBase.java
+++ b/java/org/apache/catalina/session/PersistentManagerBase.java
@@ -188,7 +188,7 @@ public abstract class PersistentManagerBase extends 
ManagerBase
  * Session that is currently getting swapped in to prevent loading it more
  * than once concurrently
  */
-private final ThreadLocal sessionToSwapIn = new ThreadLocal<>();
+private static final ThreadLocal sessionToSwapIn = new 
ThreadLocal<>();
 
 
 // - Properties
diff --git a/java/org/apache/catalina/valves/rewrite/RewriteCond.java 
b/java/org/apache/catalina/valves/rewrite/RewriteCond.java
index 47a904c..a980d81 100644
--- a/java/org/apache/catalina/valves/rewrite/RewriteCond.java
+++ b/java/org/apache/catalina/valves/rewrite/RewriteCond.java
@@ -28,7 +28,7 @@ public class RewriteCond {
 
 public static class PatternCondition extends Condition {
 public Pattern pattern;
-private ThreadLocal matcher = new ThreadLocal<>();
+private static ThreadLocal matcher = new ThreadLocal<>();
 
 @Override
 public boolean evaluate(String value, Resolver resolver) {
diff --git a/java/org/apache/catalina/valves/rewrite/RewriteRule.java 
b/java/org/apache/catalina/valves/rewrite/RewriteRule.java
index 833a12c..0aa96de 100644
--- a/java/org/apache/catalina/valves/rewrite/RewriteRule.java
+++ b/java/org/apache/catalina/valves/rewrite/RewriteRule.java
@@ -26,7 +26,7 @@ public class RewriteRule {
 
 protected RewriteCond[] conditions = new RewriteCond[0];
 
-protected ThreadLocal pattern = new ThreadLocal<>();
+protected static ThreadLocal pattern = new ThreadLocal<>();
 protected Substitution substitution = null;
 
 protected String patternString = null;
@@ -186,7 +186,7 @@ public class RewriteRule {
 protected boolean cookieSecure = false;
 protected boolean cookieHttpOnly = false;
 protected Substitution cookieSubstitution = null;
-protected ThreadLocal cookieResult = new ThreadLocal<>();
+protected static ThreadLocal cookieResult = new ThreadLocal<>();
 
 /**
  *  This forces a request attribute named VAR to be set to the value VAL,
diff --git a/java/org/apache/catalina/valves/rewrite/RewriteValve.java 
b/java/org/apache/catalina/valves/rewrite/RewriteValve.java
index 20d8ba0..9025293 100644
--- a/java/org/apache/catalina/valves/rewrite/RewriteValve.java
+++ b/java/org/apache/catalina/valves/rewrite/RewriteValve.java
@@ -77,7 +77,7 @@ public class RewriteValve extends ValveBase {
 /**
  * If rewriting occurs, the whole request will be processed again.
  */
-protected ThreadLocal invoked = new ThreadLocal<>();
+protected static ThreadLocal invoked = new