Why Kotlin native generated executable is larger than the equivalent in rust

Considering both Kotlin-native and Rust are based on LLVM, I expect there output and performance to be close!

So created 2 simple Hello World! with kotlin and rust

main.kt:

fun main() {
    println("Hello, world!")
}

main.rs:

fn main() {
    println!("Hello, world!");
}

Then generated the executable files for bot using:
kotlinc-native main.kt for kotlin and cargo build for rust

Then checked the executables/binary using the below:

ls -S -lh | awk '{print $5, $9}'

and found that the file generated by kotlin native is 1.48X the file generated by rust.

Any idea why this variance!

Moreover Rust can be optimized to be smaller, Is there something simliar in Kotlin native?

Initial setup:

$ cargo new hello_world

Build with:
$ cargo build

:point_right:t2: 589,004 bytes

Optimization Step 1:

Build with:
$ cargo build --release

:point_right:t2: 586,028 bytes

Optimization Step 2:

Change contents of main.rs to:

use std::alloc::System;

#[global_allocator]
static A: System = System;

fn main() {
   println!("Hello, world!");
}

:point_right:t2: 335,232 bytes

Optimization Step 3:

Add …

[profile.release]
lto = true

to Cargo.toml .

:point_right:t2: 253,752 bytes

Optimization Step 4:

Strip executable via …
$ strip target/release/hello_world

:point_right:t2: 177,608 bytes

So, we ended up having the file generated by kotlin native is 4.87X (~ 5X) the file generated by rust

I guess the stdlib of Kotlin is just bigger.

1 Like

Garbage collector?

2 Likes

Reference counting in case of K/N.
Considering that Rust have almost no runtime and kotlin does, the difference of about 300k seems very small.