Skip to content

perf(frida): evict captured messages without copying the whole queue - #2628

Merged
kaoru0822-kitauji merged 3 commits into
mainfrom
perf/frida-message-fifo
Oct 13, 2026
Merged

kaoru0822-kitauji merged 3 commits into
mainfrom
perf/frida-message-fifo

Conversation

@kaoru0822-kitauji

@kaoru0822-kitauji kaoru0822-kitauji commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

FridaInstrumentationManager keeps 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 by splice(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:

per message
filling the buffer 0.14 µs
buffer full, before 32 µs (about 31k msg/s ceiling on the main thread)
buffer full, after 0.14 µs (200k messages in 28 ms)

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

  • Both the session queue and the per-script queues become a small MessageFifo. Eviction advances a head offset, and the queue compacts with one slice once half its slots are evicted, so eviction is amortized O(1). Evicted slots are cleared immediately so their messages can be collected.
  • Each queued entry records the charge computed when it was admitted, so eviction no longer re-serializes.
  • Both queues are in arrival order, so the globally oldest message is always the oldest retained message of its script. Per-script eviction becomes an identity check on the head instead of indexOf plus splice. A script that has been unloaded has no queue, as before.

Retained messages, their order, messagesTruncated, and the loadScript/status result 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

  • New test: 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 that status().messages is 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:fast and format:check pass.
  • Not verified with real Frida: it can't attach on this macOS host (SIP). The benchmark runs the extracted eviction logic in isolation.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • When captured messages exceed the session’s storage limit, the oldest messages are removed across the session and their corresponding per-script records are kept in sync.
    • Recent messages remain available within the storage limit, and results report when message history has been truncated.

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>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T06:27:40.744903Z 65cae1b New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 611090b4-8058-42f7-88e9-efe99899822f
📥 Commits

Reviewing files that changed from the base of the PR and between 73dd8d6 and 281735c.

📒 Files selected for processing (2)
  • src/application/frida/FridaInstrumentationManager.test.ts
  • src/frida/FridaInstrumentationManager.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.

📝 Walkthrough

Frida message capture now stores session-wide and per-script messages in FIFO queues. The session byte limit evicts the oldest captured message. A regression test checks that repeated evictions retain a contiguous suffix of the newest messages.

Changes: Frida message capture

Layer / File(s) Summary
FIFO storage and eviction
src/frida/FridaInstrumentationManager.ts
Captured-message records now include a script ID and byte charge. FIFO operations use a head offset and periodically compact storage. Session eviction removes the oldest record and removes it from the script queue when it is also at that queue’s head.
FIFO integration and validation
src/frida/FridaInstrumentationManager.ts, src/application/frida/FridaInstrumentationManager.test.ts
Session and script message collections use MessageFifo. Script-load results and session status convert queue contents to arrays. A regression test checks the retained suffix after 2,000 large messages.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Suggested reviewers: dandgabr

Merge Risk: ⚪ Minimal · up to 28173

This change makes eviction of captured Frida messages cheaper while keeping retained messages and their order the same. No merge-blocking risk is evident from the supplied context.

Pre-merge checks · Passed 4 · Failed 1

Failed Failed checks (1 warning)

Check name Status Explanation Resolution
Description check Warning The description clearly explains the problem, implementation, performance results, tests, and real-Frida limitation. However, it omits required template sections for contract and boundary impact, comp… Add the missing template sections. Address contribution scope and maintainer decisions, contract and boundary impact, expected-effect checkboxes, compatibility and release metadata, security and process considerations, remaining proof gaps,…
Passed Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly identifies the Frida performance change and follows the required type(scope): outcome format.
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description clearly explains the problem, implementation, performance results, tests, and real-Frida limitation. However, it omits required template sections for contract and boundary impact, compatibility and release impact, scope decisions, safety considerations, and the Before merge checklist.

Resolution

Add the missing template sections. Address contribution scope and maintainer decisions, contract and boundary impact, expected-effect checkboxes, compatibility and release metadata, security and process considerations, remaining proof gaps, related issue status, and the Before merge checklist. List the exact validation commands that ran and their results.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Billing warning: we have not been able to collect payment for this subscription for more than 72 hours. Please update the payment method or pay any pending invoices in Billing to avoid service interruption.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@morluto

morluto commented Oct 13, 2026

Copy link
Copy Markdown
Owner

@greptileai review this

@kaoru0822-kitauji

Copy link
Copy Markdown
Collaborator Author

The red Static checks and test shards here come from main: since 2187e16, two Ghidra unit tests fail format:check and the Ghidra boundary fixtures lack application.java.min. Every failing file is a Ghidra or Ghidra-harness suite; none touch Frida. #2633 fixes main. I'll merge main here once it lands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +826 to +830
// 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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@kaoru0822-kitauji kaoru0822-kitauji Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 heapUsed peaks 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).

@greptile-apps

greptile-apps Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no blocking issue was found.

Summary

Replaces array-based message eviction with MessageFifo and stores each message’s original byte charge.

  • Frida sessions evict old messages without shifting the whole queue.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Incoming script message] --> B[Build bounded observation and compute charge]
  B --> C{Fits session budget?}
  C -- No --> D[Drop message and mark truncation]
  C -- Yes --> E{Enough room remains?}
  E -- No --> F[Remove oldest session entry]
  F --> G[Subtract stored charge]
  G --> H[Remove matching script queue head if present]
  H --> E
  E -- Yes --> I[Append to session and script queues]
  I --> J[Public reads return observations]
Loading

Reviews (3) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile

@morluto

morluto commented Oct 13, 2026

Copy link
Copy Markdown
Owner

@greptileai review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@kaoru0822-kitauji
kaoru0822-kitauji merged commit 6a05248 into main Oct 13, 2026
35 checks passed
@kaoru0822-kitauji
kaoru0822-kitauji deleted the perf/frida-message-fifo branch October 13, 2026 07:11
@morluto morluto mentioned this pull request Oct 13, 2026
3 tasks done
@morluto morluto added performance Performance regressions, scaling limits, and bounded-resource work area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior labels Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior performance Performance regressions, scaling limits, and bounded-resource work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants