This project is read-only.

Multi-file speed suggestion


For whatever reason, generating a file from a template is a very slow process. I don't know if this is normal or not, but it seems like it is. Here's what I'm assuming about T4 Toolbox and how I suggest to improve the speed:
I'm assuming that the way T4 Toolbox is effectively re-adding the file to the project each time it is generated. I think Visual Studio then does whatever it does when a file is added. If this is normally not a slow process then perhaps the slowness is caused by Resharper re-parsing the file or by Visual Studio parsing the XAML or perhaps by one of my add ins.
At any rate, I noticed that the Entity Framework template library that is available in Visual Studio 2010 isn't slow in the case when the file already exists (i.e. after it builds the first time). I looked through this library, and discovered that the reason for this is because it actually compares the contents that it is about to write to a file with the contents that are already there. If they are the same then it leaves the original file alone.
Apparently the process of generating the file into memory, reading the original file into memory, and then comparing the two strings is faster than adding a file to a project in Visual Studio (at least for me). In fact, I'd say at least 10x faster.
Could you change T4 Toolbox to work like this, as well? Waiting for all of my files to generate is becoming very time consuming.


OlegSych wrote Oct 13, 2010 at 2:13 PM

T4 Toolbox already works that way.

** Closed by OlegSych 10/13/2010 5:42 AM

joshmouch wrote Oct 13, 2010 at 2:13 PM

Yeah, after writing this, I poured through the T4 Toolbox code quite a bit, and found the line that does that. However, it's obviously not working. I can watch the directory in question, and see the files get renamed to whatever.cs.bak and then renamed back to whatever.cs again. And I can watch the files get removed and re-added in visual studio. So, the string comparison must be broken for whatever reason. I tried to take a look myself, but I could find an extensible point I could use to compare the strings myself just before T4 Toolbox did. Do you have any suggestions one how I could (quickly) test this?

joshmouch wrote Oct 13, 2010 at 2:14 PM

Sorry, typo: I meant I couldn't find an extensibility point.

OlegSych wrote Oct 13, 2010 at 4:53 PM

Please try the latest alpha build.

joshmouch wrote Oct 13, 2010 at 7:16 PM

That is a little bit better, but still not quite there. Before it was rewriting three different "categories" of files every time (".cs", ".xaml", and ".xaml.cs"). Now it's correctly detecting that the ".cs" and the ".xaml" haven't changed, but it is still rewriting the ".xaml.cs" files every time. If I had to take a wild guess, I'd say it's because the ".xaml.cs" files are nested underneath ("dependent upon) the ".xaml" files.

joshmouch wrote Oct 14, 2010 at 6:39 PM

As confirmation, I change the folder that the ".xaml.cs" files were being saved to so that it was no longer the same folder that the ".xaml" files were being saved to, and now it properly recognizes when the files haven't changed.
It seems that, in general, T4 Toolbox doesn't work well with the "DependentUpon" feature in Visual Studio. I say this because it causes the behavior in my last post and also because it doesn't delete the files when it should, either (like when you change the path to output the generated files).
Also, keep in mind that I never specifically told Visual Studio to nest the .xaml.cs files under the .xaml files. It just does that automatically.

wrote Feb 22, 2013 at 1:05 AM

wrote May 20, 2013 at 11:20 AM