End of Year Round up of ReSharper 10 Extensions

With the end of the year just around the corner, ReSharper 10 released less than two months ago, and having just released our second maintenance release, it’s a perfect time to take a look at some of the extensions we’ve got available for ReSharper 10.

ReSharper provides a very rich extensibility platform. It is a very modular application, composing functionality and features via a component container. Extensions can extend existing functionality, such as adding items to the Alt+Enter menu, or new inspections and Quick Fixes, or add new functionality, such as support for new languages, or synchronising settings via DropBox.

We currently have about 60 extensions available in the Extension Manager for ReSharper 10. If you haven’t yet had a look to see what’s available, grab yourself a coffee and we’ll take a wander through some of what’s out there. If you see anything you like, head over to the Extension Manager under the ReSharper menu, and search for the extension you want.

ZenSharp

The ZenSharp extension is like ReSharper’s Live Templates on steroids. While Live Templates will expand a static shortcut (like tm) into a code snippet (such as a test method), ZenSharp uses “dynamic” shortcuts to customise the generated snippet.

For example, pc will expand to public class $Name { }, where $Name is a standard Live Template “hotspot” to edit the class name. But pcPerson will expand to public class Person { }.

Similarly, pmSayHello  will expand to public void SayHello() { }. And pmsSayHello will expand to public string SayHello() { }.

The shortcuts are defined to be intuitive mnemonics that can be combined and built up to make it easy and fast to create classes, methods, properties, fields and so on. Standard types can be specified as part of the mnemonic shortcut, and once you’ve mastered it, there’s no faster way to create types and type members.

ZenSharp creating code really quickly

Enhanced Tooltips

The Enhanced Tooltip extension gives ReSharper’s tooltips a facelift. While this doesn’t exactly sound like it’s a big deal, it has a surprising impact – tooltips and parameter info popups in C# are rendered with syntax highlighting, which makes quickly reading parameter lists and method names a whole lot easier.

Furthermore, the tooltips that appear for ReSharper’s inspection warnings and suggestions are also syntax highlighted, with any member names in the message being rendered correctly, and an icon showing what kind of message is being displayed. ReSharper never looked so good!

Enhanced tooltips

ReSpeller

Office has it, so why shouldn’t ReSharper? Spelling, that is, and the ReSpeller extension provides just that. It’s available in Free and Pro versions and will look for typos in different languages, such as C#, VB and XML, and even differentiates between identifiers, string literals and comments. For example, you can configure typos in identifiers to be shown as a “hint”, and change typos in comments (such as XML documentation) to be “warning”. And it understands “CamelCasing”, treating “SayHelo” as two words, and finding the typo in “hello”.

It also supports the Alt+Enter menu, offering a Quick Fix to replace the typo, or options to add the word to the user dictionary.

The Pro edition adds extra features, like enabling support for extra languages (such as JavaScript, HTML and Razor), multiple language support and even spell checking directly in the Rename refactoring dialog box!

ReSpeller showing typos

StyleCop

While the StyleCop project itself has stalled, there are still many people using it to maintain a very consistent style when writing code within a team. The StyleCop extension provides as-you-type inspections to highlight when your code doesn’t match the expected style. It also configures ReSharper so that the code ReSharper produces or formats is in the correct style.

We find ourselves in the unfortunate position of having two different StyleCop extensions available in the Extension Manager (it’s a long story, sadly, and we’re working to resolve it). We strongly recommend you use the StyleCop by JetBrains version (search for “StyleCop by JetBrains” in the Extension Manager), as it fixes many issues, including exceptions and memory leaks, and implements improved performance throttling. It also doesn’t need the StyleCop Visual Studio plugin installed.

One thing to note is that StyleCop itself does not support C# 6. The StyleCop engine that the extension uses has a custom C# parser, rather than using ReSharper’s already parsed abstract syntax trees (one cause for performance issues), and due to the project stalling, the parser hasn’t been updated for C# 6. If you’re using Visual Studio 2015 and C# 6, we recommend you use the StyleCopAnalyzers project, which has full support for C# 6, including new rules. When the StyleCop by JetBrains extension sees that StyleCopAnalyzers is installed, it disables its own as-you-type analysis, but still provides code cleanup features and configures ReSharper to match the StyleCop settings.

