Bloomiboy Babble -- Technical, Personal...it's all here...

Here's where I do a brain dump of everything I feel like throwing out there...it's part technical, part personal, part crap, and hopefully you get to learn something...sometime.

Saturday, January 13, 2007

Siva-isms -- the ultimate roommate compilation

For the uninitiated, Siva was my apartment mate for 3-4 months in Madison. He was the ultimate room mate one can ever ask for whose replies sometimes (ok, ok, most of the times) can leave you gasping for air...you just wouldn't know what to do!

I call them "Siva-isms". In order that I not lose track of those, I compiled them into an email to be sent out to our local Madison group and I'm reproducing those here.
---

1.) "...so can I put my mouth inside and pull it out?"

Siva to 'secret person' when Siva wanted the chewing gum the 'secret
person' was eating.

2.) "...he's so PROFOUND!..."

Lisa talks about Siva when he made a superb comment in their project
meeting.

3.) "...whaaaaaaaa...??!"

Lisa to Siva when he makes another profound statement.

4.) Siva to a "regular" visitor to our house: "...you are a regular
visitor here, I don't need to tell you to f*** it, s*** it, etc...."

5.) Siva makes a statement at 1:31 am in the morning: "If you want to
eat, you should eat something now...tomorrow, one never knows if they
will be alive or not..."

---
Siva's favorite food ingredient: egg
Siva's best dish I've eaten so far: egg curry
One thing that you will always find in our fridge: Egg cartons
---
Three things Siva cannot live without: His laptop, an Internet
connection and tamilmatrimony.com
---

Me: Siva, let's watch some movie man...
Siva: Ok, let me find some DVDs from my room...
Me: Which one are you looking for?
Siva: Polar Express or something else maybe, let me find it...
Me: Ok....
<after some time...>

Me: Did you find it?
Siva: Yes man...

<and then he walks straight into the bathroom>

Me: Errr...where are you going man?!
Siva: I'm going to the bathroom to take a bath...

Me: I thought you said you found the DVD...<confused>
Siva: No man, I found my underwear which I was searching all this time...
Me: <stunned>
---

<ring,ring>
Me: Hello...
Siva: Hi, This is Siva man...where are you?
Me: I'm still in Minneapolis...

Siva: Oh, you are still there?
Me: Yeah, what did you think I was doing?
Siva: I thought you were home watching TV...
Me: No, I'm not...and what are you doing?
Siva: I'm at home, watching TV...

Me: <speechless>
---

Labels: , ,

Sunday, January 07, 2007

Ever challenged a Cop?!

Well...when you think about it, it may sound like a movie plot but the truth is that sometimes you get into situations where Sleep Induced Hallucination (my own term for a situation wherein just getting up from sleep gives you great POWER, albeit momentarily...) takes control and you blurt out what you should not have.

So read on for an incident, narrating which gives me great pleasure to notice the fact that I'm still alive.
--
Spring break holidays. Students are making preperations to decide where to make the trip of their dreams. And then there are others...

For the sake of brevity, we will restrict all the conversation to be between 3 primary characters of the story: DealO, SilentBob and me.

DealO -- he was the gentle giant. If you don't cross his path, he will try his best not to crush you.
SilentBoB -- as his name implies, he speaks *only* when needed but when he does, you better be prepared to be flabbergasted.
Me -- the usual suspect as always.
---

DealO: "Hey bloomiboy, let's plan a trip this weekend man...I want a break from Madison."
Me: "Ok".

These are those times when I feel that my enthusiasm for outdoor fun should be somewhat influenced by the atmospheric conditions instead of an every-affirmatie answer. I call this HBS -- the Hyper Bloomiboy Syndrome.

It was snowing cats and dogs (for lack of a better term) and people were struggling just to get back home and here we were planning a trip to hell...and back.

DealO: "Let's go to Mount Rushmore..."
Me: "Ok".

HBS strikes again.

A look at SilentBob and his customary nod comes into play; apparently he hasn't looked out of the house for hours now.

We call the car rental agency, and to our surprise, we get a fleet of cars to choose from. Hmmm, shouldn't that have rung a bell as to how good a climate it is for driving? that too over the weekend? that too over the LONG weekend?! Good sense never prevails when we need it.

So we snare 3 other folks and decide to rent 2 cars for the trip. One of the car consisting of the DealO, SiletBob and me -- to be driven by me with DealO in the front passenger seat and SilentBob in the back seat. We decided to leave on Friday evening, drive 12 hours, stay for a day and drive back 12 hours. Sigh...how I miss those good old days of vacationing for a week and relaxing wherever we go.

So off we went, the car literally ploughing through the already 5-6 inches of snow with the weather showing no signs of abatement. It was only a matter of time before we would need something like a car built for the MI5 agent, James Bond, with an option to melt snow in it's path.

