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

Thursday, 25 November 2010

jQuery Debug Console

Years ago it was rather frequent to implement your own JavaScript debug console, a pop up window (this was before draggable divs where so common) where you could send your "printf debugging" messages.

In the last years, Firebug, Chrome's JavaScript console, and others have come to replace these home made debug consoles, making them unnecessary for many people.
Well, in general it's also that way for me, but sometimes I feel like an old style home made console could be useful. One of the reasons is that at times it could be interesting to have more than one console.

So, I've prepared my own debug console. It's nothing far too interesting, I've just glued together some wonderful jQuery functionality (it depends on jQuery and jQuery-UI) and come up with something useful to myself (and maybe to a few others, that's why I'm sharing it here). The UI structure of the console is created through the usage of jQuery DOM manipulation magic, and the look and feel (including size) depends on a css file.

It's just a JavaScript "class", debugConsole, with a few obvious methods (log, clear, close). You can create instances of this class and keep the reference around, or you can use a "global console", through debugConsole.instance(), this way you don't need to create the console and keep a reference to it.

//create a console, keep the reference, and use it
var console = new debugConsole("console 1");

//use the default console

you can see it at work here.

Check the JavaScript "class" here.

and if you think it can be of any use to you, download it, improve it and please drop me a comment here.

Friday, 19 November 2010

Linq (IQueryable) providers

When all the Linq thing started a few years ago, there was something that confused me quite a lot, and that still today we can find in some articles.

There are articles that claim to explain how to implement a Linq Provider. This is not completely accurate, they usually explain how to implement a Linq IQueryable Provider. If we do a google search for "implement linq provider" we get a list of mixed results, many of them add "IQueryable<T>" to the title, but some of them not.

The IQueryable word is important, because it's in that case when we have to go through the (complex) process of implementing an IQueryProvider<T>. This IQueryProvider<T> implementation is who contains the querying logic-magic. This is how Linq to Sql works:
The DataTable<T> objects in our System.Data.Linq.DataContext do implement the IQueryable<T> interface. So, when we call the Where method, we're invoking the System.Linq.Queryable.Where method, that calls into the CreateQuery method of the IQueryProvider referenced by our DataTable<T>, that I think is:


However, for other "Linq systems" like Linq to Objects (I used to call it Linq to Collections) there's not an IQueryProvider<T>. The querying logic is scattered over multiple classes. Using Reflector we can see that calling to System.Linq.Enumerable.Where<T> returns an instance of WhereIterator<T>. This class implements IEnumerable<T>, and it's the MoveNext method where all the querying logic lies in.

The most important point of all this is that making our classes "Linq friendly" does not necessarily involve implementing an IQueryProvider<T>, in many cases all we need is doing our classes implement IEnumerable<T>, so that all methods in System.Linq.Enumerable can be applied to them.

You have a good sample here for querying AD users.

