Immutable Data and Memoization in C#, Part 2

By jay at April 22, 2013 21:10 Tags: , , , ,

tl;dr: Memoization can be associated with the ConditionalWeakTable class, which allows the addition of memoized computation results to immutable types. This makes the memoized results live as long as the instances that was used to create it.

 

In the first part of this article, we discussed a bit the Memoization pattern in C#. In this second part, we will discuss how to alleviate the memory management issue for memoized computation results.

 

ConditionalWeakDictionary to the rescue

In .NET 4.0 – quite a while ago now – a new class was added, the ConditionalWeakTable, to help in the creation of dynamic languages based on the DLR, where there was a need to be able to attach data to existing type instances, much like a fictional extension property could be. This topic is not covered much, and since it has to do with the GC, it is often misunderstood.

The idea is pretty simple: It is a dictionary that takes an type instance as a key, and a value associated to it. The key is stored as a weak reference, meaning that the data is held as a hard-reference as long as the key lives. When the key is collected by the GC, the hard-link is removed, making the data available for collection if it’s not referenced anywhere else.

Here’s how to use it:

More...

Immutable Data and Memoization in C#, Part 1

By jay at April 18, 2013 20:49 Tags: , , , ,

TL;DR: Immutable data and memoization are functional programming concepts that can be applied to C# programming. These patterns have their strengths and weaknesses, discussed a bit in this article. 

 

I’ve grown to not being a great fan a data mutability.

Data mutability can introduce a lot of side effects in the code, and it can be pretty complex to go back in time to know what a specific state was before the code failed. This gets worse when multiple threads are involved, and that tends to happen a lot more these days, now that even phones have multiple cores.

Sure, we can use IntelliTrace to ease that kind of debugging, but that’s pretty much limited to issues you already know about. That means you’re reacting to issues that already happened, you’re not proactively preventing those issues from happening.

So, to address this more reliably, there’s the concept of immutability. When a set of data is built, it cannot change anymore. This means that you can pass it around, do computation with it, use it any place you want, there’s not going to be any subtle concurrency issues because the data changed under your feet.

More...

[VS2012] Temporarily disable the C# static code analysis for a whole VS instance

By jay at March 30, 2013 11:52 Tags: , ,

TL;DR: It is possible to disable the Static Analysis phase in VS2012 projects by setting the DevDivCodeAnalysisRunType environment variable to “Disabled”.

 

This will be a quick post, but which might save you a lot of time if you rely heavily on Code Analysis (FxCop).

FxCop is definitely not known for its analysis speed, and when ran in every build, this takes a lot of time. I usually work on projects where FxCop is only enabled in the Release Configuration, which helps during development in Debug configuration.

But if you’re bound to run in Release configuration, such as when profiling the app, or any other task that requires to build in that configuration, then having FxCop running every single time can be time consuming.

To avoid this, there are multiple choices :

  • Use find and replace to change <RunCodeAnalysis>true</RunCodeAnalysis> to <RunCodeAnalysis>false</RunCodeAnalysis>, but then you have to remember to not check that into your source control (even if the Perform Code Analysis setting is set to Always in your CI Build definition),
  • Create an alternate configuration similar to Release that does not have the Static Code Analysis enabled, but changing configurations in VS2012 can take time (even with the Update 2) and you’ll have to maintain that configuration with the others,
  • Or you can use a little trick to disable the static code analysis for a whole Visual Studio instance.

 

That trick is a bit hidden, but here’s how you can do this :

  • Open a Developer Command Prompt for VS2012
  • type set DevDivCodeAnalysisRunType=Disabled
  • type devenv

 

Build your solution and you won’t have the static analysis running, without any modification to your solutions’ configuration or projects. Easy.

That said, remember that this is not documented and might change at any point in the future so don't rely on it too much.

 

On the Performance of WinRT/Xaml Template Expansion

By jay at February 02, 2013 15:11 Tags: , , , , , ,

TL;DR: Expanding data-bound item templates in Xaml/WinRT in Windows 8 is about a hundred times slower than with Xaml/WPF. This article details how this was measured and a possible explanation.

 

In Windows 8, Microsoft has a introduced a whole new Xaml stack, codenamed Jupiter, completely re-written to be native only.

This allows the creation of Xaml controls using C++ as well as C#.

