#cpu #performance #optimization #cargo #compression #multivers

nightly multivers-runner

Library to create a portable binary that embeds multiple versions of an executable each using a different CPU feature set

3 releases

0.1.2 Aug 11, 2024
0.1.1 Feb 17, 2024
0.1.0 Dec 15, 2023

#1215 in Hardware support

Download history 7/week @ 2024-07-21 27/week @ 2024-07-28 158/week @ 2024-08-11 29/week @ 2024-08-18 15/week @ 2024-08-25 3/week @ 2024-09-01 31/week @ 2024-09-15 16/week @ 2024-09-22 4/week @ 2024-09-29 7/week @ 2024-10-13 87/week @ 2024-10-20 72/week @ 2024-10-27

166 downloads per month

MIT/Apache

22KB
381 lines

multivers-runner

This crate can be used to create a portable binary that embeds multiple versions of an executable each using a different CPU feature set.

Take a look at cargo multivers, it does all the work for you: build the multiple versions and build the final binary that embeds them.

How Does it Work?

The build script parses a JSON description file (see an example below) that contains a set of paths to executables with their dependency on CPU features from the environment variable MULTIVERS_BUILDS_DESCRIPTION_PATH. Then, it generates a Rust file that contains a compressed source binary and compressed binary patches to regenerate the other binaries from the source.

{
  "builds": [
    {
      "path": "/path/to/binary-with-additional-cpu-features",
      "features": [
        "aes",
        "avx",
        "avx2",
        "sse",
        "sse2",
        "sse3",
        "sse4.1",
        "sse4.2",
        "ssse3",
      ]
    },
    {
      "path": "/path/to/binary-source",
      "features": [
        "sse",
        "sse2"
      ]
    }
  ]
}

At runtime, the function main uncompresses and executes the version that matches the CPU features of the host. On Linux, it uses memfd_create and fexecve to do an in-memory execution. On Windows, however, it writes the version in a temporary file and executes it.

cargo multivers

This library is used by cargo multivers to build the final binary that embeds the multiple versions.

Dependencies

~2–12MB
~136K SLoC