@@ -69,13 +69,16 @@ public static class IncrementStrategyFinder
69
69
70
70
public static VersionField ? GetIncrementForCommits ( GitVersionContext context , IEnumerable < Commit > commits )
71
71
{
72
- var majorRegex = CreateRegex ( context . Configuration . MajorVersionBumpMessage ?? DefaultMajorPattern ) ;
73
- var minorRegex = CreateRegex ( context . Configuration . MinorVersionBumpMessage ?? DefaultMinorPattern ) ;
74
- var patchRegex = CreateRegex ( context . Configuration . PatchVersionBumpMessage ?? DefaultPatchPattern ) ;
75
- var none = CreateRegex ( context . Configuration . NoBumpMessage ?? DefaultNoBumpPattern ) ;
72
+ // More efficient use of Regexes. The static version of Regex.IsMatch caches the compiled regexes.
73
+ // see: https://docs.microsoft.com/en-us/dotnet/standard/base-types/best-practices#static-regular-expressions
74
+
75
+ var majorRegex = context . Configuration . MajorVersionBumpMessage ?? DefaultMajorPattern ;
76
+ var minorRegex = context . Configuration . MinorVersionBumpMessage ?? DefaultMinorPattern ;
77
+ var patchRegex = context . Configuration . PatchVersionBumpMessage ?? DefaultPatchPattern ;
78
+ var none = context . Configuration . NoBumpMessage ?? DefaultNoBumpPattern ;
76
79
77
80
var increments = commits
78
- . Select ( c => FindIncrementFromMessage ( c . Message , majorRegex , minorRegex , patchRegex , none ) )
81
+ . Select ( c => GetIncrementFromMessage ( c . Message , majorRegex , minorRegex , patchRegex , none ) )
79
82
. Where ( v => v != null )
80
83
. Select ( v => v . Value )
81
84
. ToList ( ) ;
@@ -88,6 +91,8 @@ public static class IncrementStrategyFinder
88
91
return null ;
89
92
}
90
93
94
+
95
+
91
96
private static IEnumerable < Commit > GetIntermediateCommits ( IRepository repo , Commit baseCommit , Commit headCommit )
92
97
{
93
98
if ( baseCommit == null ) yield break ;
@@ -114,21 +119,43 @@ private static IEnumerable<Commit> GetIntermediateCommits(IRepository repo, Comm
114
119
}
115
120
}
116
121
117
- private static VersionField ? FindIncrementFromMessage ( string message , Regex major , Regex minor , Regex patch , Regex none )
122
+ private static VersionField ? GetIncrementFromMessage ( string message , string majorRegex , string minorRegex , string patchRegex , string none )
118
123
{
119
- if ( major . IsMatch ( message ) ) return VersionField . Major ;
120
- if ( minor . IsMatch ( message ) ) return VersionField . Minor ;
121
- if ( patch . IsMatch ( message ) ) return VersionField . Patch ;
122
- if ( none . IsMatch ( message ) ) return VersionField . None ;
124
+ var key = message . GetHashCode ( ) ;
125
+
126
+ if ( ! VersionFieldCache . TryGetValue ( key , out var version ) )
127
+ {
128
+ version = FindIncrementFromMessage ( message , majorRegex , minorRegex , patchRegex , none ) ;
129
+ VersionFieldCache [ key ] = version ;
130
+ }
131
+ return version ;
132
+ }
123
133
134
+
135
+ private static VersionField ? FindIncrementFromMessage ( string message , string majorRegex , string minorRegex , string patchRegex , string noneRegex )
136
+ {
137
+ if ( IsMatch ( message , majorRegex ) ) return VersionField . Major ;
138
+ if ( IsMatch ( message , minorRegex ) ) return VersionField . Minor ;
139
+ if ( IsMatch ( message , patchRegex ) ) return VersionField . Patch ;
140
+ if ( IsMatch ( message , noneRegex ) ) return VersionField . None ;
124
141
return null ;
125
142
}
126
143
127
- private static Regex CreateRegex ( string pattern )
144
+ private static bool IsMatch ( string message , string regex )
128
145
{
129
- return new Regex ( pattern , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
146
+ var key = message . GetHashCode ( ) ^ regex . GetHashCode ( ) ;
147
+
148
+ if ( ! MatchCache . TryGetValue ( key , out var match ) )
149
+ {
150
+ match = Regex . IsMatch ( message , regex , RegexOptions . IgnoreCase ) ;
151
+ MatchCache [ key ] = match ;
152
+ }
153
+ return match ;
130
154
}
131
155
156
+ private static IDictionary < int , bool > MatchCache = new Dictionary < int , bool > ( ) ;
157
+ private static IDictionary < int , VersionField ? > VersionFieldCache = new Dictionary < int , VersionField ? > ( ) ;
158
+
132
159
public static VersionField FindDefaultIncrementForBranch ( GitVersionContext context , string branch = null )
133
160
{
134
161
var config = context . FullConfiguration . GetConfigForBranch ( branch ?? context . CurrentBranch . NameWithoutRemote ( ) ) ;
0 commit comments