Repository navigation
perf(frida): evict captured messages without copying the whole queue - #2628
Conversation
Once a Frida session reached its 16 MiB capture budget, every new message evicted the oldest one with Array#shift, re-serialized it to recover its byte charge, and searched and spliced its per-script array. Each of those is linear in the retained messages, so with about 188k small messages retained the cost per message rose from 0.14 µs to 32 µs and capped the main thread at roughly 31k messages per second, which a hooked hot function can exceed. Keep both queues as head-offset FIFOs that compact once half their slots are evicted, and record each message's charge when it is admitted. Arrival order already makes the globally oldest message the oldest one of its script, so per-script eviction becomes a head check. Eviction is now amortized O(1) at about 0.14 µs per message; retained messages, their order and the truncation flag are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
|
@greptileai review this |
|
The red Static checks and test shards here come from main: since 2187e16, two Ghidra unit tests fail |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 65cae1b879
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // About 500 of these fit the 16 MiB budget, so 2,000 sends evict and | ||
| // compact the queue several times. | ||
| const filler = "x".repeat(32_000); | ||
| for (let index = 0; index < 2_000; index += 1) | ||
| script.message.emit({ type: "send", payload: { index, filler } }, null); |
There was a problem hiding this comment.
Measure the capture-budget optimization
This only checks that a synthetic 2,000-message stream preserves ordering; it does not measure elapsed time, CPU time, or peak memory against the previous queue behavior. Because this commit changes the hot eviction path specifically to improve those costs, add a representative completed workflow measurement (including cold/warm scope or explicitly unmeasured costs) so the claimed optimization and its memory impact are verified.
AGENTS.md reference: AGENTS.md:L52-L56
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
I measured the real FridaInstrumentationManager message path, main versus this branch. The harness is a temporary vitest file, not committed. It reuses this test file's FakeDeviceManager/startLocalAttachedSession fakes, loads one script, and emits small send() payloads through the actual #recordMessage path, including redaction and bounded projection. Runs were on Node 26.5 on Apple Silicon. Each phase records wall time, process CPU (process.cpuUsage) and sampled heapUsed peaks.
| phase | main (origin/main file) |
this branch |
|---|---|---|
| fill: 150k msgs (budget reached at about 136k) | 2,187 ms, 14.6 µs/msg, CPU 2,222 ms | 209 ms, 1.39 µs/msg, CPU 273 ms |
| at budget: 100k more msgs | 13,099 ms, 131 µs/msg, CPU 13,128 ms (about 7.6k msg/s ceiling) | 134 ms, 1.34 µs/msg, CPU 151 ms |
| retained / truncated | 135,409 / true | 135,409 / true |
| sampled heapUsed peak | 65 MiB fill, 79 MiB at budget | 69 MiB fill, 103 MiB at budget |
- Retained messages and the truncation flag are identical. The 2,000-message ordering test pins the retained suffix.
- Heap: the sampled
heapUsedpeaks include uncollected garbage, and GC runs at different times in the two runs (main spends about 100× longer per message). The structural extra retention is one transient pointer array during compaction, about 1 MB at about 136k messages. I haven't measured retained heap after a forced GC; treat the peak difference as unmeasured rather than as a regression or an improvement. - Not measured: real Frida (it can't attach on this macOS host because of SIP), cold versus warm runs (no cache is involved), and agent tokens (output is unchanged).
|
|
@greptileai review |
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
Problem
FridaInstrumentationManagerkeeps a session's script messages under a 16 MiB capture budget. Once the budget is full, every incoming message evicts the oldest one through#recordMessage, and each eviction does three linear-time steps:managed.messages.shift()moves every retained message.Buffer.byteLength(JSON.stringify(removed))re-serializes the evicted message to recover the byte charge it was admitted with.scriptMessages.indexOf(removed)followed bysplice(index, 1)moves the per-script array.With small
send()payloads, about 188k messages fit in the budget. A standalone reproduction of the exact eviction logic (Node 26.5, Apple Silicon) measures:Real manager path (fake Frida session through
FridaInstrumentationManager#recordMessage, details in the review thread): at the budget, 100k messages take 13,099 ms (131 µs/msg, CPU 13,128 ms) on main versus 134 ms (1.34 µs/msg, CPU 151 ms) on this branch. Retained count (135,409) and truncation are identical.A hook on a hot function (
open,malloc,objc_msgSend) can exceed 31k messages per second. When that happens, the MCP/CLI process event loop is saturated by bookkeeping rather than serving requests.Fix
MessageFifo. Eviction advances a head offset, and the queue compacts with onesliceonce half its slots are evicted, so eviction is amortized O(1). Evicted slots are cleared immediately so their messages can be collected.chargecomputed when it was admitted, so eviction no longer re-serializes.indexOfplussplice. A script that has been unloaded has no queue, as before.Retained messages, their order,
messagesTruncated, and theloadScript/statusresult shapes are unchanged. Agent tokens are unaffected because the output doesn't change. Peak memory adds at most one transient pointer array during compaction, about 3 MB at the 188k-message worst case. CPU time and elapsed time are the table above.Validation
retains the newest contiguous messages across repeated budget evictions. It sends 2,000 messages of about 32 KB through the fake Frida session and asserts thatstatus().messagesis exactly the newest contiguous suffix, in order, with truncation reported. That forces several evictions and compactions.npm run test:focused:FridaInstrumentationManager.test.ts(26),fridaMcp.test.ts,fridaCli.test.ts; 33 tests pass in total. That includes the existing eviction tests, such as "keeps an active script message bucket after its last message is evicted".npm run check:fastandformat:checkpass.🤖 Generated with Claude Code
Summary by CodeRabbit