r/rust • u/psx8888 • Sep 20 '24
Why is Rust slower than Python, Go and Nim regarding SSH?
For my test, I run 'date +\"%T.%N\"' twice on a SSH Server on my local network and calculate the time between the two executions.
Clients run on Windows10/11.
Python (paramiko): ~45ms
Nim (ssh2(libssh2)): ~44ms
Go (golang.org/x/crypto/ssh): ~4ms
Rust (ssh-rs, russh, ssh2(libssh2)): ~115-130ms
I don't know what Go's trick is, but 45ms seems like a goal Rust should easily achieve!?
Rust profile for "cargo build -r":
[profile.release]
lto = true
codegen-units = 1
strip = true
panic = 'abort'
Rust (ssh-rs): https://pastebin.com/3ucPpCzc
Rust (ssh2(libssh2)): https://pastebin.com/cQ7j6zd2
Nim (ssh2(libssh2)): https://pastebin.com/xRYFFLDN
Go (golang.org/x/crypto/ssh): https://pastebin.com/mFzvLLnk
Any idea why this happens and how I can at least get the same performance as the other implementations?
Update:
The previous tests were executed on Windows 10 (Threadripper 3990X with Mellanox card, highly optimized for latency) and Standard Windows 11 Enterprise (work notebook).
I copied the projects to a raspberry pi and retested the Rust ssh-rs, ssh2 implementations and the Go app against the same destination.
ssh-rs: ~45ms
ssh2: ~45ms
go: ~4ms
9
u/crusoe Sep 20 '24
How are you launching and timing the program?
If you are running with cargo run after compiling you are also measuring the start time of cargo itself.
Try running the binary directly it's under target/release.
3
u/dddd0 Sep 20 '24
Could be that ssh-rs uses unoptimized „pure rust“ crypto implementations.
2
u/psx8888 Sep 20 '24
That can certainly be the case!
I think the comparison between libssh2 in Rust and other programming languages such as C and Nim is more meaningful. And that's exactly the point that concerns me.
16
u/joshuamck ratatui Sep 20 '24
When in doubt about a perf issue, prefer measuring over speculating. Bump up the server log verbosity and look at the difference in call patterns. Maybe the libs have different encryption defaults? Maybe there's an extra roundtrip or two happening somewhere. The logs are where I'd start looking for that info.
7
u/Sloppyjoeman Sep 20 '24
I suspect this is an example of async rust being relatively difficult to write:
c is ancient and so has a battle tested, optimised library
go’s async features are relatively easy to use, and so a given library is more likely to take advantage of its async capabilities
rust is relatively new with relatively complex async primitives, and so is less likely to have these optimisations in
1
u/Individual_Caramel93 Mar 14 '25
Nagle's algorithm strikes again. Golang (smartly?) disables it by default. Enabling tcp_nodelay for your Nim snippet makes the time diff 3ms.
-1
u/eras Sep 20 '24
Did you check CPU usage? If it takes 100% cpu, then did you also use release mode when compiling?
I mean it stands to reason everyone would do benchmarking in release mode, but it also stands to reason that if it were so, one would also mention it, as it is always the first response :).
2
u/psx8888 Sep 20 '24
That's why I wrote this in my post. Or did I misunderstand your comment?
Rust profile for "cargo build -r":
[profile.release]
lto = true
codegen-units = 1
strip = true
panic = 'abort'4
u/heavymetalpanda Sep 20 '24
Are you doing
cargo build -rand thencargo run? How are you running the resulting binary?4
u/psx8888 Sep 20 '24 edited Sep 20 '24
The tests are build in release mode:
cargo build -r and then ./target/release/xyz.exe
and when i do use "cargo run" I add "-r"Trust me, it's in release mode. There is also no significant cpu usage. That's not my first project and all tests are stripped down for simplicity, therefore no error handling etc.
[profile.release] is also working, otherwise the binary wouldn't be stripped regarding size.
4
u/eras Sep 20 '24
There is also no significant cpu usage
Then it's not about codegen, but about network behaviour. Perhaps the
ssh-rsdoes more roundtrips than required.Does the delay decrease "too much" if you test against localhost?
You could also compare
wiresharkbetween those different clients. No deep insight required, just the amount of packets.3
u/crusoe Sep 20 '24
When you do cargo run -r cargo has to start and fork the ssh process. Since the program is built it is not rebuilt. But you're time the startup time of cargo as well.
78
u/ralphpotato Sep 20 '24
Ok I tested using your ssh-rs code and your go code, and I briefly looked at the source code in go. Here's my output with a bit of timing around the ssh commands to my home server on the same network:
Rust:
Go:
So the rust is definitely slower, but I think the main thing is that the go code must be running concurrently while the rust code is completely synchronous. Basically the rust code sends the command over the session, waits for the output, parses the output, prints the output, and then does the same thing for the next command. The go code ends up running both at the same time, and the total turnaround time also seems to be faster.
I am not super familiar with nim and you didn't add your python code so I can't compare with that. If you ran the rust code concurrently using an async runtime like tokio, you probably would see a
datediff close to go.