Wednesday, February 19, 2014

C#: Speeding up Dictionary

Level: 2 where 1 is noob and 5 is totally awesome
System: C# on .NET

I like micro optimizing, I find it very funny, relaxing and I learn a lot while doing it. When working on HaveBox, I sometimes discover odd things, which can add performance to HaveBox. Most of the work on HaveBox is micro optimization, which is often expensive and not worth the effort in larger system. But some days ago, when I worked on HaveBox, I did stumble onto something regarding the Dictionary class. It is still micro optimization, but it is so simple and cheap, it might be interesting for other than me.

My Discovery was


When trying to get a value from a key in a Dictionary, the Dictionary class is using a comparer class, to compare the given key with the keys in the dictionary. When instantiating a Dictionary class with the default constructor, The Dictionary class is using the EqualityComparer class as default comparer class. The EqualityComparer class is designed to handle general situations, and is used in other contexts than Dictionary, so it have null checks. These null checks can slow down the Dictionary.

The Trick


As you might have guess, the trick is to write you own comparer, which is amazingly simple. It is inheriting the interface IEqualityComparer, implement the 2 method and then inject it into the Dictionary contructor. Like:

1:  using System;  
2:  using System.Collections.Generic;  
3:  using System.Diagnostics;  
4:    
5:  namespace DictionaryPerformance  
6:  {  
7:    public class Program  
8:    {  
9:      public class DIYCompare : IEqualityComparer<string>  
10:      {  
11:        public bool Equals(string x, string y)  
12:        {  
13:          return x == y;  
14:        }  
15:    
16:        public int GetHashCode(string obj)  
17:        {  
18:          return obj.GetHashCode();  
19:        }  
20:      }  
21:    
22:      static void Main(string[] args)  
23:      {  
24:        IDictionary<string, string> strings = new Dictionary<string, string>();  
25:        IDictionary<string, string> stringsWithDIYCompare = new Dictionary<string, string>(new DIYCompare());  
26:    
27:        for(var i = 0; i < 1000; i++)  
28:        {  
29:          strings.Add("Ipsum " + i, "Not important");  
30:          stringsWithDIYCompare.Add("Ipsum " + i, "Not important");  
31:        }  
32:    
33:        var stopwatch = new Stopwatch();  
34:    
35:        Console.WriteLine("@Intel I5-3337");  
36:    
37:        string tempStr = "";  
38:    
39:        stopwatch.Restart();  
40:        for (var i = 0; i < 1000000; i++)  
41:        {  
42:          strings.TryGetValue("Ipsum 999", out tempStr);  
43:        }  
44:        stopwatch.Stop();  
45:    
46:        Console.WriteLine("Fetched 1 of 1000 elements 1.000.000 times with default comparer and TryGet : {0} ms", stopwatch.ElapsedMilliseconds);  
47:    
48:        stopwatch.Restart();  
49:        for (var i = 0; i < 1000000; i++)  
50:        {  
51:          stringsWithDIYCompare.TryGetValue("Ipsum 999", out tempStr);  
52:        }  
53:        stopwatch.Stop();  
54:    
55:        Console.WriteLine("Fetched 1 of 1000 elements 1.000.000 times with DIY comparer and TryGet : {0} ms", stopwatch.ElapsedMilliseconds);  
56:      }  
57:    }  
58:  }  
59:    

The Numbers


 @Intel I5-3337  
 Fetched 1 of 1000 elements 1.000.000 times with default comparer and TryGet : 56  
  ms  
 Fetched 1 of 1000 elements 1.000.000 times with DIY comparer and TryGet : 49 ms  
 Press any key to continue . . .  

That's all.

Friday, February 14, 2014

Introducing HaveBoxStates. The .NET framework for better flows in your code

Level: 3 where 1 is noob and 5 is totally awesome
System: C# on .NET

Does this looks familiar?
You sketch a program, e.g. a games, like this:


And the implementation ended up, looking like this:


Often it is quite easy to draw a flow of a program or a part of it, but expressing it in code turns out to be hard. This is here where HaveBoxStates comes in. The purpose with HaveBoxStates, is to make it easy to express flows in code, just as we draw them,

HaveBoxStates 

HaveBoxStates is a statemachine/flow engine, to express flows in code. It is very flex and lightweight, and very easy to use. People who has worked with state machines before, might find HaveBoxStates a bit unusual, because the state machine/flow is not defined/configured before use. Even thou it doesn't have strictness predefined in a model, it still as strict. Trying to enter an invalid state would blow up the machine, just as if it was predefined in a model.

Best pratices 

The most important thing I can come up with is, you should always constructor dependency inject your logic into the states. HaveBoxStates comes with HaveBox out of the box, but through a ContainerAdaptor, you can use any container.

How to use it

The following examples are taken from the concept app. So if something needs to be elaborated, you can get the full picture there. Questions in comments is also okay :-). Notice the dependency injection in the PlayGame state.

Here is how to use it:

You draw a flow. It could be this flow:


For each state, you creates a class like this(in separated files, of course):


 using GameStateMachineConcept.Dtos;  
 using GameStateMachineConcept.Dtos;  
 using GameStateMachineConcept.GameEngine;  
 using HaveBoxStates;  
   
 public class Init : IState  
 {  
      public void ExecuteState(IStateMachine statemachine)  
      {  
           statemachine.SetNextStateTo<PlayGame>(new PlayerContext{ Points = 0, Lives = 3, });  
      }  
 }  
   
   
 public class PlayGame : IState  
 {  
      private IGameEngine _gameEngine;  
   
      public PlayGame(IGameEngine gameEngine)  
      {  
           _gameEngine = gameEngine;  
      }  
   
      public void ExecuteState(IStateMachine statemachine, PlayerContext playerContext)  
      {  
           var gameContext = new GameContext { PlayerContext = playerContext, GainedPoints = 0, };  
   
           _gameEngine.Play(gameContext);  
   
           statemachine.SetNextStateTo<Scores>(gameContext);  
      }  
 }  
   
   
 public class Scores : IState  
 {  
      public void ExecuteState(IStateMachine statemachine, GameContext GameContext)  
      {  
           GameContext.PlayerContext.Points += GameContext.GainedPoints;  
   
           if (GameContext.PlayerContext.Lives == 0)  
           {  
                statemachine.SetNextStateTo<Stop>();  
           }  
           else  
           {  
                statemachine.SetNextStateTo<PlayGame>(GameContext.PlayerContext);  
           }  
      }  
 }  
   

Each state must implement an ExecuteState of one of the following signatures:

public void ExecuteState(IStateMachine statemachine)

or

public void ExecuteState(IStateMachine statemachine, <any type> <any name>)

<any type> can be any reference type and <any name> can be any name. The second parameter, is for  DTOs and transferring data between the states. HaveBoxStates automatically cast data to right type, before calling the ExecuteState method.  You just have to give the type of the DTO, forget about casting and just use it.

Because the DTO type can be of any type, and we are not using generics. IState do not enforce implementing of the ExecuteStates, so you have to remember it or else you will get a runtime exception.

The Stop state

HaveBoxStates have one builtin state. The Stop state. If the state machine needs stop, go to the Stop state.

Wrapping it up


Setting up and starting a state machine is also quite easy. Register all states with their dependencies in a container and then instantiate a state machine with the container. Notice, do not register states to IState.

 using GameStateMachineConcept.GameEngine;  
 using GameStateMachineConcept.States;  
 using HaveBox;  
 using HaveBoxStates;  
 using HaveBoxStates.ContainerAdaptors;  
 using System;  
 using System.Collections.Generic;  
   
 namespace GameStateMachineConcept  
 {  
   public class Program  
   {  
     static void Main(string[] args)  
     {  
       var container = new Container();  
       container.Configure(config =>  
       {  
         config.For<IGameEngine>().Use<GameEngine.GameEngine>();  
         config.For<Init>().Use<Init>();  
         config.For<PlayGame>().Use<PlayGame>();  
         config.For<Scores>().Use<Scores>();  
       });  
   
       var stateMachine = new StateMachine(new HaveBoxAdaptor(container));  
       stateMachine.StartStateMachinesAtState<Init>();  
     }  
   }  
 }  
   

That is all :-)

Monday, February 10, 2014

C#: Count() or Length?


Level: 4 where 1 is noob and 5 is totally awesome
System: C# on .NET

Intro


When working with some data structures such as Arrays, strings or alike in C#, we often have 2 choices if we want to know the count of elements in the structure. For most I guess the choice might by easy, people are used to use Count() from LINQ or other structures, so Count() it often is. Length might feel like as, it is just something which pops up, when you scroll the intellisense. You see it, you know what it does, but you prefer Count(), because it makes more sense prosa wise in you code.

Are there any arguments for using Length? Yes.

Performance wise, Length is superior to Count(). Under Count() is some evaluation logic, to count the elements of the structure. Under Length is a pure IL mnemonic, meaning there is directly CLR support for Length.

