Speed up Visual Studio Builds
Join the DZone community and get the full member experience.Join For Free
recently i got involved in a big project where we had a single solution with approximately 100 projects.
why 100 projects in a solution?
the reason for a 100 projects solution is that like in many modular systems these days, we have the following three tiers:
- a few core / common projects every project will use.
- a large amount of modules, independent of each other. this tier directly depends on tier 1.
- a few end-projects which loads the different modules. this tier indirectly depends on tier 2.
so, yes, we could create several solutions with each tier compiling only when needed and using dll reference instead of project references, but the amount of changes in all the tiers was still large enough and i’ve already seen this kind of build process fail miserably. so this was no go.
build time took 15 minutes for the whole solution. since we enforced a gated check-in policy in the company, this was really a pain point for the developers.
note that the developers’ computers were strong enough, with 8gb ram, intel core i7 cpu and ssd disks.
so i’ve started investigating what can be done to improve the situation.
step 1: build projects in parallel
although the pc has 8 logical cores, the build system in visual studio 2010, when using c#, still uses only a single core! (note this is not the case in c++ build system).
so after browsing the web, i’ve found how you can manually trigger msbuild yourself as a visual studio external tool to compile your solution in parallel.
more details on how to set this up can be found in the following post by scott hanselman: http://www.hanselman.com/blog/hackparallelmsbuildsfromwithinthevisualstudioide.aspx
after setting this up i’ve got an approximate build time of 10.2 minutes. not bad for a few minutes of work! also, got the following beautiful image out of my cpus, where you can really see them at work:
step 2: beware of copy local = true
in our 100-projects solution lots of projects reference each other, obviously. in addition to these references we also reference several 3 rd party components, practically from each module.
all the above caused that whenever we would compile the solution, over 4.5 gb of files were written. the majority (95%) of the writes were dlls which were copied to the output folder of each project.
to check out how many writes are done in your compilation check out this post.
anyway, 4.5 gb takes a long time to write, even on an ssd drive.
so the next step was to eliminate those writes. to do this, we changed almost all of the “ copy local ” settings in all the referenced dlls from the default true to false . this will prevent the referenced dlls to be copied to the output folder of each project.
note: some file names were blacked to protect the client’s properties.
in addition we also changed the output folder of all the projects to a single folder, so all generated dlls are copied to the one and only place where we actually run them. doing so dropped the writes while compiling to under 200 mb, a huge time saver. specifically, build time dropped to 7.5 minutes!
step 3: use ram disk
a ram disk is a logical disk which resides entirely on the ram.
it is extremely fast (faster than any ssd), but it is erased on every power-down, so only use it for temporary files.
of course, you should have enough ram to spare for this disk (the memory is pre-allocated for the disk use only), but on an 8gb pc, it’s usually not an issue.
there are several programs you can use to set up a ram disk. i used dataram ramdisk which supplies a free version with the ability to create a ram disk up to 4 gb (1 or 2 gb should be sufficient for any build).
configuration of the ram disk is very easy:
after you download, install and format your new ram disk, you can move your single output folder to it. if you want to keep your output folder in the same build drive you can simply create a symbolic link between the current output folder and a folder on the ram disk. this way makes the using of the ram disk optional, only for users with sufficient memory.
to create a symbolic link between your build folder and your new ram disk folder use the following line:
mklink /d c:\dev\mycurrnetbuildfolder \source\bin r:\ bin
where r: is your ram disk folder.
note: you should change the path according to your build folder and your ram drive settings.
result: adding the ram disk reduced compilation time to less than 5 minutes! this is a 66% reduction of the original time!
in this post we’ve seen how you can decrease build time. of course, i can’t make any guarantees. every project has its own characteristics and problems, but the steps provided can probably reduce the build time if you’re fit the profile of standard line-of-business application.
that’s it for now,
this post was first published on my blog .
Opinions expressed by DZone contributors are their own.