Another sample would be making a CSV document queryable via Linq (so, a very basic form of Linq To CSV, there are several real implementations over there, this is just a read only toy sample). We could just have a GetEntries method returning an IEnumerable, and then apply our querying logic (I'm not taking headers into account here):

public IEnumerable> GetDataEntries()
using (FileStream fs = new FileStream(this.path, FileMode.Open, FileAccess.Read))
using (StreamReader sr = new StreamReader(fs))
while (!sr.EndOfStream)
string curEntry = sr.ReadLine();
yield return curEntry.Split(this.separator).ToList();

so, given some data like:

we could do queries like:

myCsvDao.GetEntries().Where(row => row[0] == "Germany").OrderBy(row => row[1])

A nice feature here is that the IEnumerable<T>-IEnumerator<T> created by our iterator block (yes, it's rather odd to have a class that is both Enumerable and Enumerator, check out this article) is not loading all the lines into memory, but keeping the file open and reading-loading one by one, which can be pretty useful for performance considerations (and pretty shitty if we also had implemented write access and we could have concurrent operations on it...)

Update: I've found this good recent article that is pretty related to this and to my previous post.

Thursday, 18 November 2010

AsEnumerable, IQueryable, IEnumerable

Some Basic stuff that I guess is only useful to me...
The other day while digging into some code I came across the AsEnumerable extension method. It's a pretty important one. Let's say we have an IQueryable and we're chaining queries on it (that is, the extension methods in System.Linq.Queryable). Everything works fine while those queries can be expressed as expressions (the methods in Queryable expect Expressions) and the IQueryProvider associated to our IQueryable object is is able to deal with them (transform them to Sql in the case of Linq To Sql).
If we have one query that can not be expressed as a Expression or that our associated IQueryProvider is not going know how to process, we'll need to resort to Linq to Collections (that is, the extension methods in System.Linq.Enumerable).
So, how do we force the compiler (extension methods are static, so no dynamic VTables polymorphic magic, just static binding) to invoke the right method?. We just convert it to IEnumerable using AsEnumerable, and then the compiler will bind to the Linq.Enumerable methods.

Everything fine so far, but what sounded a bit odd to me about all this is that given that IQueryable inherits from IEnumerable (IEnumerable -> IQueryable), and as aforementioned all this is static binding, isn't it enough with just upcasting from IQueryable to IEnumerable?
Of course it is (as confirmed by the answer by David B in this good entry in StackOverflow), so it seems it's just a matter of style, AsEnumerable fits better in all the method chaining.

Copy and paste from David B answer:

IEnumerable orderQuery = dataContext.Orders
.Where(o => o.Customer.Name == "Bob")
.Where(o => MyFancyFilterMethod(o, MyFancyObject));

IEnumerable orderQuery =
(dataContext.Orders.Where(o => o.Customer.Name == "Bob")))
.Where(o => MyFancyFilterMethod(o, MyFancyObject));

Saturday, 13 November 2010

Morocco, murderous kingdom

I guess it should be clear to anyone reading regularly this blog that I have a rather strong sense of belonging to both Asturies (and Galiza in a lesser extent) and Europe, and no feeling at all of belonging to Spain. In fact, I feel uncomfortable when being abroad and having to show my Spanish passport or identify myself as Spanish in any other way. I come from an Atlantic land with an Atlantic culture, and almost everything people tend to identify with Spain are Mediterranean elements that have quite little to do with me or my family (neither the negative nor the positive ones).

The thing is that from time to time something happens that makes me not just uncomfortable, but purely ashamed of having a Spanish passport, and we're right now in the middle of one of those situations.

In 1975 Spain took one of the more contemptible steps in her recent history, the handing over of her last colonial territory, Western Sahara to the blood thirsty Moroccan kingdom. This means that in an unprecedented move and unlike the rest of the European powers, Spain did not carry out a process of decolonization of a territory that had been exploiting for over 1 century, but gave it away to a new oppressive power, Morocco (and also Mauritania at first). So, the Sahrawi people changed hands from a European dictatorship that had not treated them too bad (thought a dictatorship, being part of the Western world makes you accept some minimum moral laws, if only to avoid criticism from your neighbours) to the brutal, bloody Moroccan dictatorship (well, they prefer to call themselves a kingdom, it sounds more nice...)

What all this has meant for the Sahrawi people has been 35 years of brutal exploitation of their mineral resources, discrimination in favor of the new settlers sent by Morocco to replace the autoctonous population, constant attempts to destroy and replace their culture and identity... all in all, there's a clear name for this GENOCIDE.

In the last weeks, the GENOCIDE has entered a new phase, and the Moroccans are killing the Sahrawis in their own homes before the indifference of the European Union, the UN and worst of all, the main culprit of all this, Spain.

If Spanish government had a minimum of dignity, if Spain had any sort of international prestige or recognition, if Spain were not a joke country that can be looked down on by anyone, even by a shit country like Morocco... The Spanish Army (backed by the EU, that would have to support the legitimate actions of one of its members) would have been sent to Western Sahara to protect the population and heal the wound that was opened in 1975. Western Sahara could be turned temporaly into the 18th Spanish Autonomous Community, waiting for a future referendum of self determination, once the International Community were able to guarantee that the disgusting Moroccan dictatorship would never put their dirty hands on the territory.

But not, 40 years of dictatorship and 35 years of misgovernment by corrupt, ignorant politicians, in addition to the historical backwardness when compared to other European countries, have made of Spain a fucking puppet that has no other option that kneeling before the Moroccan dictator to protect some dubious economical interests...

And still some Spaniards do not understand why Basques, Catalans, Galicians, Asturians seek for independence... who in their right mind would want to be part of this joke country that can not be taken seriously by anyone?

Sahrawi people and Polisario Front RESIST!!!

Friday, 29 October 2010


Given that I consider myself a Geography-History-Politics freak, this post is sort of a self humiliation exercise, as it's basic stuff that I should already have known, and moreover given how much I like that country/countries.

Some weeks ago, I received an email from a British colleague informing us that her "identifier" in the organization for which we work for had changed from "myname/UK/whatever" to "myname/GBR/whatever". At the time, I didn't pay much attention to it as I was rather busy, but today, for some reason, it came back to my mind.

I used to think UK and Great Britain where just the same, the union of England, Scotland, Wales and Northern Island, but after some googling I turned out to be rather wrong.

Indeed, United Kingdom (UK) is that union that I mention above, but Great Britain is just the island containing England, Wales and Scotland (I used to think the name for the island was just Britain). Furthermore, GBR is not the abbreviation for Great Britain, but the abbreviation for United Kingdom of Great Britain and Northern Ireland, that is the term that should be used instead of United Kingdom.

If you think this is confusing, see the picture below :-) or better, check the wikipedia article. It was fun to read in a forum that many British citizens are not much aware of these differences.

Tuesday, 26 October 2010


Another great British documentary dealing with Islam in UK.
This time it's about a rather unknown subject to me, muslims converting to Christianity. To my surprise, there seem to be around 3000 converts from Islam to Christianity in Britain, and these people are living in fear, having received all sort of insults, threats and physical attacks from other furious, intolerant muslims.

Many muslims believe that those who leave Islam (apostates) should be punished, even with death. In fact, it seems like 36% of young muslims in UK think apostates should be killed. This 36% figure is terrifying, and it goes far beyond my most pessimistic ideas about the percentage of fanatic muslims in the West.

Hopefully, there's one absolutely respectable muslim leader that dares to claim before the cameras that muslims should be free to leave or change their faith without being attacked by other muslims. He says Qur'an does not ask for punishment to apostates, and that those Muslims that think so are misunderstanding something (for example some books-leaflets shown in the program and available in muslim British libraries that ask for murdering the apostates).

On the other side, radical Christians (USA evangelists) have also started their own XXI century crusade, and are sending volunteers to UK in an attempt to Christianize muslims. They seem to be targeting Bradford, and it's no wonder why they choose this city, as some polls say that around 25% of the population there are muslims.

