Use Rust (instead of C++) to develop some algorithms used in Flutter in a real app used in the *production environment* - Is it mature enough? Will I face troubles? Do you suggest it? Has anyone used it?

I write a Flutter app (>100kloc), and need to embed some computer vision algorithms for it. Originally, I use C++, and use the OpenCV library to develop my algorithms. Then I use dart:ffi to allow Flutter code to call my C++ code.

It works quite well, but now I need to develop much more complex algorithms, and I am worried because C++ is known to be dangerous unless you are extremely well-trained. So I want to move to Rust. I have learnt Rust and used it for several toy projects and love its safety very much. I also see some tutorials about how Rust talks with Flutter (e.g. this one).

However, I am quite not sure whether I should do it. Rust is very young. I know it is already used in production environments in laptops and servers; but I am not sure whether it is mature enough to be used in mobile platforms (Android and iOS). For example, will I meet tooling problems? Will the performance be bad because it does not have very excellent compiler optimizations? Will I have trouble when I really write down >10kloc of Rust and see an edge case, but at that time I have no way to go back?

Any suggestions and discussions are appreciated!

Related: Anyone called Rust via FFI from Flutter? but half a year has passed and Flutter and Rust has changed a lot :wink:

You should not run in to trouble with compiler optimizations — the Android OS is built with LLVM and Rust also uses LLVM, so the optimizations are there. The Android OS also does have some Rust components. I don't know about tooling for building apps, though I know its possible through various types of FFI.

3 Likes

Rust is 15 years old this year, it was started in 2006 by Graydon Hoare. Do you think people worried about the maturity of C in 1985 just because it was 15 years old? I don't think the maturity of a technology should be evaluated based on its age. Unless a project is really very young (say, less than 1-2 years), it's easy to see where it is heading and how serious its developers are.

I don't think there's any difference between Rust running in a web server and Rust running in a mobile application.

I can only help you with iOS, because I don't know Android, but on iOS, you will meet tooling problems with basically any language other than the C family and Swift (i.e., those officially endorsed by Apple). But Rust is "just another" LLVM-based, compiled language. If you compile your Rust code with Cargo upfront, as a library, then bindgen and link it to your project, it won't be any more painful than doing the same with a C library.

I'm sorry, but did you actually research how Rust works and how it is compiled? Again, it is an LLVM-based language, with all the same optimizations available to it that apply to all other languages compiling to LLVM. It also has intermediate representations to optimize.

It's hard to say "no" to this kind of question, because who knows what kind of problems one might run into when writing arbitrarily complex code. However, the design principles of Rust want to avoid bad edge/corner cases as much as possible – that's the very point of the language.

3 Likes

@alice Hi thanks for your reply! I am not an expert so I am a bit confused:

You should not run in to trouble with compiler optimizations — the Android OS is built with LLVM and Rust also uses LLVM, so the optimizations are there.

IMHO, normally people use C++ to develop high-performance algorithms with Android ndk. So I guess the compiler optimizations between C++ and Rust are to be compared. I know clang (which android also uses) uses LLVM as well. But I think many optimizations are specific to programming languages, so clang may do more optimizations before it hands the IR to LLVM compared with Rust? Please correct me if I am wrong.

The Android OS also does have some Rust components.

Wow interesting - I never know that before

Any such optimizations would be general over all the platforms and not be specific to Android or iOS platforms. Rust generally optimizes well, and is comparable to C++.

2 Likes

@H2CO3 Hi thanks for your long reply! I am convinced but just a bit confused:

Rust is 15 years old this year, it was started in 2006 by Graydon Hoare. Do you think people worried about the maturity of C in 1985 just because it was 15 years old? I don't think the maturity of a technology should be evaluated based on its age. Unless a project is really very young (say, less than 1-2 years), it's easy to see where it is heading and how serious its developers are.

I totally agree.

I don't think there's any difference between Rust running in a web server and Rust running in a mobile application.

Hmm Rust in mobile runs on ARM, while Rust in server runs on Intel x86 instruction set. IMHO, different instruction sets will lead to quite different optimizations. So Rust may be mature for intel x86, but may not have enough compiler optimizations for ARM?

I can only help you with iOS, because I don't know Android, but on iOS, you will meet tooling problems with basically any language other than the C family and Swift (i.e., those officially endorsed by Apple). But Rust is "just another" LLVM-based, compiled language. If you compile your Rust code with Cargo upfront, as a library, then bindgen and link it to your project, it won't be any more painful than doing the same with a C library.

Thanks I will try that!

I'm sorry, but did you actually research how Rust works and how it is compiled? Again, it is an LLVM-based language, with all the same optimizations available to it that apply to all other languages compiling to LLVM. It also has intermediate representations to optimize.

