Aug 3, 2013

Interesting tricks with value types in .Net

A while back I was pairing with a team mate hunting a memory allocation that should not be happing (it was not a leak but it was in a method called thousands of times per second and we do believed it was not required (or at least we should try to avoid it at all costs).

After some code inspection we nailed it down to a foreach loop, something as simple as:

var list = new List<int>();

// the code bellow allocates memory. 
foreach(var item in list)
{
}
Interestingly enough, when compiled against C# 3.5 / 4.0 compilers we could not reproduce this behavior; it was only when compiling on Mono 2.6 (on mono 2.10 it didn't happened).

After some investigation (and head banging) we found the issue; for reasons that I'll not explain here (basically to avoid gc allocations) the enumerator returned by List is a value type but it happens that in order to fulfill the foreach contract it also implements IDisposable. On .Net (at least on 3.5) it happens that the compiler completely avoids boxing this enumerator but when compiled with gmcs (the mono cs compiler) 2.6.5.0 a box gets generated for the code that calls IDisposable.Dispose()! (see the code bellow):

finally
{
     IL_0048: ldloc.2
     IL_0049: box valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator< int32>
     IL_004e: callvirt instance void [mscorlib]System.IDisposable::Dispose()
     IL_0053: endfinally
}
Well, it happens that MS C# compiler (and newer mono ones also) emit different IL for that finally block:
finally
{
     IL_004b: ldloca.s 3
     IL_004d: constrained. valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator< int32>
     IL_0053: callvirt instance void [mscorlib]System.IDisposable::Dispose()
     IL_0058: nop
     IL_0059: endfinally
}
See, no box instruction. Instead of boxing, the compiler loads the value type address and then asks the runtime to handle that address as a reference to IDisposable.

Really interesting (at least IMHO)

Happy codding!

Aug 2, 2013

Getting Windows 8 on a Macbook Pro

I am typing this post on a new, shine Mac Book Pro with a retina display, a big SSD and lots of memory ;)