StyleCop showing warnings

Extra inspections

Some extensions are designed to add new inspections, and we’ll take a look at two – Cyclomatic Complexity and Heap Allocations Viewer.

The Cyclomatic Complexity extension calculates the cyclomatic complexity of a block of code. This is a measurement of how complex your code is, and you should ideally be aiming for a very low figure – the higher the value, the more complex the code, and the harder it is to maintain and understand. The value is always shown in a tooltip, and if it’s over a certain configurable threshold, the method name (or similar) is highlighted as a warning. The extension works with C#, VB, JavaScript, TypeScript and C++.

ReSharper showing high cyclomatic complextity

The Heap Allocation Viewer extension highlights all allocations made by your code. This is an interesting extension, because it doesn’t indicate that anything is actually wrong, but it’s intended to make you aware that an allocation happens here. This is very useful when writing performance critical code, when you really need to keep track of all allocations. While it’s obvious that the new statement will allocate memory, this extension will also highlight less obvious allocations, such as boxing, string allocation, allocations when iterating with a foreach loop, and so on.

Heap allocations viewer showing boxing

Working with open source libraries

While some extensions are designed to provide new functionality across the product (such as improving the editing experience, or more inspections for your code), others are designed for a specific library or framework.

A good example of this is support for AngularJS, parsing your JavaScript code to pull out Angular directives, and add them to your HTML code completion. It will also provide improved code completion for the services and providers injected into controllers and directives.

AngularJS intellisense for injected parameters

Similarly, Unity support provides features for users of Unity 3D. It will mark methods and fields used by Unity as being in use when Solution Wide Analysis is enabled, generate message handlers via the Generate Code command and automatically mark a C# project as C# 5, so ReSharper won’t suggest incompatible C# 6 features when the project is opened in Visual Studio 2015.

Working with commercial libraries

A very nice use of ReSharper extensions is supporting commercial frameworks and products. The team at Trackerbird have done just that, and released an extension that will make it very easy for you to integrate their analysis SDK into your own project. Once installed, this extension adds an item to the ReSharper menu, offering options to set up the SDK in your project, add simple or detailed event tracking code to your project, help you implement checking for updates and so on. It also includes a context action on Application.Run that will add the code necessary to initialise and start activity tracking in your app.

Implement Trackerbird as a context action menu item

Please check out the Trackerbird blog for more details.

Annotations

ReSharper extensions are not just about custom code adding new items to the Alt+Enter menu, but can also include “external annotations”. These are files that contain annotations for pre-compiled assemblies that in turn provide hints to ReSharper’s inspections, resulting in better analysis for features such as null analysis, string formatting highlighting and telling ReSharper that type members are used externally from the code. Here are some annotations packages:

  • Community External Annotations – this is a nice open source project that maintains annotations for a number of third party libraries, as well as adding extra support for various BCL libraries. You can see the list of supported assemblies in the project readme. Contributions welcome!
  • NLog Annotations – adds null analysis and string formatting highlighting annotations for the NLog framework.
  • Caliburn.Light Annotations – adds annotations for the Caliburn.Light MVVM framework

Live Templates

Alongside external annotations, ReSharper extensions can also include settings files, which are automatically merged into ReSharper’s settings (technically, they override ReSharper’s defaults, and can get overwritten by your own settings). One of the more interesting uses for this is to package up a settings file that contains Live Templates.

There are several such packages available in the Extension Manager, such as:

  • SpecsFor templates for the SpecsFor BDD framework
  • Joar Øyen’s Live Template Macros – adding templates for BDD and a macro that converts a Live Template “hotspot” into a “snake_case_identifier”, such as given_a_new_customer .

Presentation Assistant

This extension is very useful when you find yourself giving a presentation. It will display the name of the last command run, and the keyboard shortcuts used to run it, allowing everyone to follow along with the steps you make as you make them. It shows both ReSharper and Visual Studio commands, and can be enabled from the ReSharper → Tools → Presentation Assistant.

Presentation Assistant showing keyboard shortcuts

It’s also useful for pair programming, to stop the “how did you do that?!” questions as you show off your mad keyboard skillz.

Working with settings files

