Tiny Rust or something

Sup every1 :wave:! Once upon the time i thought it would be really interesting to use Tiny C Compiler (tcc) as Rust's backend cuz it may help us provide more optimised binaries i think. This may look not that clever but i've got no experience in interacting with LLVM and building PLs. Can anyone tell me how difficult it would be and will this be useful please? :thinking:

Peace :blush:

The tiny C compiler is so tiny because it doesn't perform a lot of optimizations.

Anyway, if you want a faster/lighter-weight codegen backend, there is cranelift. Adding a whole new compiler backend is a massive amount of work, so if you have no experience with developing compilers, then you are very likely not up to the task.

I didn't mean compilation speed. Giving you hello world as example is dumb but i will do it anywaya.

C code:

#include <stdio.h>
#include <conio.h>
#include <stdint.h>

int main(int argc, uint8_t** argv) {
    puts("Hello, World!");
    getch();
    return 0;
}

Rust code:

#![feature(start)]

extern "C" {
fn puts(_arg: *const i8) -> core::ffi::c_void;
fn getchar() -> i8;
}

#[start]
fn main(argc: isize, argv: *const *const u8) -> isize {
    unsafe {
        puts("Hello, World!".as_ptr() as *const i8);
        getchar();
        0
    }
}

I compiled this on Windows with gcc and tcc for C and rustc with target x86_64-pc-windows-msvc (panic = abort) for Rust. GCC gave 84 kb, rustc gave 12 kb, TCC gave 2 kb. Am i stupid and i could optimize Rust variant more? I know smaller binaries != greater speed so i meant optimization of binary size.

Thanks for replying :wink:

It's quite likely that you didn't account for the constant overhead of each language. If you write a much less trivial program, the differences will either dissappear completely or be very small. It's even possible that Rust outright wins at such scales, as at the language level it lends itself more to optimization than C or C++.

In general it's dangerous to assume that one language is less optimized than another only based on statistics of hello world programs.

3 Likes

Please search for "rust binary size static linking" or something along those lines. You are not the first one to discover that Rust links libraries statically by default. And 84 kb vs 12 kb hardly matters nowadays, because GB and TB-sized storage is commodity.

We often recommend this page for advice on reducing binary sizes: GitHub - johnthagen/min-sized-rust: 🦀 How to minimize Rust binary size 📦

3 Likes

12 kB seems to be about as small as you can get this on x86_64-pc-windows-gnu, without dropping into assembly. That's about the size of MinGW's CRT glue, constructors, destructors, and PLT stubs.

1 Like

msvc, not gnu ^^

@H2CO3, @jjpe, @LegionMammal978, @cole-miller, thanks everyone. Optimizing binary size wasn't the theme tho, i just wanted to know how possible doing this, in my opinion interesting, thing can be.