syd 3.58.0

rock-solid application kernel
Documentation
//
// Syd: rock-solid application kernel
// benches/sandbox/elf.rs: ELF parser microbenchmarks
//
// Copyright (c) 2025 Ali Polatel <alip@chesswob.org>
//
// SPDX-License-Identifier: GPL-3.0

use std::{
    fs::{read_dir, File},
    hint::black_box,
    path::Path,
    time::Duration,
};

use brunch::{benches, Bench};
use syd::elf::ExecutableFile;

const STDPATH: &[&str] = &["/usr/bin", "/bin", "/usr/sbin", "/sbin"];

fn parse_elf_native<P: AsRef<Path>>(path: &P, check_linking: bool) {
    let _ = File::open(path)
        .ok()
        .and_then(|mut file| ExecutableFile::parse(black_box(&mut file), check_linking).ok());
}

fn main() {
    let mut paths = Vec::new();
    'main: for dir in STDPATH {
        let reader = if let Ok(reader) = read_dir(dir) {
            reader
        } else {
            continue;
        };

        for result in reader {
            let entry = if let Ok(entry) = result {
                entry
            } else {
                continue;
            };
            if entry.file_type().map(|ft| !ft.is_file()).unwrap_or(true) {
                continue;
            }
            paths.push(entry.path());
            if paths.len() >= 1000 {
                break 'main;
            }
        }
    }

    let paths = std::sync::Arc::new(paths);
    println!("Loaded {} paths for benchmarking.", paths.len());

    benches!(
        inline:
        Bench::new("parse_elf_native check_linking=0")
            .with_samples(paths.len().try_into().unwrap())
            .with_timeout(Duration::from_secs(10))
            .run_seeded(paths.clone(), |paths| {
                for path in paths.iter() {
                    black_box(parse_elf_native(path, false));
                }
            }),
        Bench::new("parse_elf_native check_linking=1")
            .with_samples(paths.len().try_into().unwrap())
            .with_timeout(Duration::from_secs(10))
            .run_seeded(paths.clone(), |paths| {
                for path in paths.iter() {
                    black_box(parse_elf_native(path, true));
                }
            }),
    );
}