Thursday, 27 October 2011

64 bits

I used to be a hardware freak back in time, with some good knowledge of processor architectures and so on. As time went on I preferred to focus just on programming, and at the same time, programming has been going higher and higher level to such extent that for most programming tasks coders do not need to be much aware of the details of the hardware on which their software runs.

That said, when this week we experienced some problems with an Asp.Net MVC application running on a 64 bits server failing to load a 32 bits COM component, I felt (as the rest of the team) a bit lost. Hopefully, this issue has turned into a rather fast and interesting learning experience.

First of all, some definitions:
  • IA32, x86-32, i386 or just x86 refer to the 32 bits architecture that was common on most computers until 3 years ago. Intel, AMD...
  • x86-64 is the 64 bits evolution of the former architecture. AMD, Intel...
  • IA64 has nothing with the above, it was a completely new 64 bits architecture developed by intel and that had little success.
  • WOW64 is the technology used by MS to run 32 bits applications on a 64 bit system (Operating System and Processor).
and some tips:
  • When on a 64 bits system, we can check whether a given Native process is 32 or 64 bits by launching ProcessExplorer and checking whether it has the wow64.dll loaded
  • We can check if a .dll is 32 or 64 bits by means of the binary dumper that comes with Visual Studio. Open the Visual Studio Command Prompt and type:
    dumpbin /headers myDll.dll |find "machine"
    you'll get: 14C machine (x32) 8664 machine (x64) My first idea was to check the 32-64 bits using the almighty PEBrowser, but oddly enough I couldn't find where to check this.
  • We can check under what option (x86, x64, Any CPU...) (more on this 2 paragraphs below) an assembly was compiled by using the corflags tool provided with the SDK

One of the first things I found is that you can't load a 32 bits dll in a 64 bits process. WOW64 only works at a whole process level, but not for independent dlls. I mean, if you're running a 64 bits process and you try to load a 32 bits dll there (or COM object) WOW64 won't get into play, you either compile the process to 32 bits or the dll to 64 bits, of course, if you don't have the source code, you're fucked.

Another interesting point, I used to think of .Net assemblies as unrelated to the processor details, just a bunch of bytecodes that get compiled to 32 or 64 native instructions by the JIT compiler, just as would happen with Python, Javascript or so... This is not completely true, and we get a hint if we notice that the CSC compiler allows us to compile our assemblies as x86, x64 or Any CPU... As explained here what those actions do is to indicate the JIT compiler how it should compile our bytecodes, 32 bits, 64 bits or "as the process on which I'm being loaded". The reason why we could need this level of control is because .Net allows us to go rather low level using unsafe code and calling into Native Code (PInvoke...).

That said, if you intend to use a 32 bits native dll from your .Net application, you'll need to compile your app with the x86 option. That means that if running on a 64 bits system the WOW64 system will be loaded and everything should be fine.

When working with Asp.Net there's something else you need to take into account. Your Web application (a bunch of dll's, not a .exe) gest hosted by the w3wp.exe process spawned by IIS. There's a w3wp.exe process for each IIS Application Pool (this means each application pool is independent from each other, thanks to that we can run Asp.Net 2, Asp.Net 4... apps on the same machine, they just need to run on different App Pools so that each pool loads the corresponding Framework version). By default, w3wp.exe is launched as a 64 bits process, so, how do I load my 32 bits assemblies + Native dll there?

Hopefully, the solution is damn easy, we just need to mark the Application Pool where we plan to load our Web Application as a 32 bits (IIS -> Advanced Properties of the Application Pool -> set Enable 32 bits applications to true)

Friday, 21 October 2011

Murder of a brave man

Well, I guess everyone knows the big news of the day, colonel Gaddafi has been murdered today by the NATO-paramilitar "rebel" forces joint venture... I don't intend to praise Gaddafi as a "good guy", as in the 70's he funded a good bunch of armed organizations that committed all sort of violent acts in the Western world. Some of these organizations had a left wing revolutionary ideology and meant to create a new and better society, but were pretty confused as to how to spread marxism and liberate the "working class", others were just a gang of criminals, islamists and other crap...

I'm not Lybian and I've never been to Lybia (and I guess I'll never be, as I have serious doubts that after the NATO bombs have taken the country back to the stone age... it willl ever be a place worth a visit...), but I think to know enought about recent history and about how our current world drowns in NeoLiberal poison to firmly believe that Lybia is much worse now than it was 1 year ago. I already expressed my views about the invasion of Lybia in this previous post, now, not only do I reaffirm those opinions, but even more, I dare to claim that Lybia will never reach again the same level of development (high quality public education system, health system, per capita rent) that it enjoyed under Gaddafi's dictatorship. The new regime, consisting of Islamists and greedy puppets of the Multinationals, will bring no good to Lybians, that I'm sure will end up remembering the old times when their kids could attend to the University for free and their natural resources were used to develop the country instead of filling foreign pockets...

These 2 videos clearly align with my understanding of the "Lybian conflict":

The images of the "rebels" murdering Gaddaffi and dragging his body is that repulsive as the images of the NATO leaders cellebrating this public execution as a triumph of liberty and Human Rights... fucking hypocrites...

As a final point, whatever one's opinion about Gaddafi is, what I think everyone will have to concede is that Gaddafi has died like a brave man, someone that firmly believed in his role as the leader of his people and that as such, chose to die fighting instead of fleeing to Algeria, Russia or some other country where his money, old friendships and "business secrets" would sure have bought him a secure location under the protection of the local government with the only condition of keeping a low profile.

Sunday, 16 October 2011

When We Leave

Another great German film dealing with the problems of a multicultural society. Being set in Berlin, it's pretty easy to figure out that it will revolve around Turkish in Germany.
What I pretty much like of this film is that it focuses on problems inside a multicultural family, but not a family with people coming from different cultures, but a family which members have developed different cultures. This way, the main character in the film is a westernized German-Turkish twenty something woman with traditional Turkish parents and older brother, and half traditional younger brother and sister.

Our young lady, Umay (the incredibly beautiful Sibel Kekilli) flees with her child from her violent husband in Turkey and returns to her family home in Berlin. Her family does not accept her breaking her marriage and try to force her to return to her husband. When this says that he's no longer interested in "a German whore" they try to take Umay's child and take him to his father. Umay has to escape from her family and keep away from them, this is emotinally devastating for her, but all her attempts of reconciling with her family (that blindly believe that she has brought shame and ruin upon them with her "horrible" action) painfully fail.
The story develops harsher as it slowly gets on, with some really tough moments, but also with some moments where we're drawn to believe that hope still exists.
As I've said, the film is rather slow (contrary to Head-On the film that gained Sibel public attention), but it's deeply intense and powerful, and profoundly beautiful. I think this is in good part thanks to the excellent performance of Sibel, well, it's not just because I consider her terribly attractive, it's because her skinny body and her "Modigliani face" can convey sadness, pain and even happyness in such a convincent manner.

This film is a must, and is one more wake up call on how Europe needs to deal with the topic of integrating not just immigrants, but the second and third generation ones.
Umay is the perfect example of integration, her best friend is a German girl, she starts a relation with a German guy, she picks up her studies with the intent to go to the University... so she's a German woman with Turkish background. On the other hand, the rest of her family seem barely integrated (either because of their obsession with tradition, or for the acceptance of the submissive role of women, or just because they don't seem to relate to non Turkish people in the whole film).

I guess Sibel had to feel really comfortable in her role, as in the real life she's a very solid example of integration, she has even criticized Islam for being inherently violent
I have experienced myself that physical and psychological violence is seen as normal in Muslim families. Unfortunately violence belongs to the culture in Islam."
My hat off to you lady :-)

Sadly enough, the film seems to be inspired by real events. Forced marriage and honour killings in Western Europe in the XXI century... shit, we've done something terribly bad in the last decades...

By the way, I guess Berlin's Tourist Office had nothing to do with this film, cause we have very few scenes where some landmark of the city is shown (you could think that it's set in any other large German city). This said, it's curious to say that I think I know one of the localizations, the place where Sibel gives her little brother a letter for Mum seems like the "50 faces" in Kreuzberg to me (you just see a small graffiti there and it's not a face... but anyway I would bet it's located there).

Update March 2012: from my last trips to Berlin I think I can also confirm that the scene from which I captured the image below is set on the Warschauer Strasse S-Bhan station.

Monday, 10 October 2011

Run Lola run

This German film from 1998 has come as a beautiful surprise to me.
As I'm preparing my next visit to Berlin (I love the Prussian Metropolis, one of those places where regardless of how many times you've been there you ever have one million things to do), I've decided to watch some films set in Berlin, just to whet my appetite even more. Somehow I stumpled upon this film, and the fact of having Franka Potente playing the main role (I absolutely love her performance in Bourne), obviously increased my interest.

What we get here is a vibrating story, where a woman has 20 minutes to obtain a huge amount of money to save his boyfriend's life. She desperately runs through Berlin trying to obtain the money and take it to her boyfriend. The idea sounds good, but what is better is that what I've just told is used as the baseline to develop 3 different stories, where the characters take different decisions and the outcome is devastatingly different. The frantic soundtrack (pretty good electronic sounds) is another plus for this film, and reminds me of the soundtrack of another good film set (partially) in Berlin, Hanna

The film works also as sort of a Berlin tour, which is always good, but don't try to follow on a map the route traced by Franka in her anguished run, cause it makes no sense. She starts in Mitte, and suddenly she's in Kreuzberg crossing the Oberbaumbr├╝cke to Mitte again... so Berlin's geography has been fairly altered.

Well, I think there's not much more I can add, just watch it and enjoy.

Tuesday, 4 October 2011

The aesthetics of your code

For better or for worse I'm someone that can spend awful amounts of time pondering on things that many (if not most) would consider irrelevant. Many of those "wasted?" brain cycles are related to programming issues that I still consider important like:

  • Initialize in the constructor or inline?

  • Should this go to a separate class? (how strictly to adhere to the Single Responsability Principle is a constant source of doubt for me)

  • Add another parameter or create a separate method (think and rethink semantics...)

  • This method should go here or there?

So when finding just by chance the excellent reflection of a StackOverflow folk on the Aesthetics of Programming and its intrinsic individual nature I could not resist to copy-paste it here:
Original source

Look at the things that are important: your project, your code, your job, your personal life. None of them are going to have their success rest on whether or not you use the "this" keyword to qualify access to fields. The this keyword will not help you ship on time. It's not going to reduce bugs, it's not going to have any appreciable effect on code quality or maintainability. It's not going to get you a raise, or allow you to spend less time at the office.

It's really just a style issue. If you like "this", then use it. If you don't, then don't. If you need it to get correct semantics then use it. The truth is, every programmer has his own unique programing style. That style reflects that particular programmer's notions of what the "most aesthetically pleasing code" should look like. By definition, any other programmer who reads your code is going to have a different programing style. That means there is always going to be something you did that the other guy doesn't like, or would have done differently. At some point some guy is going to read your code and grumble about something.

I wouldn't fret over it. I would just make sure the code is as aesthetically pleasing as possible according to your own tastes. If you ask 10 programmers how to format code, you are going to get about 15 different opinions. A better thing to focus on is how the code is factored. Are things abstracted right? Did I pick meaningful names for things? Is there a lot of code duplication? Are there ways I can simplify stuff? Getting those things right, I think, will have the greatest positive impact on your project, your code, your job, and your life. Coincidentally, it will probably also cause the other guy to grumble the least. If your code works, is easy to read, and is well factored, the other guy isn't going to be scrutinizing how you initialize fields. He's just going to use your code, marvel at it's greatness, and then move on to something else.

My 2 cents with respect to the question that sparked such a nice text is that I always use "this". On one side it seems more clear to me, on the other hand, in some brilliant languages like Python or JavaScript(well, the mechanism and semantics are quite different from C# and Java and it's not something optional, you could not do withou it) you have to use it, so what is good in JavaScript is good in the rest of the Coding world :-)

By the way, do not miss this impressive ellaboration by the same author on how to get some sort of Class Loader equivalent in C#

Saturday, 1 October 2011

Last months interesting stuff

Well, it's being a long while since the last of my posts summarizing interesting items that I came across lately, so time for one of those.

As I've been doing tons of JavaScript lately, I found these 2 posts pretty useful. I was aware of most of what is stated there, but anyway it's good to get you assumptions confirmed

jQuery performance rules

JavaScript and Ajax performance

That said, even when I'm pretty interested in any sort of performace trick (in whatever language I'm using) mainly because those things help you understand how things work internally (I've ever been a rather curious person), I tend to favor semantics and clear code over performance (I guess that's the right decision for the kind of applications that I develop, that are far from having any kind of high peformance requirements). Some weeks ago I had one discussion with one colleage and couldn't help throwing this classic sentence: premature optimization is the root of all evil

Thought the most commonly used JSON serializer in the .Net ecosystem (JavaScriptSerializer, the one used by default by Asp.Net MVC) leaves much to be desired, JSON has turned into my preferred serialization format, thought having to declare C# classes for the JavaScript objects that you want to deserialize seems like boring and time consuming, so, with all the dynamic magic in C# 4, couldn't we just deserialize to a dynamic object? Good reading here (JSON to ExpandoObject)

I'm really longing to have some time play a bit with some NoSql stuff (ok, you can call me "fashion victim") mainly CouchDB, so this thing seems pretty promising
UnQL query Language for Document Databases

Even when you think you're rather fluent in one technology, there are always things that you miss, mainly due to some initial decisions that turn into habits. In my case, I've always used classic castings and type comparisons, so I hardly remembered that C# provides the as and is operators
x is T returns true, if the variable x of base class type stores an object of derived class type T, or, if x is of type T. Else returns false.

x as T returns (T)x (x cast to T), if the variable x of base class type stores an object of derived class type T, or, if x is of type T. Else returns null. Equivalent to x is T ? (T)x : null

This excellent article about Asp.Net MVC extensibility, makes me look forward to the next installments in the series.

This good article about a new .Net collection (the Exposing a limited read-only window of a list seems rather useful), took me to this interesting research project made in Danmark, a new set of advanced .Net collections. The events on collection changes thing seems pretty interesting.