But instead of using Mac OS I decided to go through the bootcamp route and install Windows 8 (sorry, I cant't code in C# without Resharper anymore :)

Well, since this solution is an official solution (from Apple) I expected everything to go really smoothly and without any major issues (other than the Windows 8 ones... :). This was my first disappointment with Apple; first I tried 3 different flash disks (pendrive) but bootcamp just hangs without any information why (no one can blame Windows for this ;). Some times it just said "an error occurred while copying windows files to the flash disk" (or something like that). 

I even tried 2 different external hard drives with the same outcome. In the end I decide to do it the way the "pros" does, i.e, I manually created a bootable flash drive using a Windows 8 x64 image, used bootcamp to install it (but not to install the drivers) and as soon as Windows booted after the installation finished I downloaded and installed the bootcamp drivers manually.

Finally I had an working machine (hey, it is really fast) (moving from an old Q9400  to a new I7 machine makes a huge difference) and was happy... until trying to close the notebook lid (expecting to put Windows to a good sleep) and noticing it just turned the computer off. 

While googling trying to understand what went wrong I realized this is a relatively common issue with this setup (at least lots of people was asking similar questions in various online forums); the problem is that it looks like some device driver don't support sleeping. You can confirm this by running the following command in the command line:

powercfg -a

In my case I just got this:

The following sleep states are available on this system:
    Hibernate
    Fast Startup

The following sleep states are not available on this system:
    Standby (S1)
The system firmware does not support this standby state.

    Standby (S2)
The system firmware does not support this standby state.

    Standby (S3)
The system firmware does not support this standby state.

    Standby (Connected)
The system firmware does not support this standby state.

    Hybrid Sleep
Standby (S3) is not available.

Notice the line saying the system do not support S3 sleep state; this is exactly the state the machine should enter when its lid is closed.. 

Later I found this joy in a FAQ on Apple site:


23. My Macintosh running Windows 7 or 8 does not sleep when a Thunderbolt device is plugged in.  Is this normal?
Yes. Sleep is disabled in Windows 7 or 8 while a Thunderbolt device is plugged in.
Well done! Anyway, it looks like I do have an workaround; do not use thunderbolt devices. I'll try.

What do you think?

May 7, 2013

Once again

Hi

Well, I have been busy lately but I can't refrain myself from posting this; recently I have got tons of phishing emails claiming they are from local banks. In most of those financial institutions I'm not even a customer! Anyway, it doesn't hurt to try :)

Bellow you can see the original message, but pay attention to the email sender (xxxhrs@webxc31s05 and goes on). They don't even bothered to use something more realistic. There is only a single link at the bottom of the page pointing basically to www.djentachi.kz which definitively does not sound as a valid web address for this bank :)


The bottom line: as always be careful with everything coming into your email box.

Adriano

Mar 8, 2013

SSH: using specific keys with specific hosts

First of all, I have never thought I'd post something Unix related (not that I have anything against it; it just because I am more a Windows user :)

It just happens that for personal reasons I have some VMs running Ubuntu and from time to time I need to transfer files from my host computer to the guest, i.e, from my Windows machine to Ubuntu running on the VM. Since the virtualization solution I am using has some issues regarding file/folder sharing between host/guest OS, I am using scp to do the job.

Until today I used to have a single RSA key pair to authenticate me when connecting to my Ubuntu VM and also to github and bitbucket but this has proven to no be an optimal solution. You see, when I am copying files to Ubuntu I'd like to not be forced to type any password, after all this VM has no sensitive data and is off limits since it is not connected to the internet, but since I am using a single RSA key pair for most of my authentication  needs I don't want do leave it unprotected so every time I want to copy something to the guest OS (my Ubuntu running on the VM) I find myself typing a huge, complicated password - actually very often mistyping it :(.


Today I took the time and decided I'd find out how to setup multiple RSA key pairs and use one with no password at all to authenticate me with my VM and one with a strong password for each online service I use.


Actually it was easier than I thought ;). Basically the utilities I rely on (ssh and scp) allows one to specify which identity file (the RSA file containing your private key) (-i file_path) is to be used.


if you don't want to specify which file should be used every time or if the utility you use don't allow you to specify this file (but uses ssh behind the scenes) you can use ~/.ssh/config file!

Happy codding.

Mar 6, 2013

New types from .NET 4.5

Hi.

While experimenting with WPF programing I've just stumbled on a nice, new class in .Net 4.5 (thanks to Resharper) named CallerMemberNameAttribute.

The first thing you can figure out based on its name is that it is an attribute. It's only applicable to parameters and is meant to make developers live easier when it comes to find out the name of the caller method / property / event / etc :)

Basically you add this attribute to any parameter of type string with a default value and the compiler will do its magic.

Bellow you can see an example:

public partial class Form1 : Form
{
	public Form1()
	{
		InitializeComponent();
		
		button2.Click += delegate
		{
			Foo();
		};
	}

	private void Foo([CallerMemberName] string caller = null)
	{
		MessageBox.Show("Called from : " + caller);
	}
}

This is really useful for implementing INotifyPropertyChanged interface.

Note that this gives you only the name of the member calling the method, not the type in which this method is defined.

As any other feature developers should not abuse it. For instance, IMHO, using this feature with indexers should be avoided:

public partial class Form1 : Form
{
	public Form1()
	{
		InitializeComponent();
		
		button2.Click += delegate
		{
			MessageBox.Show( this[0] );
			MessageBox.Show( this[1] );
		};
	}

	private string this[int n, [CallerMemberName] string caller = null]
	{
		return n > 0 ? this[n-1] : caller + "(" + n + ")";
	}
}

For more information, look into the documentation.

Happy codding!

Jan 4, 2013

The end of the world is approaching...

Hi

First of all, Happy new year! :)

Last night I was surprised (upset?) for getting an offer (that I could not refuse) from a well know web site (which I like pretty much, by the way) claiming that that was my last chance to subscribe to their 1 month free premium plan.

I have absolutely no problem with the offer. What gets on my nerves is the fact that they have been sending  this very same offer for the past 6 ~ 9 months, saying each time that that was my last chance

Come on guys. You should already have noticed that I even went to your website and started the registration process.... you should ask yourselves why I did not finished it, instead of keeping bugging me ;)

See you!


Nov 15, 2012

Learning git

If you are a software developer you should already use some sort of SCM (if not I urge you to start to learn/use one now!). 

If you have not tried Git yet you should give it a try (but keep in mind it may take some time until you really appreciate its power).

I have been working with Git for at least 2 years and I am amazed how flexible / powerful it is. Actually it is so powerful I still learn new stuff about it almost every other day! Bellow you can find some useful resources to learn a little bit more about it:

Have fun!