Skip to content

perf(json-rpc): skip re-parse when returning owned Box<RawValue> - #4057

Merged
mattsse merged 1 commit into
alloy-rs:mainfrom
mfw78:perf/rawvalue-success-fast-path
Jul 3, 2026
Merged

mattsse merged 1 commit into
alloy-rs:mainfrom
mfw78:perf/rawvalue-success-fast-path

Conversation

@mfw78

@mfw78 mfw78 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

try_deserialize_ok calls serde_json::from_str::<Box<RawValue>> even when J is already Box<RawValue>, so the raw passthrough path (Provider::raw_request, RpcClient::request/batch with Box<RawValue>) copies and re-validates a payload it already owns on every response.

When J and T are both Box<RawValue>, return the owned payload directly behind a TypeId guard, matching the transmute_copy specialization in IntoBoxTransport::into_box_transport. The generic deserialize path is unchanged.

This adds a 'static bound to the (public) try_deserialize_ok, needed for TypeId. Both in-tree callers pass Box<RawValue>; only external callers passing a borrowed J would be affected.

The raw-request response path deserialized an already-owned Box<RawValue>
back into Box<RawValue>, repeating a byte copy and a full JSON validation
scan over a value that was already validated. When the requested response
type is Box<RawValue>, hand the owned payload back directly via a TypeId
guard, matching the existing transmute-based specialization in the boxed
transport. This removes one copy and one validation scan per raw request.

@mattsse mattsse left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this seems okay, we only use it for raw_request_dyn and there we already enforce 'static owned I believe so the additional 'static is okay here.

wdyt @DaniPopes

@github-project-automation github-project-automation Bot moved this to Reviewed in Alloy Jul 3, 2026
@mfw78

mfw78 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

The failing docs job is an upstream rustc regression, not this PR. The 2026-07-02 nightly ICEs in rustc_metadata (get_item_attrs decoder assertion) when rustdoc inlines a pub extern crate re-export, which here is prost::alloc. It is tracked in rust-lang/rust#158686 and triggered by rust-lang/rust#158569. It reproduces on main without this change (the base commit ICEs identically), and every other check passes. The job should clear on a nightly that carries the fix, or on a re-run once it lands.

@mattsse
mattsse merged commit c3db9d4 into alloy-rs:main Jul 3, 2026
28 of 30 checks passed
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in Alloy Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants