Wednesday, 29 December 2010

ValueTypes GetType

Yesterday, thinking a bit again about how .Net types get described through the MethodTable and EEClass combination (check previous entries), I came up with a question, how does this work for structs (well, value types in general)?

.Net Reference types have a 8 bytes overhead, due to the SyncBlock index and the Type Information pointer (a pointer to the Method Table, which in turn points to the EEClass). You can read more here. It's important to indicate that an Object reference points to the 4 bytes where the pointer to the Method Table lies, after that come the DataFields, and before that is the SyncBlock.

[SyncBlock(4 bytes) | pointer to the Method Table(4 bytes) | Data Fields]

So it's easy to assume that Object.GetType() makes use of the info in the Method Table to return the Type object. I've said assume cause it can't be confirmed by Reflector. If we disassemble the code we end up with this:
[MethodImpl(MethodImplOptions.InternalCall), SecuritySafeCritical]
which means that it's implemented internally within the system itself...

However, structs do not have any extra information, they exist just for performance reasons, not for any semantic reason, and were designed as lightweight as possible (read more here)

and there are structs, which are lightweight data types for storage but allow object-like method calls for familiarity and convenience.

So, if we don't have a pointer to the Type information, how does the Runtime determine the type of a value type? As usual, StackOverflow to the rescue (the question is not mine, somebody else had already been tormeted by this doubt).
As someone perfectly explains, it's the compiler who really takes care of it, not the runtime. When calling myValueType.GetType(), myValueType gets boxed, and that boxing operation involves storing onto the stack the Type for myValueType.

Memory layout of .Net objects (from this excellent article)

Value Types:

Reference Types:

Tuesday, 28 December 2010

Convert VS Tests to NUnit

I've got a small (well, not that small really) .Net library with some utilities ready to be included in other projects (StringHelpers, some Reflection and Logging stuff...).
It's been mainly stopped for the last year, but the other day needed to go back to some code that I had there and realized it would be a good idea to update it to .Net 4 (even when the only improvement that I'm aware of being using in my new code is the Generic variance one).

For different reasons... I still don't have Visual Studio 2010 installed, so I'll also switch the development to SharpDevelop 4. That should not be a problem, as for libraries or console applications SharpDevelop has little to envy.

Problem is that my tests for this library are in a Visual Studio Test Project. SharpDevelop can compile it, but it can't run it, so the way to go is to jump to NUnit. You have to manually transform the Tests from one format to another, but thanks to this Article (many thanks man, it's been pretty useful to me) it's pretty simple.

Team System NUnit
using Microsoft.VisualStudio.TestTools.UnitTesting; using NUnit.Framework;
[TestClass] [TestFixture]
[TestMethod] [Test]
[TestInitialize] [SetUp]
[TestCleanup] [TearDown]

It's really amazing that both test systems have an Assert class that seems to have more or less (I haven't checked the source, just my test conditions worked fine without changing anything) the same methods.

So, this is what you need:

  • Install NUnit (it does not come installed by SharpDevelop)

  • Do all the renaming mentioned in the article

  • Now, you can do the GUID replace explained in the article to change a test project to a class library. I didn't need it cause I preferred to keep a copy of the VS Test Project, so I removed it from the Solution and created a new Library project where I duplicated the code files

  • Go to View -> Tools -> UnitTest
    this will open a NUnit Panel. From there you can click the "Add reference to NUnit to the currently selected project". And that's all. Now you can run your tests from inside SharpDevelop or launch the NUnit GUI, open the Assembly containing your tests and run them from there.

I quite like this, it seems like you're a bit more free with the NUnit approach. Now your tests can be run from the NUnit GUI, from SharpDevelop, and even from Visual Studio.

Repeat jQuery animation

As I've said in previous posts, jQuery is a really lovely tool, mainly for DOM manipulation and animation. It makes really simple to add animations to different items, like menus, pop up "pseudo windows" (divs), items to be shown-hidden...

Over the time, some people have started to use it as a replacement for the typical, so frequent (years ago) flash intros or flash banners. There's something missing, though, a way to keep the same animation going over and over (imagine for example the typical matrix like binary cascade).
Well, it's pretty easy to chain animations by adding a callback to jQuery's animate method, so using this functionality to write something that would keep chaining one animation to another over and over turns out to be rather simple.

