You are correct that the slowness in general is coming from having to parse the content of the files to generate the CodeElements
. I solved this problem in one of our projects by limiting which files I processed based on some basic heuristics about what I was searching for.
You can do a simple String.Contains
to see if something you care about is even in the file before you parse it. Here is a simple example of this, you need to do the FileCodeModel
null check last as accessing that property requires some parsing to take place.
var fileName = projectItem.FileNames[1];
var shouldProcessFile = File.Exsits(fileName)
&& Path.GetExtension(fileName) == ".cs"
&& File.ReadAllText(fileName).Contains("FactuurStatistiek")
&& projectItem.FileCodeModel != null;
if(shouldProcessFile)
{
codeType = RecurseCodeElements(projectItem.FileCodeModel.CodeElements, fullName);
}
The above approach is the one I took and worked well for us. It still requires reading all the content of all the ".cs" files in the project and looking for something in their content but that is faster than parsing them all.
One other approach I thought of if you happen to have an insane amount of files in your project but know very few really need to be parsed is to actually mark the files that way with a comment on the first line of the file for instance if you made the first line of a file you want to consider for parsing be //PARSEME
then the above code can be modified to.
var fileName = projectItem.FileNames[1];
var shouldProcessFile = File.Exsits(fileName)
&& Path.GetExtension(fileName) == ".cs"
&& File.ReadLines(fileName).First().Contains("//PARSEME")
&& File.ReadAllText(fileName).Contains("FactuurStatistiek")
&& projectItem.FileCodeModel != null;
if(shouldProcessFile)
{
codeType = RecurseCodeElements(projectItem.FileCodeModel.CodeElements, fullName);
}
With the addition of the File.ReadLines(fileName).First().Contains("//PARSEME")
check it means that you are only really reading the first line from most files before you get to the more expensive contains check. The downside of this is of course that you need to remember to mark the parse-able files if you add new ones.