Files
orca/config/scripts/swift-cli-helper-macos.mjs
T
OrcaWinandm4air d58ab9762c perf(memory): read macOS footprints with a bundled proc_pid_rusage helper instead of top (#34053)
top costs ~263 ms of CPU per sample, so the memory panel only refreshed
footprints every 10 s. The new orca-process-footprint helper reads
phys_footprint for every same-user process in ~3 ms, so footprints now
refresh on the panel's 2 s poll. top stays as the fallback when the helper
is missing or fails.

Co-authored-by: m4air <m4air@Mac.localdomain>
2026-10-26 13:32:55 -07:00

80 lines
2.6 KiB
JavaScript

import { execFileSync } from 'node:child_process'
import { chmodSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
const repoRoot = path.resolve(import.meta.dirname, '../..')
/** Builds a single-file Swift CLI helper from native/<nativeDir>/main.swift (universal unless --single-arch). */
export function buildSwiftCliHelper({ nativeDir, executable, argv }) {
if (process.platform !== 'darwin') {
return
}
const sourcePath = path.join(repoRoot, 'native', nativeDir, 'main.swift')
const outputPath =
readArg(argv, '--output') ??
path.join(repoRoot, 'native', nativeDir, '.build', 'release', executable)
const triples = argv.includes('--single-arch')
? [process.arch === 'arm64' ? 'arm64-apple-macosx' : 'x86_64-apple-macosx']
: ['arm64-apple-macosx', 'x86_64-apple-macosx']
const workDir = mkdtempSync(path.join(tmpdir(), `${executable}-`))
try {
const builtBinaries = triples.map((triple) => {
const output = path.join(workDir, `${executable}-${triple}`)
execFileSync(
'swiftc',
[
'-O',
sourcePath,
'-target',
triple.replace('-apple-macosx', '-apple-macosx11.0'),
'-o',
output
],
{ stdio: 'inherit' }
)
return output
})
mkdirSync(path.dirname(outputPath), { recursive: true })
if (builtBinaries.length === 1) {
execFileSync('cp', [builtBinaries[0], outputPath])
} else {
execFileSync('lipo', ['-create', ...builtBinaries, '-output', outputPath])
}
chmodSync(outputPath, 0o755)
} finally {
rmSync(workDir, { recursive: true, force: true })
}
}
const DEV_SWIFT_CLI_HELPERS = [
[
'keyboard-layout-macos',
'orca-keyboard-layout',
'shifted Option composition stays conservative'
],
['process-footprint-macos', 'orca-process-footprint', 'memory panel falls back to top']
]
/** Dev bundles build each helper for the host arch; a missing swiftc only degrades that helper's feature. */
export function buildDevSwiftCliHelpers(macOsDir) {
for (const [nativeDir, executable, fallback] of DEV_SWIFT_CLI_HELPERS) {
try {
buildSwiftCliHelper({
nativeDir,
executable,
argv: ['--single-arch', '--output', path.join(macOsDir, executable)]
})
} catch (error) {
console.warn(
`[orca-dev] ${executable} helper build failed (${fallback}): ${error?.message ?? error}`
)
}
}
}
function readArg(argv, name) {
const index = argv.indexOf(name)
return index === -1 ? undefined : argv[index + 1]
}