Friday 26 April 2019

Millennium IV Film

I was (indeed I am) a big fan of the Millennium books written by Stieg Larsson. In 2009, when Millennium 1 had already become a best seller in Spain, I watched the Swedish film adaptation, and I liked it a lot. After that I furiously read the 3 books, they absolutely hooked me. Then I watched the Swedish film adaptation of the second and third book. I did not like them much, cause having read the books first the films seemed to miss many things (which is normal, you would have needed a series rather than a film). Also, I felt rather disappointed by the fact that in the third film there are moments when Lisbeth seems to be scared and not in control of the situation, when in the book this is never the case, shes always in command of whatever crappy situation she's in.

The character of Lisbeth Salander is for sure the main reason (I guess its the same for most fans of the trilogy) that got me so hooked into the story. This said, I always though that Stieg Larsson had made some mistakes when creating the character. He depicts Lisbeth as an antiauthoritarian woman with a punk-gothic look. In a country like Sweden this should have gone beyond aesthetics and she should have been a member of the real punk scene, and hence she should be vegetarian or vegan, go to concerts, wear a Mob-47 t-shirt... Anyway the character is captivating, and I think Noomi Rapace makes an amazing work in these films, indeed I've become a huge fan of her and I try to watch any film she plays in.

The USA remake of the first Millennium book seems pretty good to me also, and indeed it quite bothered me that they did not continue with a second and third installment.

Last summer I read the fourth book, the first one written by David Lagercrantz It did not hook me like the ones by Larsson, but I quite liked it and think it makes a rather decent follow up to Larsson's work. Right now I'm in the process of reading the fifth book. This book made me realize of another error in the Lisbeth character. I think that as an ultra-smart person in constant search of knowledge and empowered by it, her crappy diet makes no sense at all. Apart from a computer hacker she should be a "health hacker" taking all sort of healthy foods, smoothies, super nutrients, vitamin complements, herbal remedies and complements. This thing of surviving on junk food or taking expired antibiotics seems a bit senseless...

Last week I watched the USA film allegedly based on the fourth book. The film is entertaining, and the aesthetics are really good, but it has hardly any relation with the book other than the presence of Lisbeth Salander and the names of the other characters (just the names, because their aspect, age... does not match at all). This is not a matter of the difficulty to compress a book into a movie, its just that they are telling a different story. It's really shocking, even the relation between Lisbeth, Camilla and their father is utterly altered!

There's another thing that really bothered me. I like traveling, I love cities, and for me the city where a film is set is pretty important. If it's a city where that I've already visited I enjoy a lot seeing locations where I've been (hey, mum, I was there!) and the memories that it brings up. If it's a city where I've never set foot, the film will help me to discover it. As in the book (they did no dare to change the location from Sweden to California... which would have been quite in line with the total lack of adherence to the book...) the film is set in Stockholm. I've never been there yet, but yes, the buildings, the islands, looked like what I remember from other films or documentaries... so I suddenly was shocked when I saw Lisbeth drive to Teufelsberg, in the outskirts of Berlin! Checking the wikipedia article I find that this is not the only "fake Stockholm" in the film. The iconic bridge where she faces Camilla is in Hamburg! and even the airport is not in Scandinavia, but in Leipzig... I can accept that some generic urban scenes get recorded in a different (but similar) city, but you can not use 2 pretty well known locations of one city and make them pass as if they were in another city... it really irritates me.

Final point in my bashing against the film, the actress playing Lisbeth, Claire Foy lacks any trace of attractiveness! In the books Lisbeth is skinny, with a childish complexion, she has this punk-gothic look that can scare some people, but she's never described as ugly. In the previous films she is played by two beautiful women. Yeah, I have to concede that Noomi Rapace probably does not match everyone's idea of beauty, but for me she's extremely hot. As for Rooney Mara, what should I say?

In this film Claire Foy does a real effort to look absolutely ugly. Her haircut is just ridiculous (it's not a punk-gothic thing, it's just wearing your hair in the ugliest way you can think of). As I've said, this is not how Lisbeth is supposed to look, well, another free interpretation of the assholes that did this "adaptation".

