Skip to content

Smaller performance costs: attach() scan, save_replay triple write, startup orphan reaping #358

Closed

Description

@TMHSDigital

Summary

Three low-severity costs found together. Each is independent.

Details

  • attach() is O(items x segments) (src/utils/recordingIndex.ts:305-309). Items are sorted, but each one runs out.findIndex(...) from the start. With 50k log lines and 10k events against 600 segments, that is about 3.6e7 predicate calls, synchronous during index_recording. Fix: walk a segment cursor forward (two-pointer) or binary-search the segment starts.
  • save_replay writes the buffered footage three times (src/utils/replay.ts:278-282, 308-310, 319-329): it copies the segments to snap/, concat-copies them to joined.mp4, then stream-copies with -ss into the output. Saving a 1 h 4K buffer writes about 3x its size and needs 3x temporary space. Fix: if the measurements in save_replay copies up to an hour of segments synchronously and writes the footage 3x #293 allow it, fold the cut into the final step; otherwise drop the separate copy when the segments are no longer being written.
  • Server startup blocks on PowerShell when another instance is recording (src/index.ts:34-36, 55-56, src/utils/sessions.ts:232-243). reapOrphans() runs before server.connect, and processInfo does a spawnSync PowerShell CIM query (up to 10 s) for each foreign live pid. A second client starting while the first records waits 1-10 s before initialize is answered. Fix: connect first and reap right after (as retention pruning already does, Retention prune walks capture folders synchronously before the transport connects #294), or make processInfo async.

Getting started

  • Relevant files: listed per item
  • How to run tests: npm test

Filed by repo-audit/full-audit at b9b94fa. Severity: low. Effort: M. audit-key: performance|src/utils/recordingIndex.ts|attach

Activity

  1. added
    priority: low
    performance
    effort: medium
    on Nov 17, 2026
  2. TMHSDigital commented on Nov 17, 2026

    @TMHSDigital
    OwnerAuthor

    1bae732 fixes the attach() scan. Left as is, with reasons:

    • Startup reaping before connect: moving it after server.connect does not help on its own, since the synchronous reap still runs before initialize is handled. Deferring it further would let a fresh start_recording from this server land first, and the reaper could then classify that new, own recording as an orphan. A safe version needs reaping restricted to the records that existed at boot, or an async processInfo. It only costs time when another instance is recording, so it stays open for that.
    • save_replay triple write: save_replay copies up to an hour of segments synchronously and writes the footage 3x #293 measured the alternatives (inpoint, concat seek) and they were not frame-equivalent, so the join-then-cut stays until a variant is measured equal.
  3. TMHSDigital commented on Nov 18, 2026

    @TMHSDigital
    OwnerAuthor

    e596700 fixes the startup item. The server snapshots the live record ids and replay buffer names, connects, and then reconciles in the background. It reads the owners' details with an async processInfo and reaps only the boot ids, so a recording started meanwhile is never taken for an orphan. Verified: new tests for boot-only reaping, a foreign live owner left alone, processInfoAsync, and replay cleanup limited to the boot snapshot; the full suite passes; a built server answers initialize in about 400 ms. Still open: the save_replay triple write, which stays until a variant measures frame-equivalent (#293).

  4. added a commit that references this issue on Nov 18, 2026
  5. TMHSDigital commented on Nov 19, 2026

    @TMHSDigital
    OwnerAuthor

    Two of the three items are fixed: the attach() scan (1bae732) and startup reaping (e596700). The save_replay triple write stays on purpose: #293 measured the single-pass alternatives and they were not frame-equivalent. Closing; a new issue can track it if a variant is measured equal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort: mediumperformancepriority: low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions