Repository navigation
obs-data: parse and serialize JSON without an intermediate tree - #1461
Conversation
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)
zackees
left a comment
There was a problem hiding this comment.
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.
…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.
What
Every JSON round trip built a second, complete copy of the document in Rust:
ffi::json::convertcopies that into theJenum (Stringper key and per string value,Vecper object), andingestcopies it again intoData;snapshotclones every name,to_jbuilds theJtree,dumpscopies it into a fresh jansson tree, and the C ABI entry point then built aStringit 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_datawalks the parsed jansson tree once and fillsDatain place. Children, arrays (object elements only), scalars and nulls followobs_data_add_json_iteminlibobs/obs-data.c.hexactly, and the error path logs the same message asobs_data_create_from_json.json::dump_databuilds the jansson tree directly fromDataand returns oneCString.Data::refresh_jsonstores it and the C ABI returns its pointer, so the text is copied once instead of three times.J,loadsanddumpsstay 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 %):
Medians:
create_from_jsonget_jsonNo 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 — includingdata::parity::json_shapes_match_oracle, the random-scalar parity tests and the thread tests.cargo fmt --all --checkandcargo clippy -p obs-data --all-targets -- -D warnings: clean.soldris not available on this Windows host, so these were plaincargo --ignore-rust-versionruns, and the C oracle needed two local-only shims (MSVC intrinsics that LLVM-MinGW lacks, and C11timespec_getthat its MSVCRT target does not have). Neither shim is in this PR; PR CI is the gate.Fixes #1452