I've created a simple "class" animationsRepeater that just needs a target jQuery element to be animated, an array of animationInfo's "structs" (each animationInfo is just the same object that jQuery's animate method expects), and the number of times that I want to repeat this sequence of animationInfo's over the target object (-1 means repeat forever). Aside from the obvious run() method, I've added a pause() one.

Usage is that simple as this:

var animRepeater = new animationsRepeater($("#myItem),
[new animationInfo({left: "+=400", opacity: 0.25}, 2000, false),
new animationInfo({left: "-=400", opacity: 1}, 2000, false)],

the code above means, move "#myItem" to the left 400 pixels and change its opacity to 0.25, and do it in 2000 milliseconds. Then, move "#myItem" to the left -400pixels (so, move it to the right) and change its opacity to 1, and do it in 2000 milliseconds. The last -1 means we want to apply these 2 animations indefinitely.

You can view a sample with several squares moving here. The code there creates 5 divs and instantiates a different animationsRepeater for each of them, with slightly different values. Each animation repeater applies a move right and decrease opacity animation first, followed by a move left and increase opacity animation then. This cycle of animation repeats indefinitely.

The animationsRepeater class is here. I think the most (and only) interesting thing there is how what should be a normal loop (do animation from 1 to n times) has been transformed in a timed pseudo-recursive (it's not really recursive cause the timer thing means the previous function call is more than finished when it gets called again) call. _doAnimation calls to jQuery's animate, passing to it as callback parameter the same _doAnimation function, so we can think of the code in _doAnimation as the code that would be inside a loop and the associated loop conditions.

Thursday, 23 December 2010

typeof operator

Following last week's entry about Generics, I was thinking about the usage of C#'s typeof operator with generic types. You can do typeof(string) same as you can do typeof(T), so this brings up the question, how is typeof implemented?

When used with a non generic type, maybe one could think of typeof as an alias for a new Type("type name"). Well, first, this could not work for Generic types, cause these are only known at runtime, not at compile time... and second, the Type class has just a protected parameterless constructor.

So I compiled a few lines of code and launched Reflector on the output, coming up with this:


Type tp = typeof(String);


L_0001: ldtoken string
L_0006: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
L_000b: stloc.0


Type tp = typeof(T);


L_0001: ldtoken !T
L_0006: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
L_000b: stloc.0

so the same approach is used for both cases, involving a struct that was unknown to me, RuntimeTypeHandle, the Type.GetTypeFromHandle method and the ldtoken IL instructions.

Anyway, the most interesting of this is still the compile time/run time difference. For the non generic case, the token to load on top of the stack is already known at compile time, but for the generic case, the real value for T is know at runtime, and as we saw in the previous blog entry, the native code that will be generated for different concrete instantiations of the Generic type will be the same.
I presume the real T token is stored somewhere in the MethodTable (there's one for each instantiation) and taken from there.

Tuesday, 21 December 2010

Fratricidal wars

Months ago I mentioned in a previous entry a photojournalism exhibition held in my town back in 1997 and that had a great impact on me, "Guerras Fraticidas" (Fratricidal Wars).

This exhibition was organized by "la Caixa" foundation which also sponsors other itinerant exhibitions (I can remember a few of them like: the origin of talk in humans, science in Al Andalus, the first human settlements...) that every year go over Spain to try to raise the interest of the population of a land were culture and science seem to have been excluded from normal life for decades (sports, gossip, Big Brother crap, bull fighting... that seems to be what TV channels and city councils understand as "culture for the people").

You could find there pictures and texts regarding the most cruels of all wars, those wars were neighbors kill neighbors, families get divided between opposite sides, and chance for revenge and profit are more present than ever... civil wars. Ulster, Cyprus (until then I was unaware that the island was split between the Turkish north and the Greek south, Yugoslav wars, Lebanon... were some of the tragedies depicted there.

Yesterday, while trying to do some clean up at home to make room for new gadgets, I found the leaflet of the exhibition, and I thought it deserved to be "scanned" (indeed, I don't have a scanner, so just some blurry pics) and shared here. It's a pity that it's not in English.


leaflet front

content 1:

leaflet content 1

content 2:

leaflet content 2


leaflet back

Thursday, 16 December 2010

C# vs Java generics

For some dark, hidden reason I ended up reading the wikipedia article about Generics, and to my great surprise I found out that there are huge differences between Java generics and C# generics, and as usual :-) (I'm joking, I don't like language wars and don't know enough about java, VMs, compilers, GCs... to thoroughly evaluate them) C#/.Net win.

The difference is not in usage (that is very similar in both languages), but in implementation. I already knew how Generics are implemented in C# , and I thought this was the usual approach (and is called reification), so it was odd to see that Java is using a completely different and poor approach that is little more than a compiler artifact and syntactic sugar. JVM is absolutely unaware of Generics, while the .Net IL was extended with new instructions supporting Generics (so also the JIT was updated), this seems in line with Java's conservative approach (the language is evolving so slooooooooow... thought of course that's not the same with all the wonderful Frameworks around it) and C# very fast paced evolution.
These differences are pretty well explained in this thorough article and in this StackOverflow discussion, so it does not pay off that I try to badly sum up them here, just click the links and read... This articles led me also to read about an interesting topic that I had not touched again since Generics were announced for C# 2, code bloating-explosion. Basically the same Generic type of different reference types will share the same Jitted code, which is pretty natural and good!

Well, anyway I can't help to write a bit more here to make this entry a bit longer... So, if we play around a bit with Reflector, we can see that when we declare a Generic Type in .Net, let's say MyCollection%lt;T> the Generated IL has just a MyCollection<T> class. For each instantiation of that Generic type using different type parameters (let's say MyCollection<string>, MyCollection<Person>) we can see that the variables, properties or whatever are typed as a new Concrete Type, MyCollection`1<string> and MyCollection`1<Person>. This means, that at Runtime (not at compile time) the JIT will create those new Concrete Types. If we do a myInstace.GetType() of any instance of a Generic Type we can see that their Type is not MyCollection<T>, but MyCollection`1<string>, MyCollection`1<Person>...

I don't have a clear idea of the memory layout of .Net objects (MethodTables, InterfaceTables...) but from some Windbg + SOS.dll debugging and some reading like this I understand that for each defined Type .Net creates an EEClass structure (containing all the information about a Type) and a MethodTable (with pointers to the code itself, IL or native code once compiled). MSDN explains it fine here:

In fact, EEClass and MethodTable are logically one data structure (together they represent a single type)

So it seems like for each Concrete Type instantiated from a Generic Type (to me a Generic Type is just a Template for creating Concrete Types) an EEClass (check the update below, the same EEClass is shared for Concrete types created from reference types) and a MethodTable gets created, but as explained in one the the links above, when these Concrete Types are created from reference type arguments, the Native code generated for those methods is shared, avoiding code bloating.

There's another point of interest with regards to Generics, and more now that C# is still rather fresh, Variance (you know, the Covariant and Contravariant generic type parameters thing. Again, there are interesting differences (use site variance vs declaration site variance) between Java and C# discussed here.

I should not end this entry so full of external links without adding another great piece of wisdom, an interview to the almighty Anders Hejlsberg.

Update Reading this great technical article, I've confirmed that a different MethodTable is created for each Concrete Type, and the native code can be shared or not depending of whether the parameters are Reference or Value types. Contrary to what I'd read before, the same happens with the EEClass, that does not need to be unique and can be shared.

Saturday, 4 December 2010

FIC Xixón 2010

Last week I could enjoy one more edition of FIC Xixon, the Independent Film Festival that has been held in my town for 48 editions, and that for me is one of the few interesting things happening here in the whole year.
I attended to a good amount of films (11) and still missed a few rather promising ones that either overlapped with the others or didn't suit my schedule.
This year I was not interested in any of the retrospectives that the Festival devotes to important independent directors, but the other sections of the Festival had some really appealing films. I would like to praise in particular "Europe, what Europe?" and "A certain idea of cinema, Berlin School"

Here it goes a listing of what I watched and my impressions:

  • Falscher Bekenner (Low Profile), Berlin School section.
    Well, nothing too interesting, just entertaining. An 18 years old German guy pressured by his parents is trying to find a job while living a secret life.

  • Schläfer, Berlin School section. Pretty good film. A few weeks in the life of two scientifists is used to convey a story about friendship, state paranoia, suspicion, jealousy and selfishness

  • R, Rellumes ((Asturian for Sparkles) section.
    Really tough Danish film. One prison, two wings, each one worse than the other, one full of Danish criminals and the other full of Arab criminals. Of course each section hates (and does business with) the other... A new guy ends up in this hell and will have to strive to survive. Absolutely recommend it (if you're not a too sensitive person)

  • Animal Kingdon, Official section.
    Another harsh film, this time from Australia. Mum dies of a heroine overdose and her 18 years old only child moves to live with granny. Problem is that she and the rest of the family living under the same roof, make up one of Melbourne's worst maphias, and moreover, they're in middle of a war with the police. I absolutely love the last 10 minutes.

  • Until the next Resurrection, Llendes (Asturian for Borders) section.
    One more hard experience, a Russian documentary about people walking the thin line between dragging through life and resting in death. The worst part is that focused on some beautiful Slavic young women selling their bodies to buy their death (heroine), wasting their lives waiting for the next heroine rush. Horror gets amplified when confronted with beauty... There's no hope, there's no future, there seems to be nothing in post Soviet Kaliningrad...

  • Putty Hill, Rellumes section.
    Probably the less interesting film that I watched in this edition. A druggie youngster commits suicide and the director tries to find an explanation by asking friends and relatives. The idea sounds great, but the development is not that good. I think the best part is when a friend dedicates a graffiti to him, and the after funeral party, that at least in this part of Europe, is absolutely unthinkable (thought I think the idea is great, it seems pretty pagan to me :-)

  • Der Räuber (The Robber), Official Section.
    Pretty good Austrian film based on a true story. Prison has not managed to change the obsessions of the main character, running marathons and robbing banks. The latter will end up destroying the life that the former could have set for him. Not as intense as depicted by some reviews, but anyway a pretty good film.

  • Jerichow, Berlin School.
    Three characters with different problems establish an odd relation that will end up sinking them even more. Though one of them is Turkish, don't expect something similar to Fatih Akin films, as the German-Turkish identity conflict does not play a role here.

  • Eastern Plays, Europe, what Europe? section.
    Buff, probably my winner for this edition, as soon as I read the synopsys I highlighted it in my brochure as essential, and this time I was not wrong. Nowadays Bulgaria, awful housing states, two brothers (one teenager and one thirty something), live and awful life from which the first one tries to escape joining a Neo Nazi gang, while the second one tries to run away from his initial escape, heroine. Their lives and those of others collide, but from the initial crash some hope seems to arise. The main character is basically playing himself, and sadly died short after the film was shot. I'd like to highlight this great song by this interesting Bulgarian band, Nasekomix, and the excellent aesthetic depiction of current Neo Nazis gangs (no more military boots and bomber jackets, but more of a Metal-Hardcore look)

  • Plato's Academy, Europe, what Europe? section.
    Rather good Greek tragicomedy that pretty well fits in this section that tried to dig into the problems of nowadays European "nation-state". You're Greek, and Greeks hate Albanians. That's an easy norm, no questions, just blind obedience, no questions until the day you find that your mother is an Albanian emigrant and your whole world seems to crumble. Deals with such an important matter as identity in a nice, intelligent way. What's your sense of belonging? your parent's land, your birth place, the place where you grew up (that's my identity for me)... and in the end, are we that different?

  • Im Schatten(In The Shadows) Berlin School.
    Raw German crime film. Not much more to say, it's not something new, but it's damnend well done.

Once said that this has been another brilliant edition, it's time to state some complains. Like in previous editions, the Festival also included some extra activities, like art exhibitions, parties and concerts. My taste in music is far from conventional, and I wouldn't expect Terror EBM, Crust or Black Metal bands to be included in the post screening activities... but there are some really good Asturian "Avant-garde" bands that I think would raise the quality and heterogeneity of these activities, bands (or individuals) such as: DaRobotz, Arma X or Xera.

Finally, a list of films that seemed rather interesting but for whatever reason (well, mainly I need to work for a living...) I couldn't watch:

  • Worlds Apart (To Verdener), Danmark, Niels Arden Oplev

  • Videocrazy, Erik Gandini

  • Aurora, Cristi Puiu. Well, I had many doubts about this 3 hours Romanian thriller, it could be a masterpiece or be absolutely unbearable, my good friend Solsticiu has confirmed my worst expectations

  • Morgen, Marian Crisan

  • My Perestroika, Robin Hessman

  • How I ended this summer, Aleksei Popogrebsky