Two useful extensions for working with settings files are ReSharper Solution Settings Autodiscovery and JetBox.

ReSharper already allows you to share settings files per-solution, by creating a .sln.DotSettings file that lives next to your solution file. But this needs setting up for each solution – what if your team has a number of solutions that you want to work with?

The ReSharper Solution Settings Autodiscovery extension will look for a settings file in each of the parent folders when opening a solution. So if your solution is in C:\projects\team\solution\mySolution.sln, this plugin will look for any file that ends in *.AutoLoad.DotSettings in any of the parent folders – C:\projects\team\solution, C:\projects\team, C:\projects and so on, up to the root of the drive. This is ideal when working on a source control check out that has multiple solutions, and allows you to easily share settings between all of them, and still have the file checked into source control.

The JetBox extension will sync your global settings via DropBox. Simply log in to DropBox via the options page, and the global settings file will be synced whenever there are any changes. And because ReSharper automatically refreshes the settings in any running instances whenever the file changes, you don’t even need to close and reopen Visual Studio for the changes to take effect.

And Finally…

It doesn’t have to be all work and no play. We’ve got a couple of fun extensions too. Check out ReMoji, to add support for JBEvain’s EmojiVS extension to ReSharper’s code completion (make sure you read the instructions for installing first!)

Code completion showing emoji

And of course, who can forget Clippy?

inplace_refactoring.gif

We hope you’ve found this look at some of the extensions available for ReSharper 10 useful. It’s by no means all of the extensions that are out there, so please, have a scroll through the Extension Manager, and see if there’s anything else there that interests you.

Posted in How-To's, ReSharper Tips&Tricks | Tagged , , , | 14 Comments

Enters ReSharper Ultimate 10.0.2

Download ReSharper Ultimate 10.0.2, which includes a slew of bug fixes and improvements to ReSharper, ReSharper C++, dotTrace, dotMemory, dotCover and dotPeek.

ReSharper Ultimate 10.0.2

Highlights of this update

  • Unit testing. In addition to support for NUnit 3.0 RTM, unit testing has seen noticeable improvements in terms of support for test cases and grouping, handling of debugging sessions, time spent for setup and teardown code runs. Many presentation glitches in Unit Test Sessions have been fixed as well.
  • Bogus red code. We have fixed incorrectly highlighted red code in solutions containing .modelproj and .wixproj project types, as well as in DNX projects, projects targeting .NET Framework 4.6 and portable libraries.
  • ReSharper Build. There’s an array of improvements in the way ReSharper Build works, notably with regard to monitoring changed dependencies, respecting settings that govern whether to display results upon completing a rebuild, and better support for specifics of Visual Studio 2015.
  • JavaScript and TypeScript. Improvements include a performance tuneup, as well as fixes to incorrect code inspections, usage search and navigation.
  • In other news, Stack Trace Explorer has received a set of fixes; you can now disable ReSharper code tooltip if you prefer how other Visual Studio extensions decorate the tooltip; introducing a variable from inside a lambda expression doesn’t produce broken code anymore; and you can export items from File Structure again!

ReSharper C++, dotCover, dotTrace, dotMemory and dotPeek have received their varying shares of bug fixing in the past month but it’s ReSharper that leads the breed in terms of sheer number of improvements. For your reference, this is how distribution by subsystem looks like for issues fixed in ReSharper 10.0.2:

ReSharper 10.0.2: fixed issues by subsystem

If you’re interested, here’s the entire list of fixes across ReSharper Ultimate products.

Important note to Visual Studio 2015 users

If you have migrated to Visual Studio 2015, please make sure to install VS2015 Update 1. This is especially important if you are experiencing Visual Studio freezes upon starting debugging, pulling from your VCS repository, starting a refactoring, editing XAML in visual mode or in other scenarios described in comments to RSRP-450181. Visual Studio 2015 Update 1 is known to fix a part of MSBuild calls that these freezes are usually traced back to.

Time to download

Upon reading the above, we hope you’re now well prepared to download and install ReSharper Ultimate 10.0.2.

This release wraps up the year for the .NET team here at JetBrains. We’d like to welcome 2016, and we’re hoping to open EAP for a new ReSharper Ultimate release sometime in January.

