Lucio Tato

Really enjoy programming.

Read this first

golang Duff’s devices

I’m just starting to scratch golang surface, and until now it has been a pleasant experience.

golang feels solid and fast.

Let me share with you a few golang internals on this notes as long as I’m stumbling upon them:

Today I’ve tripped over some golang source code that looks like a copy-paste frenzy, but is a very clever assembler trick to make the processor go as fast as possible:

The code is in runtime/asm_amd64.s and looks like this:

    STOSQ
    STOSQ
    STOSQ
    STOSQ
    STOSQ
    STOSQ
    STOSQ
    STOSQ
    STOSQ
    ...

… repeated 128 27 times

and later:

    MOVQ    (SI),CX
    ADDQ    $8,SI
    MOVQ    CX,(DI)
    ADDQ    $8,DI

    MOVQ    (SI),CX
    ADDQ    $8,SI
    MOVQ    CX,(DI)
    ADDQ    $8,DI

    MOVQ    (SI),CX
    ADDQ    $8,SI
    MOVQ    CX,(DI)
    ADDQ    $8,DI

    ...

… repeated, again, 27 times, (512 LOC)

It turns out that this is a...

Continue reading →


golang first impressions

What did I do with golang until now:

  • Install on linux? great: fast, seamless.

  • Workspace organization? great, simple, clean (wks/src, wks/bin, wks/pkg, and also wsk/src/github.com/user/project)

  • Compile on linux? (small test programs) great: code auto formatted, then a multi-threaded 64-bit executable produced and installed on a single command in milliseconds.

  • get golang source on linux? no problem.

  • Compile golang source on Linux? no problem (I did this to cross-compile the multi-thread test as 32-bit exe on a 64-bit machine, to test torn-reads… but this is for another post)

  • Install on windows? fast, seamless.

  • Compile on windows the same small tests? no problem.

View →


Iteration vs. Recursion, from scratch

I’ve read in the interwebs than iteration or recursion is a matter of choice, … that they are somewhat “equivalent”…

I don’t think so.

Let’s compare “Iteration” vs “Recursion” by analyzing which core concepts are required to implement each one of this concepts:

Let’s start from scratch:

Question: What do yo need to compute something, to follow an algorithm and reach a result?

We will try to establish a hierarchy of concepts, starting from scratch and defining in first place the basic, core concepts and after that the concepts which are constructed from previously defined concepts.

  1. State: Memory cells, storage: to do something you need places to store final and intermediate result values. Let’s assume we have an infinite array of “integer” cells, called Memory, M[0..Infinite].

  2. Instructions: do something - transform a cell, change its value. alter state. Every interesting...

Continue reading →


Adding a new dimension to source code. Literally.

Note: I could have titled this “Curly braces considered harmful”, but titling like that is now considered harmful

Unidimensional source code

Source code for the most popular programming languages is unidimensional. Just a unidimensional stream of unicode points. This is an artifact if you like of the Turing machine model, or, if you prefer, it is related to the fact that programming languages are analyzed with the same tools of natural languages, and all natural languages, being “spoken”, are constrained by its transmission medium to be a unidimensional stream of sounds and silences. On the other side, computer languages, are rarely “spoken”, so they’re not naturally constrained to a unidimensional stream.

Let’s talk about C. For the C compiler, source is a stream of chars. A c compiler can compile a 100kb source written on one line without line breaks. (Javascript engines do this...

Continue reading →


Javascript, untangled

Background

I’m working on LiteScript, a compile-to-js-language, based in Javascript design (the good parts). LiteScript is also a compile-to-C-language. In order to compile to fast C, it is required to untangle some JS concepts and limit some JS dynamic features

Untangling Javascript

Javascript is extremely powerful, and this power allows a programmer to write very clever code, and also shoot herself in the foot in very imaginative ways.

Classes are Functions, and Functions are Classes

In JS a “class” is simply a Function and every Function is a class. This is the first concept to untangle.
In LiteScript, a “Class” is a type of Object, different from a “Function”.

JS Function: Object containing executable code.
Functions objects have properties: name, prototype, length.
Functions objects have several methods: apply, call, bind, toString, etc.

JS Class: see Function.

...

Continue reading →


Keep your coder’s mind at full speed: avoid mental branch mispredictions

Brains and CPUs

One can make an analogy of the human brain and a CPU, but
never is the analogy more valid than in the case of a programmer
reading source code.

The coder’s mind

When reading source code, trying to understand what the code does,
the mind of a trained programmer works much like a CPU.

We’re “following” (executing) the source code in our minds,
trying to determine what the program does.

..then calls this function, stores result here, if it’s less than 10, then it…

Branch Misprediction

Do you know what a “branch misprediction” is ?

Branch misprediction is an interesting artifact of CPU’s execution,
since it effects can “bubble” up to high-level languages.

They say “A picture is worth a thousand words”, well this explanation is illustrated with a picture, and as today it seems to worth more than ten thousands upvotes.

Please go now and read this fantastic answer...

Continue reading →