Wednesday, February 04, 2009

Why ninjas are cool

No. You are wrong. This is not yet another rant about why Dusk of Ninja & Brush is a totally awesome game. This is real ninja stuff! .NET ninja stuff – but you catch my drift.

Today I went to a Tech Talk at microsoft about ASP.NET performance. Usually these talks contain just enough to not be a complete waste of time – but they are seldomly totally awesome. Today was different. Today a ninja was doing the talk. The Ninja was Mads Kristensen. I have been reading his blog for a while, and knew that he was good. But seeing him live was a whole other ballgame. The dude knows his tech and how to present it.

First of all he cut straight to the chase saying that usually when you talk about ASP.NET optimization – or when MSDN does anyway – they talk about how optimize all the backend stuff. The point is that this matters – but not as much as you think. Usually that part of serving the page takes up less than 10% of the total time the page takes to load… What takes the other 90% is all the CSS, JS and image content stuff. So that is really where you want to optimize.

Taking it from there he ran at a gracefully fast ninja tempo through techniques like minifying, compression, outputcaching, http caching and a lot of other stuff. Techniques that I knew, but I am really underutilizing because they are somewhat hard to get into and get to work properly. Mads showed me that it was just because I was trying to use a screwdriver to drive in a nail. With a lot of handlers and helpers page, most of which were dead simple 1digit-liners, he transformed an albeit simple site page using ASP.NET AJAX, external CSS and JS from 18 HTTP requests and 200K down to 5 requests and 30K – 0K on following requests when cached. Yes – that is no content. Driving the YSlow score up to a whopping 93.

You of course have to use some thought on how to manage caching if you have a highly dynamic site. But with his handlers and helpers in that was pretty simple too. Bottom line – this can be used on almost any ASP.NET site with very little effort. I just can’t wait to get my hands on this code!

Mads. You are truly a ninja. Keep up the great work.

Sunday, February 01, 2009

Global Game Jam 09 is over

…and it has been a blast. 48 hours of game making madness around the globe. I am really looking forward to seeing how much press this event generates and to see the games that have been created. Needless to see very few will have time to play all the games that the 1500 participants worldwide has made. Here we made a massive 29 different games – and we were 150 people. A quick calculation suggests that this will result in at least 250 games made this weekend. Mind boggling. Glad I have the badge and T to show that I was in the event.

So… You are waiting to hear about our game? We went with a safe kick-ass theme: Ninjas and Zombies. You can’t really go wrong there, can you? Our game is called Dusk of Ninja and Brush and it features the last minutes of our hero Ninja while he and his friend Brush is being overrun by zombies.

Won’t go into more detail in writing, because playing says more than 1 gazillion words. But since the Unity windows beta version we used does not have the ability to publish a web player, you’ll have to wait a few days. I promise I’ll get it ready as soon as I get a capable Unity version, and I’ll post the link to the game here.

Wednesday, January 14, 2009

StringBuilder and the immutability of strings

I had a discussion today with Robin, who claimed that there was several ways to break the immutability of strings in C# – most notably by using a StringBuilder to build the string in bits. I found that very unsettling, and decided to make my own tests to see what was happening. Here is a screenshot of the program and the results.


The results are pretty interesting, and not as unsettling as i had feared. It seems that immutability of the string is indeed broken for the string gotten from the StringBuilder as it is not the same reference to the string. This is at least what I thought was one of the porperties of string immutability in C#. Luckily both an Equals call and == report the strings as being the same.

What practical implications does this have? Obviously – do not rely on ReferenceEquals when testing string. But when would you do that. The only case I can think of right now is when testing:


Using “Assert.Same” (in xUnit – Assert.AreSame in NUnit), will fail. This is not something that I would normally do – I mostly use Equals and only Same when I really mean it. If you have a different strategy, maybe it is time to revisit it.

But what is really happening here?! You have to dig a litte bit to find out what is going on. In .NET strings are usually stored in the intern pool. This is the way to ensure immutability. This is true for all cases of literal strings at least. Even strings literals concatted with + have ths ability. But in some cases, apparently when strings are generated from char arrays, like when they come from interop or StringBuilder, strings will be put in the heap instead of the intern table. To the rescue comes the String.Intern() method which will correct all wrongs:


There. Now you know some more about the inner workings of strings. Lesson to be learned: Never ever use ReferenceEquals to compare strings. And if you are forced to – Intern them first.

Friday, December 12, 2008

Robocopy to the rescue

This is a typical situation. I have been trying to find a tool that can solve a problem I have of syncronizing my web files properly. Without any luck. Until today. And the worst part: The solution was already installed as part of windows and has been that way for ages.

Enter Robocopy.

This is a command line file copier that as standard copares files in two directories and copies newer files over. Recursively as well. Nothing revolutionary in this. But here comes the good parts: It can purge extra files for you - typically web pages not used anymore. It can also save your parameters to a job that can be rerun. Very practical for automated builds.

So is there something this wonder cannot do? Yes... Do the sync over FTP. That would be nice. Maybe there is some other tool out there...

Wednesday, November 26, 2008

Adventures in Flash and Python