Posted in How-To's | Tagged , , , , , , , , | 28 Comments

Navigating to Source Code from dotMemory

We always look for ways to increase the value of ReSharper Ultimate and synergize our .NET tools. For example, in ReSharper Ultimate 9.2, we made it possible to profile ReSharper’s run configurations, so if you have both dotTrace and ReSharper you can even profile individual static methods in your project.

Now the time has come for dotMemory to get its portion of ReSharper’s functionality. dotMemory always lacked the ability to examine the source code of the profiled application. Just imagine how easier life would get if you could seamlessly continue the investigation of a suspicious object (one that may cause a leak) by examining its source code. Well, starting with dotMemory 10 and ReSharper Ultimate 10, you can!

To navigate to the source code from dotMemory

  1. In Visual Studio, open the solution that you have a memory snapshot for.
  2. In dotMemory, in any view that displays object types, right-click the type you’re interested in.
    Find declaration context menu
  3. In the context menu, select Find declaration (Visual Studio)*. This will open the Find Type Declaration window that lists all found type declarations in running Visual Studio instances.
    * As an alternative to steps 2 and 3, select a type and press Ctrl+L.
    Find type declaration window
  4. Click on the found declaration to navigate to it in Visual Studio.
    Type declaration in VS

After you navigate to any type declaration at least once, the context menu will offer an additional item, Go to declaration (<solution_name>) (also available via the Ctrl+L shortcut). Selecting it instantly navigates you to the type declaration in a specific solution, bypassing the Find Type Declaration window.

Go to declaration context menu

To see for yourself how the feature works, download the latest dotMemory or ReSharper Ultimate. If you have any comments or questions, please feel free to ask in the comments to this post. Your feedback is greatly appreciated!

Posted in dotMemory Tips&Tricks, How-To's | Tagged , , | 1 Comment

Continuous Testing webinar: recording

The recording of our recent webinar, Continuous Testing in Visual Studio using dotCover or ReSharper Ultimate, is now available:

In this webinar, we demonstrate Continuous Testing, a new feature of dotCover 10 and ReSharper Ultimate that will build and run your tests as they change. By monitoring what code is called from each test, Continuous Testing can optimise the test run by only running the tests that will exercise the changed code. ReSharper Build can also be used to further optimise the feedback cycle by speeding up the build step.

The following topics are covered:

  • A recap, with some hints and tips of ReSharper testing and dotCover code coverage
  • Enabling Continuous Testing
  • The Continuous Testing UI
  • Detecting “dirty” tests
  • Including/excluding tests from scope
  • Using ReSharper Build to speed up build time

You might also want to check out two related blog posts: Introducing Continuous Testing and Introducing ReSharper Build.

Posted in dotCover Tips&Tricks, ReSharper Tips&Tricks | Tagged , , , , , , | 3 Comments

ReSharper Ultimate 10.0.2 EAP 2: unit testing, red code fixes

ReSharper 10.0.2 EAP continues with a new build that is expected to fix a number of considerable problems in terms of:

  • Bogus red code: we have fixed incorrectly highlighted red code in solutions containing .modelproj and .wixproj project types. Since modelling projects (.modelproj) are only available in Visual Studio Enterprise and Ultimate editions, users of these editions might want to check out this fix.
  • Unit testing: NUnit tests with Ninject should now be running correctly again; NUnit’s test cases are now treated with fewer glitches; a set of problems related to grouping by category and namespace are now gone; unit test runner avoids running extra setup and teardown code for test fixtures; running the default NUnit 3.0 MSI installer no longer leads to inconclusive test runs; support for xUnit theory row test elements is restored following a regression in 10.0.1; line wrap in Unit Test SessionsOutput pane can now be controlled.
  • Inserting project references: projects should now be properly referenced again when you insert a using directive with a ReSharper action.

There are also other fixes here and there, including those related to TypeScript and JavaScript support, as well as renaming namespaces in XAML. Here’s the full list of fixes in ReSharper 10.0.2 EAP 2.

Compatible ReSharper Ultimate products are also available. Specifically, dotCover can now be properly integrated into Visual Studio again, even when installed without ReSharper, which fixes a regression introduced in EAP 1.

Posted in How-To's | Tagged , , , , | 21 Comments

