Skip to content

Commit

Permalink
improved performance by caching regular expressions (roughly 10х run …
Browse files Browse the repository at this point in the history
…time decrease on 7MB EDMX)
  • Loading branch information
AndrewK2 committed Oct 26, 2023
1 parent e98e36c commit a2db4e9
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions EDMXTrimmer/EDMXTrimmer/EdmxTrimmer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class EdmxTrimmer
private const string ATTRIBUTE_TARGET = "Target";
private const string ATTRIBUTE_AXType = "AXType";

private readonly IDictionary<string, Regex> entityTypeRegexps = new Dictionary<string, Regex>();

public EdmxTrimmer(
string edmxFile,
string outputFileName,
Expand Down Expand Up @@ -311,10 +313,22 @@ private bool EntityExists(XmlNode xmlNode, string entityType) {
if(null == typeValue) {
return false;
}
if(ENTITYNAMESPACE_ALIAS != null && Regex.IsMatch(typeValue, Regex.Escape(ENTITYNAMESPACE_ALIAS + entityType) + "\\)?$")) {
if(ENTITYNAMESPACE_ALIAS != null && IsEntityTypeMatches(entityType, ENTITYNAMESPACE_ALIAS, typeValue)) {
return true;
}
return Regex.IsMatch(typeValue, Regex.Escape(ENTITYNAMESPACE + entityType) + "\\)?$");
return IsEntityTypeMatches(entityType, ENTITYNAMESPACE, typeValue);
}

private bool IsEntityTypeMatches(string entityType, string @namespace, string source) {
var key = @namespace + entityType;

if(!entityTypeRegexps.TryGetValue(key, out var regex)) {
var pattern = Regex.Escape(@namespace + entityType) + "\\)?$";
regex = new Regex(pattern);
entityTypeRegexps.Add(key, regex);
}

return regex.IsMatch(source!);
}
}
}

0 comments on commit a2db4e9

Please sign in to comment.