3

I created 2 simple "Hello World!" programs, one with Kotlin and one with Rust:

Kotlin:

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

Rust:

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

I generated the executable files for both using: kotlinc-native main.kt for Kotlin and cargo build --release for Rust, then checked the binary sizes using ls -S -lh | awk '{print $5, $9}'.

I found that the file generated by Kotlin native is 1.48X the size of the file generated by Rust.

Why does this variance exist?

$ ./program.kexe
Hello, world!
$ ls -S -lh | awk '{print $5, $9}'

835K program.kexe
43B main.kt

$ ./rust
Hello, world!
$ ls -S -lh | awk '{print $5, $9}'

565K rust
128B deps
104B rust.d
64B build
64B examples
64B incremental
64B native

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

=> 589,004 bytes

Optimization Step 1:

Build with:

$ cargo build --release

=> 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!");
}

=> 335,232 bytes

Optimization Step 3:

Add below to Cargo.toml .

[profile.release]
lto = true

=> 253,752 bytes

Optimization Step 4:

Strip executable via

$ strip target/release/hello_world

=> 177,608 bytes

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

Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
  • 16
    Because the two languages have completely different architectures, runtimes, and maturity levels. What answer do you expect? – yole Oct 12 '18 at 13:58
  • @yole both are based on `LLVM` so expect there output and performance to be close! – Hasan A Yousef Oct 12 '18 at 14:00
  • Related: [Why are Rust executables so huge?](https://stackoverflow.com/q/29008127/155423) – Shepmaster Oct 12 '18 at 14:01
  • 2
    LLVM is a mechanism for generating executable code. It does not in any way influence the language runtime. – yole Oct 12 '18 at 14:02
  • 3
    [Please don't post your code as an image.](//meta.stackoverflow.com/q/285551) [I down voted because the same principle applies as Image of Exception](http://idownvotedbecau.se/imageofanexception/) (work for terminal text too) – Stargateur Oct 12 '18 at 14:26
  • 1
    Both might be based on LLVM, but that's just the generator as yole mentioned. the stdlibs are different, the languages are fundamentally different, different stuff is included in the result, and as a result, the size of the executable will be different. It's like comparing Kotlin Native to Java's `.jar`; of course the sizes will be different, even with the same program. – Zoe Oct 12 '18 at 14:34
  • @Stargateur the code is not posted an image, the image is just to show the number and other files in the folder if any is related to my question, even the command to show the size I posted it as code in my question – Hasan A Yousef Oct 12 '18 at 15:43
  • Thanks @Zoe I understand the case of 'jar' or any other running vis 'VM' was thinking the native code are different and not having any thing related to the standard library, – Hasan A Yousef Oct 12 '18 at 15:48
  • 2
    @HasanAYousef See my edit, to know what to do next time. That take me time where you could have done this with a simple copy/paste – Stargateur Oct 12 '18 at 16:04
  • @HasanAYousef how would the program run without the standard library? – Salem Oct 12 '18 at 16:45

1 Answers1

10

Rust doesn't have Garbage Collector

nkheart
  • 154
  • 2
  • 4
  • 1
    That would be an accurate answer if the question was "why does the K/N program use more memory" however this question is about binary size. GC bytecode doesn't necessarily include memory management instructions and function calls but in the case of K/N it's not pure GC and it employs automatic reference counting and falls back on the GC for collecting "cyclical garbage" (https://kotlinlang.org/docs/native-faq.html#what-is-kotlin-native-memory-management-model), meaning the compiler needs to emit those retains and releases in the binary as well (probably more than rust is doing!) – Ryan Pangrle May 08 '21 at 07:16