An Example


I have a reduced IL example, to show what happens IL wise when Length/Count() in C# is compiled:

 .method private hidebysig static void Main(string[] args) cil managed  
 {  
      ...  
      ...  
      ...  
   
      // We initialize 3 variables  
  [0] string[] strings,     // The string array  
  [2] int32 length,         // The length variable  
  [3] int32 count,          // The count variable  
             
      ...  
      ...  
      ...  
   
      // Getting array element count by Length  
  IL_0020: ldloc.0          // puts the string array on the stack  
  IL_0021: ldlen            // using the IL mnemonic for to get length for the array. An unsigned int is returned  
  IL_0022: conv.i4          // The Length variable is a signed int so we convert the output from ldlen  
  IL_0023: stloc.2          // Stores the value, in the variable Length  
   
      ...  
      ...  
      ...  
   
      // Getting array element count by Count()  
  IL_005f: ldloc.0          // puts the string array on the stack  
                            // Next, calling Count() and gets a signed int.  
  IL_0060: call    int32 [System.Core]System.Linq.Enumerable::Count<string>(class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0>)  
  IL_0065: stloc.3          // Stores the value, in the variable count  
      ...  
      ...  
      ...  
    
 } // end of method Program::Main  
   


Performance

To show the difference in performance, I will use the following example:


 using System;  
 using System.Diagnostics;  
 using System.Linq;  
   
 namespace ArrayCount  
 {  
   public class Program  
   {  
     static void Main(string[] args)  
     {  
       var strings = new string[1000];  
       var stopwatch = new Stopwatch();  
       var length = 0;  
       var count = 0;  
   
       Console.WriteLine("@Intel I5-3337");  
   
       stopwatch.Start();  
       for (var i = 0; i < 1000000; i++)  
       {  
         length = strings.Length;  
       }  
       stopwatch.Stop();  
   
       Console.WriteLine("Counted {0} elements by using Length : {1} ms", length, stopwatch.ElapsedMilliseconds);  
   
       stopwatch.Restart();  
       for (var i = 0; i < 1000000; i++)  
       {  
         count = strings.Count();  
       }  
       stopwatch.Stop();  
   
       Console.WriteLine("Counted {0} elements by using Count() : {1} ms", count, stopwatch.ElapsedMilliseconds);  
     }  
   }  
 }  
   

When compiling for release and running it without debugger (shift+f5) the output is:

 @Intel I5-3337  
 Counted 1000 elements by using Length : 0 ms <--- Less than millisecond, not no time:-) 
 Counted 1000 elements by using Count() : 66 ms  
 Press any key to continue . . .  

Conclusion

Now you know the difference between Length and Count(). When performance is preferred Length should always be used, but in general it depends on the context. 


Thursday, February 6, 2014

Specification by Small Talk

Level: 4 where 1 is noob and 5 is totally awesome
Disclaimer: If you use anything from any of my blog entries, it is on your own responsibility.

Intro


This a term/method I have coined, based on some good experiences I have had. Just to rule out any misunderstandings, this has nothing to do with the programming language SmallTalk. It is hardly new, and I guess most of you do it to some degree. It is not supposed to replace any specification methods. It is a suggestion for a preamble for whatever specification method is used. It is quite simple, because it is just informel communication.

Motivation


We have come far with different specifications techniques, but how the techniques are executed, sometimes troubles me. Most a companies, often have a pipeline with tasks/projects. The tasks or projects is prioritised after importance, decided on some meeting. When a task or project comes to an end, it usually releases some human resources, which has to be allocated for a new task or projects. Usually a meeting or a workshop is arranged for specifying the task or project.

This is the point which troubles me, and here is why. Without any preamble, we are ironically using a waterfall alike model for specifying. It is like, meet up with you thoughts, specify within this period, see you again at next specify sprint. I know, this is very roughly, but it is for emphasising my point. The thing is, at the first meetings, people mindset and understanding, are not aligned. Often the best responses to input, comes when the input has circled in the mind for couple of days, but by then the specification meeting or workshop is done. Another issues is also, do we have the right people at the meeting? And most importantly, is this task or project in reality the most important, or should another project have the priority? We simply don't have the best start.

So what am I suggesting?


As mention before it is quite simple, it is just talking. The concept is about to find values, share thoughts maybe discuss something technical in a relaxed way, and in the end have an aligned mindset for a specification workshop. The process can run from an idea is born, to when there are resources to bring it to live. Regardless of the work pipeline. The point is, it has to be informal. The idea can be discussed on small informal meetings, maybe by mails or at lunch. Nothing must be settled or promised. The most important part is, involved people should have the time to think about it.