Sunday 14 April 2019

Immutability via Proxies

There are many of the recent programming trends that feel quite odd to me and that I hardly manage to find appealing. Immutability is in part one of them. For sure it has its cases, but it's not easy to me to understand all the hype around it. This said, the other day I read a pretty interesting explanation about Angular, Interceptors and why they designed HttpRequest and HttpResponse as mainly immutable:

Although interceptors are capable of mutating requests and responses, the HttpRequest and HttpResponse instance properties are readonly, rendering them largely immutable.

They are immutable for a good reason: the app may retry a request several times before it succeeds, which means that the interceptor chain may re-process the same request multiple times. If an interceptor could modify the original request object, the re-tried operation would start from the modified request rather than the original. Immutability ensures that interceptors see the same request for each try.

There are multiple libraries and patterns to work with immutable objects. One simple pattern that I saw some time ago was something like this:


class Person
{
        public string Name {get;}
        public int Age {get;}
        
        public Person (string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
        
        public Person SetName(string name){
            return new Person(name, this.Age);
        }
        
        public Person SetAge(int age){
            return new Person(this.Name, age);
        }
}

//Main
var p1 = new Person("Francois", 25);

var p2 = p1.SetName("Xuan");

Automatic Properties in C# create a private backing field. In JavaScript this pattern becomes a bit more complex as there is no real privacy, so we would have to add some additional technique to the mix, like having the backing fields in a closure...

What seemed interesting to me was creating an immutable wrapper around a normal object. If we could trap the access to set and get operations on the existing object... well, seems like in JavaScript land proxies are a perfect fit!

So the idea is to have a function that receives an object and creates a proxy with that object as target. The Proxy has a set trap that prevents assignments from happening, and a get trap that when receiving an request for a set_XXX property (that indeed is a call to a set_XXX method, as you know in JavaScript method invokation involves 2 steps, getting the function and invoking it) will accept this as a valid assignment, creating a copy of the existing object with the new assigned value and wrapping it in a proxy so that ensuing set operations behave in the same way. The get trap does indeed return a function that when invoked does the cloning, assignment and proxy wrapping.

Well, you better check the code to understand what I'm trying to explain. Voila the function.


function createImmutable(item){
	let handler = {
		set: function(target, property, value){
			//do nothing, this object is no longer "directly" mutable
			console.log("hey, I'm immutable, you can't set me like this");
		},

		//object is mutable only by invoking a "set_XXXX" method
		//we trap that get and return a function that will create the new object with the mutated property
		//and returns a proxy around the new object, so that immutability continues to work in ensuing assignments
		get: function(target, property, receiver){
			if (property.startsWith("set_")){
				let propName = property.substring(4);
				console.log("assigning to " + propName + " via proxy");
				return function(value){
					//either use the trapped target or "this"
					//let newItem = new target.constructor();  
					let newItem = new this.constructor();
					Object.assign(newItem, target);
					//notice I've just doing shallow cloning
					newItem[propName] = value;
					return new Proxy(newItem, handler);
				}

			}
			else{
				return target[property];
			}
			
		}
	};

	return new Proxy(item, handler);
}

And below how to use it:

class Person{
	constructor(name){
		this.name = name;
	}
	
