Thursday 23 July 2020

UEFI and the GPT

As I said in my previus post, I've not kept myself much up to date with the evolution of hardware for the last decade, so I had missed another huge change (not so noticeable as the SSD-NVMe thing, but highly important anyway). I'm talking about the modern UEFI and GPT partitions vs the old BIOS and MBR. You can read a short and clarifying article here.

In the past, we had the BIOS and in order to boot our OS's it needed a MBR in the first sector of the disk. This MBR could store information of up to 4 primary partitions as long with the boot loader. In order to have more than 4 partitions you needed to set one of those partitions as an extended partition.

Modern computers (since 2011 I think) come with UEFI rather than the old BIOS. UEFI can work with a MBR, but it's intended to work with a GPT. This new partitioning scheme is much more powerful, allowing for up to 128 primary partitions. There's still a MBR in the first sector of the disk, the Protective MBR, in case you want to use this disk with a BIOS based computer. After that you have the GPT lied over the next disk sectors (and with a backup copy right at the end of the disk), like this:

UEFI systems use an EFI System Partition that is normally the first disk partition and contains:

An ESP contains the boot loaders or kernel images for all installed operating systems (which are contained in other partitions), device driver files for hardware devices present in a computer and used by the firmware at boot time, system utility programs that are intended to be run before an operating system is booted, and data files such as error logs.

Usually USB drives and external disks come with a MBR partitioning scheme, but if you intend use it as a bootable USB device with multiple partitions you could find useful to change it to use a GPT scheme.

Sunday 12 July 2020

My Laptop Flies

I've recently bought a new laptop, nothing out of the ordinary, 8GBs of RAM, 512 GBs SSD, 4 physical cores - 8 logical ones... in principle it's just a bit better than the laptop I got at work 2 years ago, so I got shocked by how fast it boots, the Windows 10 logon screen shows up in no time! I then installed Ubuntu, and it's the same, it hardly takes 10 seconds for the laptop to be fully operational. I assume the Windows 10 in my work laptop is bloated with some corporate stuff... but anyway, that can not account for the difference, my personal laptop boots like 10 times faster! When I got my current work laptop I thought the SSD would make a huge difference (all my personal laptops so far had magnetic hard drives), and well, it works obviously faster, but I did not get the "Quantum Leap" feeling that I'm getting now with my new one.

I did not think more about it, just enjoyed the situation :-), but a few days ago when visiting the local shop (yes, I'm an old school idiot that whenever possible favors buying stuff in a physical store rather than online, Fuck you Amazon and alikes!) for another reason, I mentioned it to the owner and he told me "yes, it's really amazing, it's all thanks to the new NVMe thing, and yes, your laptop comes with that for sure".

Far away in the past I used to be quite into hardware (northbridge-southbridge chipsets, the different buses, the internals of processors and GPUs...) but that's not been the case in at least the last decade. Before buying this laptop I did some "research" to make sure I was buying something "decent". The 8 GBs RAM and 512 GBs SSD had been clear to me since a long while, but I learned that I should discard i3 processors and go for an i5 (or in my case an AMD Ryzen 5), and that I should have a USB C port (ideally it should be USB C charging enabled, but in the end few laptops support that so far). I was surprised to note that some processors do not implement SMT (HyperThreading in Intel's parlor...), so you don't have the physical vs logical/virtual cores thing. You'll still find for sale laptopts with a 4 physical/virtual cores, and even laptops with 2 physical/4 logical cores... So the 4 physical/8 logical combo became also a must-have for me. The other must-haves were: 14" screen, weight below 1.5KGs and an ethernet port. As for this last requirement, I honestly can not understand how someone can buy a laptop without an ethernet connector (yes, you can use an usb-ethernet adapter, but anyway...). Both in Asturies and in France, with the same laptop, my WiFi speed would never go above 50 Mbps, while the ethernet speed would be around 160-190 Mbps, very close to the official limit of 200 Mbps for both internet providers. On the other side, a touch screen was not in my list. My last laptops both have touch screen and I hardly ever use it.

So I was totally unaware of this NVMe thing, and it seems to me as an improvement as big (if not bigger) as when the multicore processors where introduced. This article is a pretty nice introduction. As a summary, we all know that SSDs are much faster than magnetic HDDs (with their moving parts), but until recently we were accessing them through a slow SATA bus, that was acting as a bottleneck. Now we're using the NVMe protocol over the PCI express bus. This takes advantage of the parallelism provided by SSD's, and we end up with this (from the article):

NVMe SSD reads and writes data literally four times faster than the SATA SSDs found in previous generations. Not only that, but it locates them 10 times as fast (seek). That’s on top of the four- to five-fold improvement in throughput and ten-fold improvement in seek times that was already provided by SATA SSDs when compared to hard drives.

Thursday 2 July 2020

Throw Expressions

I've not been aware until a few days ago that C# (since version 7) features throw expressions, meaning that we can throw exceptions in an expression, not just in a statement. This is very useful combined with the "?" and "??" operators. I've put up an example:

 

class Salary
{
   public string Payer {get; private set;}

    public int Amount {get; private set;}

    public Salary(string payer, int amount)
     {
        this.Payer = payer ?? throw new ArgumentNullException(nameof(payer));
        this.Amount = amount;
     }

     public void IncreaseByPercentage(int percentage)
     {
         this.Amount = percentage > 5
            ? percentage + (this.Amount * percentage / 100)
            : throw new Exception("Increase percentage is too low"); 
     }
}

This nice feature is missing so far in JavaScript, though I think it's been proposed. For the moment we can use an arrow function to get a similar effect, though the resulting code is quite more verbose.

 

class Salary
{

    constructor (payer, amount)
    {
        //does not compile "throw expressions" are not supported so far
        //this.payer = payer ?? throw new Error("ArgumentNullException: payer");
        
        this.payer = payer || (() => {throw new Error("ArgumentNullException: payer");})();
        this.amount = amount;
    }

     increaseByPercentage(percentage)
     {
         this.amount = percentage > 5
            ? percentage + (this.Amount * percentage / 100)
            : (() => {throw new Error("Increase percentage is too low");})(); 
     }
}

It's interesting to note that theres a new feature proposed for C# 9, Simplified null parameter validation that would allow us to rewrite the constructor above like this:

 

public Salary(string payer!, int amount)
     {
        this.Payer = payer;
        this.Amount = amount;
     }