yeptris 0.1.13.3-x86_64-linux
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 +7 -0
- data/README.adoc +126 -0
- data/ext/yeptris_native/extconf.rb +47 -0
- data/ext/yeptris_native/json_ruby.c +330 -0
- data/ext/yeptris_native/yeptris_native.c +438 -0
- data/lib/yeptris/document.rb +209 -0
- data/lib/yeptris/ffi.rb +256 -0
- data/lib/yeptris/json.rb +145 -0
- data/lib/yeptris/materializer.rb +347 -0
- data/lib/yeptris/native.so +0 -0
- data/lib/yeptris/node.rb +354 -0
- data/lib/yeptris/psych/coder_shim.rb +55 -0
- data/lib/yeptris/psych/encodable.rb +14 -0
- data/lib/yeptris/psych/handler.rb +90 -0
- data/lib/yeptris/psych/parser.rb +105 -0
- data/lib/yeptris/psych/visitors.rb +383 -0
- data/lib/yeptris/psych.rb +342 -0
- data/lib/yeptris/valueml.rb +442 -0
- data/lib/yeptris/yaml.rb +278 -0
- data/lib/yeptris.rb +70 -0
- data/libyeptris.so +0 -0
- metadata +82 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c4bf9731a3b8659e5c4e5614cfffe2a4c8a6c38514eeb8e5a27c0896ec5855ad
|
|
4
|
+
data.tar.gz: 0f394e55ba45f53880cdab5616b8687d2e36a95a83ce5944bec652fe9b3c4f68
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 822a6ec603f247880ca7e32a8e21a4986869d676cfcddc862785c116c3b8815a14535854a5c2adfbb90e36cc0ef0dcab7c2fe1117b01f0ab1e18c50a82244613
|
|
7
|
+
data.tar.gz: 573a708f3cf6f69429b4ba14b111fb973904a5642dadde1539aa723ff3ed00d21449125e74087f38fcf5c32ff1cea00800d838211bcaf72c997c199ec670c831
|
data/README.adoc
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
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 drop-in namespace with the ported Psych
|
|
120
|
+
suite (143 specs), `.tml` corpora conformance, and the Encodable
|
|
121
|
+
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
|
+
* Lockstep versioning with the C library
|
|
125
|
+
(`{c-semver}.{gem-patch}`); releases published from the C repo's
|
|
126
|
+
workflow through the RubyGems trusted publisher.
|
|
@@ -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,330 @@
|
|
|
1
|
+
/* json_ruby.c — fused RFC 8259 → Ruby VALUE (TODO.restructure/22). */
|
|
2
|
+
#include <ruby.h>
|
|
3
|
+
#include <ruby/encoding.h>
|
|
4
|
+
#include <ruby/intern.h>
|
|
5
|
+
#include <stdlib.h>
|
|
6
|
+
#include <string.h>
|
|
7
|
+
#include "parse/scalars.h"
|
|
8
|
+
#include <yeptris/visit.h>
|
|
9
|
+
#include "scan/json.h"
|
|
10
|
+
|
|
11
|
+
#define YEP_JR_MAX 1000
|
|
12
|
+
#define YEP_KC 1024
|
|
13
|
+
|
|
14
|
+
typedef struct {
|
|
15
|
+
uint64_t h;
|
|
16
|
+
uint32_t len;
|
|
17
|
+
VALUE v;
|
|
18
|
+
} kcent;
|
|
19
|
+
|
|
20
|
+
typedef struct {
|
|
21
|
+
const char* p;
|
|
22
|
+
size_t len;
|
|
23
|
+
size_t i;
|
|
24
|
+
char* scratch;
|
|
25
|
+
size_t scratch_cap;
|
|
26
|
+
int depth;
|
|
27
|
+
int err;
|
|
28
|
+
rb_encoding* enc;
|
|
29
|
+
kcent kc[YEP_KC];
|
|
30
|
+
} jr;
|
|
31
|
+
|
|
32
|
+
static VALUE jr_value(jr* j);
|
|
33
|
+
static VALUE jr_object_body(jr* j, VALUE h_pre);
|
|
34
|
+
|
|
35
|
+
static void jr_ws(jr* j) {
|
|
36
|
+
while (j->i < j->len) {
|
|
37
|
+
unsigned char c = (unsigned char)j->p[j->i];
|
|
38
|
+
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') j->i++;
|
|
39
|
+
else break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/* Token-cache knob (TODO.restructure/37): off = fresh strings
|
|
44
|
+
* everywhere (JSON.parse's shape: every key pays its own aset
|
|
45
|
+
* hashing); on = interned keys/tokens (shared frozen VALUEs
|
|
46
|
+
* memoize their hash). The CI referee A/Bs the net sign per arch. */
|
|
47
|
+
enum { YEP_CACHE_ON = 0, YEP_CACHE_OFF = 1 };
|
|
48
|
+
static int yep_cache_mode = YEP_CACHE_ON;
|
|
49
|
+
|
|
50
|
+
/* Allocation-shape knob (TODO.restructure/39): pre = capa(8) up
|
|
51
|
+
* front (forces heap buffers even for tiny arrays); natural =
|
|
52
|
+
* rb_ary_new() so 3-element arrays stay EMBEDDED in the RVALUE, and
|
|
53
|
+
* bulk-path hashes get exact capacity from the counted pairs. */
|
|
54
|
+
enum { YEP_SHAPE_PRE = 0, YEP_SHAPE_NATURAL = 1 };
|
|
55
|
+
static int yep_shape_mode = YEP_SHAPE_PRE;
|
|
56
|
+
|
|
57
|
+
static uint64_t jr_hash(const char* sp, long sl) {
|
|
58
|
+
/* 8-byte-prefix key (the leptris nametab trick): one safe load
|
|
59
|
+
* of min(8, len) bytes + a multiply mix. Replaced byte-wise FNV
|
|
60
|
+
* (~15-20ns per token on the cached path, ~15k tokens per
|
|
61
|
+
* reference-corpus parse - the x86 materialization cost the
|
|
62
|
+
* decomposition isolated, TODO.restructure/37/38). */
|
|
63
|
+
uint64_t k = 0;
|
|
64
|
+
uint64_t take = (uint64_t)sl < 8 ? (uint64_t)sl : 8;
|
|
65
|
+
memcpy(&k, sp, (size_t)take);
|
|
66
|
+
k ^= (uint64_t)sl * 0x9E3779B97F4A7C15ull;
|
|
67
|
+
k *= 0xC2B2AE3D27D4EB4Full;
|
|
68
|
+
k ^= k >> 29;
|
|
69
|
+
return k;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static VALUE jr_cached(jr* j, const char* sp, long sl) {
|
|
73
|
+
uint64_t h = jr_hash(sp, sl);
|
|
74
|
+
uint32_t slot = (uint32_t)(h & (YEP_KC - 1));
|
|
75
|
+
kcent* e = &j->kc[slot];
|
|
76
|
+
if (e->v != 0 && e->h == h && e->len == (uint32_t)sl &&
|
|
77
|
+
(long)RSTRING_LEN(e->v) == sl &&
|
|
78
|
+
memcmp(RSTRING_PTR(e->v), sp, (size_t)sl) == 0) {
|
|
79
|
+
return e->v;
|
|
80
|
+
}
|
|
81
|
+
VALUE s = rb_enc_str_new(sp, sl, j->enc);
|
|
82
|
+
rb_str_freeze(s);
|
|
83
|
+
e->h = h;
|
|
84
|
+
e->len = (uint32_t)sl;
|
|
85
|
+
e->v = s;
|
|
86
|
+
return s;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
static VALUE jr_str(jr* j, int as_key) {
|
|
90
|
+
size_t start = j->i, close = 0;
|
|
91
|
+
int has_esc = 0;
|
|
92
|
+
if (!yep_json_string(j->p, j->len, &j->i, &close, &has_esc)) { j->err = -2; return Qnil; }
|
|
93
|
+
const char* sp; long sl;
|
|
94
|
+
if (has_esc) {
|
|
95
|
+
uint32_t span = (uint32_t)(close - start - 1);
|
|
96
|
+
if (span + 1 > j->scratch_cap) {
|
|
97
|
+
size_t cap = j->scratch_cap ? j->scratch_cap : 64;
|
|
98
|
+
while (cap < span + 1) cap *= 2;
|
|
99
|
+
char* ns = realloc(j->scratch, cap);
|
|
100
|
+
if (!ns) { j->err = -1; return Qnil; }
|
|
101
|
+
j->scratch = ns; j->scratch_cap = cap;
|
|
102
|
+
}
|
|
103
|
+
sl = (long)yep_finish_double_into(j->p, (uint32_t)(start + 1), (uint32_t)close, j->scratch, span);
|
|
104
|
+
sp = j->scratch;
|
|
105
|
+
} else {
|
|
106
|
+
sp = j->p + start + 1;
|
|
107
|
+
sl = (long)(close - start - 1);
|
|
108
|
+
}
|
|
109
|
+
if (yep_cache_mode == YEP_CACHE_ON && (as_key || sl <= 24)) return jr_cached(j, sp, sl);
|
|
110
|
+
return rb_enc_str_new(sp, sl, j->enc);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static VALUE jr_num(jr* j) {
|
|
114
|
+
size_t start = j->i;
|
|
115
|
+
int shape = 0;
|
|
116
|
+
int64_t iv = 0;
|
|
117
|
+
double dv = 0.0;
|
|
118
|
+
/* the fused kernel (scan/json.h): ONE grammar walk, values out */
|
|
119
|
+
if (!yep_json_number_scan(j->p, j->len, &j->i, &shape, &iv, &dv)) {
|
|
120
|
+
j->err = -2;
|
|
121
|
+
return Qnil;
|
|
122
|
+
}
|
|
123
|
+
if (shape == 0) {
|
|
124
|
+
return LL2NUM(iv);
|
|
125
|
+
}
|
|
126
|
+
if (shape == 1) {
|
|
127
|
+
return DBL2NUM(dv);
|
|
128
|
+
}
|
|
129
|
+
/* integer text beyond int64: exact Bignum from the validated
|
|
130
|
+
* span (JSON.parse's behavior). Absurd lengths degrade to the
|
|
131
|
+
* approximate double. */
|
|
132
|
+
size_t n = j->i - start;
|
|
133
|
+
if (n < 512) {
|
|
134
|
+
char buf[512];
|
|
135
|
+
memcpy(buf, j->p + start, n);
|
|
136
|
+
buf[n] = '\0';
|
|
137
|
+
return rb_cstr_to_inum(buf, 10, TRUE);
|
|
138
|
+
}
|
|
139
|
+
return DBL2NUM(dv);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/* Insert strategy (TODO.restructure/34): bulk lands all pairs in one
|
|
143
|
+
* rb_hash_bulk_insert (skips per-pair dispatch) but costs +1.4k
|
|
144
|
+
* intermediate allocations on the reference corpus — the CI referee
|
|
145
|
+
* rules per platform. aset = the per-pair rb_hash_aset loop. */
|
|
146
|
+
enum { YEP_INS_BULK = 0, YEP_INS_ASET = 1 };
|
|
147
|
+
static int yep_ins_mode = YEP_INS_BULK;
|
|
148
|
+
|
|
149
|
+
static VALUE jr_object(jr* j) {
|
|
150
|
+
j->i++; j->depth++;
|
|
151
|
+
if (yep_shape_mode == YEP_SHAPE_PRE) {
|
|
152
|
+
VALUE h = rb_hash_new_capa(8);
|
|
153
|
+
jr_ws(j);
|
|
154
|
+
if (j->i < j->len && j->p[j->i] == '}') { j->i++; j->depth--; return h; }
|
|
155
|
+
return jr_object_body(j, h);
|
|
156
|
+
}
|
|
157
|
+
jr_ws(j);
|
|
158
|
+
if (j->i < j->len && j->p[j->i] == '}') { j->i++; j->depth--; return rb_hash_new(); }
|
|
159
|
+
return jr_object_body(j, Qundef); /* created post-loop with exact capa */
|
|
160
|
+
|
|
161
|
+
/* NOTREACHED */
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
static VALUE jr_object_body(jr* j, VALUE h_pre) {
|
|
165
|
+
VALUE pairs[64];
|
|
166
|
+
VALUE* pv = pairs;
|
|
167
|
+
size_t pcap = 64, pn = 0, heap_cap = 0;
|
|
168
|
+
for (;;) {
|
|
169
|
+
jr_ws(j);
|
|
170
|
+
if (j->i >= j->len || j->p[j->i] != '"') { j->err = -2; goto out; }
|
|
171
|
+
VALUE key = jr_str(j, 1);
|
|
172
|
+
if (j->err) goto out;
|
|
173
|
+
jr_ws(j);
|
|
174
|
+
if (j->i >= j->len || j->p[j->i] != ':') { j->err = -2; goto out; }
|
|
175
|
+
j->i++;
|
|
176
|
+
VALUE val = jr_value(j);
|
|
177
|
+
if (j->err) goto out;
|
|
178
|
+
if (yep_ins_mode == YEP_INS_ASET) {
|
|
179
|
+
rb_hash_aset(h_pre == Qundef ? (h_pre = rb_hash_new_capa(8)) : h_pre, key, val);
|
|
180
|
+
} else {
|
|
181
|
+
if (pn + 2 > pcap) {
|
|
182
|
+
size_t ncap = pcap * 2;
|
|
183
|
+
VALUE* nv = malloc(ncap * sizeof(VALUE));
|
|
184
|
+
if (!nv) { j->err = -1; goto out; }
|
|
185
|
+
memcpy(nv, pv, pn * sizeof(VALUE));
|
|
186
|
+
if (pv != pairs) { free(pv); heap_cap = 1; }
|
|
187
|
+
pv = nv; pcap = ncap;
|
|
188
|
+
}
|
|
189
|
+
pv[pn++] = key;
|
|
190
|
+
pv[pn++] = val;
|
|
191
|
+
}
|
|
192
|
+
jr_ws(j);
|
|
193
|
+
if (j->i >= j->len) { j->err = -2; goto out; }
|
|
194
|
+
if (j->p[j->i] == ',') { j->i++; continue; }
|
|
195
|
+
if (j->p[j->i] == '}') { j->i++; break; }
|
|
196
|
+
j->err = -2; goto out;
|
|
197
|
+
}
|
|
198
|
+
if (yep_ins_mode == YEP_INS_BULK) {
|
|
199
|
+
VALUE h = (h_pre == Qundef) ? rb_hash_new_capa((long)(pn / 2)) : h_pre;
|
|
200
|
+
rb_hash_bulk_insert((long)pn, (const VALUE*)pv, h);
|
|
201
|
+
if (pv != pairs) { free(pv); }
|
|
202
|
+
j->depth--;
|
|
203
|
+
return h;
|
|
204
|
+
}
|
|
205
|
+
if (h_pre == Qundef) { h_pre = rb_hash_new_capa(8); }
|
|
206
|
+
out:
|
|
207
|
+
if (pv != pairs) { free(pv); (void)heap_cap; }
|
|
208
|
+
if (j->err) return Qnil;
|
|
209
|
+
j->depth--;
|
|
210
|
+
return h_pre;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
static VALUE jr_array(jr* j) {
|
|
214
|
+
j->i++; j->depth++;
|
|
215
|
+
VALUE a = (yep_shape_mode == YEP_SHAPE_NATURAL) ? rb_ary_new() : rb_ary_new_capa(8);
|
|
216
|
+
jr_ws(j);
|
|
217
|
+
if (j->i < j->len && j->p[j->i] == ']') { j->i++; j->depth--; return a; }
|
|
218
|
+
for (;;) {
|
|
219
|
+
VALUE v = jr_value(j);
|
|
220
|
+
if (j->err) return Qnil;
|
|
221
|
+
rb_ary_push(a, v);
|
|
222
|
+
jr_ws(j);
|
|
223
|
+
if (j->i >= j->len) { j->err = -2; return Qnil; }
|
|
224
|
+
if (j->p[j->i] == ',') { j->i++; continue; }
|
|
225
|
+
if (j->p[j->i] == ']') { j->i++; j->depth--; return a; }
|
|
226
|
+
j->err = -2; return Qnil;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
static VALUE jr_value(jr* j) {
|
|
231
|
+
if (j->depth >= YEP_JR_MAX) { j->err = -2; return Qnil; }
|
|
232
|
+
jr_ws(j);
|
|
233
|
+
if (j->i >= j->len) { j->err = -2; return Qnil; }
|
|
234
|
+
char c = j->p[j->i];
|
|
235
|
+
if (c == '{') return jr_object(j);
|
|
236
|
+
if (c == '[') return jr_array(j);
|
|
237
|
+
if (c == '"') return jr_str(j, 0);
|
|
238
|
+
if (c == 't') {
|
|
239
|
+
if (!yep_json_literal(j->p, j->len, &j->i, "true")) { j->err = -2; return Qnil; }
|
|
240
|
+
return Qtrue;
|
|
241
|
+
}
|
|
242
|
+
if (c == 'f') {
|
|
243
|
+
if (!yep_json_literal(j->p, j->len, &j->i, "false")) { j->err = -2; return Qnil; }
|
|
244
|
+
return Qfalse;
|
|
245
|
+
}
|
|
246
|
+
if (c == 'n') {
|
|
247
|
+
if (!yep_json_literal(j->p, j->len, &j->i, "null")) { j->err = -2; return Qnil; }
|
|
248
|
+
return Qnil;
|
|
249
|
+
}
|
|
250
|
+
if (c == '-' || (c >= '0' && c <= '9')) return jr_num(j);
|
|
251
|
+
j->err = -2; return Qnil;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/* GC strategy for the parse window (TODO.restructure/34): JSON.parse
|
|
255
|
+
* pays minor GCs mid-parse and recycles slots continuously; a blanket
|
|
256
|
+
* disable defers every collection — fresh pages each iteration, which
|
|
257
|
+
* is cheap idle and expensive exactly under memory contention (the
|
|
258
|
+
* loaded-box regression). The strategy is a runtime choice so the CI
|
|
259
|
+
* referee can A/B without rebuilds:
|
|
260
|
+
* disable (default) — pause GC for the window
|
|
261
|
+
* none — never pause
|
|
262
|
+
* start — pause, then one gc_start before returning
|
|
263
|
+
* (pay the minor GC in-window, like JSON.parse)
|
|
264
|
+
*/
|
|
265
|
+
enum { YEP_GC_DISABLE = 0, YEP_GC_NONE = 1, YEP_GC_START = 2 };
|
|
266
|
+
/* Default per arch (TODO.restructure/35, two CI rounds of evidence):
|
|
267
|
+
* - aarch64/darwin: NONE — +0 heap pages, the stdlib's own GC cadence
|
|
268
|
+
* (0.745x mean, h2h 91% on mac runners; disable was 0.899x/48%).
|
|
269
|
+
* - x86_64: DISABLE — fresh CI VMs have free pages and cheaper
|
|
270
|
+
* page faults than minor GCs (0.911-0.922x vs none's 1.06-1.20x).
|
|
271
|
+
* The mechanism cuts both ways under load: disable's +478 pages/50
|
|
272
|
+
* iters is exactly what a LOADED x86 box punishes — set
|
|
273
|
+
* YEPTRIS_NATIVE_GC=none there (documented in README). */
|
|
274
|
+
#if defined(__aarch64__) || defined(__arm__) || defined(__ARMEL__) || defined(_M_ARM64)
|
|
275
|
+
#define YEP_GC_DEFAULT YEP_GC_NONE
|
|
276
|
+
#else
|
|
277
|
+
#define YEP_GC_DEFAULT YEP_GC_DISABLE
|
|
278
|
+
#endif
|
|
279
|
+
static int yep_gc_mode = YEP_GC_DEFAULT;
|
|
280
|
+
|
|
281
|
+
int yep_rb_gc_mode(void) { return yep_gc_mode; }
|
|
282
|
+
int yep_rb_ins_mode(void) { return yep_ins_mode; }
|
|
283
|
+
int yep_rb_cache_mode(void) { return yep_cache_mode; }
|
|
284
|
+
int yep_rb_shape_mode(void) { return yep_shape_mode; }
|
|
285
|
+
void yep_rb_set_shape_mode(int mode) {
|
|
286
|
+
if (mode == YEP_SHAPE_PRE || mode == YEP_SHAPE_NATURAL) yep_shape_mode = mode;
|
|
287
|
+
}
|
|
288
|
+
void yep_rb_set_cache_mode(int mode) {
|
|
289
|
+
if (mode == YEP_CACHE_ON || mode == YEP_CACHE_OFF) yep_cache_mode = mode;
|
|
290
|
+
}
|
|
291
|
+
void yep_rb_set_ins_mode(int mode) {
|
|
292
|
+
if (mode == YEP_INS_BULK || mode == YEP_INS_ASET) yep_ins_mode = mode;
|
|
293
|
+
}
|
|
294
|
+
void yep_rb_set_gc_mode(int mode) {
|
|
295
|
+
if (mode >= YEP_GC_DISABLE && mode <= YEP_GC_START) {
|
|
296
|
+
yep_gc_mode = mode;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/* Pure grammar walk, no materialization (TODO.restructure/37): the
|
|
301
|
+
* same scan kernels through the null vtable. Decomposes scan vs
|
|
302
|
+
* materialize cost. Returns seconds for n iterations. */
|
|
303
|
+
#include <time.h>
|
|
304
|
+
double yep_rb_scan_time(const char* p, size_t len, int n) {
|
|
305
|
+
static const YeptrisVisitVTable none = {0};
|
|
306
|
+
struct timespec t0, t1;
|
|
307
|
+
clock_gettime(CLOCK_MONOTONIC, &t0);
|
|
308
|
+
for (int i = 0; i < n; i++) {
|
|
309
|
+
(void)yeptris_visit_json(p, len, &none, NULL);
|
|
310
|
+
}
|
|
311
|
+
clock_gettime(CLOCK_MONOTONIC, &t1);
|
|
312
|
+
return (double)(t1.tv_sec - t0.tv_sec) + (double)(t1.tv_nsec - t0.tv_nsec) / 1e9;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
VALUE yep_rb_parse_json(const char* p, size_t len) {
|
|
316
|
+
jr j;
|
|
317
|
+
memset(&j, 0, sizeof(j));
|
|
318
|
+
j.p = p; j.len = len; j.enc = rb_utf8_encoding();
|
|
319
|
+
VALUE already = Qtrue;
|
|
320
|
+
if (yep_gc_mode != YEP_GC_NONE) already = rb_gc_disable();
|
|
321
|
+
VALUE v = jr_value(&j);
|
|
322
|
+
jr_ws(&j);
|
|
323
|
+
if (j.i != len && j.err == 0) j.err = -2;
|
|
324
|
+
free(j.scratch);
|
|
325
|
+
if (already == Qfalse) rb_gc_enable();
|
|
326
|
+
if (yep_gc_mode == YEP_GC_START && j.err == 0) rb_gc_start();
|
|
327
|
+
if (j.err == -1) rb_raise(rb_eNoMemError, "yeptris native json");
|
|
328
|
+
if (j.err != 0) rb_raise(rb_path2class("Yeptris::ParseError"), "native json parse failed");
|
|
329
|
+
return v;
|
|
330
|
+
}
|