ReSharper Ultimate 10.0.2 EAP

As we continue working on critical issues discovered in ReSharper 10, today we open the Early Access Program for ReSharper Ultimate 10.0.2.

The initial EAP build is mostly about Unit Testing, it comes with fixes to such issues as inabilty to run a single test or a group of selected tests with NUnit 3.0 (RSRP-450313), broken key bindings (RSRP-429324) or hanging Visual Studio (RSRP-449802).

Here is the full list of fixes for ReSharper for your reference. ReSharper C++ 10.0.2 EAP introduces a set of performance improvements and bug fixes as well.

Please download and try the ReSharper Ultimate 10.0.2 EAP build and let us know if it improves your experience with ReSharper compared to previous versions (10.0 and 10.0.1). ReSharper 10.0.2 release is currently planned for mid-December, so please expect more EAP builds in upcoming weeks.

Posted in How-To's | Tagged , , , | 12 Comments

Continuous Testing in dotCover and ReSharper Ultimate

Over the last couple of years, continuous testing has been the top anticipated dotCover feature. Now, the wait is finally over as dotCover 10 supports continuous testing in Visual Studio! Before we move on to the nitty-gritty details to show you how it works, let’s try to recall what continuous testing actually is.

Continuous testing

In a nutshell, continuous testing means testing ‘without interruption.’ It implies that tests are run in the background, so you have actual test results instantly, as soon as you’ve changed your code. This dramatically speeds up a developer’s workflow as there’s no need to manually rebuild the project and re-run all tests after making changes: the impacted tests are run in the background automatically (the testing session is triggered by some explicit action, e.g., saving changes).

If you practice test-driven development, the benefit is even more pronounced: the Red-Green-Refactor workflow becomes virtually seamless. All the boring routines go away, leaving you only with the fun part:

1. Write a test.
2. Write a stub.
3. Build.
4. Run the tests (they must fail).
5. Update the stub to return the expected result.
6. Build.
7. Run the tests (they must pass).
8. Write the code.
9. Build.
10. Run the tests (they must pass).
11. Refactor the code.
12. Build.
13. Run the tests (they must pass).

So, how to use continuous testing in dotCover? It’s very easy, with only three steps to follow.

1. Open Continuous Testing Session window

Continuous testing is managed via the Continuous Testing Session window. To open it, use the menu ReSharper | Unit Tests | Show Continuous Testing Session.

Continuous testing is off by default

2. Select continuous testing mode

By default, continuous testing is switched off. To make it work, select one of the modes in Mode:

Select continuous testing mode

A wide variety of combinations are available. For example, you can tell dotCover to automatically build and run dirty tests after you save the project. Or, run dirty tests only after you build the project.

What does ‘dirty test’ mean? It is the test that was impacted by code changes. For example, you have a test that covers some code, you change the code, and the test result is now obsolete, i.e. the test becomes ‘dirty.’ In addition, dotCover considers as ‘dirty’ all new tests, tests that were aborted by user (aborted), and tests that failed to complete for some reason (inconclusive).

3. Just work

Once continuous testing is enabled, you can simply start working. Initially dotCover will mark all tests as dirty.

The current tests status can be checked on the continuous testing icon in the status bar (bottom right corner of the main Visual Studio window). This handy assistant shows you whether there are any failed tests or all of them pass successfully. The Continuous Testing Session window displays the results in more detail:

Continuous testing status

As we already mentioned, your workflow with the enabled continuous testing is almost seamless. Suppose you change code that is covered by some tests and save the project (and you’ve selected the On ‘Save’ Build and Run Dirty Tests mode). dotCover builds the project and re-runs (only) dirty tests in the background:

Working with continuous testing

To navigate to a failed test, click the test in Continuous Testing Session window or click the number next to the continuous testing icon.

Failed test icon

Another option to navigate to the covering tests is to do it right from the code. Simply click on the code and select Show covering tests in the context menu. This opens the list of all tests that cover this line of code. Here you navigate to the corresponding tests or force them to run:

Show covering tests

Next steps

If you’re willing to try continuous testing right now, download and install ReSharper Ultimate.

