Files
ghostty/pkg/highway/src/detect.zig
Mitchell Hashimoto 00dfd67bee pkg/highway: replace resolveTargetQuery with direct CPU detection
The previous runtime_detect.zig called std.zig.system.resolveTargetQuery
which pulled in the entire Zig target/CPU model table infrastructure for
every architecture (~4,000 symbols, ~175 KB of data tables, ~130 KB of
code). This bloated the binary by ~500 KB and shifted code layout enough
to cause a measurable icache/branch-predictor regression in unrelated
hot paths like the terminal parser (~20% more cycles for identical
instruction counts).

Replace with minimal, direct CPU feature detection per architecture:
CPUID + XGETBV inline assembly on x86, sysctlbyname on Darwin AArch64,
and getauxval/prctl via std.os.linux (direct syscalls, no libc) on
Linux for AArch64, PPC, S390x, RISC-V, and LoongArch.

Split into per-architecture files under src/detect/ for
maintainability.
2026-04-23 21:23:12 -07:00

50 lines
1.8 KiB
Zig

const builtin = @import("builtin");
const HwyTargets = @import("targets.zig").Targets;
const x86 = @import("detect/x86.zig");
const aarch64_darwin = @import("detect/aarch64_darwin.zig");
const aarch64_linux = @import("detect/aarch64_linux.zig");
const ppc = @import("detect/ppc.zig");
const s390x = @import("detect/s390x.zig");
const riscv = @import("detect/riscv.zig");
const loongarch = @import("detect/loongarch.zig");
/// Detect Highway targets at runtime using minimal, direct CPU feature
/// probing.
///
/// Previous versions called std.zig.system.resolveTargetQuery which
/// drags in the full Zig target/CPU model tables for every architecture,
/// bloating the binary by ~300 KB and causing code-layout regressions in
/// unrelated hot paths (icache / branch-predictor pressure).
///
/// This version uses only inline assembly (CPUID on x86, MRS on AArch64)
/// and lightweight syscalls (sysctlbyname on Darwin, getauxval on Linux),
/// so it adds no data tables and no std.Target dependency.
pub export fn ghostty_hwy_detect_targets() callconv(.c) i64 {
return switch (builtin.cpu.arch) {
.x86_64, .x86 => x86.detect(),
.aarch64, .aarch64_be => detectAarch64(),
.powerpc, .powerpc64, .powerpc64le => ppc.detect(),
.s390x => s390x.detect(),
.riscv32, .riscv64 => riscv.detect(),
.loongarch32, .loongarch64 => loongarch.detect(),
else => 0,
};
}
fn detectAarch64() i64 {
var t: HwyTargets = .{};
// All AArch64 implementations have NEON.
t.neon_without_aes = true;
if (comptime builtin.os.tag.isDarwin()) {
return aarch64_darwin.detect(&t);
} else if (comptime builtin.os.tag == .linux) {
return aarch64_linux.detect(&t);
}
// Other OS: return baseline NEON.
return @bitCast(t);
}