Question about Crystal, Compiled Code, and Performance

I know about clang, what I meant was that you can’t write a piece of code in Crystal and emit LLVM and expect it will be exactly the same as an identical piece of LLVM emitted C code. If I’m not mistaken, the two piece of code will have some big differences, like GC, std classes, etc.

For example, I wrote two programs, one in Crystal, and one in C, both of them do about the same thing, print the string "HELLO!\n"

test.cr

print "HELLO!\n"

test.c

#include <stdio.h>

int main()
{
    printf("HELLO!\n");
    return 0;
}

I then made each emit LLVM-IR using the following commands

crystal-vs-c$ crystal build test.cr --emit llvm-ir -o test-cr.ll
crystal-vs-c$ clang -emit-llvm -c test.c -S -o test-c.ll

The difference between the two is night and day, the C code ends up being a succinct 25 lines of LLVM-IR where the Crystal code comes out to around 70000 lines of LLVM-IR. I tried emitting LLVM without the prelude but, it won’t work since it can’t find print without it.

GC/reference overhead are both good points though, I didn’t think of that.

Again not a master of LLVM so maybe I’m missing something else but, I would love to learn more about LLVM internals from someone who works with it all the time. :slight_smile: