Skip to content

Commit f60e371

Browse files
authored
Optimize performance
Creating HashSet is expensive when reading large file, since the validCommentChars do not change over the course of a single read API, initialize it once and use to to read entire file. Improves performance by about 15%
1 parent 6b2b99b commit f60e371

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Src/ConfigurationReader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ private static void Parse(StringReader reader, Configuration config)
2626
{
2727
var currentSection = new Section(Section.DefaultSectionName);
2828
var preCommentBuilder = new StringBuilder();
29+
HashSet<char> validCommentChars = new HashSet<char>(Configuration.ValidCommentChars); // Initialize once for ecah file to optimize performance
2930

3031
int lineNumber = 0;
3132

@@ -45,7 +46,7 @@ private static void Parse(StringReader reader, Configuration config)
4546
continue;
4647
}
4748

48-
var comment = ParseComment(line, out int commentIndex);
49+
var comment = ParseComment(line, validCommentChars, out int commentIndex);
4950

5051
if (commentIndex == 0)
5152
{
@@ -109,7 +110,7 @@ private static void Parse(StringReader reader, Configuration config)
109110
}
110111
}
111112

112-
private static string ParseComment(string line, out int commentCharIndex)
113+
private static string ParseComment(string line, HashSet<char> validCommentChars, out int commentCharIndex)
113114
{
114115
// A comment starts with a valid comment character that:
115116
// 1. is not within a quote (eg. "this is # not a comment"), and
@@ -124,7 +125,6 @@ private static string ParseComment(string line, out int commentCharIndex)
124125
int index = 0;
125126
int quoteCount = 0;
126127
int length = line.Length;
127-
HashSet<char> validCommentChars = new HashSet<char>(Configuration.ValidCommentChars); // Use HashSet<char> for O(1) lookup instead of Array.IndexOf for O(n)
128128
while (index < length) // traverse line from left to right
129129
{
130130
char currentChar = line[index];

0 commit comments

Comments
 (0)