[ 
https://issues.apache.org/jira/browse/LANG-1830?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Maksym Korshun updated LANG-1830:
---------------------------------
    Description: 
ClassUtils.toCleanName() currently validates the array suffix using 
String.matches(), 
which recompiles the regular expression on every call.

Replace the repeated call to String.matches("(?:
[])+") 
with a private static final Pattern and reuse it for validation.

This reduces unnecessary regex compilation in a frequently used utility method 
without changing behavior.

 

A simple JMH benchmark comparing the current implementation with a precompiled 
{{Pattern}} showed approximately 4x-9x higher throughput, depending on the 
input.

Benchmark                                      (tail)   Mode  Cnt         Score 
  Error  Units
ClassUtilsRegexBenchmark.precompiledPattern        []  thrpt    2  32800762.230 
         ops/s
ClassUtilsRegexBenchmark.precompiledPattern      [][]  thrpt    2  25525841.466 
         ops/s
ClassUtilsRegexBenchmark.precompiledPattern  [][][][]  thrpt    2  20276144.578 
         ops/s
ClassUtilsRegexBenchmark.precompiledPattern         [  thrpt    2  59534277.654 
         ops/s
ClassUtilsRegexBenchmark.precompiledPattern        ][  thrpt    2  63698539.468 
         ops/s
ClassUtilsRegexBenchmark.precompiledPattern     [][]x  thrpt    2  20136295.686 
         ops/s
ClassUtilsRegexBenchmark.stringMatches             []  thrpt    2   6529300.056 
         ops/s
ClassUtilsRegexBenchmark.stringMatches           [][]  thrpt    2   5999979.754 
         ops/s
ClassUtilsRegexBenchmark.stringMatches       [][][][]  thrpt    2   5506269.938 
         ops/s
ClassUtilsRegexBenchmark.stringMatches              [  thrpt    2   7142911.032 
         ops/s
ClassUtilsRegexBenchmark.stringMatches             ][  thrpt    2   7220328.509 
         ops/s
ClassUtilsRegexBenchmark.stringMatches          [][]x  thrpt    2   5474452.510 
         ops/s
||Benchmark||Score (ops/s)||
|ClassUtilsRegexBenchmark.precompiledPattern|32800762.230|
|ClassUtilsRegexBenchmark.precompiledPattern|25525841.466|
|ClassUtilsRegexBenchmark.precompiledPattern|59534277.654|
|ClassUtilsRegexBenchmark.stringMatches|6529300.056|
|ClassUtilsRegexBenchmark.stringMatches|5999979.754|
|ClassUtilsRegexBenchmark.stringMatches|7142911.032|

 

 

  was:
ClassUtils.toCleanName() currently validates the array suffix using 
String.matches(), 
which recompiles the regular expression on every call.

Replace the repeated call to String.matches("(?:\\[])+") 
with a private static final Pattern and reuse it for validation.

This reduces unnecessary regex compilation in a frequently used utility method 
without changing behavior.


> Avoid repeated regex compilation in ClassUtils.toCleanName()
> ------------------------------------------------------------
>
>                 Key: LANG-1830
>                 URL: https://issues.apache.org/jira/browse/LANG-1830
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.*
>            Reporter: Maksym Korshun
>            Priority: Minor
>
> ClassUtils.toCleanName() currently validates the array suffix using 
> String.matches(), 
> which recompiles the regular expression on every call.
> Replace the repeated call to String.matches("(?:
> [])+") 
> with a private static final Pattern and reuse it for validation.
> This reduces unnecessary regex compilation in a frequently used utility 
> method without changing behavior.
>  
> A simple JMH benchmark comparing the current implementation with a 
> precompiled {{Pattern}} showed approximately 4x-9x higher throughput, 
> depending on the input.
> Benchmark                                      (tail)   Mode  Cnt         
> Score   Error  Units
> ClassUtilsRegexBenchmark.precompiledPattern        []  thrpt    2  
> 32800762.230          ops/s
> ClassUtilsRegexBenchmark.precompiledPattern      [][]  thrpt    2  
> 25525841.466          ops/s
> ClassUtilsRegexBenchmark.precompiledPattern  [][][][]  thrpt    2  
> 20276144.578          ops/s
> ClassUtilsRegexBenchmark.precompiledPattern         [  thrpt    2  
> 59534277.654          ops/s
> ClassUtilsRegexBenchmark.precompiledPattern        ][  thrpt    2  
> 63698539.468          ops/s
> ClassUtilsRegexBenchmark.precompiledPattern     [][]x  thrpt    2  
> 20136295.686          ops/s
> ClassUtilsRegexBenchmark.stringMatches             []  thrpt    2   
> 6529300.056          ops/s
> ClassUtilsRegexBenchmark.stringMatches           [][]  thrpt    2   
> 5999979.754          ops/s
> ClassUtilsRegexBenchmark.stringMatches       [][][][]  thrpt    2   
> 5506269.938          ops/s
> ClassUtilsRegexBenchmark.stringMatches              [  thrpt    2   
> 7142911.032          ops/s
> ClassUtilsRegexBenchmark.stringMatches             ][  thrpt    2   
> 7220328.509          ops/s
> ClassUtilsRegexBenchmark.stringMatches          [][]x  thrpt    2   
> 5474452.510          ops/s
> ||Benchmark||Score (ops/s)||
> |ClassUtilsRegexBenchmark.precompiledPattern|32800762.230|
> |ClassUtilsRegexBenchmark.precompiledPattern|25525841.466|
> |ClassUtilsRegexBenchmark.precompiledPattern|59534277.654|
> |ClassUtilsRegexBenchmark.stringMatches|6529300.056|
> |ClassUtilsRegexBenchmark.stringMatches|5999979.754|
> |ClassUtilsRegexBenchmark.stringMatches|7142911.032|
>  
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to