nosj 0.3.2 → 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 +4 -4
- data/CHANGELOG.md +11 -0
- data/Cargo.lock +31 -31
- data/README.md +17 -0
- data/ext/nosj/Cargo.toml +1 -1
- data/ext/nosj/src/errors.rs +3 -1
- data/ext/nosj/src/gen/mod.rs +52 -49
- data/ext/nosj/src/gen/ruby.rs +9 -0
- data/ext/nosj/src/lib.rs +17 -0
- data/lib/nosj/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d57a15d6dbdf7dc81518662ef44348f538b6dcf2a95098c3491c741b9e2517b6
|
|
4
|
+
data.tar.gz: e5508e9cf8ea61c51e21599ef4a887f024ab08a920a0c642dfcb3ac7fd7b4954
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9a7797c890345c879153069e63808fa916fe0e5944376680a86ba30696ef7eec16a6a0f45ae7c428c08783c56bb1a08bed448c27d3b7a7075caea064edee4205
|
|
7
|
+
data.tar.gz: 29113d820443054acafaeda1a976f4a388424466d79d09353fac5980cc80d803937a800b6384ed9395d9386735420cbd378cc8a6e6629cd5bdbefd1bd70fd5bd
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
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
|
+
|
|
1
12
|
## [0.3.2] - 2026-07-19
|
|
2
13
|
|
|
3
14
|
- **IMPORTANT**—Fixed: the precompiled platform gems linked `libruby`
|
data/Cargo.lock
CHANGED
|
@@ -17,9 +17,9 @@ dependencies = [
|
|
|
17
17
|
|
|
18
18
|
[[package]]
|
|
19
19
|
name = "aho-corasick"
|
|
20
|
-
version = "1.1.
|
|
20
|
+
version = "1.1.5"
|
|
21
21
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
22
|
-
checksum = "
|
|
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.
|
|
47
|
+
version = "2.13.1"
|
|
48
48
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
49
|
-
checksum = "
|
|
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.
|
|
68
|
+
version = "1.9.1"
|
|
69
69
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
70
|
-
checksum = "
|
|
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.
|
|
79
|
+
version = "1.18.0"
|
|
80
80
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
81
|
-
checksum = "
|
|
81
|
+
checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34"
|
|
82
82
|
|
|
83
83
|
[[package]]
|
|
84
84
|
name = "fast-float2"
|
|
85
|
-
version = "0.2.
|
|
85
|
+
version = "0.2.4"
|
|
86
86
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
87
|
-
checksum = "
|
|
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.
|
|
103
|
+
version = "0.3.4"
|
|
104
104
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
105
|
-
checksum = "
|
|
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.
|
|
124
|
+
version = "0.2.189"
|
|
125
125
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
126
|
-
checksum = "
|
|
126
|
+
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"
|
|
127
127
|
|
|
128
128
|
[[package]]
|
|
129
129
|
name = "libloading"
|
|
@@ -200,7 +200,7 @@ dependencies = [
|
|
|
200
200
|
|
|
201
201
|
[[package]]
|
|
202
202
|
name = "nosj_native"
|
|
203
|
-
version = "0.
|
|
203
|
+
version = "0.4.0"
|
|
204
204
|
dependencies = [
|
|
205
205
|
"ahash",
|
|
206
206
|
"magnus",
|
|
@@ -217,18 +217,18 @@ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
|
|
217
217
|
|
|
218
218
|
[[package]]
|
|
219
219
|
name = "proc-macro2"
|
|
220
|
-
version = "1.0.
|
|
220
|
+
version = "1.0.107"
|
|
221
221
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
222
|
-
checksum = "
|
|
222
|
+
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
|
223
223
|
dependencies = [
|
|
224
224
|
"unicode-ident",
|
|
225
225
|
]
|
|
226
226
|
|
|
227
227
|
[[package]]
|
|
228
228
|
name = "quote"
|
|
229
|
-
version = "1.0.
|
|
229
|
+
version = "1.0.47"
|
|
230
230
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
231
|
-
checksum = "
|
|
231
|
+
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
|
232
232
|
dependencies = [
|
|
233
233
|
"proc-macro2",
|
|
234
234
|
]
|
|
@@ -241,18 +241,18 @@ checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
|
|
|
241
241
|
|
|
242
242
|
[[package]]
|
|
243
243
|
name = "rb-sys"
|
|
244
|
-
version = "0.9.
|
|
244
|
+
version = "0.9.130"
|
|
245
245
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
246
|
-
checksum = "
|
|
246
|
+
checksum = "02faf625bb10ba893e3ae620f19c9fb1b5f8fcae0fe4eb86bb3f2230fad75edb"
|
|
247
247
|
dependencies = [
|
|
248
248
|
"rb-sys-build",
|
|
249
249
|
]
|
|
250
250
|
|
|
251
251
|
[[package]]
|
|
252
252
|
name = "rb-sys-build"
|
|
253
|
-
version = "0.9.
|
|
253
|
+
version = "0.9.130"
|
|
254
254
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
255
|
-
checksum = "
|
|
255
|
+
checksum = "05be6f9c86fe5482808162826f6c047d093b3670582296c770de1d94f8066694"
|
|
256
256
|
dependencies = [
|
|
257
257
|
"bindgen",
|
|
258
258
|
"lazy_static",
|
|
@@ -271,9 +271,9 @@ checksum = "cca7ad6a7e21e72151d56fe2495a259b5670e204c3adac41ee7ef676ea08117a"
|
|
|
271
271
|
|
|
272
272
|
[[package]]
|
|
273
273
|
name = "regex"
|
|
274
|
-
version = "1.13.
|
|
274
|
+
version = "1.13.1"
|
|
275
275
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
276
|
-
checksum = "
|
|
276
|
+
checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"
|
|
277
277
|
dependencies = [
|
|
278
278
|
"aho-corasick",
|
|
279
279
|
"memchr",
|
|
@@ -283,9 +283,9 @@ dependencies = [
|
|
|
283
283
|
|
|
284
284
|
[[package]]
|
|
285
285
|
name = "regex-automata"
|
|
286
|
-
version = "0.4.
|
|
286
|
+
version = "0.4.18"
|
|
287
287
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
288
|
-
checksum = "
|
|
288
|
+
checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2"
|
|
289
289
|
dependencies = [
|
|
290
290
|
"aho-corasick",
|
|
291
291
|
"memchr",
|
|
@@ -368,18 +368,18 @@ checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e"
|
|
|
368
368
|
|
|
369
369
|
[[package]]
|
|
370
370
|
name = "zerocopy"
|
|
371
|
-
version = "0.8.
|
|
371
|
+
version = "0.8.56"
|
|
372
372
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
373
|
-
checksum = "
|
|
373
|
+
checksum = "556764e583adb45a9f8d413c2a147fa7e8d821e48e12b14fd560b607998b75eb"
|
|
374
374
|
dependencies = [
|
|
375
375
|
"zerocopy-derive",
|
|
376
376
|
]
|
|
377
377
|
|
|
378
378
|
[[package]]
|
|
379
379
|
name = "zerocopy-derive"
|
|
380
|
-
version = "0.8.
|
|
380
|
+
version = "0.8.56"
|
|
381
381
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
382
|
-
checksum = "
|
|
382
|
+
checksum = "f2ab42fc20575779bd240faa45f94a74256f755c0fa9e89f0ede20d91d0cdfc1"
|
|
383
383
|
dependencies = [
|
|
384
384
|
"proc-macro2",
|
|
385
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.
|
|
8
|
+
version = "0.4.0"
|
|
9
9
|
edition = "2021"
|
|
10
10
|
authors = ["Yaroslav Markin <yaroslav@markin.net>"]
|
|
11
11
|
license = "MIT"
|
data/ext/nosj/src/errors.rs
CHANGED
|
@@ -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
|
-
|
|
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())
|
data/ext/nosj/src/gen/mod.rs
CHANGED
|
@@ -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::
|
|
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
|
|
40
|
-
///
|
|
41
|
-
///
|
|
42
|
-
///
|
|
43
|
-
///
|
|
44
|
-
///
|
|
45
|
-
///
|
|
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:
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
-
|
|
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
|
-
|
|
317
|
-
|
|
318
|
-
|
|
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
|
|
data/ext/nosj/src/gen/ruby.rs
CHANGED
|
@@ -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
|
|
data/lib/nosj/version.rb
CHANGED