Can Swift be as memory efficient as Rust?


#1

Looking to properly benchmark Go, Nodejs, Rust and Swift. I have a problem with this code which seem inefficient compare to Rust. How did Rust manage memory so efficiently?

Rust code use 40MB+ memory and completed in 4 secs
https://github.com/kostya/benchmarks

Swift code use 1GB+ without autoreleasepool and completed in 13 secs

If adding autoreleasepool {} in the loop scope to force release memory it will use 60MB+.

I like to have your advice how we can speed Swift to be comparable as Rust?


#2

Why don’t you ask on a Swift forum?


#3

No one able to answer and I like to hear from Rust experienced.


#4

I have a theory.
In rust Strings are Vec<u8>. They are growable. In Swift Strings are structs and structs get copied as soon you manipulate them in Swift. This could explain the memory usage.