I will not discuss the philosophical choice of ditching managed WPF in favor of a native rewrite, but make a simple comparison of the performance between the two.

 

Template Expansion Performance

I worked on a project that had performance issues for a UI-Virtualized control, where the initial binding of data as well as the realization of item templates, was having a significant impact on the fluidity of the scrolling of a GridView control.

To isolate this, I created a simple UI: More...

Configuring Multiple TFS 2012 Build Services on one Machine

By jay at October 27, 2012 12:18 Tags: , , ,

Since TFS2010, because there’s been the introduction of project collections, build agents are quite the trouble for IT admins.

On one hand, there’s the fact that putting every project in a single collection can be a maintenance and project isolation nightmare, and on the other hand the fact that there can officially be one build controller per machine tied to a single project collection, forcing to have many machines to support continuous integration build environments.

Luckily, there’s been a workaround published two years ago on how to run multiple build services on the same machine, and it’s working pretty fine.

 

However with TFS2012, this hack needs to be adjusted to work properly.

Prior to running the configuration tool, instead of setting this:

set TFSBUILDSERVICEHOST=buildMachine-collection2

The new environment variable is:

set TFSBUILDSERVICEHOST.2012=buildMachine-collection2

I’m guessing that this can be used to run both 2010 and 2012 build services on the same machine without interferences, though I haven’t tried it.

Just as a reminder, to use this feature, you need to set non-overlapping build working folders and different listening ports for every new instance. This will avoid conflicts…

Works pretty great, now with TFS2012 !

C# Async Tips and Tricks, Part 3: Tasks and the Synchronization Context

By jay at September 29, 2012 21:12 Tags: , , , , , , , ,

TL;DR: It is possible to mix C# async and basic TPL style programming, but when doing so, the synchronization context capture feature of C# async is not forwarded to TPL continuations automatically, making UI dependent (and others) code fail and raise exceptions. This can lead to the termination of the process when exceptions are not handled properly, particularly in WinRT/C# apps.

 

I’ve discussed in a previous article of this series, the relation between async Task/Void methods and the ambient SynchronizationContext.

Just as a simple reminder, when executing a async method, whether it is Task, Task<T> or Void returning, the caller’s SynchronizationContext is captured to ensure that all the code in an async method is executed in the same context. The main scenario for this is to easily execute UI bound code in an async method.

It is important to remember that async methods are based on the TPL framework, and that async methods (except in infamous async void) return System.Threading.Tasks.Task instances.

More...

VS2012: Create multiple file links with a drag and drop between two projects

By jay at September 26, 2012 19:08 Tags:

Ce billet est disponible en francais.

With more and more platforms appearing in the Microsoft landscape, multi-targeting is getting more and more common.

Multi-targeting is used when the Portable Libraries don't cut it. It's interesting when you're not in the mood of making too many abstractions to cope with missing classes and methods for your selected platforms intersection.

If you go with the multi-targeting, you've got multiple choices, such as using Project Linker, and create multiple projects for the same source files and link the files between your two projects.

At this point though, Project Linker does not support VS2012, so you're left with the Add as Link trick in Visual Studio, which does not support folders.

Well, in Visual Studio 2012, there a new feature that allows just that.

More...

VS2012: How to change a project’s physical location

By jay at September 26, 2012 19:02 Tags: ,

Ce billet est disponible en francais.

Physically moving a project in a Visual Studio solution has always been cumbersome. It’s often needed if you rename your [insert favorite language] project’s folder.

In previous versions of Visual Studio, you would move the project into the new location and either :

  • Remove the project from the solution and add it back using the new location
  • Go the warrior way, and hack around the very user-friendly .sln file format and change all the references

That’s been an annoying situation, but that could be worked around.

But every time, you’d lose your solution wide settings, such as default startup project or build configuration. And if you have many build configuration, I think you understand the pain of restoring all these settings properly.

Fortunately, there’s been a very small change in VS2012 that allows to provide a new location of a project file, if it failed to load because it was not found.

More...

About me

My name is Jerome Laban, I am a Software Architect, C# MVP and .NET enthustiast from Montréal, QC. You will find my blog on this site, where I'm adding my thoughts on current events, or the things I'm working on, such as the Remote Control for Windows Phone.