Typed deserialization improves with `#[inline]` on `PinnedInput::as_ptr`
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- Half a day
- Newbie friendliness
- 45/100
- Issue type
- Refactor
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- rust
- Domain
- performance
Research direction
The change under discussion is an inline attribute on PinnedInput::as_ptr in sonic-rs/src/reader.rs; compare it with the existing inline hints on Read::slice, peek and peek_n in the same file. Start by running the reproduction from the issue against a local patch with #[inline] added, and check whether the reported timings hold. Done means a benchmark result and a recorded decision on whether to keep the attribute, with the binary-size tradeoff noted.
Written by the indexing model from the issue text.
Description
Describe the bug
The example below parses only code and msg from a 35-byte JSON object. The code type is either String or FastStr. The msg type stays String. Adding #[inline] to PinnedInput::as_ptr improves both variants.
Debian Bookworm ARM64 results, in ns/op. Each cell is the median and range of 24 batch means:
Attribute on PinnedInput::as_ptr |
String | FastStr |
|---|---|---|
| None | 86.6, range 84.2–102.5 | 83.1, range 80.4–180.6 |
#[inline] |
57.6, range 56.5–62.1 | 57.4, range 56.1–63.2 |
#[inline(always)] |
57.9, range 56.3–69.4 | 57.2, range 55.9–64.8 |
The benchmark includes result destruction, checks successful parsing, and alternates type order. Each batch runs 1,000,000 iterations. The two inline attributes produced similar results.
To Reproduce
Create an empty Cargo project with these two files.
Cargo.toml:
[package]
name = "sonic-inline-repro"
version = "0.1.0"
edition = "2024"
[workspace]
[dependencies]
serde = { version = "=1.0.229", features = ["derive"] }
sonic-rs = { version = "=0.5.10", default-features = false }
src/main.rs:
use serde::{Deserialize, de::DeserializeOwned};
use std::{hint::black_box, time::Instant};
const JSON: &[u8] = br#"{
"code": "0000",
"msg": "ok"
}"#;
#[derive(Deserialize)]
struct Response<C> {
code: C,
msg: String,
}
#[inline(never)]
fn parse<C: DeserializeOwned>(input: &[u8]) -> sonic_rs::Result<Response<C>> {
sonic_rs::from_slice(input)
}
fn measure<C: DeserializeOwned + AsRef<str>>(iterations: usize) -> f64 {
let response = parse::<C>(JSON).unwrap();
assert_eq!(response.code.as_ref(), "0000");
assert_eq!(response.msg, "ok");
drop(response);
let start = Instant::now();
for _ in 0..iterations {
let result = black_box(parse::<C>(black_box(JSON)));
assert!(result.is_ok());
}
start.elapsed().as_nanos() as f64 / iterations as f64
}
fn main() {
measure::<String>(100_000);
measure::<sonic_rs::FastStr>(100_000);
for round in 0..12 {
let (string, faststr) = if round % 2 == 0 {
let s = measure::<String>(1_000_000);
(s, measure::<sonic_rs::FastStr>(1_000_000))
} else {
let f = measure::<sonic_rs::FastStr>(1_000_000);
(measure::<String>(1_000_000), f)
};
println!("{round},{string:.2},{faststr:.2}");
}
}
Pin the tested FastStr version, then run the example:
cargo update -p faststr --precise 0.2.34
cargo run --release
Each output row contains the round, String ns/op, and FastStr ns/op.
To compare the inline change, check out the published source:
git clone https://github.com/cloudwego/sonic-rs.git
git -C sonic-rs checkout 7eca25c81884264043eb5434c1fa3fcba49600b1
Add this section to Cargo.toml:
[patch.crates-io]
sonic-rs = { path = "sonic-rs" }
Add #[inline] immediately above PinnedInput::as_ptr in sonic-rs/src/reader.rs, then rerun the same command. Replace it with #[inline(always)] for the third comparison.
Expected behavior
Would #[inline] be appropriate for this small accessor? Read::slice, peek, and peek_n already request inlining. Making the accessor available for cross-crate inlining appears useful here.
There is a tradeoff. On macOS, this program's __text section grows from 303,420 to 337,660 bytes with #[inline]. A broader benchmark check is needed before adopting the change.
Screenshots
Not applicable.
sonic-rs version:
sonic-rs 0.5.10, faststr 0.2.34, serde 1.0.229.
Environment:
Apple M4, Debian 12 Bookworm ARM64 through OrbStack Docker, glibc 2.36, one pinned vCPU. Rust 1.99.1, LLVM 23.1.1, release defaults, no custom allocator or CPU flags. The same example also improves on macOS 27.2 ARM64. x86_64 and current main are untested.
- Dominant language
- Rust
- Stars
- 951
- Forks
- 81
- Avg merge
- 3d 19h
- Merged PRs (30d)
- 8
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cloudwego/sonic-rs
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100
cloudwego/sonic-rs#246 · 1 reaction ·
Maintainers usually reply within 1 day
-
sonic-simd fails to build on 32-bit x86 (i686): `arch::x86_64` imported under a `target_feature = "sse2"` gatePossibly taken @alex09x claimed this 120 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
cloudwego/sonic-rs#228 · 1 comment · 2 reactions ·
Maintainers usually reply within 1 day
-
Serialize to io::Write without staging every string in a 6x bufferPossibly taken @MattJackson claimed this 1 day ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 35/100
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 15/100
Maintainers usually reply within 1 day
-
sonic-rs does not compile for 32-bit x86: `MetaNode` is 12 bytes because `u64` is 4-byte alignedPossibly taken @JamBalaya56562 claimed this 60 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 72/100
cloudwego/sonic-rs#258 · 1 reaction ·
Maintainers usually reply within 1 day
All issues in cloudwego/sonic-rs
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
jelmer/buildlog-consultant#364 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
solutionscay/skiff#310 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 75/100
valkey-io/valkey-large-object#222 ·
Maintainers usually reply within 1 day
-
bug user-priority/P1
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day