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
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 …
[profile.release] lto = true
to
Cargo.toml
.
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