Skip to content

Optimizations

Ondřej Perutka edited this page Sep 23, 2016 · 7 revisions

As the Arrow Client is intended to be used in embedded systems, we understand your need for making it as much resource efficient as possible. Using Rust as a language for the reference implementation may not look like a step in this direction (mainly in terms of memory consumption); however, we made this choice because of numerous other benefits we get from using Rust. The most important one in comparison with C or C++ is guaranteed memory safety. This document will provide you with some useful tips on creating smaller executables.

Compile time options

  1. Use the --release option (this will strip away all debug symbols and it will also enable code optimizations).
  2. Do not use the --features discovery option unless you want to use the network scanning feature.

If you want to generate as small executable as possible, use the following command:

cargo build --release

Currently, the size of the resulting binary will be around 1.5 MB for ARM.

Stripping the binary

After compilation, you can do even more to make the binary smaller. You can strip away all remaining symbols using:

strip -s arrow-client

This step will save you another ~400 kB without any additional costs. The size of the resulting binary will be approximately 1.1 MB.

Compressing the binary

Supposing 1.1 MB is still huge for you, you can make a compressed binary. There are basically two options. Both of them will cost you some RAM. The first option is using gzexe:

gzexe arrow-client

This will create a shell script with embedded binary data (the compressed executable). The size of the file will be ~470 kB. In order to run such application, your system will need a shell and gzip for decompressing the executable. The executable will be decompressed into a temporary file which will be deleted soon after the application is started. However, the shell process will run together with the application the whole time. The shell process will be idle, so it will not consume any CPU time, but it requires some extra RAM. Depending on your system, the amount of required extra memory may vary between units and a few hundreds of kB.

The second option is using upx:

upx -9 arrow-client

This command will create a self-contained compressed executable. The size of the executable will be ~440 kB. The application will need to allocate an extra block of memory where the inner executable will be decompressed. The amount of required extra memory will be around 1 MB.

Disabling jemalloc

You can save even more by disabling jemalloc in Rust. Jemalloc is a memory allocator used by Rust applications as a replacement for the standard memory allocator in your C library. Disabling jemalloc can reduce the size of your binary by another ~30 %. However, you will have to build your own version of Rust. See the Compilation page for more details.

Clone this wiki locally