As an agnostic who views Christianity and Islam mainly as two suffering and alienation inducing ideologies (OK, I'm not going to deny that a few good things have been done in the name of both religions, but the global result is fairly negative), all this modern times proselytism sounds awful and decadent, but also, and confusing as it may seem, I have to admit that a small part of my mind, aware of having been brought up in a Christian cultural background, seems to be happy of seeing some Christians trying to counteract the growth of Islam.

This brings to my mind that same as other years I had seen muslim stalls on the streets of London leafleting and calling people to conversion... this year I've seen groups of Christians singing and leafleting trying to draw attention. It may seem harmless and funny now (poor crazy guys...) but this is just another chapter of the religious war that has prevented Humanity from full development for so many centuries...

Monday, 18 October 2010

Lazy Dynamic Proxy

Lazy<T> has been a nice addition to the .Net 4 framework, providing us with a standard way to do lazy initialization. It's one of those damn simple thinks that you wonder, why wouldn't I think about it before?.
In short, Lazy instances are proxies around instances of T that won't get created until they are used for first time, through the Lazy<T>.Value property.

So, instead of directly creating an instance of CostlyObject (costly either in terms of initialization time or memory consumption), we create an instance of Lazy<CostlyObject> and use the .Value property in order to access any method or property.

Lazy p1 = new Lazy(()=> new Person("xose", 34));

You can see the full code here.

I pretty like it, but I see a problem with it, its usage is not transparent. The client code is fully aware of being working with a proxy object and needs to adapt to its semantics (use myLazy.Value.Whatever instead of myObject.Whatever). We can think this is OK, but well, we could think of "lazyness" as an implementation detail that we don't want to share with the client.
Even worse, this won't work for already existing code. There's not inheritance relationship between Lazy<T> and T, so we can't happily pass a Lazy<T> object to a piece of code expecting a T object...

All this I've said above immediately rings a bell for me, Dynamic Proxies and Interception. This time we don't want to intercept calls to log something, but to create the real object.
So, time again for Castle DynamicProxy.
My first intention was to use a Class Proxy. This does not make sense. I would be creating an instance of a class inheriting from the T class (ProxyGenerator.CreateClassProxy<T> creates that new class and returns an instance of that class). Given how constructors and inheritance work, any child class constructor need to call a Base class constructor (either in an explicit or implicit fashion) so I would be building the costly object since the first moment (unless I were using some trick of having a dumb Base constructor that is not costly, but in the end is not useful...) so that's not the way to go.

As we have a problem with class inheritance here, we'll have to use a different approach, Interface implementation.
As you can read in this excellent tutorial, we have 3 options when it comes to interface proxies. 2 of them have the same problem as the previous mechanism, thought the created proxy is implementing an interface, it inherits from the proxied class, so it's useless for this. But we have a third option, Proxy without target:

Proxy without target. This one is tricky. You don’t supply target implementation for the interface. Dynamic Proxy generates it for you at runtime, using interceptors to provide behavior for its methods.

So, we generate a proxy class that implements the interface, and use an interceptor to lazy create and reference the instance of the real class.
We have 2 classes then, one that generates the Proxy, and another one for the Interceptor

public class LazyGenerator
ProxyGenerator generator = new ProxyGenerator();

//T is an interface
public T GetLazy(Func constructor) where T:class
T proxy = generator.CreateInterfaceProxyWithoutTarget(new LazyInterceptor(constructor));
return proxy;

public class LazyInterceptor: IInterceptor where T:class
private T target;
private Func constructor;

public LazyInterceptor(Func constructor)
this.constructor = constructor;

public void Intercept(IInvocation invocation)
Console.WriteLine("intercepted"); = ?? this.constructor();

//now invoke the method in the real object
//this works fine both for methods and properties, as in the case of a property this invocation.Method would be the get_propertyName, set_propertyName
invocation.ReturnValue = invocation.Method.Invoke(, invocation.Arguments);

The constructor delegate passed to the Interceptor contains the invocation to constructor of the proxied class (it's just the same approach used by Lazy<T>.
You've got a full sample here where I lazy create a Repository. (zip with the needed Castle Assembly here).

As you can see in the comments it's interesting how this line in the interceptor:

invocation.Method.Invoke(, invocation.Arguments);

works both for intercepted method calls and properties.
This is pure logic, a C# property "X" gets translated by the C# compiler into a "get_X" and a "set_X" method, so that's what we have in the invocation.Method when we access a property through the proxy.

If you happen to read this blog regularly (which I doubt :-)) you'll probably have realized that this entry is rather related to this previous one.

Thursday, 14 October 2010

jQuery Object

Quite often when I get back to jQuery after some time without touching it, I get some confusion with the almighty jQuery object (well, I would say this is sample of the God Object antipattern).
When we use some of the selection methods to obtain DOM elements, we get an instance of the jQuery object containing the selected DOM elements (DOM elements, not jQuery wrappers, so we usually have to wrap them into a jQuery object again with $(this) for additional processing). This instance is similar to an Array, but it's not, it's an Array-like object.
It has a length property and the DOM elements are accessible through numeric indexes, so how is this?
Each DOM element to be contained by the instance of the jQuery object is added to this instance through an increasing numeric property, and a length property is also added and kept updated. Bear in mind that for these numeric properties we can only use the "[]" notation, not the "." notation. Yes, all this is one of those magical JavaScript things. What this means is that we can do this with an object:

var obj1 = {}; = "xana";

obj1["age"] = 30;

obj1["0"] = "item1";

obj1.length = 1;

obj1[1] = "item2";
obj1.length = 2;

so we get an Array-like object, with a length of 2, 2 items accessible via numeric index and other 2 properties ("xana" and 30).
As it's not a real Array, there's some gotcha to take into account, for example, we should not iterate it with a for in loop if what we want is iterating its items collection. I mean, we can iterate (thought it's not much common) a true Array like this:

for (index in ar)

but used on our Array-like object, we would get a listing of all it's other properties and methods along with the indexed values.

Of course, the usual way to iterate our jQuery object is either with the .each jQuery instace method (not to be confused with the .each "static method")

var $aux = $("<ul><li>xose</li><li>xuan</li></ul>");
//jQuery object with an only item (ul)
var $peopleList = $aux.children("li");
//jQuery object with two items (li)

or with a normal for loop:

for(var i=0; i<$peopleList.length; i++)

you can view/test the code here

Tuesday, 12 October 2010


Yesterday I watched Boarding Gate, and impressive film by French director Olivier Assayas. It has common elements and a very similar feeling to the other film from this director that I had watched before, Demonlover (well, Demonlover is even better for me, but anyway both films are excellent).

The plot in both films deals with the dark side of the business world. Spying and fighting between two companies trying to gain control of the distribution of Anime porn in the West (Demonlover) and the troubles of a business investor going downhill and involved in a harsh sort of relationship with a femme fatale (Boarding Gate). Both films succed on adding interest and complexity to the story by setting up part of it in societies that still keep a high dose of mystery and charm for many of us, Japan and China.

The combination of action, eroticism and violence will keep you hooked until the end of both stories, and well, when I say eroticism this takes me to the other great common denominator of both films, they are starred by some absolutely gorgeous, ultra sexy women: the viking goddess Connie Nielsen and Gina Gerson in one case and Asia Argento and an Asian beauty in the other case.

Time to get my hands on any of the other Assayas' films, but these two have set the bar quite high.

Thursday, 7 October 2010

Nested ordered list

To my surprise I found out the other day that there does not seem to be a html+CSS way to create a nested ordered list. I mean, the typical index, something like this:
1. Europe
1.1 Germany
1.1.1 Hesse
1.1.2 Bavaria
1.2 UK
2. Asia

Using the normal html structure you would get something like this:

1 Europe
1 Germany
1 Hesse
2 Bavaria
2 UK
2 Asia

so, you have to resort to JavaScript to get the right numbering. It's pretty simple, and much more if you're using jQuery's magic, but anyway I think it can be helpful (at least as my personal repository), so here it is:

(you also need a change to your html, instead of <ol> elements you'll have to use <ul>, as it's javascript who's going to take care of all the numbering)

function orderNestedList($ulItem, curValue)
var newValue = curValue + (++index) + ".";
var children = $(this).children("ul");
if (children.length > 0)
orderNestedList($(children[0]), newValue);

you can see it at work here

Saturday, 2 October 2010

The Third Jihad

I've just watched this great North American documentary and I can only praise it and recommend it. It's one more wake up call to Westerns to stand up to radical Islam, to reverse the process of Islamization of our societies.

It's pretty good at pointing out the main enemies, risks, and weaknesses:

  • As I already commented in a previous post, Wahhabism, Saudi Arabia and the use of their petrodolars to indoctrinate moderate muslims in Western countries.

  • The absolute incapacity of Western society to understand the problem, prefering to live in a state of self delusion.

  • The faint hearted attitude of many Westerns (normal people and governments) that do not have the courage to say NO to the stupid requirements of some of the "new neighbours". Sure our culture has to change in many aspects, but it's evolution what it needs, not involving based on the "advice" of a medieval society

  • Treacherous politicians that put their private businesses before the public ones (that is, the people who elected them). As their private businesses are in hands of the Saudi theocracy... we know what happens...

It also reveals some facts that were unknown to me:

  • The cultural jihad is far more dangerous than the violent Jihad.

  • Non muslims are not allowed to set foot on Mecca

  • Saudis have given good amounts of money to Western universities to fund studies on "Arabic culture", "mutual understanding"... sounds good, but it's just pure and dirty proselytism...

  • Many muslims in USA that claim to be moderate when in public, are not that moderate at all

I quite like that from the first moment it tries to make it clear that the problem is not Islam, but radical Islam, and it's nice to see a (very)moderate muslim narrating the documentary. But I'd like to add something with regards to this, as I think moderates could be not that harmless sometimes:
I have no problem with moderate religious people, whether they are Christians, Jews, Muslims... when these people live with other moderate or very moderate people. Problem is that when moderate people live with extremist people, the former can get infected, and much more when the extremists have money and power...

There are moments in the documentary that are absolutely revolting, like the demonstrations of radical muslims asking for punishmen for homosexuals, their intentions to apply Sharia in Europe, their absolute convintion of bearing a superior moral order...
but sure the worst moments are when some of the contributors assert that the battle against extremist Islam in Euope is already lost, and it's terrible to hear that because probably they're sadly right, just think that in some European countries muslims will surpass non muslims in less than 50 years...

I have to admit that there are a few things I don't agree with:

One of them is the title itself. The producers claim that there have been 2 other big Jihads previous to the current one, the first in the 8th century, when they conquered southern Italy and most of the Iberian Peninsula (but not my homeland, Asturies:-)), and the second in the 16th and 17ht centuries, when the Ottoman Empire conquered good part of Eastern Europe but hopefully was defeated twice at the doors of Vienna.
From my perspective none of these previous invasions can be considered as dangerous as the current one, the Islamic invaders at those two occasions were not at all as harmful as they are now, they brought a culture that was partially compatible with the European culture of the time, nothing to do with the Wahhabi medieval barbarism with which they try to replace Western 21st century civilization.

Another point I disagree with is their vision on Iran. I don't think Iran is such a threat, on the contrary I hope some day the Iranian people (much more moderate than many Westerns think) will manage to overthrow the theocratic government that they've been suffering for decades and become again the great society they once were.

Anyway this film is an absolute must, and if I've not managed to convince you yet, maybe this statement that opens the film will do.

"This is not a film about Islam. It is about the threat of Radical Islam. Only a small percentage of the world's 1.3 billion Muslims are radical. This film is about them."

Wednesday, 22 September 2010

Multicore Bottleneck

Today, while reading some materials about Parallel Programming, partitioning datasets and so on, some old echos from the past came to my mind.

In most of the simple samples that I've seen lately, a bunch of data gets loaded into memory, and then the memory dataset is partitioned and each thread reads data from one of the data partitions. We do the whole loading into memory before launching the threads because otherwise different threads would be accessing to disk simultaneously and the I/O bottleneck would keep threads stuck while the I/O operation for other thread gets completed.

OK, I've said bottleneck. There's one that's been well known since long ago, Memory bottleneck. Years ago, as CPUs got faster and faster, the memory access technology was unable to keep up, and we had the problem of a fast CPU on hold waiting for the data retrieval from memory... We have caches and branch prediction to soften the problem, but anyway, it's still there.

Since some years ago CPUs speed has barely increased, but we have several cores, and more and more cores to come... This means, that it's not only that an operation will be we waiting for the memory to provide the data it needs, it's that multiple parallel operations will be waiting for the same memory to provide those data through the same data bus that we had years ago.
This sounds messy, so I thought sure there was some work around in place for this that I was missing (it's been a long while since the times when I was a hardware freak following the last improvements in chipsets and buses...), but after some googling it doesn't seem so, and in fact we can say that Multicore makes the memory bottleneck worse.

Saturday, 18 September 2010


I was in London last week, and once again (despite having been there many times) the omnipresence of CCTV cameras in public spaces continues to astonish me.
I have mixed feelings about them, but in general, however strange this may seem to many of my friends, in this particular case positive feelings win.

I think central London (taking as central a really large area) is one of the safest places that one can find in a modern metropolis. I don't like being surveilled by the the police, the state or whoever... but if I weigh up the moral disturbance that being observed causes me against the risk of being mugged by a violent gang, finding all public toilets, phones... vandalized, etc... I'm afraid I've turned so conservative to happily accept being monitored...
Well, maybe this thought is common to many Londoners, as otherwise it would be hard to understand that a population that has so fiercely opposed to Identity Cards could accept the attack to privacity that CCTV involves.

Related to this (but with a much broader scope) Tate Modern is hosting an exhibition that seems pretty interesting:

Exposed: Vouyerism, surveillance and the camera

No doubt CCTV has been, is, and will ever be a hot, controversial topic, and street art clearly reflects it:

Banksy, Marble Arch, London, 2004:

Marble Arch, London, September 2010:

Frankfurt, August 2010:

Update 2011/06/02
When I was in Munich last April (world famous for its impressive Bavarian baroque architecture, but its less known Art Nouveau treasures are also stunning) I came across this good addition to this post:

I think it reads something like: "Dictatorship of Safety"

Wednesday, 8 September 2010

Some good films

I've seen some rather good films lately, but I don't feel like writing a full entry for each of them (as I've said before my knowledge about cinema is rather limited, I rarely watch old films, I'm much more into stories than into interpretations...).
So here's a listing of a few good films to spend your time with:

  • WAZ. Great horror/thriller film. It's been compared to Saw and Seven in most reviews, and I agree with it, but would like to point that it's not that bloody as the last Saw installments, there's blood and "cruelty", but these are absolutely necessary. A well thought story about catharsis through revenge, about destroying those who destroyed you, using the same weapons. Same as with Amanda in Saw, I seem to love this kind of vindictive women.

  • Before the Devil knows you're dead. Very good story about 2 brothers trying to save their spoiled lives by robing their parent's jeweler's. The great plan ends up being a total disaster, and the consequences are even worst. I like how it depicts the decadent lives of 2 men and how they sink more and more.

  • In Dreams, pretty good and not much known terror film. Revolves around the pain of a woman whose only child has been murdered by a serial killer. The woman has terrible nightmares about the new crimes to come and is about to get nuts. Intense film where you almost taste the suffering of the main character.

  • Desert Flower. Based on a true story about a Somali woman that has suffered female genital cutting, I was expecting a harsher experience, but anyway it's an entertaining film and a good story. It's strange to see how something I reject so much as it's the fashion industry (I see it as a clear example of how banal and lacking in interests our Western Society has turned) in this case manages to fix the life of someone (in a sense a victim of that same social decay).

  • Man on Fire. Another good story about broken lives, revenge and redemption. A war mercenary turned into a body guard unexpectedly finds a reason to live and to die for. The setting for the story (modern day crime ravaged Mexico) fits pretty well.

  • The burning plain. Great film writtend and directed by the screenwriter of Amores perros, 21 grams... and starring Charlize Theron. So, the credentials are great, and the film really lives up to them. An interesting story stretching over more than a decade and told in a non lineal way. Sons that repeat their parent's "sins" and painfully drown in fear, guilt and remorse end up finding redemption in love. As a plus, I can't help saying that Chalize Theron along with a great interpretation gives us great views of her body ;-)

Friday, 3 September 2010

Chernobyl wild life sanctuary

This entry is related to the one I posted last week regarding how happy the planet would feel if he managed to get ridden of that plague that we are...

Reading an article in Muy Interesante (a Spanish popular science magazine) about Chernobyl 24 years after the accident, I've found that wildlife has flourished in the place, and rare species have returned, leading the Ukrainian government to designate the area a wildlife sanctuary.
There's some controversy around this, with claims against this positive effect, but anyway:

  • on one side it remains as a really interesting research case, from which we can learn about how organisms are able to adapt to cope with high radiation levels, and how really damaging different radiation levels are to life

  • on other side, We, Humans, seem rather more damaging for other species than a Nuclear explosion and its associated radiation...

Over the years I've watched several documentaries about the Chernobyl accident and its effects, but no doubt that the best one I've watched is this, an excellent research that reveals what governments kept hidden for many years, that after the first explosion there were chances for a much worse second one to happen, one that could have wiped out from the map a good part of Europe.
I remember from another documentary (that I can't find now) how lucky we were in Asturies regarding the radiation, winds kept blowing in such a way that the main radioactive cloud did not reach us. You can see the cloud behavior on this video.

Monday, 30 August 2010

Duck Typing and C#

The dynamic keyword in C# 4 (and the underlying DLR machinery making it possible) is amazing and powerful. Some people tend to get confused with Object, var and dynamic... but differences should be rather obvious:

  • var has nothing to do with dynamism, it's just compile time type inference (aka wrist friendliness).

  • Object is just that, everyone's ancestor that can be downcasted (with all its risks and inconveniences) to whatever we need

  • dynamic is pure dynamism, compiler assumes you know what you're doing when you declare something as dynamic and does not interfere. At runtime it will try to find the Method or Property that you're invoking-accessing, either using that object's implementation of IDynamicMetaObjectProvider, or resorting to Reflection. This is what we're used in languages like JavaScript, Python and so on... and is called Duck Typing

So dynamic and DLR are incredibly powerful, allowing us to mix the dynamic and static paradigms in the same language, but anyway, there are some limitations for which we'll need some extra help.

Let's say we have two compatible types (same public methods-properties) but while one of them implements an interface defining those semantics, the other one, for whatever reason (comes from a different code base for example) doesn't. Say we would like to use instances of both types in a piece of code. That's fine while that piece of code is dynamic, I mean, we're passing the object of one of the compatible types to a method expecting a dynamic parameter, but what happens if that method was implemented in a static fashion, expecting typed parameters (an interface)?
We would need to create a new class that implements that interface and redirects calls to the methods with the corresponding name in the initial class. Well, that's what dynamic proxies are for, intercepting and redirecting calls!.

For some odd reason, even in .Net 4, dynamic proxies are not part of the Framework (while Java has had them since the 1.3 times), so we would have to resort to one of the third party implementations out there (Castle, LinFu).
Castle DynamicProxy seems to be the most widely used, but I was already aware (I had toyed with it a bit some years ago) of the problem with its lack of documentation. Hopefully, this excellent tutorial encouraged me to jump onto it again.
Castle DP is arguably a powerful beast, but at first sight it didn't seem to provide out of the box support for what I needed (in principle you normally use it to intercept classes that already implement the common interface).
Googling for "C# and Duck Typing" I found a good article that explains how to accomplish what we want and shows me that I had read too fast the Castle samples.
He's using the CreateInterfaceProxyWithoutTarget. This is an interesting Proxy Generation method, as it does not need a class that already implements the interface, all the magic is in the interceptor. For this case Mauricio creates an interceptor that holds a reference to the proxied object, so we can invoke the corresponding method.

Proxy without target. This one is tricky. You don’t supply target implementation for the interface. Dynamic Proxy generates it for you at runtime, using interceptors to provide behavior for its methods.

The usage:


is very similar to the usage of this nice library that I had found some time ago (by the way, it also provides other useful functionalities like conversion between compatible delegates)
Note that this library has its roots in the pre-Extension Methods times, so that's why the syntax is uglier:

ICanAdd adder = DuckTyping.Cast<ICanAdd>(myAdder);

Back in 2008 the author (David Meyer) wondered if the advent of C# 4 would turn the library obsolete, as we've seen above, some parts of it are still useful, so I'll keep it in my programmer's toolkit.

You can see a usage sample here.
In order to compile it you'll need the DeftTech.DuckTyping.dll Assembly. You can get it all in this zip.

Saturday, 28 August 2010

There will come soft rains

I've just come across by chance this beatiful poem expressing how much Nature would enjoy a world without humans.

There will come soft rains and the smell of the ground,
And swallows circling with their shimmering sound;

And frogs in the pool singing at night,
And wild plum trees in tremulous white;

Robins will wear their feathery fire,
Whistling their whims on a low fence-wire;

And not one will know of the war, not one
Will care at last when it is done.

Not one would mind, neither bird nor tree,
If mankind perished utterly;

And Spring herself when she woke at dawn
Would scarcely know that we were gone.

The most astonishing thing is that this poem dates back to 1920!
At first it surprised me a bit finding someone that almost 100 years ago had the idea of the extinction of mankind revolving around his head. I think that nowadays, due to the many issues that we're facing now (climate change, globalization of the economy, oil depletion, fish depletion, religious fanatism, moral decline of Western society and above all, overpopulation) this idea of humanity being wiped out is not so strange for a certain percentage of the population, but wouldn't have expected such thoughts in that time.
Well, thinking it twice, I guess that after having witnessed in WWI how much the destructive power of humans had increased, thinking of the "total annihilation" was a pretty natural thought.

This idea of how great it would be for the planet to get rid of us (the architects of the Sixth Extinction) is rather clear to me, and is pretty well set out by Michael Boulter in that great documentary "Dodo's Guide to surviving extinction", but it's something that I don't think is understood by most of the population (when thinking about the many challenges that humanity will confront in the near future people tend to think about the end of the world, but no guys, it's not the end of "the world", it's just the end of humanity). This is normal due to our terribly anthropocentric view of everything, an anthropocentrism that will be one of the main causes of our self destruction...

I watched a documentary some years ago where Sir Martin Rees stated that the probability of extinction of Human race by the end of this century was around 50%, well, if we think in terms of a partial destruction (getting to an apocalyptical world like the ones depicted in The Book of Elli or The Road) I bet those chances are much higher...

By the way, "there will come soft rains" was the title of the E150's last track in their split 7" with Ivich (buff... that was 1997...). It was a beautiful melodic electronic song composed by their drum player (Carlos) that had little to do with the fast and furious music usually distilled by these BCN Punks.

Friday, 20 August 2010

Floods, Shock and Profit

At this point in time I guess everyone is aware to a greater or lesser extent of the terrible floods that devastated Pakistan last month.

In the last days media have warned of the attempts of Fundamentalist Islam to proselytize in the refugee camps, trying to gain supporters among these people who have lost it all...
What these bastards are trying is what Naomi Klein describes so brilliantly in her splendid book The Shock Doctrine, exploiting a chaos situation where people are weak, confused and terryfied, to make them accept a wicked and oppressive ideology...

I wonder if these repulsive fanatics will be trying to blame us (Westerns, Kafirs, infidels...) of the floods... of course they could resort to some Conspiracy Theory and say that we're using weather as a weapon through HAARP or something similar... or maybe, they'll be telling the victims that Allah has sent the rains to punish them for not being devote enough muslims...

During the floods part of the rescue services already took care of discriminating those who were not muslims

...were not rescued from their homes because rescuers felt that Muslims must be given priority...

Parallel Programming

An fast write up mainly to share a couple of links (and a reminder of things I'd like to go more in depth in the near future)

In these times when multi-core processors are ubiquitous (even for smartphones in the near future) it's clear that parallel programming is more and more important. .Net framework has tried to address this doing Parallel Extensions mainstream by including them in .Net 4 framework.

Parallel Extensions look exciting and promising to me, so finding this free book (Patterns for Parallel Programming: Understanding and Applying Parallel Patterns with the .NET Framework 4) has been really good news. I've only gone through the first pages and I've already learnt some interesting things about dynamic partitioning, oversubscription...

This reminded me about another excellent free book (Threading in C# by Joseph Albahari) that I had been reading some months ago. It's great news to see that it's been updated for .Net 4, including a full new chapter on Parallel Extensions (and many updates to the previous chapters to reflect other MultiThreading improvements to the Framework).

We tend to think of Parallelism just in terms of Threads or Processes, but there's more to it, for example:

  • SIMD instructions (that's SSE for x86 architecture). These instructions leverage Data Level Parallelism, performing the same instruction on multiple data, for example if we want to add different pairs of points, an only Add instruction could run in parallel over the different pairs. There's a good explanation here
    Mono has added support for SIMD instructions, which can be really useful for Multimedia applications doing heavy use of matrices and vectors. It's contained in an independent assembly that can be used both from Mono and from Microsoft's implementation.

  • GPGPUs. The programmability of GPUs has gone a long way since the initial vertex, pixel shaders. These shaders are used for graphic tasks, so while advanced and interesting, do not seem so revolutionary as being able to run "normal calculations" on the GPU and use the results from the code running on the CPU.
    So far, there are main technologies in this field:

    • CUDA. This is the first one I was aware of. I remember how surprised I was when I saw some code a couple of years ago showing how dynamically compile and load into the GPU some code embedded in a Python script

    • OpenCL (Update, 2010/09/27): I've just read a very interesting entry on InfoQ about using OpenCL via pure Java (code written in a Java subset gets translated to OpenCL code at runtime). It's pretty interesting what the author says on current usages of GPUs in common programming:

      Looking at the OpenCL side, I have seen everything from financial services to energy exploration (I used to work in that field). I also know folk that are looking at GPUs for seismic data analysis. Applications that have the potential to see major benefits of using the GPU for computation include the big-data analytics domain.

    • Direct Compute. Thought it's Microsoft Centric there don't seem to be direct bindings from .Net, but there are some articles out there on how to make them work together.

  • By the way, back to the shaders, it's really amazing being able to run Shaders in your browser via Silverlight!!!

Saturday, 14 August 2010

Windows Uptime

It's a bit odd that Windows does not provide an uptime command whose sole purpose were that, telling us the Uptime of our machine, but well, we have different options to get it:

  1. net statistics server
    as this command will display a bit more of information than just the uptime, we can trim it to just our desired line by using the grep equivalent provided by the Windows Resource Kit, qgrep:
    net statistics server | qgrpe -e "Statistics since"
    As you can test, it returns when the the computer was last booted.

  2. SystemInfo
    this command provides a lot of information about our system, but because of that it can take some seconds to run, so it's less useful for this task. Again, you can refine the results with qgrep:
    SystemInfo | qgrep -e "System Up Time"
    Again, it returns when the computer was last booted.

  3. WMI and WSH
    WMI is one of those wonderful Windows technologies of which potential not everyone is fully aware. I'm used to work with it from WSH (another underrated Windows technology), so I've prepared a script that uses WMI for either getting when the computer was booted:

    var wmiDate = WScript.CreateObject("WbemScripting.SWbemDateTime");
    var wmiObj = GetObject("winmgmts:\\\\" + strComputer + "\\root\\cimv2");
    var aux = wmiObj.ExecQuery("
    Select * from Win32_OperatingSystem");
    //WMI queries always return a collection, so get the first element (the only element in this case)
    var osInfo = new Enumerator(aux).item();
    wmiDate.Value = osInfo.LastBootUpTime;

    or obtaining how many seconds have elapsed since then:

    var wmiObj = GetObject("winmgmts:\\\\" + strComputer + "\\root\\cimv2");
    //returns a collection with an only item
    var aux = wmiObj.ExecQuery("
    Select * From Win32_PerfFormattedData_PerfOS_System");
    var osPerfInfo = new Enumerator(aux).item();
    UpTime: " + osPerfInfo.SystemUpTime + " seconds");

    As you can see in the full source for this second case I had to add some extra code to turn the seconds into weeks, days, hours... this made me think that with .Net I just would need to create a TimeSpan object from those seconds, and it would already provide me the Days, Hours, minutes... so, why not to use the successor technology to WSH, one with full access to the .Net power? this takes us to:

  4. WMI and Windows PowerShell
    After playing for a while with the PowerShell console (If you have PowerShell installed, it should be here: C:\WINDOWS\system32\windowspowershell\v1.0)
    I realized this would involve several lines and I would better write a PowerShell script, so I came up with this uptime.ps1 file (it's my first PowerShell script, so please, be understanding):

    $perfObject = gwmi Win32_PerfFormattedData_PerfOS_System
    $seconds = $perfObject.SystemUpTime
    write-output ($seconds.ToString() + " seconds uptime")

    $timeSpan = new-object System.TimeSpan -argumentlist ($seconds * 1000 * 1000 * 10)
    write-output ($timeSpan.Days.ToString() + " days, " + $timeSpan.Hours.ToString() + " hours, " + $timeSpan.Minutes.ToString() + " minutes, " + $timeSpan.Seconds.ToString() + " seconds")

    in order to run it from the console you'll need to set permissions for running .ps1 scripts (yes, I agree it's weird that it's not enabled by default, but MS is taking security really serious), so type:

    Set-ExecutionPolicy RemoteSigned

    then, if you cd to the folder containing the script, just type:


    and that's all.

Friday, 13 August 2010

Developers will have their say

I guess I'm not the most appropriate person to do predictions about the PDA-smartphone market (I'm using this now almost forgotten word, PDA, as my personal homage and because it's the Multimedia-PDA functionality what is really interesting to me, the Mobile Phone functionality has little interest for someone that some months spends less than 1 euro in calls), I just have my own very limited experience with a few of these gadgets and some theoretical knowledge.

Being a rather pro-Microsoft guy (that also loves open source, I don't see any contradiction on this, but it would be long to explain...), it would be natural that I felt rather attracted to Windows Phone 7, but that's not the case because my rather lame experiences with Windows Mobile 5 and 6 have made me quite reluctant to any Microsoft offerings in this sector.

Currently I have a jailbroken (thanks black rain!) iPod Touch 2G, that for 180 euros seems absolutely unbeatable value for money to me. My main problem with it is that my specific model only admits tethered jailbreaks, and well, that the idea of being forced to resort to jailbreaking for having a really useful gadget is shitty, if I'm paying for the hardware, why don't you fucking Apple give me full control over my legitimate purchase???

Because of this I've been following with growing interest the evolution of Android, waiting for the release of affordable (prepay card is an absolute must for me given my minimum consumption) gadgets, and it seems we're almost there, with some models around 300 euros that seem rather acceptable. I was dreaming of Android as some kind of open source iOS, that as such would be fully accessible to tech-aware users.
These dreams have almost vanished as I've realized that with regards to Freedom, Android is little better than my Iphone OS. I mean, you get an open source OS, but tainted by each manufacturer so that in the end you need to root it (sort of jailbreaking) to get full access to it...

So, when I found this excellent article today, I could only add new variables to the equation that sooner or later I'll have to solve before buying a new toy...
I absolutely agree with almost everything said in that article, Microsoft has been treating us (developers) really (and increasingly) well. Things like Linq, C# 4 and the DLR, COM (using it, not creating it) should be in the Tate Modern of Software Development... and programming for the Windows Phone could end up being so fun that would make us (developers, wow, it's good to feel as if we had power over those masses of programming-unaware poor sheeps) change trends...

The article reminded me of this crazy video of Steve Ballmer gone nuts (I think I had first watched it on this interesting Swedish documentary against consumerism).

This Ballmer's video is also great :-D