I know clang (which android also uses) uses LLVM as well. But I think many optimizations are specific to programming languages, so clang may do more optimizations before it hands the IR to LLVM compared with Rust? Please correct me if I am wrong.

It's hard to say "no" to this kind of question, because who knows what kind of problems one might run into when writing arbitrarily complex code. However, the design principles of Rust want to avoid bad edge/corner cases as much as possible – that's the very point of the language.

Sure, I believe in Rust :wink:

Again, this is not a problem. The optimizations that are ARM-specific will generally be in LLVM, so Rust also benefits from them.

3 Likes

Note that Google now supports Rust on Android:

If that is not sufficient endorsement of Rust on Android I don't know what would be.

I don't particular have a need for extreme performance but of course when I first discovered Rust I put it through it's paces by reimplementing some of my C++ code in Rust by way of comparison. I soon found I could match the performance of C++ and in some cases exceed it by a little.

You might be interested in the many benchmarks here:

6 Likes

Any such optimizations would be general over all the platforms and not be specific to Android or iOS platforms. Rust generally optimizes well, and is comparable to C++.

Get it. Thanks!

Again, this is not a problem. The optimizations that are ARM-specific will generally be in LLVM, so Rust also benefits from them.

Thanks! Originally I was worried whether it is in LLVM or in the compiler steps before giving ir to LLVM. Now I know it.

Note that Google now supports Rust on Android:

Interesting! If the Android team dare to use Rust to develop the operating system itself, it must be a language that is quite stable and high-performance.

I don't particular have a need for extreme performance but of course when I first discovered Rust I put it through it's paces by reimplementing some of my C++ code in Rust by way of comparison. I soon found I could match the performance of C++ and in some cases exceed it by a little.
You might be interested in the many benchmarks here:
Rust vs C clang - Which programs are fastest?

Wow Rust seems to be really fast!

It's not just the Android team. There are many working on supporting Rust in the Linux kernel:

The fact that Linus said something like "not entirely terrible idea" when first talking of Rust in the kernel is high praise indeed.

2 Likes

I'm someone who actually uses Rust together with Flutter.
Overall once you setup build process there is no difficulty with it.
The most painful part of it is the same as with C++ you need to write a lot of boilerplate code to enable single FFI function.

But regarding machine learning and specifically OpenCV I believe C++ would be more mature for that particular purpose.
Primarily because it would be pain in ass to use OpenCV from within Rust, as I'm not sure how reliable are current bindings.
But if you're looking outside OpenCV then Rust should be good.

From mobile perspective any code would be alien whether it is C++ or Rust.
You basically build dynamic library and bundle it with your apk, so the real difference is how to build it, that's all.
Overall process would be the same whether it is C++ or Rust(which is pretty mature despite flutter being a pain about FFI)

7 Likes

@ZiCog Yeah I know that news. It is quite promising, though Linus seems to only want some things that is not that core to Linux to be written in Rust first

@DoumanAsh Ha how do you know I use OpenCV really a lot! I do see the opencv binding: GitHub - twistedfall/opencv-rust: Rust bindings for OpenCV 3 & 4 .
IMHO the binding is mostly generated automatically by code. So I wonder where will it be non-stable?

Thanks!

That is a very reasonable approach. Luckily the Linux kernel is very modularised. This modules can be built independently of the kernel and loaded dynamically.

2 Likes

OOPS wait a bit: SIMD/NEON in Stable Rust? - #5 by n_b Rust seems not to support SIMD :frowning:

you would have to use something else to compile ARM NEON code. It could be C with intrinsics, it could be an assembly file compiled with nasm or gas.

That is a really big problem. You know, there are tons of array operations, and I definitely want to have SIMD to speed up!

In addition there are also matrix operations. So I hope I could use libraries such as eigen which is highly optimized. But rust does not seem to have such a high-performance lib...

@alice @H2CO3 @ZiCog @DoumanAsh Could you please provide some suggestions? Thanks!

1 Like

That is a somewhat different question.

If you want to write code that depends heavily on libraries lie OpenCV, Eigen or whatever that are written in C or C++ it may well make sense to write it in C++.

2 Likes

Neon instructions are not stable so you better off with C++ if you want to benefit from it.
Although in practice if you're using C++ libraries like eigen I doubt you'll get equally performant alternative in Rust right now

Ha how do you know I use OpenCV really a lot ! I do see the opencv binding: GitHub - > twistedfall/opencv-rust: Rust bindings for OpenCV 3 & 4 .
IMHO the binding is mostly generated automatically by code. So I wonder where will it be non-stable?

I'm not really sure since I never used it.
I have acquaintance who played with opencv and it seemed to work for him just fine, but I do not remember details

1 Like

@ZiCog Well that is a problem...