Skip to content

obs-data: parse and serialize JSON without an intermediate tree - #1461

Merged
zackees merged 2 commits into
obs-rust:masterfrom
MCPVOT:fix/1452-json-direct
Oct 19, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
MCPVOT:fix/1452-json-direct

Conversation

@MCPVOT

@MCPVOT MCPVOT commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

What

Every JSON round trip built a second, complete copy of the document in Rust:

  • parse: jansson parses the text into a C tree, ffi::json::convert copies that into the J enum (String per key and per string value, Vec per object), and ingest copies it again into Data;
  • serialize: snapshot clones every name, to_j builds the J tree, dumps copies it into a fresh jansson tree, and the C ABI entry point then built a String it immediately threw away.

So a key or a string was allocated two or three times per direction, and the whole document crossed a third representation each way.

This builds each side straight from the other:

  • json::load_data walks the parsed jansson tree once and fills Data in place. Children, arrays (object elements only), scalars and nulls follow obs_data_add_json_item in libobs/obs-data.c.h exactly, and the error path logs the same message as obs_data_create_from_json.
  • json::dump_data builds the jansson tree directly from Data and returns one CString. Data::refresh_json stores it and the C ABI returns its pointer, so the text is copied once instead of three times.
  • J, loads and dumps stay public and unchanged: no API break.

The produced text and the object order are identical, so the change is invisible to callers.

Numbers

Bench: cargo bench -p obs-data --bench data_comparison (#1449), 286 KB document (2000 child objects, 5 ints and 1 string each). Host: Windows 11, rustc 1.97.1 (the repo pins 1.98; built with --ignore-rust-version), x86_64-pc-windows-gnu.

The C side is untouched by this PR, so it is the drift control. Three alternating runs per side (the machine drifts run to run; a plain before/after pair showed the C path itself moving 5-35 %):

run create_from_json rust C ratio get_json rust C ratio
before-1 45.97 ms 27.21 ms 1.69 23.97 ms 14.22 ms 1.69
before-2 40.52 ms 25.16 ms 1.61 23.86 ms 19.15 ms 1.25
before-3 63.57 ms 27.31 ms 2.33 31.42 ms 15.51 ms 2.03
after-1 32.26 ms 26.73 ms 1.21 19.46 ms 14.16 ms 1.37
after-2 36.44 ms 19.99 ms 1.82 16.16 ms 9.77 ms 1.65
after-3 33.06 ms 30.05 ms 1.10 16.33 ms 15.69 ms 1.04

Medians:

Rust before Rust after Δ Rust/C before → after
create_from_json 45.97 ms 33.06 ms −28.1 % 1.69 → 1.21
get_json 23.97 ms 16.33 ms −31.9 % 1.69 → 1.37

No run of one side overlaps the other in either direction. The before numbers are the local RED evidence: the same bench on the parent commit, recorded because a performance fix has no failing unit test to commit.

Correctness

  • cargo test -p obs-data: 89 passed, 0 failed — including data::parity::json_shapes_match_oracle, the random-scalar parity tests and the thread tests.
  • cargo fmt --all --check and cargo clippy -p obs-data --all-targets -- -D warnings: clean.
  • Local note: soldr is not available on this Windows host, so these were plain cargo --ignore-rust-version runs, and the C oracle needed two local-only shims (MSVC intrinsics that LLVM-MinGW lacks, and C11 timespec_get that its MSVCRT target does not have). Neither shim is in this PR; PR CI is the gate.

Fixes #1452

Every JSON round trip built a second, complete copy of the document in
Rust. Parsing copied jansson's tree into the `J` enum and then copied
that into `Data`; serializing copied `Data` into `J` and that into a new
jansson tree, and the C ABI entry point then built a `String` it threw
away. A key or string was allocated two or three times per direction.

- `json::load_data` walks the parsed tree once and fills `Data` in
  place. Children, arrays (object elements only), scalars and nulls
  follow `obs_data_add_json_item` in `libobs/obs-data.c.h` exactly.
- `json::dump_data` builds the jansson tree straight from `Data` and
  returns one `CString`; `Data::refresh_json` stores it and the C ABI
  returns its pointer, so nothing is copied twice.
- `J`, `loads` and `dumps` stay public and unchanged.

Measured with the obs-rust#1449 bench (2000 objects, 5 ints and 1 string each),
three alternating runs per side with the untouched C path as the control
(Windows 11, rustc 1.97.1, x86_64-pc-windows-gnu):

  create_from_json  rust 45.97 -> 33.06 ms (median, -28.1%)
                    rust/C ratio 1.69 -> 1.21
  get_json          rust 23.97 -> 16.33 ms (median, -31.9%)
                    rust/C ratio 1.69 -> 1.37

No run of one side overlaps the other. The before numbers are the local
RED evidence: the same bench on the parent commit.

`cargo test -p obs-data`: 89 passed, 0 failed, including
data::parity::json_shapes_match_oracle. fmt and clippy -D warnings are
clean. soldr is not available on this Windows host; PR CI is the gate.

Fixes obs-rust#1452

Assisted-by: Hermes Agent (Nous Research)
@MCPVOT
MCPVOT requested a review from zackees as a code owner October 19, 2026 21:30

@zackees zackees 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.

Coordinator review: LGTM, and welcome!

Checked load_data/fill_value against obs_data_add_json_item in obs-data.c.h: objects recurse, arrays keep only object elements, and string, integer, real, true and false map one to one, all in jansson's iteration order. The load flags (JSON_REJECT_DUPLICATES) and the error log line are the same as obs_data_create_from_json. A non-object/array root can't reach the _ => None arm, because json_loads without JSON_DECODE_ANY already rejects it, so that's fine. On the way out, dump_data keeps JSON_PRESERVE_ORDER and the indent/compact flags. The existing JSON parity tests cover the unchanged text, and the bench drift control with the C side untouched is a careful way to measure.

Nit, non-blocking: the load_data doc says "a null becomes an empty object", but the code (correctly, like C's obs_data_add_json_null) does set_obj(key, None), i.e. an unset object item. Worth rewording in a follow-up.

@zackees
zackees enabled auto-merge October 19, 2026 21:42
@zackees
zackees added this pull request to the merge queue Oct 19, 2026
Merged via the queue into obs-rust:master with commit 39bc968 Oct 19, 2026
23 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 20, 2026
…1452) (obs-rust#1470)

The load_data doc said a null becomes an empty object. It does not: like
obs_data_add_json_null, it stores an object item with no value
(set_obj(key, None)), which is what makes the item serialize back to
null. Spotted in the review of obs-rust#1461.
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.

bug: obs-data: JSON round-trip 27-35% slower than C (obs_data_create_from_json / obs_data_get_json)

2 participants