Rust Programming

8438 readers
42 users here now

founded 5 years ago
MODERATORS
401
402
15
Announcing Rust 1.52.1 (blog.rust-lang.org)
submitted 3 years ago* (last edited 3 years ago) by [email protected] to c/[email protected]
403
 
 

This seems like a really interesting project.

404
 
 

tl;dr: Two optimizations that would allow writing easy code and let the compiler optimize things out are not performed, so back go manual state machines.

405
406
407
408
409
1
This Week in Rust 386 (this-week-in-rust.org)
submitted 3 years ago by [email protected] to c/[email protected]
410
411
 
 

This is my attempt to satisfy my needs for TUI file exploring and I hope others might find it useful too.

412
413
 
 

Hi! I was playing around with matrix-sdk-rust and i wrote just for fun this bot that converts twitter links into nitter ones.

414
415
 
 

Hi!

I'm the creator of Macchina, a basic system information fetching tool, it's a minimal take on Neofetch, comes with multiple built-in themes and arguments to allow you to change its looks and behavior on the fly!

I first introduced it on this very website, and the feedback I got made me push through the times Macchina made me want to punch myself in the face, but I made the right decision to choose Rust for this little project of mine, because Rust made me enjoy programming again!

It's available on crates.io, the AUR, and Github!

416
417
418
 
 

Very good read, I needed this.

419
420
 
 

Between Lemmy versions v0.8.10 and v0.9.0, we did a major database rewrite, which helped to improve compilation time by around 30% (see below). In this post we are going to explain which methods were effective to speed up Rust compilation for Lemmy, and which werent.

Compilation time comparison

Here is a comparison of debug compilation times for Lemmy. All measurements were done in with Rust 1.47.0, on Manjaro Linux and an AMD Ryzen 5 2600 (6 cores, 12 threads). Results are taken as the average of 5 runs each.

v0.8.10 v0.9.0-rc.11 improvement
Clean build 195s 129s 34%
Incremental build 23s 17s 26%

Build Graph

The build graph and statistics can be generated with the following command, which will generate a file cargo-timing.html.

cargo +nightly build -Ztimings

In our experience, it is really the most useful tool in order to speed up compilation time. There are a few ways to use it:

  • The table at the bottom shows which crates took longest to compile. You can try disable unused features for slow crates, or remove those dependencies entirely. If your own crates are slow, split them up (see below).
  • In the bar graph near the top, you can mouse over each crate to see which other crate it has to wait for in order to start compilation. You can try to remove these dependencies, or features that you don't need.

Splitting code into workspaces

This is often tricky to do, but proved to be the most effective way to speed up compilation. At the same time, it helped to better organise our code into independent parts, which will make future maintenance work easier.

As a first step, it is enough to split the code into crates which depend on each other linearly, as this is relatively easy and already helps to speed up incremental builds (because only part of the project needs to be recompiled when something is changed).

In case you have a particular dependency which takes very long to build, you should take all code which does not depend on that dependency, and move it into a separate crate. Then both of them can be built in parallel, reducing the overall build time.

Later you can try to reorganise your code so that crates in your project can be compiled in parallel. You can see below how we did that in Lemmy:

Before:

After:

As you can see in the before screenshot, all Lemmy crates are compiled linearly, which takes longer. In the after screenshot, at least some of the database crates are compiled in parallel, which saves time on any computer with multiple CPU cores.

For more details on how to use cargo workspaces, have a look at the documentation

Disabling debug info

This is very easy to do, as it only requires putting the following into Cargo.toml:

[profile.dev]
debug = 0

The effect is that debug information isn't included in the binary, which makes the binary much smaller and thus speeds up the build (especially incremental builds). We never use a debugger in our development, so changing this doesn't have any negative effect at all for us. Note that stack traces for crashes still work fine without debug info.

Here it is in the documentation

Removing dependencies

This only had limited success, for the most part. We weren't willing to remove or replace any of our dependencies, because then we would have to remove features, or have a lot of extra work to reimplement things ourselves.

However, there is one specific dependency where we could improve compilation time by around 50 seconds. Refactoring the database allowed us to remove diesel's 64-colum-tables feature. This was especially effective because diesel only has a single crate, and as a result is compiled on a single thread. This resulted in a period of 40 seconds where one thread would compile diesel, while all other threads were idle. There is still room for improvement here, if diesel can be split into multiple, parallel crates.

Lemmy issue with more details

With actix we noticed that it includes a compression library by default, which is useless in our case because we use a reverse proxy. Thanks to @robjtede from the actix for making the necessary changes to allow removing that library. In the end the effect was limited however, because the library could be built very early and in parallel to other things, without blocking anything. It should help with compilation on devices with few CPU cores at least.

Issue in the actix-web repo

Other optimisations

cargo bloat was commonly recommended in order to find large functions, and split them up. But that didn't help at all in our case, probably because our project is quite large. It might be more useful for smaller projects with less code and less dependencies.

It was also suggested to reduce macro usage, but there wasn't much opportunity in our case because diesel uses a lot of macros. We removed a few derive statements that weren't needed, but without any noticable effect.

421
 
 

Apologies if this question isn't really appropriate for this community, but Rust and Kotlin are my two favorite programming languages, and currently, I use both for different projects. However, I'm curious as to if people here think Kotlin still has a place when Rust exists? I'm specifically speaking architecturally: disregarding existing legacy code or support, do you think in the future, the Rust platform should replace the Kotlin platforms (JVM, LLVM Native, Android, Web) for everything Kotlin can do, or do you think Kotlin can do some things better than Rust?

422
 
 

Haven't tried it yet but it looks super neat.

423
424
 
 

Last I heard, Qt and GTK bindings exist for Rust, but they're far from complete and incredibly hard to use compared to coding in their native languages (C++ and C respectively). Has that changed recently? Is it now possible to build reasonably complicated GUIs in Rust, and how difficult would it be for someone whose only GUI experience comes from web development and some JavaFX to learn, compared to just coding it in the GUI framework's native language?

425
 
 

Just curious as I've been getting interested in web development with Rust. Are any of the other Rust websites open source so I can see how they implemented them?

view more: ‹ prev next ›