Repository navigation
perf(native): Cut N-API round-trips in NativeSerdeDeserializer - #12170
Conversation
Speeds up NativeSerdeDeserializer by another 1.6-1.8x by removing N-API calls from the neon wrappers (uses neon's `sys` feature): - RootHolder::new_in resolves a value's type with one napi_typeof instead of an is_a probe per candidate type plus a re-check in downcast. - Strings are read with one N-API call: the UTF-16 length is O(1) and bounds the buffer, with an exact-size fast path for ASCII. - NativeStruct::entries() reads keys and values in one with_context and looks values up by the original (already internalized) key handle, replacing get_field_by_key. - NeonArray::to_vec reads all elements in one with_context; SeqAccess gets a size_hint. A Symbol/BigInt value now fails with the field/element path instead of a leaked "Cannot convert a Symbol value to a string" TypeError. backend-native's lock moves neon 1.0.0 -> 1.1.1 (already used by rust/cube): the raw bindings return Result<(), Status> there. Release build, `yarn bench`, master vs this change, mean of two runs: | case | before | after | speedup | | --- | --- | --- | --- | | PostgresQuery sqlTemplates(), 1 call | 0.090 ms | 0.054 ms | 1.67x | | PostgresQuery sqlTemplates(), x100 in Rust | 8.83 ms | 5.32 ms | 1.66x | | wide object, 1000 string fields | 0.435 ms | 0.244 ms | 1.79x | | array of 10k numbers | 0.901 ms | 0.571 ms | 1.58x | | 200 nested member-like objects | 1.31 ms | 0.80 ms | 1.63x | Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Claude finished @ovr's task in 2m 43s —— View job Looks good: 0 high, 0 medium, 1 low. Review details
Findings
Checked and found sound
Notes, no action needed
Review threadsThe listing returned no prior threads of mine (0). None were resolved or re-affirmed. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #12170 +/- ##
==========================================
- Coverage 85.15% 81.31% -3.85%
==========================================
Files 264 515 +251
Lines 89211 109723 +20512
Branches 0 4281 +4281
==========================================
+ Hits 75966 89217 +13251
- Misses 13245 19880 +6635
- Partials 0 626 +626
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
- deserialize_struct reads only the fields serde declares (aliases included) instead of every own property: values of other properties are never fetched, so no Get, typeof or Root for them, and keys are matched through a stack buffer without allocating. A compiled cube has 13 own properties (5 functions, 5 objects) while CubeDefinitionStatic needs only `name`. - Object keys come from a cached JS `Object.keys` instead of napi_get_all_property_names: v8::Object::GetPropertyNames always collects keys through KeyAccumulator's hash set, while Object.keys copies the map's enum cache. Non-enumerable properties are now skipped, as in JSON.stringify. Adds CubeDefinitionStatic / MeasureDefinitionStatic cases to the deserializer benchmark. Release build, interleaved A/B against the previous commit, median of 7: | case | before | after | speedup | | --- | --- | --- | --- | | CubeDefinitionStatic from a compiled cube | 2.66 us | 0.88 us | 3.02x | | MeasureDefinitionStatic from a measure | 1.23 us | 0.64 us | 1.92x | | wide object, 1000 string fields | 246 us | 220 us | 1.12x | | PostgresQuery sqlTemplates() | 53.3 us | 51.0 us | 1.05x | | 200 nested member-like objects | 820 us | 781 us | 1.05x | | array of 10k numbers | 579 us | 582 us | 1.00x | Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Claude finished @ovr's task in 2m 57s —— View job Approve-level: no blocking issues. 2 low, plus 1 earlier thread that still applies. Review details
Findings (inline)
What I checked and found correct
Review threadsThe listing returned 1 of my threads. I resolved 0 and re-affirmed 1: |
Addresses review comments on #12170: - read_js_string's non-ASCII fallback asks N-API for the UTF-8 length and reads once into an exact buffer, instead of reading into a 3 * utf16_len buffer and shrinking it. - NeonStruct::collect_entries prefixes the failing field inline, like NeonArray::to_vec, instead of passing it out through a side variable. - Documents that deserialize_struct / entries_for_fields can't honour #[serde(deny_unknown_fields)]. Adds a non-ASCII case to the deserializer benchmark. Interleaved A/B, median of 7: | case | 3x buffer | exact buffer | speedup | | --- | --- | --- | --- | | wide object, 1000 non-ASCII string fields | 306 us | 285 us | 1.08x | | wide object, 1000 string fields | 206 us | 206 us | 1.00x | | PostgresQuery sqlTemplates() | 45.2 us | 45.3 us | 1.00x | Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Claude finished @ovr's task in 2m 3s —— View job Looks good to merge: 0 high, 0 medium, 3 low. No correctness regressions found. Review details
Findings (posted inline)
Checked and fine
Review threadsThe listing returned 0 of my own unresolved threads, so there were none to resolve or re-affirm. I did not build or run the tests in this round. These findings come from reading the code. |
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
|
Claude finished @ovr's task in 2m 8s —— View job Changes requested: the latest commit doesn't compile. 1 high, plus 2 earlier threads that still applyReview details
High
Checked, nothing to raise
Review threadsThe listing returned 2 of my threads. I resolved 0 and re-affirmed 2, because the code at both sites is unchanged:
|
The applied review suggestion in 61595a1 replaced one line too many: it dropped the `{` that opens deserialize_struct's body, breaking the cubenativeutils build, and left half of the old comment behind. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Reviewing PR #12170
|
Addresses review comments on #12170: - The cached `Object.keys` may have been replaced by a polyfill, so its result is downcast to an array and each key to a string instead of being wrapped with `from_raw`. The per-key check costs 7-11% on struct deserialization (CubeDefinitionStatic 0.87 -> 0.98 us, still 5.5x faster than master) and nothing measurable on maps. - An unsupported value is reported by its type (`Unsupported JsValue of type Symbol`) instead of `<unprintable>`: coercing a Symbol to a string throws. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Claude finished @ovr's task in 2m 24s —— View job LGTM, no blocking issues. 0 high, 0 medium, 1 low (test coverage). 1 earlier thread is still open. Review details
Finding
Checked and found correct
Review threadsThe listing returned 1 thread of mine. I resolved 0 and re-affirmed 1: Not runThe repo wasn't built and its tests weren't run locally. This is a review only. |
Addresses review comments on #12170: - Tests keys that don't fit, or end too close to the end of, the 128-byte stack buffer entries_for_fields matches keys in. - Tests that a replaced Object.keys returning no array or non-string keys fails with a TypeError, in a fresh process each since Object.keys is cached per addon instance. Before the checks both cases panicked ("internal error in Neon module"). - Spells the buffer bound with char::MAX_LEN_UTF8 instead of a bare 4. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Claude finished @ovr's task in 2m 38s —— View job Looks good: 0 high, 0 medium, 1 low. One dead trait method remains; no correctness or safety issues found. Review details
Findings
Checked and found sound
Review threadsThe listing returned 0 of my own unresolved threads, so there was nothing to resolve or re-affirm. The earlier threads on this PR were opened by @ovr and were left alone. Not run: no Rust build or bridge tests in this job. The review is from reading the code. |
Flatten native error handling for batched array and object reads while preserving field and element context. Reserve filtered entries for at most the number of declared fields. Add deserializer benchmarks for long ASCII and Unicode strings and medium-length Unicode strings. Validated with a release bridge build, 194 bridge tests, 11 Rust tests, Clippy, TypeScript, oxlint, and rustfmt.
|
Claude finished @ovr's task in 1m 43s —— View job Verdict: no blocking issues. 0 high, 0 medium, 1 low (nit), plus 1 earlier thread that still applies. Review details
FindingsLow (benchmark only, no inline comment): the Checked and found correct
Review threadsThe listing returned 1 thread of mine. I resolved 0 and re-affirmed 1. Thread |
read_js_string_into now decides itself whether the copy is complete and returns a Cow over the caller's buffer, so entries_for_fields no longer repeats the truncation check. The buffer is MaybeUninit, avoiding a 128-byte zero fill per key, and read_js_string shares the same copy helper. Tests cover embedded NUL and unknown keys whose length straddles the 128-byte buffer. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The deserializer was its only caller and now uses entries. It also returned non-enumerable keys through napi_get_all_property_names, so a future caller would see a different key set than entries. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Claude finished @ovr's task in 2m 1s —— View job No issues found. 0 high, 0 medium, 0 low. Review details
Checked and found sound
Review threadsThe listing returned 1 of my own threads. I resolved it ( |
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Claude finished @ovr's task in 1m 37s —— View job LGTM: no issues found (0 high, 0 medium, 0 low) Review details
What I checked
Review threadsThe listing returned 0 of my own threads, so there was nothing to resolve or re-affirm. |
…ork, thanks @cavi21 (backport #12461 to v1.6) neon 1.1.1 frees N-API async work inside `debug_assert_eq!`, so release builds never call `napi_delete_async_work`. neon 1.1.2 fixes it upstream (neon-bindings/neon#1145). On v1.6 the shipped addon (packages/cubejs-backend-native/Cargo.lock) is still on neon 1.0.0, which is not affected: the bump to 1.1.1 came with #12170 (first released in v1.8.3). This backport only moves the rust/cube workspace lockfile off 1.1.1 and switches `get_cubestore_result` to `compute_scoped`, which neon 1.0.0 also provides. (cherry picked from commit 62dbc3f) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Check List
Description of Changes Made
Cuts N-API round-trips in
NativeSerdeDeserializer(cubenativeutilsneon wrappers, using neon'ssysfeature):napi_typeofper value instead of anis_aprobe per candidate type; ASCII strings are read with a single copy and non-ASCII ones into an exactly sized buffer.with_context; values are looked up by the original key handle; keys come from a cached JSObject.keys(N-API'snapi_get_all_property_namesalways takes V8's slowKeyAccumulatorpath).deserialize_structreads only the fields serde declares, so e.g.CubeDefinitionStaticno longer fetches and roots a compiled cube'smeasures,dimensionsandsqlfunctions.Behaviour changes: non-enumerable properties are skipped (as in
JSON.stringify); Symbol/BigInt values fail with the field/element path instead of leaking aTypeError;#[serde(deny_unknown_fields)]isn't honoured bydeserialize_struct(no bridged struct uses it). backend-native'sCargo.lockmoves neon 1.0.0 → 1.1.1 to matchrust/cube.Release build, master vs this PR (78cb37c), interleaved A/B, median of 7 rounds:
🤖 Generated with Claude Code