Repository navigation
perf: reuse owned payloads in built-in request codecs - #1493
Conversation
Signed-off-by: Will Killian <[email protected]>
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
In-place codec encoding crates/core/src/codec/anthropic.rs, crates/core/src/codec/gemini_generate_content.rs, crates/core/src/codec/oci_genai.rs, crates/core/src/codec/openai_chat.rs, crates/core/src/codec/openai_responses.rs, crates/core/tests/unit/codec/openai_chat_tests.rs, crates/core/tests/unit/codec/parity_tests.rs |
The codecs patch owned request content and return the modified request. OpenAI Responses reads input from the mutable content when preserving string-form input. Tests check parity, header preservation, and reuse of large payload allocations. |
Priority:
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Refactor
Possibly related issues
- NVIDIA/NeMo-Relay issue 1492: This change addresses codec payload copying, but does not address post-release memory retention or a request-size cap.
Merge Risk: 
Minimal · up to e046a
-
No merge-blocking issue was identified in the owned codec encoding changes; the PR is ready for normal merge checks.
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passed-
Full details: Description check
-
Explanation
The description includes all required template sections, completed checklist items, detailed implementation notes, reviewer guidance, related issue information, and validation results. It also clearly documents the unchanged Hermes link-check failure.
-
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment @coderabbitai help to get the list of available commands.
Overview
Reduce payload copying when managed LLM execution encodes requests containing large images or other unchanged content. All five built-in request codecs now use the existing owned-encoding hook to patch the request they already own, while borrowed encoding continues to preserve its input.
Details
encode_owned_with_baselinefor OpenAI Chat, OpenAI Responses, Anthropic Messages, Gemini GenerateContent, and OCI GenAI. Delegate borrowed baseline-aware encoding through an explicit request clone so both paths retain the same merge and validation behavior.chatRequestdirectly, removing its additional full body copy.The managed unary and streaming paths already call this hook through
run_request_intercepts_with_codec_and_recorder; Python's managed bridge extracts the underlying Rust codecs for built-in codec objects. Standalone borrowedcodec.encode(annotated, original)still preserves its input, and custom codec adapters retain their existing behavior. No public API or breaking changes.Measured memory impact
Compared unmodified main at
8109e75036c1c97c26fab532fcf6a83f2152babewith this change on macOS 27.0, ARM64, with a 44 MiB synthetic image payload (11 strings of 4 MiB each). The probe calls publicllm_call_executewith a deterministic provider continuation and flushes subscriber work after each call. An auditing wrapper verified one owned-encoder invocation per call and preservation of the original image buffer through to the provider continuation in the changed build.Approximate medians in MiB with one non-retaining subscriber and the default allocator cache policy:
Allocated per call is cumulative requested allocation volume, not simultaneous residency. Peak live memory tracks outstanding Rust allocation sizes through a
GlobalAllocwrapper forwarding toSystem; it excludes allocator overhead and cached freed blocks. For OCI, process maximum RSS also fell by about 44 MiB: approximately 186 → 142 MiB without an intercept and 230 → 186 MiB with a temperature edit.The complete matrix covered 288 measured calls: baseline/candidate × six request shapes × intercept/no intercept × subscriber/no subscriber × default/disabled large cache × three measured repetitions after process warm-up. The subscriber runs delivered two events per measured call. Disabling
MallocLargeCachepreserved the qualitative result.MallocNanoZone=0was inherited in both runs. Both builds used the same instrumented development profile; no timing claim is made. Measurements cover the Rust managed unary path; Python and streaming routing were verified by source inspection.For OpenAI, Anthropic, and Gemini, this removes one payload copy per call but does not demonstrate lower whole-call peak memory or RSS in this workload. Later observability-body and middleware annotation copies still determine the peak. OCI removes two copies and also lowers the peak. This is a copy-reduction improvement related to #1492, not a claim to repair a proven cumulative native allocation leak.
Validation
cargo test -p nemo-relay --lib codec:: --locked: 654 passed.cargo fmt -p nemo-relay -- --checkandgit diff --check: passed.uv run pre-commit run: Rust formatting, workspace Clippy/check, copyright/file hygiene, and Cargo/uv/npm audits passed. The documentation link check failed on the unchanged Hermes URLhttps://hermes-agent.nousresearch.com/docs/developer-guide/relay-shared-metricsreturning HTTP 503; a focused retry failed identically.just test-rust: 6,514 passed, 4 skipped.Where should the reviewer start?
Start with
OpenAIChatCodec::encode_owned_with_baselineincrates/core/src/codec/openai_chat.rsandowned_encoding_reuses_payloads_across_provider_codecsincrates/core/tests/unit/codec/parity_tests.rs. Then review OCI's direct nested-body mutation incrates/core/src/codec/oci_genai.rsand the existing managed dispatch incrates/core/src/api/shared.rs.Related Issues: (use one of the action keywords Closes / Fixes / Resolves / Relates to)
Summary by CodeRabbit