Tools don’t matter

Tools don’t matter
I don’t use vim or emacs, but for some reason whenever I used to read a vim or emacs article or blog post, I used to download, install and spend the rest of the day reading books, manuals and playing with the editors. I’ve done this several times in the last few years with each iteration making a more firm resolve to switch to vim/emacs for my day to day tasks. However, I always ended back at Visual Studio.Net/Eclipse/Notepad++ as the tools of my day to day work. For some reason I always felt inferior to all the Guru* Hackers** using vim and emacs and having this crazy productivity and superior intellect. Why couldn’t I be part of this “elite” club? The same holds true for several other areas as well, for e.g. downloading Python, Ruby, J and spending the day learning the language only to forget the next week.
All this kind of reminded me of my experiences with finding the perfect weapon while playing Call of Duty 4 over the past year. I spent 3 hours a day almost every day for the past one year playing this game, reaching the max prestige level (the “elite” club) in the multiplayer version. I became really really good at it and could kick a lot of ass no matter what weapon I was using. But I remember when I started out and I really sucked, I became obsessed with finding the perfect weapon with the perfect set of perks and addons. I used to wander the forums asking people about which weapons and perks to use on which map and what the best tips were etc. Thinking that having the perfect weapon would make me a good player. In the end, the only thing that mattered was all the hours I put in to learn all the maps, routes, tricks and my ability(I like to think). The surprising thing was that once I mastered the game, it didn’t really matter what weapon I chose, I was able to adapt any weapon and do a decent job.
Based on my experiences with the game I started thinking about why I felt I needed to be using vim/emacs. I realized that I was really efficient at doing everything that vim/emacs claimed to do using my own toolchain. And I didn’t really need to un-learn the toolset that I am the master at and pick up another toolset because a lot of people claim that it’s only way to do it right. The same applies to my language/platform of choice (C#/.Net) and my OS of choice (Vista). For some reason I was giving more weight to the opinions posted by people on the Internet than to my own experiences. I have no angst for the vim/emacs folks, they’re probably folks whose first editor was vim/emacs and that’s what they became comfortable with like to share their passion for the tool and help others in mastering it. For me coming from a DOS background with Turbo Pascal and Turbo C++ as the first tools, it’s a different perspective of what an editor should be able to do.
I’ve now made peace with my choice of tools, I know it’s limitatons and advantages compared to the other tools and I think that’s more important. The reason I have this confidence now is that I’ve written, read and debugged enough code to have an appreciation for our craft and what it entails. You just can’t get here by reading blog posts and books, you have to actually do it to the get confidence to be able to make those decisions. I am going to continue checking out new tools because I inherently love learning new things, but I’ve learned to be comfortable in my own choices.
To summarize, I’d say Pick a tool/language/platform “YOU” are comfortable with, stick with it for a non-trivial time and once you master it, you can do everything that some other tool claims to do, because in the end the tool does not matter, what really matters is the mind using It.
*,** – Really interesting that I spewed the term Guru Hackers…Guru was what the term used for master programmers before Paul Graham made Hacker the defacto term.

I don’t use vim or emacs, but for some reason whenever I used to read a vim or emacs article or blog post, I used to download, install and spend the rest of the day reading books, manuals and playing with the editors. I’ve done this several times in the last few years with each iteration making a more firm resolve to switch to vim/emacs for my day to day tasks. However, I always ended back at Visual Studio.Net/Eclipse/Notepad++ as the tools of my day to day work. For some reason I always felt inferior to all the Guru* Hackers** using vim and emacs and having this crazy productivity and superior intellect. Why couldn’t I be part of this “elite” club? The same holds true for several other areas as well, for e.g. downloading Python, Ruby, J and spending the day learning the language only to forget the next week.

All this kind of reminded me of my experiences with finding the perfect weapon while playing Call of Duty 4 over the past year. I spent 3 hours a day almost every day for the past one year playing this game, reaching the max prestige level (the “elite” club) in the multi-player version. I became really really good at it and could kick a lot of ass no matter what weapon I was using. But I remember when I started out and I really sucked, I became obsessed with finding the perfect weapon with the perfect set of perks and addons. I used to wander the forums asking people about which weapons and perks to use on which map and what the best tips were etc. Thinking that having the perfect weapon would make me a good player. In the end, the only thing that mattered was all the hours I put in to learn all the maps, routes, tricks and my ability(I like to think). The surprising thing was that once I mastered the game, it didn’t really matter what weapon I chose, I was able to adapt any weapon and do a decent job.

Based on my experiences with the game I started thinking about why I felt I needed to be using vim/emacs. I realized that I was really efficient at doing everything that vim/emacs claimed to do using my own toolchain. And I didn’t really need to un-learn the toolset that I am the master at and pick up another toolset because a lot of people claim that it’s only way to do it right. The same applies to my language/platform of choice (C#/.Net) and my OS of choice (Vista). For some reason I was giving more weight to the opinions posted by people on the Internet than to my own experiences. I have no angst for the vim/emacs folks, they’re probably folks whose first editor was vim/emacs and that’s what they became comfortable with like to share their passion for the tool and help others in mastering it. For me coming from a DOS background with Turbo Pascal and Turbo C++ as the first tools, it’s a different perspective of what an editor should be able to do. I like the deep intellisense that Visual Studio and Eclipse are able to provide, this coupled with Notepad++ for quick edits is all I need right now.

I’ve now made peace with my choice of tools, I know it’s limitations and advantages compared to the other tools and I think that’s more important. The reason I have this confidence now is that I’ve written, read and debugged enough code to have an appreciation for our craft and what it entails and I don’t think I could have gotten this confidence by reading blog posts and books, you have to actually do it to the get confidence to be able to make those decisions. I am going to continue checking out new tools because I inherently love learning new things, but I’ve learned to have confidence in my own choices and to make an objective decision on the tool to use for a particular task.

To summarize, I’d say Pick a tool/language/platform “YOU” are comfortable with, stick with it for a non-trivial time and once you master it, you can do everything that some other tool claims to do, because in the end the tool does not matter a lot, what really matters is the mind using It.

*,** – Really interesting that I spewed the adjective Guru Hackers, “Guru” was what the term used for master programmers before Paul Graham popularized “Hacker” as the de facto term.

Real Programmers

Real Programmers

Windows Azure Distilled – A Programmer’s view

The launch of Windows Azure has coincided with the launch of the entire cloud computing initiative from Microsoft which makes a lot of business sense but it also makes it hard to differntiate the individual parts of the initiative, here’s my understanding so far.


Windows Azure

This is the base platform that provides a generic cloud computing platform for developers to host applications on.  Azure basically offers two core services,

Compute Service

This is what people would normally associate with cloud computing. You write an app that conforms to the cloud hosting requirements and then deploy it to the cloud where it can be managed and scaled as per the load and performance requirements. Currently there are two types of compute services that can be deployed on Azure

Web Role – This currently is a WebForms ASP.Net application, but looks like support for ASP.Net MVC and other languages are in the works too. This is just like hosting your application with a hosting provider except that you get load balancing, failover etc. for free. One important caveat here is that the load balancing does not support sticky sessions(as of the CTP) which is a good thing but it does require the developer to use the storage service to store session data.

Worker Role – This is more like a Windows Service that is deployed on the cloud, it can make outgoing connections but incoming connections are disallowed. But again you get the benefits of load balancing and failover.

The compute services are different from other similar cloud computing options available because it does not require the developer to supply a VM image (unlike Amazon EC2) of the application environment to host,  instead the applications run in VMs that are built and maintained by the Microsoft Data Centers that host the applications.

This decision definitely has it pros and cons, it’s good because

- No licensing costs for the OS

- No need for updating of the VMs as the applications evolve

- Simpler deployment strategy

- API available for management of service instances and for reporting on application health

on the other hand

- it does restrict the flexibility of what software and services can be accessed from the host VM

This diagram from MSDN gives a nice overview

All though in reality the deployment internally looks like below with IIS7 and Windows Server 2008 VMs.

 

Azure Hosting

Azure Hosting

 

Storage Service

This is the second part of the base Azure platform and is the equivalent of Amazon S3, SQS and SimpleDB all rolled under one name. The equivalents in the Storage Service are called,

Blob - For storing binary data, most commonly file streams.

Queue  - For reliable persistent messaging. I found it strange that it was lumped with the storage services because the primary purpose of this seems to be inter service communication between the applications running in the cloud. 

Table - For storing tabular data, different from traditional database tables though.

The important thing to remember here is that these services are available to any application that can talk over HTTP so WPF, WinForms, and non-cloud ASP.Net applications can make use of the storage services as well and benefit from having their data stored in an easily scaled environment. The storage services expose a RESTFul API which makes it easy to access these services from any platform that can talk over HTTP but ofcourse if you’re using .Net you get some builtin support from the framework.

 

Azure Storage Services

Azure Storage Services

 

The developer story for Windows Azure is as good as anything else from Microsoft, the SDK includes tools to simulate both the compute services and the storage services on your development box, further with the VS addin you can deploy your cloud application straight to the Azure hosting platform, talk about ease of deployment. Here’s a quick overview of the important tools included in the Azure SDK.

Development Fabric – Simulates the Azure Compute Services (aka Azure Fabric) on your dev machine complete with a load balancer and failover monitoring.

Development Storage – Simulates the Azure storage services on your dev machine, the CTP implmentation requires SQL Server 2008 Express but you can also work with SQL Server 2008 if you want.

CSPack – Command line tool that prepares a service for deployment either to the Development Fabric or to the Windows Azure Fabric.

CSRun - Command line tool that runs a service (generated from CSPack) on the Development Fabric.

So the development cycle for a Azure app consists of developing locally using the Development Fabric and Development Storage and then deploying to the Azure platform when ready.  Deployments would not normally bring down the sites because the instances are load balanced and are not sticky. 

The other good thing is that services can be configured manually be editing the service configuration file or programatically by accessing the ServiceHosting API at runtime, so no redeployment is required if only service configurations are changed (which includes things like number of instances to run).

That’s basically the core Azure platform, the rest of the stack (.Net Services, SQL Data Services and Live Services) are cloud applications built by Microsoft on top of the core Azure platform. Again all these applications expose their API in a RESTFul manner as well making them open to both any technology that can talk over HTTP.

 

Azure Services Platform

Azure Services Platform

 

Personally for me the Windows Azure SDK section from MSDN and this document from David Chappel were the most useful pieces of documentation to understand the entire stack.

Using Windows Azure SDK with SQL Server 2005/2008

I downloaded the Windows Azure SDK today and was going through the quick starts from MSDN , the quickstarts mention that you need SQL Server Express 2005/2008 to run the samples, but I only had SQL Server 2008 developer edition installed. I decided to give it a try anyways and immediately hit a snag when trying to build the samples because as expected it could not find the SQLExpress instance that it was assuming would be present on the localhost.

Here are the changes I had to make to get the samples and the SDK to work with SQLServer 2008 installed without an instance name on my laptop.

1. In Windows Azure SDK\v1.0\samples\MSBuild\Microsoft.Samples.ServiceHosting.targets , add the “/server” option to the UpdateSampleTablesDB target to make it look like

<Target Name=”UpdateSamplesTableDB” DependsOnTargets=”BuildSubProjects”>

    <Message Text=”$(DevTableGenCommand) /database:$(SamplesDBName) /server:localhost $(DevtableGenForceCreateFlag) @(DevTableGenAssemblies)”/>

    <Exec Condition=”‘$(SamplesDBName)’!=”” 

            Command=”$(DevTableGenCommand) /database:$(SamplesDBName) /server:localhost $(DevtableGenForceCreateFlag) @(DevTableGenAssemblies)” 

    WorkingDirectory=”$(MSBuildProjectDirectory)”/>

  </Target>

2. Change the datasource in the connection string used in  ”Windows Azure SDK\v1.0\bin\DevelopmentStorage.exe.config” to localhost

<connectionStrings>

    <add name=”DevelopmentStorageDbConnectionString”

         connectionString=”Data Source=localhost;Initial Catalog=DevelopmentStorageDb;Integrated Security=True”

         providerName=”System.Data.SqlClient” />

  </connectionStrings>

3. Change the dbServer for the Table service in the developmentStorageConfig section to localhost in “Windows Azure SDK\v1.0\bin\DevelopmentStorage.exe.config”

    <services>

      <service name=”Blob”

               url=”http://127.0.0.1:10000/”/>

      <service name=”Queue”

               url=”http://127.0.0.1:10001/”/>

      <service name=”Table”

               url=”http://127.0.0.1:10002/”

        dbServer=”localhost“/>

 

    </services>

HTH

A case study in micro-optimization

Last week I saw a wrap up from cedric’s coding challenge on his blog, the problem looked simple enough, “write a counter function that counts from 1 to max but only returns numbers whose digits don’t repeat”.

My first stab at it consisted of a brute force solution of looking at every natural number up to a given limit and determining if the digits were unique, the crux of which was this function that given a number determined if the digits repeated or not

private static bool DoDigitsRepeat(long num) {
····//Used to track which digits have already been encountered
····int used = 0;
····while (num > 0) {
········int digit = (int)num % 10;
········num = num / 10;

········int index = 1 << digit;
········if ((used & index) == index) {
············return true;
········}
········else {
············used |= index;
········}
····}
····return false;
}

The good thing about this solution was that it met the secondary goal for the problem which was to determine the biggest gap in the sequence of generated numbers, but in terms of performance it sucked! It worked well for the smaller limits defined in the problem but when you tried to push and generate all matching numbers till the max possible i.e. 9876543210 it just took too long (I think it was under a minute, but that was still too long)

I went back to the problem page and saw that the scripting weenies who had tried to use string functions to prune numbers had the worst performance of all solutions, that made me feel a bit better until I saw CrazyBob’s Java solution(the fast version), the comments indicated that the solution found the total count in under half a second. This solution was not able to determine the biggest gap though because the numbers were generated out of order but nevertheless this was an excellent solution.

So began my quest to come up with a faster solution. I felt sure that I could use the bit twiddling hacks to come up with a faster solution, it was just a question of hitting the right spot.

The first non-brute force solution that I tried was to enumerate all possible subsets of {0,1,2,3,4,5,6,7,8,9} and then to generate permutations from that set making sure that zero was never in the first place. Subset generation is pretty easy to do, all you need to do is count from 0 to 2^n where n is the number of elements in the set, the bit patterns of all the numbers in this range can be used to generate all the subsets. For the first cut I tried to be Object-Oriented and used the below class to implement a BitTable.

public class BitTable
{
····private uint _storage = 0;
····public BitTable(uint value) {
········_storage = value;
····}

····public void Set(int index) {
········Debug.Assert(index >= 0 && index < 32);
········_storage |= (uint)(1 << index);
····}

····public void Reset(int index) {
········Debug.Assert(index >= 0 && index < 32);
········_storage &= (uint)~(1 << index);
····}

····public bool IsSet(int index) {
········Debug.Assert(index >= 0 && index < 32);
········uint val = (uint)(1 << index);
········return (_storage & val) == val;
····}
}

Of course as I soon found out, all the method calls to BitTable were really slowing things down (and by slow I mean a 1-2 seconds slower than CrazyBob’s solution), so I dropped the class and moved all the operations inline, also I realized that since I was using value types to hold the state in the search/call tree, I didn’t need to set and unset the state after each recursive call. Here’s the final version of this line of thinking.

class Beust4
{
private static int _total = 0;
public static void Run() {
····_total = 0;
····//Generate all possible subsets of a set of 10 elements
····//2^10 = 1024
····for (int i = 1; i <= 1024; ++i) {
········Permute(i, 0L);
····}

····Console.WriteLine("nTotal: {0}", _total);
}

private static void Permute(int digits, long current) {
····for (int index = (current > 0) ? 0 : 1; index <= 9; ++index) {
········if ((digits & (1 << index)) == (1 << index)) {
············if ((digits & ~(1 << index)) == 0) {
················//Console.Write("{0}, ", (current * 10) + index);
················++_total;
················return;
············}
············else {
················Permute(digits & ~(1 << index), (current * 10) + index);
············}
········}
····}
}

I felt really good about this attempt but to my surprise when I ran it, I took 1.2 seconds on average which was still more than 2 times slower than CrazyBob’s Java solution. I got really stuck at this point and I had to make sure that there was not something obvious that I was missing. The first thing I did was to port CrazyBob’s solution to .Net so that I could compare both solutions, I’ve uploaded the C# version of CrazyBob’s solution here in case anyone wants to take a look.

On the surface it looks like the bit twiddling based solution should run faster because it does not make all those method calls, the other thing that I suspected was causing problems was the number of recursive function calls that were being made, so I put in some code to check the number of recursive function calls that were being made and to my surprise I found that CrazyBob’s solution was making 8877691 recursive calls as compared to my solution which was making almost 10 times that number. Also the actual soultion count is 8877690 which meant that the number of calls in CrazyBob’s solution was near optimal. So it was clear that it was the number of calls that were costing me the half second. Btw CrazyBob’s C# version still ran in 700ms on average on my laptop, which was still 500ms faster than my C# version.

I then started to think about alternate ways to attack the issue, one track I went down was to consider all the digits as a complete graph and then coming up with a way to enumerate all paths in the graph, traversing a n edge in the graph would remove other edges from the graph and make them not available. This reminded me on Knuth’s Dancing Links Algorithm and I read up on that a bit, this paper from Knuth on the subject was an excellent read. It looked to me that CrazyBob had used an approach similar to the DLX algorithm, but after reading the entire paper from Knuth it still didn’t strike me as to why using a DLX approach would provide such excellent performance as compared to my version.

So I went back a bit to comparing both solutions and I think the two key observations that I saw that

  1. CrazyBob’s solution went down the search tree to one level above the last and then just generated all the solution from there instead of recursing down to the last level, so for e.g. supposing it was generating 5 digit numbers and it had already generated 4321, then at that level it didn’t make additional recursive calls to add the final digit, it was able to add the last digit at the same level pruning the search tree quite a bit. In contrast my solution was basically doing a method call for every digit of every number in the solution set.
  2. The above optimization was made possible by using the length of the final solution as the key, so first all 1 digit solutions were generated, followed by two digit ones and so on.

Cool, so now I ported my bit twiddling version to generate based on the length of the numbers and the optimization to prune the search tree one level above the last came naturally. I ran my solution and guess what, it was still 200 ms slower than CrazyBob :( Aaaarghhhh!!!!!

for (int len = 1; len <= 10; ++len) {
····Generate(len, 0, 0xFFF >> 2, 0);
}

private static void Generate(int maxLen, int currentLen, int availableDigits, long currentValue) {
····bool last = (currentLen == maxLen - 1);
····for (int digit = (currentValue == 0) ? 1 : 0; digit <= 9; ++digit) {
········if ((availableDigits & (1 << digit)) != (1 << digit))
············continue;

········if (last) {
············++_total;
············//Console.Write("{0}, ", (currentValue * 10) + i);
········}
········else {
············Generate(maxLen, currentLen + 1, availableDigits & ~(1 << digit), (currentValue * 10) + digit);
········}
····}
}

But I knew I was getting close, at this point I knew what was killing me basically to determine which bits were set I was iterating from 0 to 9 and then testing if that bit was set in the number or not, this test was killing me because most of the times the bit was not set and I was doing a huge huge number of unnecessary tests. So I needed a way to iterate through only the set bits. The first solution I tried used a hashtable but that caused a even bigger degradation in performance. Finally for a lack of a better way to express this in C# I had to waste 4KB of memory and use an array to allow me to iterate through the indexes of the set bits in a number.

The final solution ran in 350ms on average, almost a 50% improvement over CrazyBob’s solution, woot!!!!! A further optimzation to move the if statement from inside the loop to outside which makes the code more readable but incredibly smelly because of the near duplicate code in both the if and else blocks shaved off another 50ms. Here’s the final version without the optimzation for moving the if statement outside which makes the code a bit shorter.

class Beust5
{
····private static int _total = 0;
····private static int[] _pre = null;
····public static void Run() {
········_total = 0;
········_pre = new int[(1 << 10) + 1];
········for (int i = 0; i <= 10; ++i) {
············_pre[1 << i] = i;
········}

········for (int len = 1; len <= 10; ++len) {
············Generate2(len, 0, 0xFFF >> 2, 0);
········}

········Console.WriteLine("nTotal: {0}", _total);
····}

····private static void Generate2(int maxLen, int currentLen, int availableDigits, long currentValue) {
········bool last = (currentLen == maxLen - 1);
········int x = availableDigits;
········while (x != 0) {
············//digit will contain the lowest set bit
············int digit = _pre[x ^ (x & (x - 1))];
············x &= (x - 1);

············//Avoid starting with zero
············if (digit == 0 && currentValue == 0)
················continue;

············if (last) {
················++_total;
················//Console.Write("{0}, ", (currentValue * 10) + i);
············}
············else {
················Generate2(maxLen, currentLen + 1, availableDigits & ~(1 << digit), (currentValue * 10) + digit);
············}
········}
····}
}

What will be the next generation internet application platform?

A few years ago I was a firm believer in the Rich Connected Client application model, which was based on running applications installed locally on the users desktop. From the time of the Ajaxian explosion, the quality and quantity of Ajax based web applications has continued to increase, applications like FaceBook have introduced new paradigms whereas apps like Live Maps have made existing apps much more convenient and accessible. Today you have to really argue hard to even consider a desktop based application for anything that is non-computation intensive (Even this category is questionable now, for e.g. a few years back movie editing web apps would have been out of the question).

So what is it that makes the web such a successful application platform

  • Uniform and simple model (Web Browser, urls, can click when hand is visible) – Once a user learns the basics of working with a web application that knowledge can be easily applied to other applications.
  • Client platform independence – The decoupling of the server and client with an agreed contract (HTML+CSS+JS) means that the traditional problems of targetting various platforms with different APIs is no longer existent on the client side.
  • Machine independence - The user is no longer restricted to the machine on which the application was installed. This also results in a much simpler deployment model.
  • Data independence – The user’s data is now available on the network which means that not only can the user run the application from anywhere but can also access his data from anywhere.

Now what would the next generation internet application platform look like? I think that in addition to the above characteristics, the next generation of platforms would involve the following.

  • Full use of computing resources available locally – Having a powerful CPU and GPU seems like such a waste when all your applications have to be funnelled through the browser. So the next generation platform would allow access to the computing power available locally.
  • Better integration with the local resources – This is sort of related to the point above, but would allow internet applications to access local disks, settings, registry etc.
  • Better security model – Of course all this has already been attempted with ActiveX and XPCOM, but the security models there have been weak and non-intutive to users, a better solution is needed.

So it looks like the direction being taken by Microsoft Silverlight and Adobe AIR are steps in the right direction to building the next generation internet application platform. However Microsoft has a great oppurtunity here push the envelope with Silverlight and introduce new standards for desktop integration of internet applications, their extensive user base means that any API created by them has a very good chance of being successful and catching on with the other players in this space.

PsTools to the rescue

We were having memory issues on one of our production servers today, no one was able to get in via Remote Desktop or any other remote access tools. PsTools by Mark Russinovich was a life saver, it allowed me to query the processes running on the remote machine and also their memory usage, we were able to diagnose the issue and fix it quickly once we knew what the cause was. The tool suite also has a bunch of other useful tools that work on remote boxes as well.

Subversion as a deployment tool

I was thinking on the way to work today that subversion would be a great tool to overcome some of the difficulties associated with frequent deployments to the web serevers. Here’s how I see it working

  1. Create a production/live build folder in your source tree and add it to the repository.
  2. Modify our build system to create the live builds in this folder and commit to the repository.
  3. On the live server the site is deployed as a checkout of the live build folder.
  4. Once the build passes unit tests and QA all we need to deploy is to update the working copy on the live server. The big advantage here is that rollbacks etc. are automatically handled because we can always roll back to a previous version. Also you get a nice history of all the updates to the live server.

Programmer Competency Matrix – 32 attributes to evaluate programmers

Having worked with programmers with an extreme variance in skills, I sometimes get the feeling that there is an big lack of good programmers but when I thought about it a little more I realized that it’s not very clear cut, some of the programmers have strong areas and if you confine the tasks into their strong areas then they tend to deliver well. So I started thinking about all the lines on which we can evaluate a programmer, here’s what I have so far…

Programmer Competency Matrix (the table is too big to fit on this blog post and needs a whole page of it’s own)

After having spent a whole afternoon on this I realize that even this is not comprehensive, this matrix is more biased towards non-visual programmers, so a big majority of web devs will not be able to relate well to this matrix, but I am tired and will come back to this at a later time.

An alternative model of computation for concurrency

I recently found this document that I had written for my company newsletter, copied verbatim below.

Concurrency is one of the hot subjects in computer science today. This has partly to do with the fact that processors (1) are reaching their physical limits and thus we need to start looking at new avenues of achieving performance. Herb Sutter the renowned author has written an excellent article (2) named “The Free Lunch Is Over – A Fundamental Turn Toward Concurrency in Software” which captures very beautifully why concurrency is going to be important in the years ahead.

Let us look at a few issues surrounding concurrency today and also look at an alternative model of computation that solves these problems.

The most prevalent model of computation in both hardware and software today is the Von Neumann architecture (3) which is an implementation of the Turing (4) machine which in turn is based on the work of Alan Turing(5). Programs written in this model have a shared memory area also known as the store which can be to store data. The store is divided into chunks called cells and named cells are called variables.

A function written in the Neumann model can make use of the store to aid in its computation, thus any time a function uses data other than those provided as parameters to the function, it is in fact using the store.

The alternative model of computation we’ll look at is called the functional model. The functional model is based on the work of Alonzo Church (6) called Lambda Calculus(7), who came up with this model at approximately the same time as Alan Turing. The good thing though is that both models of computation are equivalent i.e. any computation that can be expressed in one model can also be expressed in the other.

The functional model is based on mathematics, in this model there is no store and all computation is done by evaluating mathematical functions. A mathematical function is one in which the value of a function is totally dependent on the values of the parameters and not on any external state. Also the term variable in this model refers to a named value and not to a named cell.

Consider a simple function
f(x) = g(x) + h(x)

The function f takes x as a parameters and returns the result of evaluating function g with parameter x and adding it to the result of evaluating the function h with parameter x.

In Neumann style programs it is possible that f will return a different output for the same value of x, where as in functional style programs it is guaranteed that no matter how many times f is called with a particular value of x, the output will always be the same. This is because functional programs do their computations without the use of any external state whereas the computations in Neumann style programs are affected by the state of the store.

Let us understand this with a concrete example
int y = 2;
foo(x)
{
return x *y;
}

bar(x)
{
int y = 2;
return x * y;
}

The function foo uses a variable y from the free store to do its computation, thus if some other function were to alter the value of the cell y, foo would start returning different results.

On the other hand bar does not use any external state and for any value of x it will always return twice of x.

It is possible to write functional programs in Neumann languages by totally avoiding the use of store and modeling the entire computation using only functions that use local variables. On the other hand functional languages do not have the concept of a shared store so writing Neumann style programs in them is not possible.

Now let’s take a look at how these computation models interact with concurrency. There are two ways parallelize programs, implicitly and explicitly.

In implicit parallelization, the compiler does the hard work of looking at the program and deciding what instruction sequences can be parallelized. This is the most commonly used method of parallelization and in fact most of the programmers are not even aware that the compiler does this under the hood.

Neumann style programs are hard to optimize for parallelization since functions can be implicitly dependent on each other via the store. For e.g.
baz(x)
{
foo(x);
return bar(x);
}

int y = 0;
foo(x)
{
y = 2 * x;
}

bar(x)
{
return y * 6;
}
In this case foo and bar need to execute sequentially because they make use of the cell ‘y’ from the store. This is an extremely simple example but hidden dependencies like this can get incredibly complicated to figure out. Thus the compiler has to be conservative about what it can parallelize and only parallelize code that it absolutely knows is safe to do so which unfortunately is not a lot.

On the other hand since functions written in a functional language are not dependent on external state they can be very easily parallelized by evaluating different functions in parallel.
For e.g.
baz(x)
{
foo(x);
bar(x)
combo(foo(x));
}
Since foo, bar and combo are purely mathematical functions, the evaluation of baz can be parallelized by evaluating foo and bar in parallel and then evaluating combo, since combo depends on the value from foo.

Now let’s turn our attention to explicit parallelism, where the programmer explicitly programs parallelism. This is done by creating parallel execution sequences called threads that are scheduled by the operating system to execute on the available processors.

In the Neumann style the problem that arises with explicit parallelism is that since all threads share the same common store they need to be careful not to overwrite the data of other threads. The canonical way to handle this has been to but checks around code that accesses shared data to ensure that only a single thread has access to shared data at any point of time. This blocking behavior in turns leads to issues like deadlock where all threads keep waiting for access to a shared location on the store. Further the complexity of this solution increases exponentially with the number of threads, number of shared data areas and the number of places where the shared data is accessed.

A better solution to this problem is actually doing the lock on the memory itself rather than on the code that accesses the memory. Transactional memory systems (8) have started making their way into mainstream computing but they’re still a long way off from being an ideal solution.

In 1978 C.A.R Hoare(9) (best known for inventing QuickSort) wrote a paper titled Communicating Sequential Processes (10), this classic paper introduces a new way to tackle the problem of explicit parallelism, which is in a way similar to the model used by functional programming languages. In this model concurrent processes share state information by passing messages to each other rather than sharing a common store. These messages can be passed asynchronously or synchronously. Thus the whole problem of preventing access to shared data is made non-existent.

There is one language today that combines aspects of functional programming and message passing concurrency to come up as an ideal distributed computing platform. That language is Erlang (11) from Ericsson laboratories. This language and platform came about as the result of research in 1980s to find out which features of computer languages were suited for programming telecommunication systems. In addition to concurrency, this language has some other cool features like error recovery and hot updates that enable any system built using Erlang to keep on running. Joe Armstrong, the brains behind Erlang has written a very good article (12) on all the fuss about Erlang and I would encourage anyone who is interested to take a look at it.

I had originally intended for this article to be an introduction to Erlang but ran out of my 1000 word limit even before I could get past the introduction, thus I decided to pick up the one big feature of Erlang which is concurrency and focus on that. In terms of support for reliability and concurrency, Erlang is unrivaled and I believe that in the years ahead some aspects of Erlang or Erlang itself will become mainstream the way has Ruby has.

References
1. www.cise.ufl.edu/research/revcomp/physlim/PhysLim-CiSE/PhysLim-CiSE-5.doc – Physical limits of computing.
2. http://www.gotw.ca/publications/concurrency-ddj.htm – The free lunch is over.
3. http://en.wikipedia.org/wiki/Von_Neumann_architecture – The Von Neumann architecture.
4. http://en.wikipedia.org/wiki/Turing_machine – Turing machine.
5. http://en.wikipedia.org/wiki/Alan_Turing – Alan Turing
6. http://en.wikipedia.org/wiki/Alonzo_Church – Alonzo Church
7. http://en.wikipedia.org/wiki/Lambda_calculus – Lambda calculus
8. http://en.wikipedia.org/wiki/Transactional_memory – Software Transactional Memory
9. http://en.wikipedia.org/wiki/C._A._R._Hoare – C.A.R. Hoare
10. http://en.wikipedia.org/wiki/Communicating_sequential_processes – Communicating Sequential Processes
11. http://www.erlang.org/ – Erlang
12. http://www.pragmaticprogrammer.com/articles/erlang.html – More about Erlang
13. http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10142 – Concepts, Techniques, and Models of Computer Programming – This is an amazing book that talks about the various models of computation.

Hosting multiple sites on the same IP and port using IIS

Recently I came across an interesting feature which I hadn’t seen before, I used to think that in IIS you can only host one website per IP address and port. But apparently you can host multiple websites on the same IP on IIS as long as the sites have unique domain names. The key to set it up is to use the host header value when setting up a website, the host header value can be specified at the time of setting up the webiste or from the website properties from the “Web Site” tab and then clicking “Advanced” next to the IP Address and then edit the ip settings to bring up a dialog that allows you to specify the host header value.

IIS Hot header value configuration

This way you can setup two independent websites on the same IP and IIS will use the incoming request url domain to decide which website to use to serve the request.