	say(word){
		return `${word}, I'm ${this.name}`; 
  }
}


console.log("started");
let p1 = new Person("Francois");

console.log("p1 says: " + p1.say("hi"));

let immutableP1 = createImmutable(p1);

console.log("immutableP1" + JSON.stringify(immutableP1));
immutableP1.name = "Xuan";
console.log("immutableP1" + JSON.stringify(immutableP1));

let immutableP2 = immutableP1.set_name("Xuan");
console.log("immutableP2" + JSON.stringify(immutableP2));

console.log(immutableP2.say("hi"));

let immutableP3 = immutableP2.set_name("Emmanuel");
console.log("immutableP3" + JSON.stringify(immutableP3));

I've uploaded the code to a gist

Wednesday 10 April 2019

IOCP and Tasks

When years ago I read this so detailed article about the low level internals of async calls in Windows I was fascinated. I still think that the article is essential, but after reading more on the topic I would say there is a small inaccuracy in it, related to its main statement "There is no Thread".

After reading a bit about IOCP and Managed Thread Pools, I came across this question and its excellent answer, that adds some additional wisdom to the initial article.

There is no actual user mode thread from your process involved in any of this operation (beyond just the initial asynchronous call). If you want to act on the fact that your data has arrived (which I assume you do when you're reading from a socket!), then you have to dequeue the completed items from your IOCP.

The point of IOCPs is that you can bind thousands of IO handles (sockets, files, ...) to a single IOCP. You can then use a single thread to drive those thousands of asynchronous processes in parallel.

Yes, that one thread doing the GetQueuedCompletionStatus is blocked while there is no completion pending on the IOCP, so that's probably where your confusion came from. But the point of IOCPs is that you block that one thread while you can have hundreds of thousands of network operations pending at any given time, all serviced by your one thread. You would never do a 1-to-1-to-1 mapping between IO handle/IOCP/Servicing Thread, because then you would lose any benefit from being asynchronous, and you might as well just use synchronous IO.

So I'll try to summarize how I understand it. When your code invokes and asynchronous I/O operation and you await for the returned Task, there will be a thread waiting for the Task to complete, but it's a Thread from the I/O ThreadPool, so it's not a new thread created just for this specific operation, but an existing thread from the pool that is being reused (well, obviously if it's the first usage of the IO Thread Pool by the process, the thread does not exist yet and has to be created, but then it will be reused multiple times). The "There is not thread" really means "There is not a new thread specific just to this operation". When the I/O operation is completed a message is posted to the IOCP and an I/O Thread will pick up the message. If the code that had launched the async operation had a SynchronizationContext, this will have been captured by the Task, and then this I/O Thread will dispatch the execution (of the continuation) to this SynchronizationContext, that in turn will use the original thread that had started the operation to run the continuation, else, the I/O Thread itself will run the continuation (the "asynchronous callback" as it's also called).

We can know if one thread belongs to the ThreadPool (Thread.CurrentThread.IsThreadPoolThread) or not, but it seems like there's no way to know if if it belongs to the Worker Thread Pool or the I/O Thread Pool.

Saturday 6 April 2019

North Korea Documentary

DW, the English language German channel that has the nice habit of releasing some excellent documentaries, put out a few days ago the best documentary about North Korea that I've ever watched. All the dictator's men explores the topic of how a country suffering a devastating economical blockage can afford a Nuclear Program (not even keeping the standard of living of its population extremely low would seem enough...). You should watch the documentary, but long in short we can say that by exporting workers to other countries (Mongolia, China, Eastern Europe) where they'll work hard for a salary that will go directly to the pocket of the North Korean government! so yes, basically they are exporting slaves...

The second half of the documentary is also particularly interesting but in a totally different sense. This time (almost for the first time in a Western documentary) they give us a not so gloomy perspective of life in Pyongang. In the last years there's been a construction boom to try to better accommodate the population, and there seems to be a real scientific elite. Scientific development in North Korea would be a really interesting topic for another documentary. From what I read somewhere time ago, due to the sanctions, North Korea is supposed to design and build good part of the technological gadgets (TV Screens, smartphones, laptops...) used by the population. Also, they are supposed to have "an army of hackers ready to attack the West..." Regardless of how real this "cyber-army" is, I think the country has an important level of know-how in multiple matters.

I won't end this article without short mention to my thoughts about the North Korean nuclear program. For sure I don't like them to have nuclear weapons, as I don't like that any other country possesses them. This said, until the day that all the other Nuclear Powers decide to destroy all their weapons, I think they are in their perfect right to develop a Nuclear Program. Furthermore, I don't understand why the world is so worried about North Korea developing these weapons. Whatever I think of their dictator, I don't see North Korea as a crazy society willing to annihilate the rest of the planet. What really should worry, scare and drive us crazy is the fact that a Fundamentalist, medieval country like Pakistan joined the group of Nuclear Powers many years ago. At any point some "religious" leader could decide to wipe out the infidels from the face of the Earth...