The process starts by someone wants something. As an example, lets say a customers gets an idea for a new feature in the system. He takes contacts his contact from the system supplier company, and tells about the idea. The contact person talk with a coworker about it. Maybe they invite the customer to come by to hear more about it. The co-worker mention the idea at lunch, and then finds out that another coworker, knows a lot about the domain of the idea. Thereby the best person for the job is chosen, instead if the available person at the time. The point is to let idea spread like rings in water through out the company/department. 

Another gain. Maybe the idea is has more value than first anticipated. Maybe it has so much value, that the pipeline has to be reorganised. Projects without values, could even be avoided, by this early evaluation. Value is always best discovered sooner than later. 

Maybe involved developers can test or try things, to improve the idea or give it the best solution. When running a time boxed project, developers do not have much time to do so.

It all sounds good, are there any pitfalls?


Oh Yes. The biggest pitfall is to settle on a solution, so the specification workshop is all about that solution. Some specification techniques, like specification by example, is about focusing on problems, instead of the solutions. In my book, it is the way to specify.

Summary

Specification by small talk, is preparing a project for specification workshop. Less introduction and more focus. It is also for an early evaluation, and preparing for developing. It is a interesting concept I'll explore some more.

Friday, January 24, 2014

The SQL NULL paradox

Level: 1 where 1 is noob and 5 is totally awesome
System: MS SQL Server or PostgreSQL

The Paradox 


One of the best things about computing, it is so logical. We can always trust in 1 + 1 = 2, 1 < 2 or 1 = 1. So now I'm going to throw 4 queries and their results, as an example:

SELECT CASE WHEN 42 > 42 THEN 'True' ELSE 'False' END; -- Result: False
SELECT CASE WHEN 42 < 42 THEN 'True' ELSE 'False' END; -- Result: False
SELECT CASE WHEN 42 != 42 THEN 'True' ELSE 'False' END;-- Result: False
SELECT CASE WHEN 42 = 42 THEN 'True' ELSE 'False' END; -- Result: True

Hopefully no surprise there. Now I'm going to throw 4 queries more, and their result:

SELECT CASE WHEN NULL > NULL THEN 'True' ELSE 'False' END; -- Result: False
SELECT CASE WHEN NULL < NULL THEN 'True' ELSE 'False' END; -- Result: False
SELECT CASE WHEN NULL != NULL THEN 'True' ELSE 'False' END;-- Result: False
SELECT CASE WHEN NULL = NULL THEN 'True' ELSE 'False' END; -- Result: False

The three first lines, seems logical, but wait a minute:-). How is this possible? If NULL != NULL then NULL = NULL  must be true, else it is illogical. Or is it?

The Explanation


Actually it is not paradox. The reason, some might expect NULL = NULL to be true, is because they misunderstand NULL. They mistake NULL for being a value. If NULL was a value, then NULL = NULL would be true. So if it is not a value, what is it then?

The ANSI SQL-92 Specification says about NULL:
"null value (null): A special value, or mark, that is used to indicate the absence of any data value."   
I can understand the confusion, because have just written, it is not a value. But you have to notice it says "A special value". Absence of data value, is the right way to see NULL,  unknown or missing.

To be able to handle NULL values defined in the SQL-92 specs. Databases servers such as SQL Server and PostgreSQL uses trinary logic and not binary logic. It means, logic operations can have 3 out comes: True, False or NULL.

Because NULL is considered to be unknown, any comparison to NULL, even NULL vs. NULL will have False as result, because it is unknown what we compares. While using any arithmetic operator with NULL, will have NULL as result. The latter makes good sense. Lets say you add something to unknown, then result is unknown.

So in the end, it's all logical. For more read Handling Null Values for SQL Server, or 9.2. Comparison Operators for PostgreSQL. Even better read them both:-)

Wednesday, January 22, 2014

Data Modelling with Immutability

Level: 3 where 1 is noob and 5 is totally awesome
Disclaimer: If you use anything from any of my blog entries, it is on your own responsibility.

Intro


Data modelling with immutability, is a thing I have been thinking about for a while now, and if I had green field project I would think it in, from start. The concept is simple, instead of change states on objects, we create new ones(Well, sometimes it makes more sense to use mutable entities, but I'll get back on that). It is a concept, which makes it possible to create better data models, which resembles the things, we want to model more precisely.

Through out this blog post, I'll use the following Person-Address model, when clarifying the concept. The model is simple. It resembles a person and an address for this person.

The Concept


The conception is as mention before; instead of change the state of entities in the data model, we create a new ones. As an example, let us look, at our Person-Address model. Lets say we have a Person, who decides to move, which is the same as change address. A way to model this change of address, could be updating the address entity with the new address. But, not only from a modelling perspective is this wrong, but it can also give some technical challenges later on. Such querying the person former addressed, can be difficult.

The right thing to do, is to somehow mark the old address entity obsolete(but do not delete), create a new address entity, with the new address, and connect it to the person as the current address. Why is this right? It is right, because when a person is moving, they a moving to a new location/address. The new address has another house, while the old house is still standing at the old address. So by modelling address a immutable, we have the old address and the new address, just like in the real world. Actually, if the address entity was updated with a new address, it would be the same as saying, we are  dumping a new address and a house, on the old address. Not really possible in reality, eh?

The tricky part of modelling with immutability, is identifying what should be immutable and what should be mutable. Lets take a look at the Person-Address model again. Person. No matter the name of a person, the person would ideally always be the same. Thereby the person is mutable. So if a person changes name, the person entity should be updated. The entity should not be replaced, like in the in address example. If the entity was replaced, it would be same as saying it is a new person. So thing which remains the same, no matter the state, should be mutable.

Versioning and event sourcing

First a quick definition, of versioning and event sourcing. Event sourcing, is storing the event which caused an entity to change. A version is the result of an event.

Some might think, by using immutable entities in a model, we would get event sourcing, as an positive side-effect. Well, if we have to be absolute correctly, it wouldn't be event sourcing. Because we are not storing an event, we ares storing a result of an event a.k.a. a version. We are also only getting versioning for the immutable entities, because we are creating a new entity for every change. Mutable entities state changes should, like immutable entities events, be event sourced.

By modelling this way, you should always have the most true data model. As positive side effect, you should always be able, to query versions and events for every entity in your model. Happy modelling.

Friday, January 17, 2014

T-SQL: Comma Separating Values into Fields

Level: 2 where 1 is noob and 5 is totally awesome
System: SQL Server from 2005 and up


Visualising data


When querying data from databases, I'm always trying to get resultsets which are most presentable as possible. It save me a lot of time, because I then don't need to transform data further. The upsides are, I can either save a resultset as csv or just copy paste it to some spreadsheet or to a mail, right after the querying. The down side is, I have to type a bit more SQL.

In this blog post, I'll present one of my favorites methods, to easily visualize data in a smart way. I'm going to explain the method, using these 2 tables:

Items:
Id          Name                                               ColorId
----------- -------------------------------------------------- -----------
1           Cranberries                                        1
2           Plants                                             2
3           Shrimps                                            1
4           Ocean                                              3
5           Sky                                                3
6           Roses                                              1  

And

Colors:
Id          Color
----------- --------------------------------------------------
1           Red
2           Green
3           Blue

The most used way to show, the color for each item would be to join the tables like:

 SELECT Items.Id, Items.Name, Color FROM Items  
 INNER JOIN Colors ON Items.ColorId = Colors.Id  

Which gives:

Name                                               Color
-------------------------------------------------- -----------------
Cranberries                                        Red
Plants                                             Green
Shrimps                                            Red
Ocean                                              Blue
Sky                                                Blue
Roses                                              Red

It is readable, and if we sort by color it might be more readable. It might be most readable, if we could group the items by color. It requires a bit more SQL; but the result is better:

 SELECT   
  STUFF(  
      (SELECT ', ' + Name  
      FROM Items  
      where Items.ColorId = Colors.Id  
      FOR XML PATH (''))  
      , 1, 1, '') AS Items,  
 Color  
 FROM Colors;  

Gives:

Items                                Color
------------------------------------ ------------------------------------------
Cranberries, Shrimps, Roses          Red
Plants                               Green
Ocean, Sky                           Blue

Explanation


The magic happens in the inner SQL. FOR XML PATH is used as a smart way for concatenating string columns to one string column. By adding the ',' to Name, we are getting a comma separated list, where the first char is ','. For more about FOR XML PATH see: http://technet.microsoft.com/en-us/library/ms189885.aspx

STUFF are used to remove the first char(in this case ','), from the comma separated lists. Stuff is a sql command for inserting strings into strings, but is can also remove chars. In this case we are overwriting first char with a empty string. For more about STUFF see: http://technet.microsoft.com/en-us/library/ms188043.aspx

The rest should be self explanatory :-)