The other car was right behing us when we hit the Interstate highway. The only other vehicles on the highway were the trailers with their enormous 18 wheels crushing the snow in their path. I was driving behind one of those mammoths since it was best to let it define a path which we could follow. This went on for an hour after which I gathered enough confidence to overtake the truck and speed a little bit. At that rate, it would have taken us double the time to just reach Rushmore!

The companion car behind us followed suit, but they were lagging behind. After about another hour or so they were no longer in sight. We were not too concerned but wanted to make sure that they were OK. DealO decides to give them a call.

DealO: "Hey guys, where are you?!"
Car2: "We are right behind your car..."
DealO: "Hmmm, the only things behind us are the tracks of the car as far as we can see..."
Car2: "No, no, we are very close, no worries..."

We planned to keep driving as we were, with DealO following up on their progress every 15 minutes. After 3-4 such calls...

DealO: "Hey guys, we really can't see you..."
Car2: "Uhhh...actually our car has fallen into a ditch and we are waiting for some help..."
DealO: "What?!"

Hearing DealO scream was enough to jolt the car and it's passengers.

Car2: "Actually, there's not much to worry about...we will be on track in about 30 minutes, let's keep going"

Somehow, it did not sound convincing but we were too motivated to stop now. The snow had not stopped. The visibility had reduced to around 10 meters in front of us and our speed had dropped to 25 miles/hour. Car2 was not in sight. Things were looking ominous for the trip. We decide to take an exit road and stop there until car2 shows up.

So here we are: stopped directly in the middle of an exit with no sign of humanity except the sign boards. The wind is bellowing outside and we decide to rest for some time. The funny part of resting when you are driving is that you never know when you transition over from the real world to the surreal one. All of us were soon dozing off.

Zzzz...

The next thing I remember is being woke up by SilentBob...I squinch my eyes, look in the rear-view mirror and see a Cop car parked behind us with t;s flashing lights. I wake up DealO, which was more like waking up a bear from hibernation. But this was no time to worry about sleeping preferences; we needed to be ready for some answers.

I see the cop get out his car, his hand on his gun holster, slowly walking towards our car. He comes towards my window and knocks on it. I slide down the glass.

Officer: "What are you guys doing here?"
Me: "Sir, we are waiting for our other car to show up. They should be here any minute."
Officer (with an unchanged expression): "Do you know you are parked in an exit?"
Me: "Yes". Oh, oh, not a good answer.
Officer(not very pleased now): "Well, you should not park here. Carry on to the exit and park at some gas station ahead."

Me: "Ok officer, sorry for the trouble..."

The officer then starts walking towards his car.

I feel a slight quake then...Oh phew, it's DealO who has completely woken up from his slumber...or sort of.

Me: "DealO, a cop just told us to move from here, so I'm going to drive ahead and park elsewhere..."
DealO: "Cop?...Let me talk to him...we can stay right here, why should we move?!"

I'm thinking now: WHAT did he just say?! And even before SilentBob or I could comment on his question...

DealO: "Hey, YOU!!"

you, yoU, yOU, YOU?! He called the Cop "YOU"?! Another drastic effect of Sleep Induced Hallucination. At that time I wished I was hallucinating. I wished that this whle trip was a figment of my imagination and that I was actually seated in a toilet. I REALLY felt my stomach churn now...but the damage was done.

The Cop had now turned back towards us.

Cop: "WHAT?!"

His relaxed walk has changed into a more alert one and his hand was again on his gun. I could see all this in my rear-view mirror. I'm now gulping saliva as if I'm drinking Pepsi.

By this time, DealO has realized the enormity of his statement and I can almost hear him whisper "Holy s***!"

The cop knocks on DealO's window which DealO lowers slowly.

DealO: "Errr...Is there...ummm...a gas...uhhh...a gas station close by?"
Cop(with a stoic and unemotionless face): "I don't know!"
DealO: "Do we have to...errr...move from here?"
Cop: "Yes! I already told your friend. Now MOVE!"

At that time, I don't think I needed to look at DealO to get an affirmative nod from him. I started the car immediately and waited for the cop to return back to his car before I start driving. We were in sort of a panic mode...well, all except SilentBob. He's as usual enjoying the proceedings.

Now that we had parked for almost 30 minutes, there was quite a bit of snow that had accumulated on the car windshield. When in panic, common sense does not apply. I start driving...with no idea where I was going; thanks to the accumulated snow. It was SilentBob who spoke his first words of the trip and something I was always very thankful for.

SilentBob: "Blomiboy, look where you are going!!"

It was then I switched on the wipers to remove some of the snow and then negotiate the exit curve correctly. Thankfully I was at a very slow speed due to the snow, otherwise we would have ended in a ditch ourselves just like car2.

Epilogue:
We reached a gas station which was right at the end of the exit where we had stalled (just our luck!). Car2 was still in the ditch. Finally, good sense prevailed between the three of us and we decided to call this trip off. In hind sight, this was the correct decision since we had travelled only about 100 miles in 3 hours!

But...history did not repeat itself the next year. We planned the Rushmore trip, the same time of the year, back to Mount Rushore and had a successful and enjoyable trip with more folks than before!

Edit: Silent Bob pointed out that car actually told us at one point of time that they we ahead of us! When in fact, they were rotting in a ditch! Tch, tch...

Labels: , ,

Wednesday, September 27, 2006

.NET and Strings and some interesting tidbits

Strings are one of the simplest yet most abused data structure in most programming languages, including .NET. With the support of String to be a native data type and hence the possibility of it's rampant usage, it makes it even more important for us to understand how they work.

Two important things to be considered as part of the System.String or simply string implementation in .NET are:
1.) Strings are reference types (although they are primitive types)
2.) Strings are immutable -- more about this follows.

Without going into details of another .NET programmatic paradigm called AppDomains, let me simply say that they are light-weight processes, just like most can imagine Threads to be, but definitely above that. AppDomains can be thought about the be independent light-weight processes that allow data to be shared across them without the overhead of a process. They have all the independence of a process without any issues like one crashing another and hence provides isolation.

The .NET framework defines and loads it's own standard AppDomains within the execution of the CLR and the other .NET programs. The following image has been reproduced from here.


The only thing of interest to us here in this article is the System Domain, and a specific part of that called the "Global String Literal Map". This is nothing but a mapping of all strings that get used within the execution of all .NET programs within the context of a CLR instance. Simply said, the strings across the global space of the CLR is shared and hence avoid their creation multiple times over. To achieve this, they have to be immutable.

The immutability of strings is again something that is new as compared to other programming languages. What this does *not* mean is that string cannot be modified. They can. What this means is that any string, if changed, would create another string whose address would then be different. The previous string will still be stored in the system (provided it is in scope) and not be modified. There references are maintained by the "Global String Literal Map" in the System domain, by the CLR.

So, anytime you do a strResult = strVal1 + strVal2, you are creating a new string as expected, but compared to strVal1 = strVal1 + strVal2, you end up creating a new strVal1 while the previous value of strVal1 also remains although not pointed to by strVal1.

Here's another example, which is somewhat analogous.

string str1 = "Some String";
string str2 = "Some String"; //note str1 and str2 are literally the same.

? Object.ReferenceEquals(str1, str2) will return true in the Command window. This means that they are pointing to the same global storage of string data.

This process in .NET-speak is called "Interning".

This specific behavior of string leads to bad programming when used without prior design. String manipulation in .NET is mush faster as opposed to it's prior versions such as Visual Basic, but is still substantial. .NET stores all string representation as Unicode value, which means at least 4 bytes per character to be represented. To avoid all this extra storage would be a good thing to do. This is a tip that you may find all over the 'net, but usage of the StringBuilder class (provided by .NET) for significant amount of string additions is the best thing you can do to get your program to run X times faster, where X depends on how bad a programmer you are. ;)

The System.Text.StringBuilder uses a combination of preallocation of storage and string represented in array format so that repeated addition of string data is stored in subscript values instead of new strings being created. Finally if you need to get the string value back, you can use the ToString() method. The usage of this class is highly recommended, when possible.
---

I did want to write about one interesting aspect of the optimizations performed by the .NET CLR. It highlights how one has to tailor's one's mind specific to the framework of the language under consideration. The usual thought process is that C# is so similar to C, and so any C programmer can easily adapt to C# and be able to write great programs; it does hold true under most cases, but there are always cases when common logic belies observations.

Let's us consider two simple loop, placed side by side for comparison:

Loop 1


int[] intArr = new int[10000];
// timer start here
for (int j = 0; j < intArr.Length; j++)
{
intArr[j] = j;
}
// timer end here

Loop 2

int[] intArr = new int[10000];
// timer start here
int length = intArr.Length;
for (int j = 0; j < length; j++)
{
intArr[j] = j;
}
// timer end here



Loop 1 requires the length of the array to be checked at every loop iteration to decide whether the go ahead or not, while the other pre-stores the length of the one-dimensional array and uses that to make the decision. Given a decent C background, one is bound to believe that avoiding extra comparisons will make it run faster, hence Loop 2.

Which one do YOU think will run faster in .NET?

The answer is below in white font color, highlight it with your mouse to read it...