IMPORTANT! If your tests that use external files fail during continuous testing (while being successful in a traditional unit testing session), you should properly set dotCover’s Shadow-Copy option. The following post describes in details how this option works.

Should you have any questions, please feel free to ask in the comments to this post. Your feedback is greatly appreciated!

Posted in dotCover Tips&Tricks, How-To's, ReSharper Tips&Tricks | Tagged , , , , | 22 Comments

ReSharper Build webinar: recording and Q&A

The recording of our recent webinar with Matt Ellis, Speeding up MSBuild: Tips, Tricks and ReSharper Build, is now available:

In this webinar Matt talks about some of the ways that help achieve faster build times in Visual Studio, including ReSharper Build introduced in ReSharper 10. The following topics are covered:

  • MSBuild refresher
  • Tips for speeding up MSBuild
  • Common sense tips
  • The CopyLocal myth
  • HintPath for references
  • Checking for slow targets and tasks
  • XAML & multi-pass compilation
  • Incremental compilation
  • ReSharper Build

Questions and Answers

There were some popular questions asked during the webinar, as well as those that we didn’t get the chance to answer on the day.

Q: How does ReSharper Build’s public API surface monitoring handle InternalsVisibleToAttribute?

A: ReSharper Build will only rebuild dependent projects if an assembly’s public API has changed. The  InternalsVisibleToAttribute allows other assemblies to see  internal types and type members as though they were public. Unfortunately, ReSharper Build doesn’t yet work with this scenario – the public API monitoring is currently only for public types and type members.

To work around this issue, you can mark the referencing projects as “always build” in the ReSharper Build options page – this disables ReSharper Build’s heuristics, and always calls MSBuild on the project, where it will rebuild, based on timestamps. The issue is RSRP-450733, if you wish to vote, track or add any further details.

Q: Can ReSharper Build be used in a Continuous Integration server or as a command line tool?

A: The current implementation is targeted at reducing the solution build time in a running instance of Visual Studio, specifically at improving the feedback cycle for edit/compile/test or edit/compile/debug. At this point we don’t have any plans to extend these scenarios to include continuous integration or other kinds of usage outside of Visual Studio.

Q: What are the impacts (if any) when using TFS with multiple developers some of which have ReSharper and some which do not?

A: None. ReSharper Build optimises the solution build for running instances of Visual Studio, by skipping projects that don’t need to be built. It doesn’t alter the project files or the solution file, or anything else that can affect other developers, or a continuous integration process.

Q: If you already have everything set up with your build using Visual Studio, does ReSharper reuse these settings? I want to make sure if I start using ReSharper the output of the build (structure, etc) will be the same.

A: ReSharper Build uses MSBuild to build the projects in your solution, just like Visual Studio does. Put simply, ReSharper Build replaces Visual Studio’s solution build management with its own, which means ReSharper Build gets to decide if MSBuild should be called for a project, or not. It does not alter the project build itself – when a project is built, the normal build process is followed.

Q: How do I force a complete rebuild when for example changing a method that is only used via reflection?

A: The “Rebuild” menu item will rebuild a selected project, or the whole solution. Also, the consuming project that is performing the reflection can be marked as “always build” in the ReSharper Build options. This disables ReSharper Build’s heuristics, and always calls MSBuild for a project.

Q: What kind of speedup do you experience with the ReSharper solution?

A: This is a tricky question to answer, because it very much depends on the scenario. If a public API is changed in a project referenced by everything else, then there isn’t much of a speedup – everything is rebuilt, and it takes as long as MSBuild normally would.

However, if the change is an internal implementation detail to the same root project, then only that project is built and the speedup is huge – seconds instead of minutes. Similarly, if a public API is changed, but the build is triggered for tests, then only the projects used by the tests are rebuilt, and this can also have a huge (positive) impact on the build time. The speedups vary, and the only way to really see how much of an improvement you’ll get is to try it with your own solutions! But it’s worth pointing out this feature was initially written to make things faster for the ReSharper solution, and the whole team uses it!

Posted in ReSharper Tips&Tricks | Tagged , , | 4 Comments

ReSharper Ultimate 10.0.1

Are you having issues with ReSharper 10, particularly in terms of unit testing, performance in web applications and/or red code in UWP applications?

If you are, please download ReSharper 10.0.1, a bugfix update which addresses all the major issues that we acknowledged last week.

As usual, the ReSharper Ultimate installer contains compatible updates to ReSharper C++, dotTrace, dotMemory, dotCover, and dotPeek.

Please comment to let us know whether the update improves ReSharper experience compared to the initial v10 release, and if there are any outstanding hot issues so that we can address them in further releases.

Posted in How-To's | Tagged , , | 128 Comments

Optimizing Load Balancing in PLINQs with ReSharper Ultimate

When filtering large amounts of data, using a PLINQ instead of a sequential LINQ can provide significant performance boosts. Still, optimal parallelization is quite a complex task that depends on a variety of factors, such as the number of CPU cores, the nature of input data, and others. In some cases, a query that seems parallelizable works even slower after being transformed into PLINQ.

Optimizing a PLINQ is, first of all, the task of finding the optimal load balance between worker threads. This usually requires multiple experiments with various input data, loads, and computer configurations. By using the right tools, you can find the best solution faster. In this post we’ll explain why ReSharper Ultimate (or more specifically ReSharper with dotTrace integrated in Visual Studio) is possibly the best candidate and why it is more convenient than the standard approach of measuring overall query time.

Sample application

Suppose we have a large project with many subsystems and we’ve just implemented a new feature that allows searching for a specific string in files stored inside zip packages*. Our application will use it to find particular files in numerous zips.

*This example, as well as the entire post, is inspired by PLINQ performance problems we faced when processing multiple NuGet packages in ReSharper. However, this post is not about optimizing a specific algorithm; it’s more about giving you a basic understanding of how you can analyze PLINQ performance with ReSharper Ultimate.

Our feature is based on the ZipPackage class (representing a zip package) and the FileInZip class (representing a file inside a zip). The FileInZip class provides the ContainsString method that finds a particular string in the file. This allows us to use simple LINQs for searching (e.g., …where file.ContainsString(str)…). To perform the search, a FileInZip instance extracts the corresponding file from a zip package to MemoryStream. We won’t provide the exact class implementation in this post but you can get the entire example on GitHub.

Sample app classes

Creating a run configuration

Now we want to test the feature on some sample zip packages without running the entire application (remember, it’s huge). Should we create an integration test for this? No, much easier. With ReSharper Ultimate we are able to run specific static methods using run configurations (read this post for a lot more details about the feature). In a nutshell, we should write a static method that contains only the code we need. ReSharper Ultimate’s run configurations feature will allow us to run just this method in a separate JetLauncher process. In this way we can run and debug any part of our code by isolating it from the main application. For example, anywhere in our project we can create the following static method:

Then we just use Alt+Enter on the method and select Debug | Run:Method's action list

This opens the Run Configuration Properties window. Clicking Save & Execute will run the method (under the JetLauncher process) and save the particular run configuration for future use.

Run configuration properties

Our method writes the result to the debug output. Debug output

OK, the algorithm works as expected, but how can we measure its effectiveness? Let’s profile our method! If you have ReSharper Ultimate (or both ReSharper and dotTrace) installed on your machine, you can profile any run configuration. So, all we need to do is start profiling the run configuration we’ve just created.

Profiling run configuration

To profile a static method, we once again use Alt+Enter on the method and select Profile (<profiling type>)*, where <profiling type> is the type of profiling that will be used by dotTrace. We’ll use the Timeline profiling type as it not only provides time data for method calls but also binds them to the timeline, which is extremely important when analyzing multi-threaded applications.

*The default profiling type can be selected in Performance Snapshots Browser which can be opened via ReSharper | Profile | Show Performance Snapshots… Profile through the action list

This runs profiling and automatically takes a performance snapshot when the process completes its work. Then the collected snapshot is opened in dotTrace Timeline Viewer.

If you’re new to Timeline Viewer, find more details about it here. In a few words, it is a part of dotTrace that allows you to slice and dice the collected snapshot data using filters. For example, select time intervals where a certain event takes place (UI freeze, GC and so on) and find out what methods stand behind that. Or, vice versa, select time intervals where a specific method is executed and see what subsystems are working at this moment.

