rust

  • sans-io pattern for protocol state machines
  • map/fold can contain specific loop optimizations due to types and ownership for iterator mutation/reuse.
    • ex. let flat = flatten.into_iter().fold(Vec::new(), |mut accumulator, list| { acc.extend(list); acc }); is likely as fast as a for loop because acc is a moved vec.
  • std::intrinsics contains llvm intrinsics
    • branch prediction with likely, select_unpredictable (cmov's for branchless), unlikely etc
  • llvm-mca with -bottleneck-analysis for asm profiling
    • supported by godbolt
  • https://github.com/rust-lang/rust/issues/93740#issuecomment-1041391284 futex/mutex comparisons
  • rustc uses stacker crate for segmented stacks to avoid stack overflow
  • cargo test -- --nocapture for println!()
  • rls removed in 1.65 for rust-analyzer
  • HashTable uses SwissTable(DoS resistance)
  • https://rust-lang.github.io/api-guidelines/checklist.html
  • reference counting can be checked by statically
  • default allocator can be changed to jemalloc to avoid heap fragmentation
  • cargo feature flag propagation can break for not additive features
    • libraries could use RUSTFLAGS='--cfg feature_name="FEATURE_VAL"' and a build.rs like the curve25519-dalek crate (cross compilation?)
  • cargo update -p PACKAGE --precise VERSION" to pin Cargo.lock.
  • cargo tree for dependency tree visual
  • CROSS_CONTAINER_ENGINE=podman cross build --target x86_64-unknown-freebsd
    • freebsd tests only run on qemu runner
    • set the image tag for newer cross
  • RUSTFLAGS="-Z location-detail=none -Zfmt-debug=none -C relocation-model=static -Ctarget-feature=+crt-static" cargo +nightly run -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort,optimize_for_size --target x86_64-unknown-linux-gnu --release with eyra for full source build
[target.x86_64-unknown-freebsd]
image = "ghcr.io/cross-rs/x86_64-unknown-freebsd:main"
#runner = "qemu-user"
  • rust-toolchain.toml to set nightly/beta and components like llvm-tools-preview
  • cargo release profile
[profile.release]
lto = true
codegen-units = 1 # reduce parallel codegen for increased optimization
strip = true
opt-level = "z" # smallest binary instead of speed
panic = "abort"
[build]
# build extra targets and features
rustflags = [
          #"-Zlocation-detail=none",
          #"-Zfmt-debug=none",
          #"-Ctarget-cpu=native",
          #"-Clink-args=nostartfiles",
          #"-Clink-args=-Wl,",
          #"-Clink-args=-Wl,-n,-N,--no-pie,--no-dynamic-linker,--build-id=none,--gc-sections,-nmagic",
           "--cfg",
           "tokio_unstable"]
target = [
#    "x86_64-unknown-freebsd",
    "x86_64-unknown-linux-gnu",
]
#[target.x86_64-unknown-linux-gnu]
#linker = "./ree"

#[unstable]
#build-std = [
#          "std",
#          "panic_abort",
#]
#build-std-features = [
#                   "optimize_for_size",
#                   "panic_immediate_abort",
#]

fn main() {// minimal with origin
    println!("cargo:rustc-link-arg=-nostartfiles");

    // The following options optimize for code size!

    // Tell the linker to exclude the .eh_frame_hdr section.
    println!("cargo:rustc-link-arg=-Wl,--no-eh-frame-hdr");
    // Tell the linker to make the text and data readable and writable. This
    // allows them to occupy the same page.
    println!("cargo:rustc-link-arg=-Wl,-N");
    // Tell the linker to exclude the `.note.gnu.build-id` section.
    println!("cargo:rustc-link-arg=-Wl,--build-id=none");
    // Disable PIE, which adds some code size.
    println!("cargo:rustc-link-arg=-Wl,--no-pie");
    // Disable the `GNU-stack` segment, if we're using lld.
    println!("cargo:rustc-link-arg=-Wl,-z,nognustack");
}