In .NET, Loop 1 will run faster. The CLR sees that the comparison factor is the Length property of the array, which can never be an unacceptable value and consequently avoids all checks for the "ArrayIndexOutofBounds" condition. Hence it runs faster.

Enjoy.

.NET: NGEN versus JITting

For those not very familiar with the details on how the Common Language Runtime (CLR) for .NET functions, this will come as a surprise.

The basic premise of the CLR was to delay the interpretation of the .NET assemblies until the they actually get used. Actually, the level of granularity is more at a function level than at a module level. This is to say that every function gets "Just-in-time" compiled (called JITting) when it is first called, not when a module is loaded in memory. This late execution model was a decision made based on application execution heuristics. We are assuming that this study has been made over a large variety of applications making sure that the working set encompassed a wide range of scenarios to match every day program executions.


Windows executables for .NET are called Portable Executables (PEs) for reasons that is self-explanatory. The format what is fed into the JIT compiler is called the Intermediate Language (IL or sometime s MSIL).
So basically,
NGEN (for "Native Generation"): pre-compiled code
JITting (For "Just-in-time" compilation): compile code only when needed .

Each has it's own good points and shortcomings.
---

Your argument will seemingly be this: Why would I prefer JITting over NGEN anytime since I would need to JIT my function once per execution of the program? It's not as straightforward as that. NGENing your program will generate the compiled version of the IL for that specific machine. It will then become machine specific (more or less) but depending on how much of it is x86 compatible (and we assume it is 100%), should still allow you to run it on various machines. With the kind of machine heterogeneity out there, you definitely don't want these kind of dependencies. With machine-specific code, you always face the "hit" of falling back to use unoptimized code in case the specific assembly instructions are not available on the machine (e.g. MMX).

Note that this was one of the basic premise because of which the Internet is extremely successful.
Java users will note the distinct similarity between IL and Java bytecode interpretation, the JITter and the JVM, etc. There are definitely parallels but the usage is dramatically different.

There are some things one should be aware of when deciding between NGEN and JITting:
  • The resulting native image is, as a rule, generally larger than the corresponding IL code. As is the case with many applications, many methods are not executed during the course of an instance of the application. Thus, the in memory image of the code would be larger if the entire assembly is stored as a native image.
  • The native image cannot reflect the way that the code is actually used. By dynamically compiling, the .NET infrastructure (CLR) can adjust the code image to reflect the current state of the application and machine. For example, some methods may end up occupying a different virtual memory page than the rest of the application, causing a fragmentation which has a negative impact on the working size of the application.
  • By producing a native image, the image is now strongly bound to specific versions of other assemblies. Essentially, a contract has been established where the native image is dependent on specific versions of other assemblies being available, down to a level of a module version identifier (MVI) being assigned to each module when it is compiled. The MVI is a unique identifier that is stored with the native code to allow it to check other compiled dependencies to verify that the correct version of the module is being loaded. If it is not, the CLR must fall back to the IL version of the code and use JIT.
The above are not specific instances, but more to be uses as a defining guide for your decision making process.

Best scenarios for NGEN would be to make sure that there is not too much of code revamp in your application, i.e. frequent changes. Good choices would be code heavily dependent on mathematical or graphical operations that can be optimized largely with CPU-based optimizations. Examples of code that does not change can also serve as good candidates, e.g. the .NET framework itself.

In conclusion, JITting (the default behavior) should work for most of your cases, and NGEN should be seen as a performance enhancement for very specific scenarios.

Sunday, September 17, 2006

got my MacBook!

I finally got my MacBook -- one word: ROCKS!

I went for the most basic macbook edition pictured here. (Apologies, if the link is dead after a few months or so...). It was the 1.83 Intel Core Duo with 512MB RAM and 60GB HDD.

Looks wise, this is the best laptop I have ever seen. Ergonomically, Apple has done an amazingly good job or making sure that the laptop features do last for a log time to come. Some examples:

1.) MagSafe power slot: Have you walked across a room in a hurry only to find that you tripped over the power cord of your friend's laptop which then flew across the room in that melee? No more issues. The MagSafe cord will ensue quick disconnection when you yank it beyond a threshold. The magnetic feature of the power slot ensures that.
2.) CD drive: They are the slip-in variety (like the ones found in cars), rather the usual tray-based ones.
3.) Inbuilt Camera (iSight): Just too good, the resolution and the pinhole sized camera just take your breath away, not to mention the microphone alongside.
Others: soft keys, large trackpad, inbuilt Bluetooth support and he list can go on and on.

From the software perspective, Mac OS X (Tiger) is too sleek. With it's inherent security support and detection capabilities, it just takes your breath away. If you have used Solaris GUI or Linux-based XWindows, then Mac OS X is eons better than those.

An amazing value for money package, is you ask me. In fact, I'm posting this blog from my Mac...;)