yeptris 0.6.5.4-arm64-darwin

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a8a7f8a4a161e5fd4ab2939bb11e2fddc09aa6ec252a50d9c875f46664def921
4
+ data.tar.gz: 05d5d7645e9cedab48e897aaf5b1b275cfc957ea0de41d01e2d08099fbacefc3
5
+ SHA512:
6
+ metadata.gz: 659d27bc75c7417f55ebc7211d23591013bb54d39fa43c9f678e5c1dc64480b36ffb996e23f6d7863c3f1d7d89a0b4e406a2ecc6935f432121cce47d8e3fbbb3
7
+ data.tar.gz: 0604c90986db709783fddda38b48f8087dac6b98d768c93e5b33e617604b57d1aec7a133a72d7708b8959fdf37d5530c7ca78c7ad14898578c15c75ac543337f
data/README.adoc ADDED
@@ -0,0 +1,141 @@
1
+ = yeptris — YAML for Ruby at libleptris speed
2
+
3
+ An FFI-based (no C extension) Ruby YAML library over
4
+ https://github.com/leptris/yeptris[libyeptris] — the YAML counterpart
5
+ of libleptris. Psych-compatible semantics, one shared library, zero
6
+ compilation at install.
7
+
8
+ == Install (development)
9
+
10
+ The gem loads a shared `libyeptris` — from `YEPTRIS_LIB_PATH`, a
11
+ vendored `lib/platform/<tag>/` copy, or the system paths. For
12
+ development against a local build:
13
+
14
+ ....
15
+ # the sibling C checkout: ~/src/leptris/yeptris
16
+ cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DYEPTRIS_BUILD_SHARED=ON
17
+ cmake --build build
18
+
19
+ cd ~/src/leptris/yeptris-ruby
20
+ YEPTRIS_LIB_PATH=../yeptris/build/src/libyeptris.dylib bundle exec rspec
21
+ ....
22
+
23
+ Without `YEPTRIS_LIB_PATH` the spec helper falls back to a vendored
24
+ `lib/platform/<tag>/` copy, then to the sibling checkout's
25
+ `build-validate` — any `libyeptris.{so,dylib,dll}` path works.
26
+
27
+ == Usage
28
+
29
+ [source,ruby]
30
+ ----
31
+ require "yeptris"
32
+
33
+ Yeptris::YAML.load("name: yeptris\nrating: 10\n")
34
+ # => {"name" => "yeptris", "rating" => 10}
35
+
36
+ # brace mixed hashes on Ruby 3.x: a trailing symbol key otherwise
37
+ # splits the literal into keywords
38
+ Yeptris::YAML.dump({"name" => "yeptris", "tags" => [:yaml, :fast]})
39
+
40
+ doc = Yeptris::Document.parse(config_yaml)
41
+ doc.root["server"]["port"].to_i
42
+ doc.serialize
43
+ ----
44
+
45
+ `Yeptris::YAML.load` defaults to Psych's YAML 1.1 implicit typing
46
+ (`yes` is `true`, `017` is octal); pass `schema: :core_12` for YAML
47
+ 1.2 core semantics. Dump builds through the library's DOM mutation
48
+ API, so the writer's sizing/escape machinery applies to synthesized
49
+ trees unchanged.
50
+
51
+ Handles are document-scoped: `Document#free` releases everything
52
+ (one C call), a GC finalizer backs it up, and any use after free
53
+ raises `Yeptris::FreedError` — never a segfault.
54
+
55
+ == Two load surfaces: YAML and strict JSON
56
+
57
+ `Yeptris::YAML.load` keeps the Psych contract for EVERY input —
58
+ including JSON-shaped ones. `{"a": [1,]}` is legal flow YAML (spec
59
+ production [141] allows the trailing comma); `"1e3"` is a Psych
60
+ String. These semantics never flip because input happens to look
61
+ like JSON.
62
+
63
+ `Yeptris::JSON.load` is the STRICT RFC 8259 surface — exact
64
+ `JSON.parse` semantics by construction (spec-pinned in
65
+ `spec/json_parity_spec.rb`: every value and every error case,
66
+ Bignum integers, exponent-only floats, duplicate keys). Engines,
67
+ fastest first:
68
+
69
+ 1. **Native materializer** (opt-in build; see below): a fused C scan
70
+ → `VALUE` parser. Order-alternating interleaved profile, 152 KB /
71
+ ~29k-value corpus, Ruby 3.4:
72
+ +
73
+ ....
74
+ CI referee (gated at 1.05 on both platforms, order-alternating
75
+ interleave, N=200; prefix-hashed token cache):
76
+ ubuntu (x86_64) mean 0.745x vs JSON.parse, 200/200 head-to-head
77
+ macos (arm64) mean 0.759x vs JSON.parse, 169/200 head-to-head
78
+ ....
79
+
80
+ The parse-window GC strategy is per-arch and runtime-switchable:
81
+ `Yeptris::Native.gc_mode` / `YEPTRIS_NATIVE_GC` = `:none`
82
+ (steady-state: +0 heap pages, the stdlib's own GC cadence — the
83
+ default on arm64) or `:disable` (no GC during the parse — the
84
+ default on x86_64, faster where pages are free). On a LOADED x86_64
85
+ box prefer `:none`: `:disable` grows ~478 heap pages per 50 parses,
86
+ and page growth under contention is exactly the loaded-box
87
+ regression. `:start` (in-window collection) measured 8x — dead.
88
+ 2. **Record-drain fallback** (always available): the strict-JSON
89
+ validator gates, then the value records convert without the Psych
90
+ quirk table — exact parity with engine 1, spec-pinned.
91
+
92
+ The committed profile (`benchmark/json_profile.rb`) is the fair
93
+ benchmark of record — any performance claim runs through it.
94
+
95
+ === Building the native materializer (opt-in)
96
+
97
+ ....
98
+ cd ext/yeptris_native
99
+ YEPTRIS_LIB_PATH=/path/to/libyeptris.dylib ruby extconf.rb && make
100
+ cp native.bundle ../../lib/yeptris/ # or .so on Linux
101
+ ....
102
+
103
+ `require "yeptris"` picks it up automatically (`Yeptris::JSON` then
104
+ uses it; missing builds silently use the fallback — the gem installs
105
+ without compiling).
106
+
107
+ == Shipped beyond the original plan
108
+
109
+ * The columnar value drain (bulk, pre-converted typed columns — the
110
+ load fast path) and the bulk DOM builder (one `document_build`
111
+ call per dump).
112
+ * **Ruby Marshal 4.8 emission** (libyeptris >= 0.1.11): the C side
113
+ converts value records into Marshal bytes; one `Marshal.load`
114
+ materializes the whole graph. ~10× faster than the columnar walk
115
+ on JSON-shaped input, ~5× on YAML, ~50× on the per-node DOM walk
116
+ (`Node#to_ruby` becomes bulk). Alias identity preserved through
117
+ object links; merge keys and timestamps return UNSUPPORTED for the
118
+ record-walk fallback.
119
+ * `Yeptris::Psych` — the Psych-compatible namespace with the ported
120
+ Psych suite (143 specs), `.tml` corpora conformance, and the
121
+ Encodable object protocol (`include Yeptris::Psych::Encodable` +
122
+ `encode_with`/`init_with` — no `respond_to?`, no ivar reflection;
123
+ the library never reaches into an object's internals).
124
+ ** CO-EXISTENCE (issue #69): `require "yeptris/psych"` loads the
125
+ namespace WITHOUT touching the top-level `Psych` constant — it
126
+ coexists with stdlib psych in ANY load order. The process-
127
+ exclusive drop-in rebind (`::Psych = Yeptris::Psych`) is now
128
+ OPT-IN: `require "yeptris/psych/drop_in"`. Once the drop-in runs,
129
+ stdlib psych must NOT be loaded afterwards (its require re-opens
130
+ the rebound module and clobbers constants). Bundles that cannot
131
+ control the load order (activesupport et al.) should use
132
+ `Yeptris::Psych` / `Yeptris::YAML` directly.
133
+ ** `Yeptris::YAML.safe_load(yaml, permitted_classes: [], aliases:
134
+ false)` — Psych's safe_load semantics on the native surface:
135
+ plain data by default; a leaf that would materialize to a
136
+ non-permitted class (a compat_11 date) raises
137
+ `Yeptris::Psych::DisallowedClass`; alias use without `aliases:
138
+ true` raises `Yeptris::Psych::AliasesError`.
139
+ * Lockstep versioning with the C library
140
+ (`{c-semver}.{gem-patch}`); releases published from the C repo's
141
+ workflow through the RubyGems trusted publisher.
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Builds the native materializer DLL for the CURRENT Ruby and
4
+ # installs it under its minor-versioned name
5
+ # (lib/yeptris/native-<major.minor>.so).
6
+ #
7
+ # The leptris-ruby Windows lesson (#207/#227): a PE DLL cannot
8
+ # resolve Ruby imports lazily — it must bind the build Ruby's
9
+ # x64-ucrt-rubyNNN.dll. One DLL per supported minor therefore ships
10
+ # in the Windows platform gems, and the loader in lib/yeptris.rb
11
+ # picks by RUBY_VERSION at require. Ruby 3.3 has no arm64 build:
12
+ # that cell ships no artifact and falls back loudly to the FFI
13
+ # ladder.
14
+ #
15
+ # Standalone by design: the release workflow runs this under each
16
+ # Ruby minor (3.3/3.4/4.0) with no bundler context.
17
+
18
+ require "rbconfig"
19
+ require "fileutils"
20
+
21
+ root = File.expand_path("..", __dir__)
22
+ ext_dir = File.join(root, "ext", "yeptris_native")
23
+ minor = RUBY_VERSION[/\A\d+\.\d+/]
24
+
25
+ Dir.chdir(ext_dir) do
26
+ system(RbConfig.ruby, "extconf.rb") or abort "extconf failed under #{RUBY_VERSION}"
27
+ # mkmf's link binds the CURRENT Ruby's runtime DLL — exactly
28
+ # what the versioned naming is for.
29
+ success = system("make")
30
+ abort "make failed under #{RUBY_VERSION}" unless success
31
+ so = Dir.glob("native.{so,dll}").first
32
+ abort "native bundle not produced under #{RUBY_VERSION}" unless so
33
+ dest = File.join(root, "lib", "yeptris", "native-#{minor}.so")
34
+ # The artifact must reference only this minor's Ruby DLL.
35
+ imported = `strings #{so} 2>/dev/null`[/[a-z0-9-]*ruby\d{3,}\.dll/i]
36
+ if imported && !imported.include?("ruby#{minor.delete('.')}")
37
+ abort "#{so} imports #{imported} but was built under #{RUBY_VERSION} — refuse to mis-name it"
38
+ end
39
+ FileUtils.cp(so, dest)
40
+ puts "Installed native materializer for Ruby #{minor} -> #{dest} (#{imported || 'no ruby dll string found'})"
41
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+
5
+ # libyeptris location: YEPTRIS_LIB_PATH (file or dir), then sibling
6
+ # checkouts. CI sets YEPTRIS_LIB_PATH (the built shared library) and
7
+ # YEPTRIS_SRC (the C checkout) explicitly.
8
+ lib_path = ENV["YEPTRIS_LIB_PATH"]
9
+ candidates = []
10
+ if lib_path
11
+ candidates << File.dirname(lib_path)
12
+ candidates << lib_path if File.directory?(lib_path)
13
+ end
14
+ candidates << File.expand_path("../../../../yeptris/build-validate/src", __dir__)
15
+ candidates << File.expand_path("../../../../yeptris/build/src", __dir__)
16
+ candidates << File.expand_path("../../../yeptris/build-validate/src", __dir__)
17
+ candidates << File.expand_path("../../../yeptris/build/src", __dir__)
18
+
19
+ # Source root: YEPTRIS_SRC (CI / explicit), then sibling checkouts.
20
+ src_roots = [ENV["YEPTRIS_SRC"]].compact
21
+ src_roots << File.expand_path("../../../../yeptris/src", __dir__)
22
+ src_roots << File.expand_path("../../../yeptris/src", __dir__)
23
+ src_root = src_roots.find { |d| d && File.directory?(File.join(d, "include")) }
24
+ abort "yeptris sources not found (set YEPTRIS_SRC)" unless src_root
25
+
26
+ $INCFLAGS << " -I#{src_root}/include -I#{src_root}/yeptris"
27
+ %w[build-validate/generated build/generated].each do |g|
28
+ d = File.join(File.dirname(src_root), g)
29
+ $INCFLAGS << " -I#{d}" if File.directory?(d)
30
+ end
31
+
32
+ libdir = candidates.find do |d|
33
+ d && File.directory?(d) && Dir[File.join(d, "libyeptris*.{dylib,so,dll}")].any?
34
+ end
35
+ abort "libyeptris not found (set YEPTRIS_LIB_PATH)" unless libdir
36
+
37
+ $LIBPATH << libdir
38
+ # Platform gems bake a RELATIVE rpath so the bundle finds the
39
+ # vendored libyeptris next to itself; absolute build-time paths
40
+ # would dangle on user machines.
41
+ rpath = ENV["YEPTRIS_RPATH"] || libdir
42
+ $LDFLAGS << " -Wl,-rpath,#{rpath}" if RUBY_PLATFORM =~ /linux|darwin/
43
+ have_library("yeptris", "yep_json_string") or abort "yep_json_string not exported — rebuild libyeptris"
44
+ have_library("yeptris", "yeptris_visit_json") or abort "yeptris_visit_json missing"
45
+
46
+ $CFLAGS << " -O3 -fvisibility=hidden" unless RUBY_PLATFORM =~ /mswin|mingw/
47
+ create_makefile("yeptris/native")
@@ -0,0 +1,354 @@
1
+
2
+ /* json_ruby.c — fused RFC 8259 → Ruby VALUE (TODO.restructure/22). */
3
+ #include <ruby.h>
4
+ /* rb_hash_new_capa is Ruby 3.2+; older Rubies grow dynamically (the
5
+ * FFI fallback stays correct on every minor either way). */
6
+ #if RUBY_API_VERSION_MAJOR > 3 || (RUBY_API_VERSION_MAJOR == 3 && RUBY_API_VERSION_MINOR >= 2)
7
+ #define HASH_NEW_CAPA(n) rb_hash_new_capa(n)
8
+ #else
9
+ #define HASH_NEW_CAPA(n) rb_hash_new()
10
+ #endif
11
+
12
+ #include <ruby/encoding.h>
13
+ #include <ruby/intern.h>
14
+ #include <stdlib.h>
15
+ #include <string.h>
16
+ #include "parse/scalars.h"
17
+ #include <yeptris/visit.h>
18
+ #include "scan/json.h"
19
+
20
+ #define YEP_JR_MAX 1000
21
+ #define YEP_KC 1024
22
+
23
+ typedef struct {
24
+ uint64_t h;
25
+ uint32_t len;
26
+ VALUE v;
27
+ } kcent;
28
+
29
+ typedef struct {
30
+ const char* p;
31
+ size_t len;
32
+ size_t i;
33
+ char* scratch;
34
+ size_t scratch_cap;
35
+ int depth;
36
+ int err;
37
+ rb_encoding* enc;
38
+ int strict_dup; /* json gem >= 3: duplicate keys raise (issue #37) */
39
+ VALUE dup_key;
40
+ kcent kc[YEP_KC];
41
+ } jr;
42
+
43
+ static VALUE jr_value(jr* j);
44
+ static VALUE jr_object_body(jr* j, VALUE h_pre);
45
+
46
+ static void jr_ws(jr* j) {
47
+ while (j->i < j->len) {
48
+ unsigned char c = (unsigned char)j->p[j->i];
49
+ if (c == ' ' || c == '\t' || c == '\n' || c == '\r') j->i++;
50
+ else break;
51
+ }
52
+ }
53
+
54
+ /* Token-cache knob (TODO.restructure/37): off = fresh strings
55
+ * everywhere (JSON.parse's shape: every key pays its own aset
56
+ * hashing); on = interned keys/tokens (shared frozen VALUEs
57
+ * memoize their hash). The CI referee A/Bs the net sign per arch. */
58
+ enum { YEP_CACHE_ON = 0, YEP_CACHE_OFF = 1 };
59
+ static int yep_cache_mode = YEP_CACHE_ON;
60
+
61
+ /* Allocation-shape knob (TODO.restructure/39): pre = capa(8) up
62
+ * front (forces heap buffers even for tiny arrays); natural =
63
+ * rb_ary_new() so 3-element arrays stay EMBEDDED in the RVALUE, and
64
+ * bulk-path hashes get exact capacity from the counted pairs. */
65
+ enum { YEP_SHAPE_PRE = 0, YEP_SHAPE_NATURAL = 1 };
66
+ static int yep_shape_mode = YEP_SHAPE_PRE;
67
+
68
+ static uint64_t jr_hash(const char* sp, long sl) {
69
+ /* 8-byte-prefix key (the leptris nametab trick): one safe load
70
+ * of min(8, len) bytes + a multiply mix. Replaced byte-wise FNV
71
+ * (~15-20ns per token on the cached path, ~15k tokens per
72
+ * reference-corpus parse - the x86 materialization cost the
73
+ * decomposition isolated, TODO.restructure/37/38). */
74
+ uint64_t k = 0;
75
+ uint64_t take = (uint64_t)sl < 8 ? (uint64_t)sl : 8;
76
+ memcpy(&k, sp, (size_t)take);
77
+ k ^= (uint64_t)sl * 0x9E3779B97F4A7C15ull;
78
+ k *= 0xC2B2AE3D27D4EB4Full;
79
+ k ^= k >> 29;
80
+ return k;
81
+ }
82
+
83
+ static VALUE jr_cached(jr* j, const char* sp, long sl) {
84
+ uint64_t h = jr_hash(sp, sl);
85
+ uint32_t slot = (uint32_t)(h & (YEP_KC - 1));
86
+ kcent* e = &j->kc[slot];
87
+ if (e->v != 0 && e->h == h && e->len == (uint32_t)sl &&
88
+ (long)RSTRING_LEN(e->v) == sl &&
89
+ memcmp(RSTRING_PTR(e->v), sp, (size_t)sl) == 0) {
90
+ return e->v;
91
+ }
92
+ VALUE s = rb_enc_str_new(sp, sl, j->enc);
93
+ rb_str_freeze(s);
94
+ e->h = h;
95
+ e->len = (uint32_t)sl;
96
+ e->v = s;
97
+ return s;
98
+ }
99
+
100
+ static VALUE jr_str(jr* j, int as_key) {
101
+ size_t start = j->i, close = 0;
102
+ int has_esc = 0;
103
+ if (!yep_json_string(j->p, j->len, &j->i, &close, &has_esc)) { j->err = -2; return Qnil; }
104
+ const char* sp; long sl;
105
+ if (has_esc) {
106
+ uint32_t span = (uint32_t)(close - start - 1);
107
+ if (span + 1 > j->scratch_cap) {
108
+ size_t cap = j->scratch_cap ? j->scratch_cap : 64;
109
+ while (cap < span + 1) cap *= 2;
110
+ char* ns = realloc(j->scratch, cap);
111
+ if (!ns) { j->err = -1; return Qnil; }
112
+ j->scratch = ns; j->scratch_cap = cap;
113
+ }
114
+ sl = (long)yep_finish_double_into(j->p, (uint32_t)(start + 1), (uint32_t)close, j->scratch, span);
115
+ sp = j->scratch;
116
+ } else {
117
+ sp = j->p + start + 1;
118
+ sl = (long)(close - start - 1);
119
+ }
120
+ if (yep_cache_mode == YEP_CACHE_ON && (as_key || sl <= 24)) return jr_cached(j, sp, sl);
121
+ return rb_enc_str_new(sp, sl, j->enc);
122
+ }
123
+
124
+ static VALUE jr_num(jr* j) {
125
+ size_t start = j->i;
126
+ int shape = 0;
127
+ int64_t iv = 0;
128
+ double dv = 0.0;
129
+ /* the fused kernel (scan/json.h): ONE grammar walk, values out */
130
+ if (!yep_json_number_scan(j->p, j->len, &j->i, &shape, &iv, &dv)) {
131
+ j->err = -2;
132
+ return Qnil;
133
+ }
134
+ if (shape == 0) {
135
+ return LL2NUM(iv);
136
+ }
137
+ if (shape == 1) {
138
+ return DBL2NUM(dv);
139
+ }
140
+ /* integer text beyond int64: exact Bignum from the validated
141
+ * span (JSON.parse's behavior). Absurd lengths degrade to the
142
+ * approximate double. */
143
+ size_t n = j->i - start;
144
+ if (n < 512) {
145
+ char buf[512];
146
+ memcpy(buf, j->p + start, n);
147
+ buf[n] = '\0';
148
+ return rb_cstr_to_inum(buf, 10, TRUE);
149
+ }
150
+ return DBL2NUM(dv);
151
+ }
152
+
153
+ /* Insert strategy (TODO.restructure/34): bulk lands all pairs in one
154
+ * rb_hash_bulk_insert (skips per-pair dispatch) but costs +1.4k
155
+ * intermediate allocations on the reference corpus — the CI referee
156
+ * rules per platform. aset = the per-pair rb_hash_aset loop. */
157
+ enum { YEP_INS_BULK = 0, YEP_INS_ASET = 1 };
158
+ static int yep_ins_mode = YEP_INS_BULK;
159
+
160
+ static VALUE jr_object(jr* j) {
161
+ j->i++; j->depth++;
162
+ if (yep_shape_mode == YEP_SHAPE_PRE) {
163
+ VALUE h = HASH_NEW_CAPA(8);
164
+ jr_ws(j);
165
+ if (j->i < j->len && j->p[j->i] == '}') { j->i++; j->depth--; return h; }
166
+ return jr_object_body(j, h);
167
+ }
168
+ jr_ws(j);
169
+ if (j->i < j->len && j->p[j->i] == '}') { j->i++; j->depth--; return rb_hash_new(); }
170
+ return jr_object_body(j, Qundef); /* created post-loop with exact capa */
171
+
172
+ /* NOTREACHED */
173
+ }
174
+
175
+ static VALUE jr_object_body(jr* j, VALUE h_pre) {
176
+ VALUE pairs[64];
177
+ VALUE* pv = pairs;
178
+ size_t pcap = 64, pn = 0, heap_cap = 0;
179
+ for (;;) {
180
+ jr_ws(j);
181
+ if (j->i >= j->len || j->p[j->i] != '"') { j->err = -2; goto out; }
182
+ VALUE key = jr_str(j, 1);
183
+ if (j->err) goto out;
184
+ jr_ws(j);
185
+ if (j->i >= j->len || j->p[j->i] != ':') { j->err = -2; goto out; }
186
+ j->i++;
187
+ VALUE val = jr_value(j);
188
+ if (j->err) goto out;
189
+ if (yep_ins_mode == YEP_INS_ASET || j->strict_dup) {
190
+ VALUE h = h_pre == Qundef ? (h_pre = HASH_NEW_CAPA(8)) : h_pre;
191
+ if (j->strict_dup && !NIL_P(rb_hash_aref(h, key))) {
192
+ j->err = -3;
193
+ j->dup_key = key;
194
+ goto out;
195
+ }
196
+ rb_hash_aset(h, key, val);
197
+ } else {
198
+ if (pn + 2 > pcap) {
199
+ size_t ncap = pcap * 2;
200
+ VALUE* nv = malloc(ncap * sizeof(VALUE));
201
+ if (!nv) { j->err = -1; goto out; }
202
+ memcpy(nv, pv, pn * sizeof(VALUE));
203
+ if (pv != pairs) { free(pv); heap_cap = 1; }
204
+ pv = nv; pcap = ncap;
205
+ }
206
+ pv[pn++] = key;
207
+ pv[pn++] = val;
208
+ }
209
+ jr_ws(j);
210
+ if (j->i >= j->len) { j->err = -2; goto out; }
211
+ if (j->p[j->i] == ',') { j->i++; continue; }
212
+ if (j->p[j->i] == '}') { j->i++; break; }
213
+ j->err = -2; goto out;
214
+ }
215
+ if (yep_ins_mode == YEP_INS_BULK) {
216
+ VALUE h = (h_pre == Qundef) ? HASH_NEW_CAPA((long)(pn / 2)) : h_pre;
217
+ rb_hash_bulk_insert((long)pn, (const VALUE*)pv, h);
218
+ if (pv != pairs) { free(pv); }
219
+ j->depth--;
220
+ return h;
221
+ }
222
+ if (h_pre == Qundef) { h_pre = HASH_NEW_CAPA(8); }
223
+ out:
224
+ if (pv != pairs) { free(pv); (void)heap_cap; }
225
+ if (j->err) return Qnil;
226
+ j->depth--;
227
+ return h_pre;
228
+ }
229
+
230
+ static VALUE jr_array(jr* j) {
231
+ j->i++; j->depth++;
232
+ VALUE a = (yep_shape_mode == YEP_SHAPE_NATURAL) ? rb_ary_new() : rb_ary_new_capa(8);
233
+ jr_ws(j);
234
+ if (j->i < j->len && j->p[j->i] == ']') { j->i++; j->depth--; return a; }
235
+ for (;;) {
236
+ VALUE v = jr_value(j);
237
+ if (j->err) return Qnil;
238
+ rb_ary_push(a, v);
239
+ jr_ws(j);
240
+ if (j->i >= j->len) { j->err = -2; return Qnil; }
241
+ if (j->p[j->i] == ',') { j->i++; continue; }
242
+ if (j->p[j->i] == ']') { j->i++; j->depth--; return a; }
243
+ j->err = -2; return Qnil;
244
+ }
245
+ }
246
+
247
+ static VALUE jr_value(jr* j) {
248
+ if (j->depth >= YEP_JR_MAX) { j->err = -2; return Qnil; }
249
+ jr_ws(j);
250
+ if (j->i >= j->len) { j->err = -2; return Qnil; }
251
+ char c = j->p[j->i];
252
+ if (c == '{') return jr_object(j);
253
+ if (c == '[') return jr_array(j);
254
+ if (c == '"') return jr_str(j, 0);
255
+ if (c == 't') {
256
+ if (!yep_json_literal(j->p, j->len, &j->i, "true")) { j->err = -2; return Qnil; }
257
+ return Qtrue;
258
+ }
259
+ if (c == 'f') {
260
+ if (!yep_json_literal(j->p, j->len, &j->i, "false")) { j->err = -2; return Qnil; }
261
+ return Qfalse;
262
+ }
263
+ if (c == 'n') {
264
+ if (!yep_json_literal(j->p, j->len, &j->i, "null")) { j->err = -2; return Qnil; }
265
+ return Qnil;
266
+ }
267
+ if (c == '-' || (c >= '0' && c <= '9')) return jr_num(j);
268
+ j->err = -2; return Qnil;
269
+ }
270
+
271
+ /* GC strategy for the parse window (TODO.restructure/34): JSON.parse
272
+ * pays minor GCs mid-parse and recycles slots continuously; a blanket
273
+ * disable defers every collection — fresh pages each iteration, which
274
+ * is cheap idle and expensive exactly under memory contention (the
275
+ * loaded-box regression). The strategy is a runtime choice so the CI
276
+ * referee can A/B without rebuilds:
277
+ * disable (default) — pause GC for the window
278
+ * none — never pause
279
+ * start — pause, then one gc_start before returning
280
+ * (pay the minor GC in-window, like JSON.parse)
281
+ */
282
+ enum { YEP_GC_DISABLE = 0, YEP_GC_NONE = 1, YEP_GC_START = 2 };
283
+ /* Default per arch (TODO.restructure/35, two CI rounds of evidence):
284
+ * - aarch64/darwin: NONE — +0 heap pages, the stdlib's own GC cadence
285
+ * (0.745x mean, h2h 91% on mac runners; disable was 0.899x/48%).
286
+ * - x86_64: DISABLE — fresh CI VMs have free pages and cheaper
287
+ * page faults than minor GCs (0.911-0.922x vs none's 1.06-1.20x).
288
+ * The mechanism cuts both ways under load: disable's +478 pages/50
289
+ * iters is exactly what a LOADED x86 box punishes — set
290
+ * YEPTRIS_NATIVE_GC=none there (documented in README). */
291
+ #if defined(__aarch64__) || defined(__arm__) || defined(__ARMEL__) || defined(_M_ARM64)
292
+ #define YEP_GC_DEFAULT YEP_GC_NONE
293
+ #else
294
+ #define YEP_GC_DEFAULT YEP_GC_DISABLE
295
+ #endif
296
+ static int yep_gc_mode = YEP_GC_DEFAULT;
297
+
298
+ int yep_rb_gc_mode(void) { return yep_gc_mode; }
299
+ int yep_rb_ins_mode(void) { return yep_ins_mode; }
300
+ int yep_rb_cache_mode(void) { return yep_cache_mode; }
301
+ int yep_rb_shape_mode(void) { return yep_shape_mode; }
302
+ void yep_rb_set_shape_mode(int mode) {
303
+ if (mode == YEP_SHAPE_PRE || mode == YEP_SHAPE_NATURAL) yep_shape_mode = mode;
304
+ }
305
+ void yep_rb_set_cache_mode(int mode) {
306
+ if (mode == YEP_CACHE_ON || mode == YEP_CACHE_OFF) yep_cache_mode = mode;
307
+ }
308
+ void yep_rb_set_ins_mode(int mode) {
309
+ if (mode == YEP_INS_BULK || mode == YEP_INS_ASET) yep_ins_mode = mode;
310
+ }
311
+ void yep_rb_set_gc_mode(int mode) {
312
+ if (mode >= YEP_GC_DISABLE && mode <= YEP_GC_START) {
313
+ yep_gc_mode = mode;
314
+ }
315
+ }
316
+
317
+ /* Pure grammar walk, no materialization (TODO.restructure/37): the
318
+ * same scan kernels through the null vtable. Decomposes scan vs
319
+ * materialize cost. Returns seconds for n iterations. */
320
+ #include <time.h>
321
+
322
+
323
+ double yep_rb_scan_time(const char* p, size_t len, int n) {
324
+ static const YeptrisVisitVTable none = {0};
325
+ struct timespec t0, t1;
326
+ clock_gettime(CLOCK_MONOTONIC, &t0);
327
+ for (int i = 0; i < n; i++) {
328
+ (void)yeptris_visit_json(p, len, &none, NULL);
329
+ }
330
+ clock_gettime(CLOCK_MONOTONIC, &t1);
331
+ return (double)(t1.tv_sec - t0.tv_sec) + (double)(t1.tv_nsec - t0.tv_nsec) / 1e9;
332
+ }
333
+
334
+ VALUE yep_rb_parse_json(const char* p, size_t len, int strict_dup) {
335
+ jr j;
336
+ memset(&j, 0, sizeof(j));
337
+ j.p = p; j.len = len; j.enc = rb_utf8_encoding();
338
+ j.strict_dup = strict_dup;
339
+ VALUE already = Qtrue;
340
+ if (yep_gc_mode != YEP_GC_NONE) already = rb_gc_disable();
341
+ VALUE v = jr_value(&j);
342
+ jr_ws(&j);
343
+ if (j.i != len && j.err == 0) j.err = -2;
344
+ free(j.scratch);
345
+ if (already == Qfalse) rb_gc_enable();
346
+ if (yep_gc_mode == YEP_GC_START && j.err == 0) rb_gc_start();
347
+ if (j.err == -1) rb_raise(rb_eNoMemError, "yeptris native json");
348
+ if (j.err == -3) {
349
+ rb_raise(rb_path2class("Yeptris::ParseError"), "duplicate key \"%s\" in JSON object",
350
+ RSTRING_PTR(j.dup_key));
351
+ }
352
+ if (j.err != 0) rb_raise(rb_path2class("Yeptris::ParseError"), "native json parse failed");
353
+ return v;
354
+ }