Optimizing C# Refactoring Performance with Web Projects in VS 2005

One complaint I've heard a lot is the performance of the new C# refactoring feature with web pages in VS 2005.  VS 2005 added support for enabling refactoring everywhere (for example: even within <% %> blocks defined inline with a .aspx page), which is nice from a feature-completeness perspective.  The problem, though, is that by checking every file and possible context, it can end up slowing down performance considerably.  The more pages you have in a project, the slower and slower refactoring gets (to the point of being unusable).

Unfortunately, with the current UI there is no way to tell the refactoring engine to "include/exclude" pages or individual projects when you do a refactoring change (ideally there would be a checkbox for indicating whether you wanted <% %> blocks, <script runat="server"> events, etc. checked or not so that you could configure this and turn off the overhead if you don't need it -- which for a lot of people is most/all of the time).  The C# team is investigating ways to enable these scenarios, as well as increase refactoring performance for the first SP.

One optimization trick that you can make before that, though, is to set a registry entry that will exclude ASP.NET pages and inline .asmx and .asax code from being included as part of a C# refactoring operation.  C# class files defined under the \app_code directory in web site projects will be included as part of the refactoring operation when this registry entry is set, as well as all ASP.NET code-behind pages that are open in VS at the time that you apply the refactoring (note: the C# parser already has these files open so refactoring is very fast).  The re-factoring will also continue to work across all other class libraries and projects in the solution.

To apply this registry setting:

  1. Close all running instances of VS 2005
  2. Click Start->Run in Windows and run "regedit"
  3. Navigate to this registry location: HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0\CSharp\Options\Editor
  4. Right-click on the Editor node and choose "New DWORD value"
  5. Name the value "OpenAllVenusFilesOnRefactor" and leave the value as 0

Then, when you restart VS 2005 and perform a re-factoring - you should find performance very fast.  It isn't as complete a check as before, but for many cases it might end up being much more useful than before.

Hope this helps,

Scott

P.S. VS 2005 Web Application Projects don't compile the .aspx portion of files, and don't support refactoring code within them, so they also don't have an issue with refactoring performance.

UpdateRaj implemented the trick above and apparently saw his refactoring perf go from 1 minute 10 seconds to less than a second.  Cool! :-)

 

15 Comments

  • You rock Scott. The current refector (without this reg key) is kind of a running joke where i work...if we're XPing with another dev and the refactor kicks in, we all have a chuckle and comment &quot;what a great feature.&quot;

  • Thanks for this info!

  • seems to be working, thanks alot

  • Hi Scott,

    Refacoring is a cool feature in C# code editor, is there any such feature for VB.NET as well.

    If not , i am just curious to know why its only for C#.



    Thanks for the info

    Tarun

  • Hi Scott,



    Like many others, I had tried the refactoring, but because it was so slow have not been using it. The bigger problem is that even though it is only slow for ASP.NET projects...if you spend most of your time in ASP.NET, you tend to not use it when working on small class library or windows forms / services apps just because you are not yet in the habbit of using the feature.



    I ran a test for my largest V2.0 web site, just trying a rename parameter refactor and it used to take over 2 minutes (seriously) to complete....



    With your change, it now is *instant* ie much less than 1 second, and so fast I cannot measure it.



    I completely understand the limitations introduced by changing this registry setting, but at least the refactor feature will now be of use to me :-)



    Thanks,



    David Taylor

  • We just moved all our Web Sites (8 projects or so) into Web Applications. Now our entire solution (22 projects) builds in seconds, compared to over a minute before. It's just not just refactoring which has a huge perf gain here. Our team is really excited about moving over. VS is now snappy (like it should be).

  • Hi Scott.



    Is there a way to open all code-behind pages in the editor before refactoring? It would be nice with a macro or something that opens all code-behind files.



    Tomas

  • Hi Tomas,



    You can multiple select files in the solution and then select &quot;open&quot; on them. This is one way to open up all the files. You can then use the Windows-&gt;Close All Documents menu item to shut them all when you are done.



    My guess is that you could probably create a macro pretty easily to automate this if you want.



    Hope this helps,



    Scott

  • Hi,

    I have a project with VB and C# with VS2005 Professional. I tried the registry change. Unfortunately my class diagrams renaming is take more than 10 minutes. Is there really nothing that turns this refactoring off or increases its speed?

    Jelle

  • Hi Jelle,

    Can you send me email (scottgu@microsoft.com) with more details about your scenario? I can then help you with this.

    Thanks,

    Scott

  • Thanks Scott this is great along with the web application proejcts &#39;addin&#39;. You truely are in touch with the VS.NET users.
    However, why wasn&#39;t this done in VS.NET 2005 release?
    The slow down for web site generation and the &#39;find references&#39; functionality would surely have been a part of the testing carried out by MSFT before they shipped the product.
    I currently work in an environment which maintains different versions of a product in VS 2003 and 2005.
    I breath a sigh of relief when I am working with web applications in 2003 as I know my build will be less than 15 seconds. In comparison the code (which is very similar) in 2005 which runs as a website takes over a minute to build.
    Obviously the answer is to use your web application extension for 2005, however this doesn&#39;t answer the above question and incidentally doesn&#39;t fit in with the companies policy.

  • Hi Gary,

    Unfortunately refactoring support was added really late in the product cycle (it wasn't in the Betas), and so the performance issues associated with it were missed until it was too late to-do. It is being optimized for SP1 with web-site projects, though, and should improve significantly there.

    The VS 2005 Web Application Project option doesn't have any issues with refactoring performance. So I would definitely recommend that approach if you want refactoring in web projects.

    Note that the VS 2005 Web Application Project option is a fully supported Microsoft product -- so you get the same level of product support that you get with Visual Studio. It will also be included in the upcoming VS 2005 SP1, and will ship in all VS versions going forward. Does this meet your company's policies?

    Thanks,

    Scott

  • I did this now, and it looks like things are faster, but just curious.. why does the key disapear after starting vsnet?
    I did it once more, and then it persisted..

    Also, why do I get this text in the output window?
    "Rename '%1' to '%2'"

  • Is is more than nothing ;)
    Big thanks!

  • I think refactoring from Visual Studio 2005 is generally a bad joke.

    I would advise you a good Add-In for VS2003/2005 such as "Resharper" for doing refactoring etc.

    Much better, faster and with a billion of advanced programming features. --> It has errorhighlighting , without dooing a complete build ... o_O

    Greetings

Comments have been disabled for this content.