If we now take a look at the Threads Diagram in Timeline Viewer, we’ll see that the profiled static method utilizes just one Main thread (as expected) and requires about 70 seconds to complete:Threads Diagram showing sequential LINQ

The Top Methods filter shows that the highest own time belongs to the Ionic.Crc.CRC32.SlurpBlock method. This is kind of expected as this method is responsible for zip extraction (the app uses the DotNetZip library to work with zip packages).

Top Methods and Call Tree

As 70 seconds is too long for us, we decide to parallelize our LINQ so that multiple threads can unzip packages at the same time.

Using PLINQ

The most obvious solution is to add the AsParallel method to the input sequence in our LINQ.

After profiling, we see that this solution kind of works – overall execution time has decreased to 39 seconds. Still, that result is far from ideal. The Threads Diagram shows that the data is split among 8 threads and, while some threads do most of the job, others complete their work much earlier.Timeline Diagram for PLINQ with AsParallel

So, why does this happen? Let’s use dotTrace’s filtering capabilities to find out the root cause. Our input data are zip files stored on a disk, so we should take a look at how threads treat these files. To do this, we simply turn on the File I/O filter in Interval Filters. Now, all other filters show the data only for the time intervals where file I/O takes place.

Threads Diagram with applied File IO filter

The File I/O: File Name sub-filter shows how File I/O time is distributed among specific files:

File IO File Name subfilter

The sub-filter states that some files require more time than others. If we now turn on a filter by a specific file, we’ll see that each zip file is processed by exactly one thread:

Threads Diagram with applied File IO File Name filter

Therefore, the problem lies in the zip-per-thread load partitioning. As zip files have different sizes, threads take different amounts of time to process them. This means we need to implement partitioning in a way that lets each thread extract files from any zip package.

Custom partitioner

Our solution is to create a custom partitioner that will split the input data into equal chunks. In our case the partitioner must provide not zip packages to PLINQ, but the files inside these packages. For this purpose, we can try using a “default” partitioner, an instance of the Partitioner class. We just need to prepare the input data for the partitioner (files in zips) and specify whether we want to use dynamic or static partitioning (this is specified in the partitioner’s Create method).

With dynamic partitioning, it will try to balance the load dynamically so files will be provided to threads as they are requested. This can provide a speed boost on computers with many cores. The static partitioner prepares all chunks before the start, resulting in a lower overhead. This approach may be faster on computers with only a few cores.

Let’s try dynamic partitioning:

After we profile the method, we can see that the load is now much more balanced. The makes the method take only 29 seconds to complete, which means a 10-second (25%) improvement. The default static partitioner on an 8-core computer shows almost the same results:

Threads Diagram for default dynamic partitioner

As we can see on Threads Diagram though, the partitioning is still not perfect as some threads complete their work a little bit earlier than the others. This happens because the “default” partitioner knows nothing about the internal structure of our input data (text files).

The problem is that bigger files require more time for extraction. This means we need to fill the chunks not based on the number of extracted files, but on their size. We can try to solve this task by creating our own static partitioner. Such a partitioner must be based on the Partitioner class and must implement the GetPartitions method, which returns enumerators for the given number of partitions.

On an 8-core machine, our custom partitioner works a little better than the default partitioners. The load is more balanced now with about 10% additional improvement (from 29 seconds down to 26).

Threads Diagram for custom static partitioner

What next? The only way to ensure your PLINQ works as fast as possible is to test it on as many configurations as possible. For example, if we try all the partitioners we’ve just created on a 4-core PC, we’ll see somewhat different results:

Threads Diagrams on a 4-core PC

While the default static and dynamic partitioners are still close, the custom one increases its lead. It takes 30 seconds, which is about a 15% improvement compared to 35-37 seconds. The small difference in absolute time values between 8-core and 4-core PCs may be explained by the large number of garbage collections which block worker threads (as our algorithm allocates a LOT of memory for zip extraction). Excessive GCs would make a good target for optimization too, but that’s a story for another post.

As you can see, PLINQ performance offer rich opportunities for experimenting, and ReSharper Ultimate can help a lot. We hope you find this post helpful and give a try to ReSharper Ultimate features on your own.

Posted in dotTrace Tips&Tricks, How-To's, ReSharper Tips&Tricks | Tagged , , , | 10 Comments