The last couple of weeks I have been making a nice little Flash application, a questionnaire, for a government project. One of the features of the questionnaire was the ability to print out results based on the answers given.

In order to do pretty printing of the results, I used the awesome AlivePDF library to generate PDFs for the user to print. Neat stuff. It was a bit dodgy to get it up and running - especially because you cannot force a download from your Flash file. Instead the library uses a neat little trick of generating the PDF content in the Flash and then posting the content to another web-page that converts the post to a download-file. Pretty neat stuff.

Included with the library is a PHP script that does that little trick, and I managed to get it up and running. Especially AFTER I discovered that the reason it did not work was that my IE8 beta just didn't accept what was going on. Not even in compat mode. So I had to use Firefox. Discovering that I had to go there was a stroke of luck.

All that out of the way. Or so I thought. Because suddenly things got spicy. Clearing the solution with operations revealed the fact that they had no PHP running anywhere, so could I please make it work with Python? Yes, of course... Because I know Python (NOT). But how hard can it be? The PHP script is only 10 lines of code for crying out loud!

So I got my feet wet in Python. The language itself does not seem that hard, but getting to know the environment and how to test was not easy. But I managed to bang out a Python script that does what the doctor ordered. And only 'round 100 times slower than a real Python haX0r. :P

So if you ever need to use Python and AlivePDF, here is the script. It is missing any kind of error handling code, and may not work in all scenarios. And I will probably not be able to fix it for you. But knock yourself out.

  1: #!/usr/bin/env python
  3: import sys, cgi
  4: import cgitb; cgitb.enable()
  6: query = cgi.FieldStorage()
  7: dict = {}
  8: for param in query.qs_on_post.rsplit("&"):
  9: 	parsplit = param.rsplit("=")
 10: 	dict[parsplit[0]] = parsplit[1]
 13: print "Content-Type: application/pdf"
 14: print "Content-Length: " + query.headers["content-length"]
 15: print "Content-disposition:" + dict["method"] + "; filename='" + dict["name"] + "'"
 16: print
 17: print

Wednesday, April 30, 2008

It must be a sign, but an undeciferable one

How come that the day GTA4 should have arrived in my mail, the postal delivery people in my region decides to go on a strike. So I am left out of the party even though I had done my duty and pre-ordered the game. Luckily they are showing Chelsea-Liverpool CL semi on Viasat TV6 which I can watch for free with the digital tuner in my new TV.

Nothing as cruels as this happens without a reason. So. What is there to learn? Never pre-order a game? Could be. With Halo 3 I got a fair warning getting to pay more than I had to - even though Coolshop has a pricing guarantee. Never covet a game so much that your belly hurts? Actually I am still looking more forward to Rock Band than I had to GTA4. But the last couple of days the GTA fever got to me too.

Bottom line. I am angry, hurt and confused. And I don't know why...

Tuesday, April 29, 2008

Installing on consoles

One of the reasons that I have ditched PC gaming in favor of console gaming is that it is just much easier. I mean, buy the game, put the disc in the drive, off you go. Can't get better than that. But in the next gen game things have changed somewhat. Sometimes when i put the disc in my 360 I need a title update. So an installer has been downloaded, runs and reboots either the game or the console. Usually this doesn't take long and you are back in the action.

This is all well and dandy but the other day when I wanted to try out the Gran Turismo 5 Demo, oops, PROLOGUE, on the PS3, I got a nasty surprise. Not only did the game need an update of the PS3 core system - which took a couple of minutes - but before starting the game proper it needed to install to the harddrive... WTF?! Seriously. I was treated with a non-progress bar teling me how the install went. Five minutes later it read somewhere between 5 and 10% complete, so I decided that I really needed to get back to work instead of this.

Now I don't own a PS3, so I don't know if this pain is something every PS3 owner needs to endure before every game experience. What I do know is that it is the most mind numbingly stupid thing I have experienced on a console. Not only have they ruined the slot-in-and-play experience that makes consoles such a joy. They have introduced onto the user the ardurous task of disk management. Good grief. And for what? Reducing load times. Bullcrap. I have no hard stats on this, only a gut feeling. But apart from the odd elevator experience, load times are not that much worse in this gen. Developers need to get clever in order to alleviate this until we get a new portable media with almost instant load times. With this moves, Microsofts seemingly troubling decision of making the hard drive optional on the 360 suddenly seems like a wise defense against developer zeal.

Let us revisit the issue of the game requiring a specific system version before playing. In my book this is also somewhat unsettling. It stresses the feeling that the PS3 system was not finished from the get go. But worse, can you trust Sony not to let this slip the other way so that you cannot play an older game on a newer system revision? You have to wait for the devs to put out an update for the game. Potentially rendering some unplayable for an uncertain amount of time.

All in all this is not the way to go. Not only did Sony mark the wrong date in the calendar for the next-gen party. They showed up unprepared, sloppily dressed. They didn't bring anything to the bar and all their exclusive friends are doing good business in other circles. Now they are acting obnoxious. No wonder the hard-core gaming audience has moved to another room to party. People who bought the system to play games are really getting thrashed with the short end of the stick. The system is really only attractive as a home theater system. And that is quite a different party.