nosj 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7152dbc291ed6d291977555ba6a6b36f6d3d52dad57348a0baf8271212d1bb25
4
- data.tar.gz: '083bd8ca6fac180455b4847c1634f4ac28455db8e6e85def4e14393700e3008f'
3
+ metadata.gz: d57a15d6dbdf7dc81518662ef44348f538b6dcf2a95098c3491c741b9e2517b6
4
+ data.tar.gz: e5508e9cf8ea61c51e21599ef4a887f024ab08a920a0c642dfcb3ac7fd7b4954
5
5
  SHA512:
6
- metadata.gz: 254595b89839c50a48adedc03f8ec70498cc0cf6876390565f3dd188cc5dfca564c498443d0091c23bbf6562bb11b1724a25c073fa9e76986b8b1779cc91765d
7
- data.tar.gz: 4d321fb92d1199649dc10f84853aa3124e40cc642b3da680772f392c315374116f04d09882f4fd95f2c445941b384f15e23ab89fc3c735edebd8868855cb8582
6
+ metadata.gz: 9a7797c890345c879153069e63808fa916fe0e5944376680a86ba30696ef7eec16a6a0f45ae7c428c08783c56bb1a08bed448c27d3b7a7075caea064edee4205
7
+ data.tar.gz: 29113d820443054acafaeda1a976f4a388424466d79d09353fac5980cc80d803937a800b6384ed9395d9386735420cbd378cc8a6e6629cd5bdbefd1bd70fd5bd
data/CHANGELOG.md CHANGED
@@ -1,3 +1,28 @@
1
+ ## [0.4.0] - 2026-09-05
2
+
3
+ - Ractors: on Ruby 4.0+, `NOSJ.parse`, `NOSJ.generate`, and every
4
+ other entry point (lazy documents, partial parsing, the file APIs,
5
+ NDJSON, patches, reformatting, statistics) can be called from inside
6
+ a Ractor. Before this release each of them raised
7
+ `Ractor::UnsafeError` outside the main Ractor. Values parsed with
8
+ `freeze: true` are Ractor-shareable, so a document parsed in one
9
+ Ractor can be handed to another without copying.
10
+ - Updated Rust dependencies.
11
+
12
+ ## [0.3.2] - 2026-07-19
13
+
14
+ - **IMPORTANT**—Fixed: the precompiled platform gems linked `libruby`
15
+ into the extension. The arm64-darwin binaries recorded the build runner's
16
+ absolute Ruby path, so `require "nosj"` failed with `Library not
17
+ loaded: /Users/runner/hostedtoolcache/...` on any machine with Ruby
18
+ installed elsewhere (#2), and the Linux binaries carried a
19
+ `libruby.so` runtime dependency that a statically built Ruby (the
20
+ ruby-build default) cannot satisfy. The cause was magnus's `embed`
21
+ feature—needed only by the fuzz harness, which now enables it
22
+ itself—pulling `rb-sys/link-ruby` into the gem build. Extension
23
+ symbols now resolve from the host process at load time, and the
24
+ release build refuses to stage a binary that links libruby.
25
+
1
26
  ## [0.3.1] - 2026-07-17
2
27
 
3
28
  - Fixed: `NOSJ.minify` / `NOSJ.reformat` produced unparseable output
data/Cargo.lock CHANGED
@@ -17,9 +17,9 @@ dependencies = [
17
17
 
18
18
  [[package]]
19
19
  name = "aho-corasick"
20
- version = "1.1.4"
20
+ version = "1.1.5"
21
21
  source = "registry+https://github.com/rust-lang/crates.io-index"
22
- checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
22
+ checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba"
23
23
  dependencies = [
24
24
  "memchr",
25
25
  ]
@@ -44,9 +44,9 @@ dependencies = [
44
44
 
45
45
  [[package]]
46
46
  name = "bitflags"
47
- version = "2.13.0"
47
+ version = "2.13.1"
48
48
  source = "registry+https://github.com/rust-lang/crates.io-index"
49
- checksum = "b4388bee8683e3d04af747c73422af53102d2bd24d9eadb6cbc100baef4b43f8"
49
+ checksum = "b588b76d00fde79687d7646a9b5bdf3cc0f655e0bbd080335a95d7e96f3587da"
50
50
 
51
51
  [[package]]
52
52
  name = "cexpr"
@@ -65,9 +65,9 @@ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
65
65
 
66
66
  [[package]]
67
67
  name = "clang-sys"
68
- version = "1.8.1"
68
+ version = "1.9.1"
69
69
  source = "registry+https://github.com/rust-lang/crates.io-index"
70
- checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4"
70
+ checksum = "157a8ba7b480713b56f4c09fd13fc3e0a22a5dfab8097ba61cbc5feef950788a"
71
71
  dependencies = [
72
72
  "glob",
73
73
  "libc",
@@ -76,15 +76,15 @@ dependencies = [
76
76
 
77
77
  [[package]]
78
78
  name = "either"
79
- version = "1.16.0"
79
+ version = "1.18.0"
80
80
  source = "registry+https://github.com/rust-lang/crates.io-index"
81
- checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e"
81
+ checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34"
82
82
 
83
83
  [[package]]
84
84
  name = "fast-float2"
85
- version = "0.2.3"
85
+ version = "0.2.4"
86
86
  source = "registry+https://github.com/rust-lang/crates.io-index"
87
- checksum = "f8eb564c5c7423d25c886fb561d1e4ee69f72354d16918afa32c08811f6b6a55"
87
+ checksum = "c6e8948ce679d00a02a94739ea185595dca7118ed04feb991127e443bd3d761f"
88
88
 
89
89
  [[package]]
90
90
  name = "getrandom"
@@ -100,9 +100,9 @@ dependencies = [
100
100
 
101
101
  [[package]]
102
102
  name = "glob"
103
- version = "0.3.3"
103
+ version = "0.3.4"
104
104
  source = "registry+https://github.com/rust-lang/crates.io-index"
105
- checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280"
105
+ checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b"
106
106
 
107
107
  [[package]]
108
108
  name = "itertools"
@@ -121,9 +121,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
121
121
 
122
122
  [[package]]
123
123
  name = "libc"
124
- version = "0.2.186"
124
+ version = "0.2.189"
125
125
  source = "registry+https://github.com/rust-lang/crates.io-index"
126
- checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
126
+ checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"
127
127
 
128
128
  [[package]]
129
129
  name = "libloading"
@@ -137,8 +137,9 @@ dependencies = [
137
137
 
138
138
  [[package]]
139
139
  name = "magnus"
140
- version = "0.9.0"
141
- source = "git+https://github.com/matsadler/magnus#4e46772050e47cd6cd988fa935263cc5c583e388"
140
+ version = "0.8.2"
141
+ source = "registry+https://github.com/rust-lang/crates.io-index"
142
+ checksum = "3b36a5b126bbe97eb0d02d07acfeb327036c6319fd816139a49824a83b7f9012"
142
143
  dependencies = [
143
144
  "magnus-macros",
144
145
  "rb-sys",
@@ -148,8 +149,9 @@ dependencies = [
148
149
 
149
150
  [[package]]
150
151
  name = "magnus-macros"
151
- version = "0.9.0"
152
- source = "git+https://github.com/matsadler/magnus#4e46772050e47cd6cd988fa935263cc5c583e388"
152
+ version = "0.8.0"
153
+ source = "registry+https://github.com/rust-lang/crates.io-index"
154
+ checksum = "47607461fd8e1513cb4f2076c197d8092d921a1ea75bd08af97398f593751892"
153
155
  dependencies = [
154
156
  "proc-macro2",
155
157
  "quote",
@@ -198,7 +200,7 @@ dependencies = [
198
200
 
199
201
  [[package]]
200
202
  name = "nosj_native"
201
- version = "0.3.1"
203
+ version = "0.4.0"
202
204
  dependencies = [
203
205
  "ahash",
204
206
  "magnus",
@@ -215,18 +217,18 @@ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
215
217
 
216
218
  [[package]]
217
219
  name = "proc-macro2"
218
- version = "1.0.106"
220
+ version = "1.0.107"
219
221
  source = "registry+https://github.com/rust-lang/crates.io-index"
220
- checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
222
+ checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
221
223
  dependencies = [
222
224
  "unicode-ident",
223
225
  ]
224
226
 
225
227
  [[package]]
226
228
  name = "quote"
227
- version = "1.0.46"
229
+ version = "1.0.47"
228
230
  source = "registry+https://github.com/rust-lang/crates.io-index"
229
- checksum = "dfbc457d0c7a0759a614551b11a6409e5951f6c7537be1f1b7682b9ae9230368"
231
+ checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
230
232
  dependencies = [
231
233
  "proc-macro2",
232
234
  ]
@@ -239,18 +241,18 @@ checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
239
241
 
240
242
  [[package]]
241
243
  name = "rb-sys"
242
- version = "0.9.128"
244
+ version = "0.9.130"
243
245
  source = "registry+https://github.com/rust-lang/crates.io-index"
244
- checksum = "45ca28513560e56cfb79a62b1fce363c73af170a182024ce880c77ee9429920a"
246
+ checksum = "02faf625bb10ba893e3ae620f19c9fb1b5f8fcae0fe4eb86bb3f2230fad75edb"
245
247
  dependencies = [
246
248
  "rb-sys-build",
247
249
  ]
248
250
 
249
251
  [[package]]
250
252
  name = "rb-sys-build"
251
- version = "0.9.128"
253
+ version = "0.9.130"
252
254
  source = "registry+https://github.com/rust-lang/crates.io-index"
253
- checksum = "ce04b2c55eff3a21aaa623fcc655d94373238e72cac6b3e1a3641ff31649f99a"
255
+ checksum = "05be6f9c86fe5482808162826f6c047d093b3670582296c770de1d94f8066694"
254
256
  dependencies = [
255
257
  "bindgen",
256
258
  "lazy_static",
@@ -269,9 +271,9 @@ checksum = "cca7ad6a7e21e72151d56fe2495a259b5670e204c3adac41ee7ef676ea08117a"
269
271
 
270
272
  [[package]]
271
273
  name = "regex"
272
- version = "1.13.0"
274
+ version = "1.13.1"
273
275
  source = "registry+https://github.com/rust-lang/crates.io-index"
274
- checksum = "2a0e75113e14dc5acb068cd0786884f214f1312650a3d36d269f5c4f3cdee8a2"
276
+ checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"
275
277
  dependencies = [
276
278
  "aho-corasick",
277
279
  "memchr",
@@ -281,9 +283,9 @@ dependencies = [
281
283
 
282
284
  [[package]]
283
285
  name = "regex-automata"
284
- version = "0.4.15"
286
+ version = "0.4.18"
285
287
  source = "registry+https://github.com/rust-lang/crates.io-index"
286
- checksum = "1f388202e4b80542a0921078cc23b6333bcf1409c1e3f86404cae4766a6131db"
288
+ checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2"
287
289
  dependencies = [
288
290
  "aho-corasick",
289
291
  "memchr",
@@ -366,18 +368,18 @@ checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e"
366
368
 
367
369
  [[package]]
368
370
  name = "zerocopy"
369
- version = "0.8.54"
371
+ version = "0.8.56"
370
372
  source = "registry+https://github.com/rust-lang/crates.io-index"
371
- checksum = "b7cbbc0a705a0fd05cc3676525980d2bf5a9bc4adac6d6475209a7887cf59d19"
373
+ checksum = "556764e583adb45a9f8d413c2a147fa7e8d821e48e12b14fd560b607998b75eb"
372
374
  dependencies = [
373
375
  "zerocopy-derive",
374
376
  ]
375
377
 
376
378
  [[package]]
377
379
  name = "zerocopy-derive"
378
- version = "0.8.54"
380
+ version = "0.8.56"
379
381
  source = "registry+https://github.com/rust-lang/crates.io-index"
380
- checksum = "e2e817b7b52d0c7358d3246da9d69935ebb18116b2b102b4230dac079b4862f5"
382
+ checksum = "f2ab42fc20575779bd240faa45f94a74256f755c0fa9e89f0ede20d91d0cdfc1"
381
383
  dependencies = [
382
384
  "proc-macro2",
383
385
  "quote",
data/README.md CHANGED
@@ -16,6 +16,7 @@ faster than Yajl—[see Benchmarks](#benchmarks).
16
16
  - It does **byte-splicing edits**: `NOSJ.splice` changes a field inside a passing payload 10–51× faster than parse-mutate-generate, and RFC 6902 JSON Patch / RFC 7386 merge patch apply straight to the raw string.
17
17
  - It is **great to debug with**: parse errors carry line, column, and a caret snippet pointing at the break, and `NOSJ.stats` X-rays a mystery blob (depth, value counts, key histogram) faster than parsing it.
18
18
  - It accelerates a **Rails** application in both encoding and decoding.
19
+ - It is **Ractor-safe**: call it from any Ractor on Ruby 4.0+, and parse with `freeze: true` to get shareable results.
19
20
  - It comes **precompiled** (platform gems built with per-platform optimizations,
20
21
  nothing to compile on install).
21
22
  - Otherwise, same API and option names as gem json.
@@ -300,6 +301,22 @@ NOSJ.parse(%({\n "a": 1,\n "b": }))
300
301
  # ^
301
302
  ```
302
303
 
304
+ ### Ractors
305
+
306
+ On Ruby 4.0+, every entry point works inside a Ractor: parse and
307
+ generate, lazy documents, partial parsing, the file APIs, NDJSON,
308
+ patches, reformatting, and statistics.
309
+
310
+ ```ruby
311
+ workers = Array.new(4) do
312
+ Ractor.new(payload) { |src| NOSJ.parse(src, freeze: true) }
313
+ end
314
+ workers.map(&:value)
315
+ ```
316
+
317
+ `freeze: true` returns deeply frozen, Ractor-shareable values, so a
318
+ document parsed in one Ractor can be handed to another without a copy.
319
+
303
320
  ## Benchmarks
304
321
 
305
322
  Every installed JSON gem, benchmark-ips: AWS EC2 c7a.2xlarge (AMD EPYC 9R14, Zen 4), Ruby 4.0.6 + YJIT, json 2.21.1, Oj 3.17.4, RapidJSON 0.4.0, FastJsonparser 0.6.0, Yajl 1.4.3, PGO build, 2026-07-16. `×N` = times slower than nosj.
data/ext/nosj/Cargo.toml CHANGED
@@ -5,7 +5,7 @@
5
5
  # required by lib/nosj.rb as "nosj/nosj" (Init_nosj set explicitly in
6
6
  # lib.rs). Gem name, module, and API are plain nosj.
7
7
  name = "nosj_native"
8
- version = "0.3.1"
8
+ version = "0.4.0"
9
9
  edition = "2021"
10
10
  authors = ["Yaroslav Markin <yaroslav@markin.net>"]
11
11
  license = "MIT"
@@ -16,7 +16,7 @@ name = "nosj"
16
16
  crate-type = ["cdylib", "rlib"]
17
17
 
18
18
  [dependencies]
19
- magnus = { git = "https://github.com/matsadler/magnus", features = ["embed", "rb-sys"] }
19
+ magnus = { version = "0.8", features = ["rb-sys"] }
20
20
  rb-sys = { version = "0.9.124", default-features = false }
21
21
  ahash = "0.8.11"
22
22
  # Read-only file mapping for the file entry points (load_lazy_file,
@@ -9,9 +9,11 @@ cargo-fuzz = true
9
9
 
10
10
  [dependencies]
11
11
  libfuzzer-sys = "0.4"
12
- # Must match the extension's own magnus line so cargo unifies the
13
- # rb-sys link (one libruby, one VM).
14
- magnus = { git = "https://github.com/matsadler/magnus", features = ["embed", "rb-sys"] }
12
+ # Same magnus version as the extension so cargo unifies them into one
13
+ # crate. Only this harness enables "embed" (rb-sys/link-ruby): fuzz
14
+ # binaries host their own VM, while the extension must never link
15
+ # libruby (its symbols resolve from the host process at load time).
16
+ magnus = { version = "0.8", features = ["embed", "rb-sys"] }
15
17
  # The extension as an rlib; its lib target is named `nosj` (the bundle
16
18
  # name), so rename the dependency to keep fuzz code readable.
17
19
  nosj_ext = { path = "..", package = "nosj_native" }
@@ -16,7 +16,9 @@ use magnus::{Class, Error, ExceptionClass, Module, Object, RModule, RObject, Rub
16
16
  /// extension is loaded without lib/nosj.rb).
17
17
  pub(crate) fn nosj_exception(ruby: &Ruby, name: &str) -> ExceptionClass {
18
18
  let lookup = || -> Result<ExceptionClass, Error> {
19
- let m: RModule = ruby.define_module("NOSJ")?;
19
+ // A lookup, not define_module: defining is main-Ractor work, and
20
+ // this runs wherever a parse fails.
21
+ let m: RModule = ruby.class_object().const_get("NOSJ")?;
20
22
  m.const_get(name)
21
23
  };
22
24
  lookup().unwrap_or_else(|_| ruby.exception_runtime_error())
@@ -29,20 +29,24 @@ mod walker;
29
29
 
30
30
  use magnus::rb_sys::{AsRawValue, FromRawValue};
31
31
  use magnus::{Error, RString, Ruby, Value};
32
- use std::cell::RefCell;
32
+ use std::cell::Cell;
33
33
 
34
34
  use errors::raise_fail;
35
35
  use keys::GenKeyCache;
36
+ pub(crate) use ruby::warm_up;
36
37
  use walker::Gen;
37
38
 
38
39
  /// Per-thread generate scratch: the pooled output buffer (capacity
39
- /// survives across calls) and the pre-escaped key cache. One
40
- /// thread_local, borrowed ONCE for the whole call: in a shared library
41
- /// every thread_local access is a `__tls_get_addr` call, and the old
42
- /// take/put-back shape paid two of them plus struct moves per call
43
- /// (measurable on 45-byte documents). A recursive generate via a user
44
- /// `to_json` hits the failed `try_borrow_mut` arm and runs on a fresh
45
- /// scratch instead of panicking the RefCell.
40
+ /// survives across calls) and the pre-escaped key caches. A call takes
41
+ /// the scratch OUT of the thread-local and stores it back afterwards:
42
+ /// off the main Ractor, Ruby threads run M:N over a pool of native
43
+ /// threads, so a thread resuming after a `to_json` / `as_json` callback
44
+ /// may be on another native thread, where a reference into the old
45
+ /// thread's cell would alias whatever that thread is running. An empty
46
+ /// cell (a recursive generate holds the scratch, or a thread hop left
47
+ /// it elsewhere) means a fresh one. The Box moves as one pointer; the
48
+ /// cost over a plain borrow is one thread-local address lookup per
49
+ /// call, measured at parity (feature/ractor, 2026-09).
46
50
  struct GenScratch {
47
51
  buf: Vec<u8>,
48
52
  keys: GenKeyCache,
@@ -52,12 +56,31 @@ struct GenScratch {
52
56
  html_keys: GenKeyCache,
53
57
  }
54
58
 
59
+ impl GenScratch {
60
+ fn fresh() -> Box<Self> {
61
+ Box::new(GenScratch {
62
+ buf: Vec::new(),
63
+ keys: GenKeyCache::with_capacity(256),
64
+ html_keys: GenKeyCache::with_capacity(64),
65
+ })
66
+ }
67
+ }
68
+
55
69
  thread_local! {
56
- static GEN_SCRATCH: RefCell<GenScratch> = RefCell::new(GenScratch {
57
- buf: Vec::new(),
58
- keys: GenKeyCache::with_capacity(256),
59
- html_keys: GenKeyCache::with_capacity(64),
60
- });
70
+ static GEN_SCRATCH: Cell<Option<Box<GenScratch>>> = const { Cell::new(None) };
71
+ }
72
+
73
+ /// Run `f` on this thread's scratch, then store it back, replacing one a
74
+ /// recursive generate stored meanwhile (the outermost call's is the warm
75
+ /// one; the replaced scratch's key cache keeps its shadow, exactly as
76
+ /// the old fallback arm's did).
77
+ fn with_scratch<R>(f: impl FnOnce(&mut GenScratch) -> R) -> R {
78
+ let mut scratch = GEN_SCRATCH
79
+ .with(Cell::take)
80
+ .unwrap_or_else(GenScratch::fresh);
81
+ let result = f(&mut scratch);
82
+ GEN_SCRATCH.with(|cell| cell.set(Some(scratch)));
83
+ result
61
84
  }
62
85
 
63
86
  /// `NOSJ.generate(obj, opts = nil)`, registered as a variadic native
@@ -174,27 +197,18 @@ fn generate_scratched_into<R>(
174
197
  cap_hint: usize,
175
198
  finish: impl FnOnce(&Ruby, &[u8]) -> Result<R, Error>,
176
199
  ) -> Result<R, Error> {
177
- GEN_SCRATCH.with(|cell| match cell.try_borrow_mut() {
178
- Ok(mut scratch) => {
179
- let scratch = &mut *scratch;
180
- let GenScratch {
181
- buf,
182
- keys,
183
- html_keys,
184
- ..
185
- } = scratch;
186
- let keys = if cfg.mode == nosj::emit::EscapeMode::HtmlSafe {
187
- html_keys
188
- } else {
189
- keys
190
- };
191
- generate_with(ruby, obj, cfg, cap_hint, buf, keys, finish)
192
- }
193
- Err(_) => {
194
- let mut buf = Vec::new();
195
- let mut keys = GenKeyCache::default();
196
- generate_with(ruby, obj, cfg, cap_hint, &mut buf, &mut keys, finish)
197
- }
200
+ with_scratch(|scratch| {
201
+ let GenScratch {
202
+ buf,
203
+ keys,
204
+ html_keys,
205
+ } = scratch;
206
+ let keys = if cfg.mode == nosj::emit::EscapeMode::HtmlSafe {
207
+ html_keys
208
+ } else {
209
+ keys
210
+ };
211
+ generate_with(ruby, obj, cfg, cap_hint, buf, keys, finish)
198
212
  })
199
213
  }
200
214
 
@@ -258,10 +272,7 @@ pub(crate) fn emit_into(
258
272
  cfg: &opts::GenConfig,
259
273
  out: &mut Vec<u8>,
260
274
  ) -> Result<(), Error> {
261
- GEN_SCRATCH.with(|cell| match cell.try_borrow_mut() {
262
- Ok(mut scratch) => emit_one(ruby, obj, cfg, out, &mut scratch.keys),
263
- Err(_) => emit_one(ruby, obj, cfg, out, &mut GenKeyCache::default()),
264
- })
275
+ with_scratch(|scratch| emit_one(ruby, obj, cfg, out, &mut scratch.keys))
265
276
  }
266
277
 
267
278
  /// Formatting strings holding a newline (or carriage return) would
@@ -313,17 +324,9 @@ pub(crate) fn generate_lines_bytes_into<R>(
313
324
  "formatting options containing newlines would break JSON Lines framing",
314
325
  ));
315
326
  }
316
- GEN_SCRATCH.with(|cell| match cell.try_borrow_mut() {
317
- Ok(mut scratch) => {
318
- let scratch = &mut *scratch;
319
- let GenScratch { buf, keys, .. } = scratch;
320
- generate_lines_with(ruby, values, cfg, cap_hint, buf, keys, finish)
321
- }
322
- Err(_) => {
323
- let mut buf = Vec::new();
324
- let mut keys = GenKeyCache::default();
325
- generate_lines_with(ruby, values, cfg, cap_hint, &mut buf, &mut keys, finish)
326
- }
327
+ with_scratch(|scratch| {
328
+ let GenScratch { buf, keys, .. } = scratch;
329
+ generate_lines_with(ruby, values, cfg, cap_hint, buf, keys, finish)
327
330
  })
328
331
  }
329
332
 
@@ -37,6 +37,15 @@ fn as_json_id() -> rb_sys::ID {
37
37
  as rb_sys::ID
38
38
  }
39
39
 
40
+ /// Resolve the `OnceLock`s here now (init, main Ractor): an initializer
41
+ /// that enters the VM must never race between Ractors (see lib.rs), and
42
+ /// an initialized lock never blocks again.
43
+ pub(crate) fn warm_up() {
44
+ as_json_id();
45
+ to_json_id();
46
+ utf8_encindexes();
47
+ }
48
+
40
49
  /// Whether `v` is a `JSON::Fragment` (pre-rendered JSON to splice
41
50
  /// verbatim: the gem accepts fragments even under `strict`, and
42
51
  /// ActiveSupport's encoder passes them through). The class is resolved
data/ext/nosj/src/lib.rs CHANGED
@@ -37,6 +37,14 @@ use magnus::{method, prelude::*, Error, Ruby};
37
37
  // "nosj/nosj"), not the package name nosj_native (see Cargo.toml).
38
38
  #[magnus::init(name = "nosj")]
39
39
  fn init(ruby: &Ruby) -> Result<(), Error> {
40
+ // Declared before any method exists: Ruby marks the methods defined
41
+ // after this call as callable from every Ractor. What backs it: the
42
+ // generate scratch is taken out of its thread-local for a call (see
43
+ // gen::GEN_SCRATCH), the key caches hold only shareable VALUEs
44
+ // (interned strings, static symbols), and the warm-up at the end of
45
+ // init resolves every lazily-initialized static on the main Ractor.
46
+ // SAFETY: a flag write on the VM's extension-load state.
47
+ unsafe { rb_sys::rb_ext_ractor_safe(true) };
40
48
  compile_info();
41
49
 
42
50
  let module = ruby.define_module("NOSJ")?;
@@ -104,6 +112,15 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
104
112
  // its `generate` through a Ruby frame into C, so skipping our own
105
113
  // forwarder frame is a straight per-call win on small documents.
106
114
  module.define_singleton_method("generate", method!(gen::generate_entry, -1))?;
115
+
116
+ // Warm-up on the main Ractor: magnus resolves a TypedData class (and
117
+ // gen/ruby.rs its interned IDs) behind a blocking lazy initializer
118
+ // that calls into the VM, and two Ractors racing that first touch
119
+ // deadlock (the loser parks natively and never joins the VM barrier
120
+ // the winner needs).
121
+ let _ = <state::ShadowHandle as magnus::TypedData>::class(ruby);
122
+ let _ = <lazy::LazyNode as magnus::TypedData>::class(ruby);
123
+ gen::warm_up();
107
124
  Ok(())
108
125
  }
109
126
 
@@ -83,7 +83,7 @@ pub(crate) fn ensure_marked_shadow(slot: &mut Option<&'static mut VStackShadow>)
83
83
  }));
84
84
  let ptr = std::ptr::from_mut::<VStackShadow>(shadow).cast_const();
85
85
  let handle: Obj<ShadowHandle> = ruby.obj_wrap(ShadowHandle(ptr));
86
- ruby.gc_register_mark_object(handle);
86
+ magnus::gc::register_mark_object(handle);
87
87
  *slot = Some(shadow);
88
88
  }
89
89
  }
data/lib/nosj/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  module NOSJ
4
4
  # The gem version.
5
- VERSION = "0.3.1"
5
+ VERSION = "0.4.0"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nosj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Markin