String.GetHashCode() Internals

While I was eating dinner, I had this somehow suprising question in my mind: How is String.GetHashCode() implemented? Is it fast?

It needs a linear time based on the length of the string. This is understandable if you think about what it is doing. It needs to generate a “Fingerprint”-number based on its content, so you need to take a look at all the content at least once. So it is O(N).

I was going to do something more useful while I noticed all the precompiler conditions. That is very interesting! The function returns different results whether it is compiled for 32bit or 64bit. The debug version of the framework (Microsoft internal only?) even changes its resulting hashcodes with every buildnumber.

We want to ensure we can change our hash function daily.
This is perfectly fine as long as you don’t persist the
value from GetHashCode to disk or count on String A
hashing before string B.  Those are bugs in your code.

So you better avoid storing hashcodes and you should not exchange hashcodes across process-boundarys. Your communication partner may use a different number of bits!

Last but not least: the result is not stored but recomputed every time you call the function. The implementation is following the advice to not store its result itself.

Posted in .net, C#, coding | Leave a comment

Youtube wieder schneller im Firefox

While I had decided to write new articles only in English, the following article has a pure German audience so I figured out to make an exception for this article.

Wer wie ich seinen Internetanschluss bei der Telekom hat, dem kann nicht entgangen sein, dass Youtube Videos extrem langsam sind und man manchmal außer der drehenden Ladeanimation nichts zu sehen bekommt. Es war zuletzt wirklich sehr schlimm.

Es gab bis vor kurzem den Telekom Youtube Turbo, der zwar funktionierte, dies aber sehr geheimnisvoll gemacht hat. Es gibt keine Einstellungen und auch keine Beschreibungen. Er ist auch für den Firefox 8 nicht verfügbar.

Frustriert habe ich mich umgeschaut und gelesen, dass der Telekom-Proxy wohl schneller ist. Vermutlich hat auch der Telekom Youtube Turbe den verwendet. Ich wollte aber nicht, dass alle meine Seitenaufrufe durch den Proxy laufen, daher habe ich mir FoxyProxy besorgt, was viele Optionen erlaubt. Die Einrichtung geht in wenigen einfachen Schritten:

1. Das AddOn FoxyProxy in den Firefox installieren und den Firefox neu starten.

2. In den Optionen von FoxyProxy einen neuen Proxy anlegen* und ihn auf den Telekom-Proxy (www-proxy.t-online.de mit Port 80) einstellen.

Proxydetails einstellen

3. Bei den Url-Mustern noch ein Muster für Youtube hinterlegen:

Url-Muster für Youtube hinterlegen

4. Der FoxyProxy-Modus sollte “Verwende Proxies entsprechend ihrer konfigurierten Muster und Prioritäten” sein. Das sollte bei euch dann alles so aussehen:

Verwende Proxies entsprechend ihrer konfigurierten Muster und Prioritäten

Und nun wieder viel Spaß auf Youtube!

 

*Es gibt Bonuspunkte, wenn man den Proxy Magenta färbt Winking smile

Posted in Allgemein | Leave a comment

Dependency injection & their Pitfalls*

After I had my dependency injection set up and running, I noticed a shift in my way of developing. I started to be more focused on only one class and their implementation at a time. If I felt the need for another service instance, I just added it to the constructor and would go on coding. I thought that the dependency injection container would take care of how to create the new service for me.

image

This is nothing fancy so far. After coding for straight 20 minutes without any testing in between I decided to test it. I noticed my machine was doing really nothing but burning cpu for a bunch of seconds and quitting then with a StackoverflowException. I had no idea what was going on.

The exception was not really telling too much about the problem. The only thing was a truncated stacktrace. It had something to do with the container trying to create an instance of a service. Without any further information it would be hard to pinpoint this so I decided to search for trace information. I am using Microsoft Prism with the Unity container for inversion of control. So does Unity provide any traces of what is going on? Surprisingly, this is not the case! There is no magic switch.

So I had no choice but removing constructor arguments here and there until I found the root problem. I had managed to create a circular reference:

image

The MSDN is telling me, that it is my responsibility to prevent this and it also warns of endless recursion. Endless recursion always ends with an StackoverflowException.

My solution was simple. I removed the circular reference through removing one reference. It was not used anyway.

*The name of this article is freely leaned on the name of the blog EAI Technologies & their Pitfalls of an ex coworker Winking smile

Posted in C#, coding, dependency injection, Prism | Leave a comment