Skip to content

perf(elf): accelerate large retained-section output emission #74

Closed

Description

@zackees

Context

The current published Linux benchmark at source SHA be6a8cd032ea6dc788978264c860fe761d5090b6 shows native reld behind Wild 0.10.0 in every LTO configuration:

Configuration Wild reld reld delta
no-LTO 0.4617 s 0.6651 s +44.1%
ThinLTO 0.4148 s 0.6421 s +54.8%
full-LTO 0.3929 s 0.4778 s +21.6%

Evidence: https://github.com/zackees/reld/actions/runs/32808116570 and https://raw.githubusercontent.com/zackees/reld/benchmark-stats/x86_64-linux/latest.json

Fixed process startup is not the bottleneck: both Wild and reld measured 0.0021 s, below 0.5% of reld's published final-link latency.

The workload deliberately retains a runtime-consumed 256 MiB policy table on Linux/Windows (ci/e2e/link-workload/crates/app/src/rules.rs). That makes large unchanged-section emission a major part of all three measured links.

A controlled Linux linker-only probe used a runnable _start plus one referenced, allocatable 256 MiB .rodata section. reld's --time --no-fork --threads=4 breakdown attributed:

  • 7.116 s of 7.811 s (91.1%) to Write data to file;
  • 0.502 s (6.4%) to Compute build ID;
  • negligible time to string merging, symbol resolution, layout, and flush/unmap.

An alternating five-trial Wild/reld probe over the retained section produced noisy medians of 1.361 s for Wild and 1.553 s for reld (reld +14.2%). A separate three-trial output-mode A/B on Docker overlayfs measured 1.045 s median for buffer-then-write versus 2.025 s for mmap. The latter is directional evidence only: variance was high and GitHub's Linux runner uses a different filesystem, so it does not justify a blanket default-mode flip.

Relevant implementation points:

  • crates/reld-core/src/elf_writer.rs::write_file_contents
  • crates/reld-core/src/elf_writer.rs::write_section_raw
  • crates/reld-core/src/file_writer.rs::copy_section_data
  • crates/reld-core/src/fs.rs::default_file_write_mode_for_file
  • crates/reld-core/src/elf_writer.rs::compute_hash

Today, large sections are split across Rayon workers and copied with copy_from_slice into the selected output buffer. Linux defaults to mmap except on Btrfs and vfat. Build-ID computation then performs a second full-output BLAKE3 pass.

Proposal

  1. Add a focused, retained large-section performance repro that exercises the existing ELF output path without timing compilation. It must produce and execute a real ~256 MiB output and report output creation, data emission, build-ID hashing, and flush/unmap separately.
  2. Run the exact captured Rust workload on GitHub's Linux runner with:
    • mmap output;
    • buffer-then-write output;
    • alternating/round-robin linker order so page-cache and writeback effects do not systematically penalize the last linker;
    • median plus dispersion, not a single sample.
  3. Implement the fastest correctness-preserving path for large relocation-free, uncompressed, unrelaxed sections. Candidate approaches to evaluate include:
    • direct ranged writes/copies from the input file into the output rather than faulting both mappings through copy_from_slice;
    • better batching/chunk sizing for large sections;
    • filesystem-aware buffered output where measured to win;
    • avoiding redundant initialization or page dirtying.
  4. Treat fused/incremental build-ID hashing as a follow-up within this issue only if output emission is no longer dominant; its measured ceiling is much smaller than the copy path.

Acceptance criteria

  • RED evidence is committed first: a focused retained-section repro and exact-SHA GitHub Linux baseline demonstrate that the existing Write data to file path dominates or exceeds an explicit performance budget.
  • The repro compiles/assembles inputs outside the timed loop, links only during timing, retains the large section through a real reference, asserts an output of approximately 256 MiB, and executes the output successfully.
  • Baseline evidence records runner/filesystem metadata, output mode, thread count, output size, per-phase timings, median, and dispersion.
  • Trials alternate or round-robin linker/output-mode order; fixed-order dirty-page or page-cache bias cannot determine the result.
  • GREEN evidence demonstrates at least a 10% reduction in reld's median final-link time for each of no-LTO, ThinLTO, and full-LTO on the existing GitHub Linux workload.
  • The optimized path preserves executable behavior, section contents/layout, relocations, permissions, and build-ID correctness under the existing validation/diff oracles.
  • Focused tests cover eligibility boundaries: relocated, relaxed, compressed, zero-fill/NOBITS, partial-link, and cross-filesystem cases must use a correct supported path rather than an unsafe fast path.
  • Existing mmap and buffer flags retain their documented semantics.
  • No blanket filesystem default change lands without evidence on that filesystem; Docker overlayfs results alone are insufficient for an ext4 policy change.
  • Ordinary small-link latency does not regress by more than 2% in a controlled comparison.
  • Linux correctness gates, self-hosting, and the scheduled large-output coverage remain green.
  • The final PR includes the exact workflow run and before/after benchmark evidence; published measurements remain raw final-link latency with no startup subtraction.

Decisions

  • Priority: P2. This is the largest measured native-Linux phase and directly affects the public score, but it does not block correctness or releases.
  • Scope: native ELF output emission. Windows and macOS currently bridge to LLD and should not be used to justify changes to reld's native writer.
  • Keep the benchmark workload unchanged. The retained policy data exposed a real large-section cost; changing the corpus would hide rather than optimize it.
  • Do not begin with string merging or a daemon. Phase evidence makes those lower-leverage for this workload.
  • Do not assume mmap or buffering wins everywhere. Select or change defaults only from target-filesystem evidence.
  • Keep routing-pass optimization separate. The reld-only ELF capability pre-scan may merit a later micro-optimization, but it is outside the measured dominant writer phase.

Open questions

  • Which path wins reproducibly on the GitHub runner's filesystem: mmap, buffer-then-write, direct ranged copy/write, or a hybrid?
  • Can unchanged section bytes be copied directly while preserving the current build-ID and validation model, or should the writer expose a mixed mapped/direct-write output abstraction?

Related issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions