schemurai 3.1.0 → 3.1.1

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: 663b805a46f6c7bf28678bcc50b2ffae37d3337b3025710a75c0782a5d7c8182
4
- data.tar.gz: 41a280754bfa72b41626e44176ef7cf0c983cc27a504aedf049759df005498b1
3
+ metadata.gz: 967138a71d8f45234a75282fb65c665f58ca6611ee9a859ab4b69631e8342daa
4
+ data.tar.gz: eb53632ae8b019e44fe588ecf30e258879574810c2172edba60e34a6f56a106c
5
5
  SHA512:
6
- metadata.gz: 88be3ae2e41ce4d46cbb5c79dc66e9bfe7d9041d63619b17dc27dc7977384679435df13f0e234701033eca5f829f9d4e7321b9b08ad8e9b6fdfdf0fa5d64c03c
7
- data.tar.gz: a0ea620a03a983a0ad22319c4a22727c316ced09711bd8565a716d8ebcf4c523bc827e957b347c0b19886d487f7529d305b425f63e1e0ffa4b3b7d8f1822e406
6
+ metadata.gz: 941e91894ff671660b76e3800da82b58509994e594df8ab7f15f494ff321a7b45c001a5d35fbfc4611d2e8c844ebd5684b234e965fbfbf861c8d330ef1ccb048
7
+ data.tar.gz: 5657384c954dd8c665627a00fa4de17e0f691f7199a605de10984ec3318563249f986cc790020cc5849b99fbbe309d408525f121b3d70aa514dfc0e03201e208
@@ -137,7 +137,7 @@ checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"
137
137
 
138
138
  [[package]]
139
139
  name = "schemurai-native"
140
- version = "3.1.0"
140
+ version = "3.1.1"
141
141
  dependencies = [
142
142
  "base64-simd",
143
143
  "base64-turbo",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "schemurai-native"
3
- version = "3.1.0"
3
+ version = "3.1.1"
4
4
  edition = "2024"
5
5
  rust-version = "1.93"
6
6
  publish = false
@@ -5,10 +5,12 @@ fn main() {
5
5
  let definitions = fs::read_to_string("protocol.def").expect("read native protocol");
6
6
  let mut output = String::new();
7
7
  for line in definitions.lines() {
8
- let (fields, opcode) = if let Some(fields) = line.strip_prefix("SN_OPCODE(") {
9
- (fields, true)
8
+ let (fields, prefix) = if let Some(fields) = line.strip_prefix("SN_OPCODE(") {
9
+ (fields, "OP_")
10
+ } else if let Some(fields) = line.strip_prefix("SN_FIELD(") {
11
+ (fields, "FIELD_")
10
12
  } else if let Some(fields) = line.strip_prefix("SN_CONSTANT(") {
11
- (fields, false)
13
+ (fields, "")
12
14
  } else {
13
15
  continue;
14
16
  };
@@ -18,16 +20,15 @@ fn main() {
18
20
  .split(',')
19
21
  .map(str::trim)
20
22
  .collect();
21
- assert_eq!(fields.len(), if opcode { 3 } else { 2 });
23
+ assert_eq!(fields.len(), if prefix.is_empty() { 2 } else { 3 });
22
24
  let name = fields[0];
23
25
  assert!(
24
26
  name.bytes()
25
27
  .all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == b'_')
26
28
  );
27
- let value: i32 = fields[if opcode { 2 } else { 1 }]
29
+ let value: i32 = fields[if prefix.is_empty() { 1 } else { 2 }]
28
30
  .parse()
29
31
  .expect("protocol integer value");
30
- let prefix = if opcode { "OP_" } else { "" };
31
32
  output.push_str(&format!("pub const {prefix}{name}: i32 = {value};\n"));
32
33
  }
33
34
  let directory = PathBuf::from(env::var_os("OUT_DIR").expect("Cargo output directory"));
@@ -344,13 +344,18 @@ static VALUE build_runner(VALUE self, size_t index) {
344
344
  r->caches = NULL;
345
345
  r->matches = NULL;
346
346
  r->frame_indices = NULL;
347
- r->frame_indices = st_init_numtable();
348
- r->matches = st_init_numtable();
349
- r->caches = sn_caches_new();
350
- sn_caches_runtime(r->caches, r->matches);
351
347
  RB_GC_GUARD(self);
352
348
  return object;
353
349
  }
350
+ /* Child factories and direct Boolean/type callbacks do not evaluate. Keep
351
+ * their observation state absent until an execution or invocation scope needs it. */
352
+ static void prepare_runner(runner_holder *r) {
353
+ if (r->caches) return;
354
+ if (!r->frame_indices) r->frame_indices = st_init_numtable();
355
+ if (!r->matches) r->matches = st_init_numtable();
356
+ r->caches = sn_caches_new();
357
+ sn_caches_runtime(r->caches, r->matches);
358
+ }
354
359
  typedef struct { uint32_t kind; size_t attempted; } native_outcome;
355
360
  extern int sn_valid_with_limits(const void *, const void *, size_t, VALUE, size_t, const native_limits *, native_outcome *);
356
361
  static native_limits read_limits(VALUE budget) {
@@ -671,14 +676,23 @@ int sn_same(VALUE a, VALUE b) { return a == b; }
671
676
  int sn_same_class(VALUE a, VALUE b) { return CLASS_OF(a) == CLASS_OF(b); }
672
677
  int sn_string_equal(VALUE a, VALUE b) { return rb_str_cmp(a, b) == 0; }
673
678
 
674
- struct access { VALUE object; const char *name; };
679
+ static struct { ID id; const char *symbol; } fields[] = {
680
+ #define SN_OPCODE(name, symbol, value)
681
+ #define SN_CONSTANT(name, value)
682
+ #define SN_FIELD(name, symbol, value) [SN_FIELD_##name] = {0, symbol},
683
+ #include "protocol.def"
684
+ #undef SN_OPCODE
685
+ #undef SN_CONSTANT
686
+ #undef SN_FIELD
687
+ };
688
+ struct access { VALUE object; ID name; };
675
689
  static VALUE get_field(VALUE arg) {
676
690
  struct access *a = (struct access *)arg;
677
- return rb_funcall(a->object, rb_intern_const(a->name), 0);
691
+ return rb_funcall(a->object, a->name, 0);
678
692
  }
679
- /* Rust supplies a fixed, NUL-terminated field name from a static byte string. */
680
- int sn_field(VALUE object, const char *name, VALUE *value) {
681
- struct access a = {object, name};
693
+ /* Rust supplies an accessor index from the shared protocol. */
694
+ int sn_field(VALUE object, int name, VALUE *value) {
695
+ struct access a = {object, fields[name].id};
682
696
  int state = 0;
683
697
  *value = rb_protect(get_field, (VALUE)&a, &state);
684
698
  if (state) { *value = rb_errinfo(); rb_set_errinfo(Qnil); }
@@ -687,9 +701,11 @@ int sn_field(VALUE object, const char *name, VALUE *value) {
687
701
  static struct { ID id; const char *symbol; int opcode; } opcodes[] = {
688
702
  #define SN_OPCODE(name, symbol, value) {0, #symbol, SN_OP_##name},
689
703
  #define SN_CONSTANT(name, value)
704
+ #define SN_FIELD(name, symbol, value)
690
705
  #include "protocol.def"
691
706
  #undef SN_OPCODE
692
707
  #undef SN_CONSTANT
708
+ #undef SN_FIELD
693
709
  };
694
710
  int sn_opcode(VALUE symbol) {
695
711
  ID id = SYM2ID(symbol);
@@ -898,6 +914,7 @@ static VALUE callback_scope(evaluation_call *call) {
898
914
  scope->active_budget = call->budget;
899
915
  scope->active_limits = call->limits;
900
916
  copy_states(scope, runner, 1, 0);
917
+ prepare_runner(scope);
901
918
  sn_caches_child(scope->caches, runner->caches, !NIL_P(call->budget), 1);
902
919
  runner->callback_scope = object;
903
920
  }
@@ -1814,7 +1831,8 @@ static VALUE evaluate_runner_body(VALUE context) {
1814
1831
  h->report = NULL;
1815
1832
  call.report = h;
1816
1833
  }
1817
- clear_runtime((VALUE)&call);
1834
+ /* New runners are empty; finish_evaluation clears every completed or
1835
+ * interrupted execution before it makes the runner available again. */
1818
1836
  r->active_budget = budget;
1819
1837
  r->report_sink_done = 0;
1820
1838
  if (!NIL_P(r->report_seed)) {
@@ -1949,6 +1967,7 @@ static VALUE finish_evaluation(VALUE context) {
1949
1967
  static VALUE evaluate_runner(VALUE self, VALUE instance, VALUE budget, int diagnostics) {
1950
1968
  runner_holder *runner;
1951
1969
  TypedData_Get_Struct(self, runner_holder, &runner_type, runner);
1970
+ prepare_runner(runner);
1952
1971
  if (runner->busy) {
1953
1972
  VALUE nested = build_runner(runner->owner, runner->root);
1954
1973
  runner_holder *child;
@@ -2168,15 +2187,19 @@ void Init_schemurai_native(void) {
2168
2187
  binary_index = rb_ascii8bit_encindex();
2169
2188
  for (size_t i = 0; i < sizeof(opcodes) / sizeof(opcodes[0]); i++)
2170
2189
  opcodes[i].id = rb_intern_const(opcodes[i].symbol);
2190
+ for (size_t i = 0; i < sizeof(fields) / sizeof(fields[0]); i++)
2191
+ fields[i].id = rb_intern_const(fields[i].symbol);
2171
2192
  rb_ext_ractor_safe(true);
2172
2193
  VALUE schemurai = rb_define_module("Schemurai");
2173
2194
  VALUE native = rb_define_module_under(schemurai, "Native");
2174
2195
  VALUE protocol = rb_define_module_under(native, "Protocol");
2175
2196
  #define SN_OPCODE(name, symbol, value) rb_define_const(protocol, "OP_" #name, INT2NUM(SN_OP_##name));
2176
2197
  #define SN_CONSTANT(name, value) rb_define_const(protocol, #name, INT2NUM(SN_##name));
2198
+ #define SN_FIELD(name, symbol, value)
2177
2199
  #include "protocol.def"
2178
2200
  #undef SN_OPCODE
2179
2201
  #undef SN_CONSTANT
2202
+ #undef SN_FIELD
2180
2203
  VALUE klass = rb_define_class_under(native, "Program", rb_cObject);
2181
2204
  rb_define_alloc_func(klass, allocate);
2182
2205
  rb_define_method(klass, "initialize", initialize, 4);
@@ -106,3 +106,55 @@ SN_CONSTANT(ENCODING_SEVEN_BIT, 3)
106
106
  SN_CONSTANT(CALLBACK_RUNTIME, 0)
107
107
  SN_CONSTANT(CALLBACK_BOOLEAN, 1)
108
108
  SN_CONSTANT(CALLBACK_TYPE, 2)
109
+
110
+ /* Fixed native constructor accessors; IDs are initialized before Ractor use. */
111
+ SN_FIELD(ADDITIONAL, "additional", 0)
112
+ SN_FIELD(CODE, "code", 1)
113
+ SN_FIELD(CONDITION, "condition", 2)
114
+ SN_FIELD(CONTAINS, "contains", 3)
115
+ SN_FIELD(COUNT_CONTAINS, "count_contains", 4)
116
+ SN_FIELD(DENOMINATOR, "denominator", 5)
117
+ SN_FIELD(DEPENDENCIES, "dependencies", 6)
118
+ SN_FIELD(DEPENDENT_REQUIRED, "dependent_required", 7)
119
+ SN_FIELD(DEPENDENT_SCHEMAS, "dependent_schemas", 8)
120
+ SN_FIELD(DYNAMIC_ANCHOR, "dynamic_anchor", 9)
121
+ SN_FIELD(ELSE_BRANCH, "else_branch", 10)
122
+ SN_FIELD(EXCLUSIVE_MAXIMUM, "exclusive_maximum", 11)
123
+ SN_FIELD(EXCLUSIVE_MINIMUM, "exclusive_minimum", 12)
124
+ SN_FIELD(FORMAT, "format", 13)
125
+ SN_FIELD(FORMAT_ASSERTION, "format_assertion", 14)
126
+ SN_FIELD(FRAGMENT, "fragment", 15)
127
+ SN_FIELD(ITEMS, "items", 16)
128
+ SN_FIELD(ITEMS_LIST, "items_list", 17)
129
+ SN_FIELD(MASK, "mask", 18)
130
+ SN_FIELD(MAX_CONTAINS, "max_contains", 19)
131
+ SN_FIELD(MAX_ITEMS, "max_items", 20)
132
+ SN_FIELD(MAX_LENGTH, "max_length", 21)
133
+ SN_FIELD(MAX_PROPERTIES, "max_properties", 22)
134
+ SN_FIELD(MAXIMUM, "maximum", 23)
135
+ SN_FIELD(MESSAGE, "message", 24)
136
+ SN_FIELD(MIN_CONTAINS, "min_contains", 25)
137
+ SN_FIELD(MIN_ITEMS, "min_items", 26)
138
+ SN_FIELD(MIN_LENGTH, "min_length", 27)
139
+ SN_FIELD(MIN_PROPERTIES, "min_properties", 28)
140
+ SN_FIELD(MINIMUM, "minimum", 29)
141
+ SN_FIELD(MULTIPLE_OF, "multiple_of", 30)
142
+ SN_FIELD(NAME, "name", 31)
143
+ SN_FIELD(NEGATIVE_P, "negative?", 32)
144
+ SN_FIELD(NODE, "node", 33)
145
+ SN_FIELD(NUMERATOR, "numerator", 34)
146
+ SN_FIELD(PARSE_JSON, "parse_json", 35)
147
+ SN_FIELD(PATTERN, "pattern", 36)
148
+ SN_FIELD(PATTERNS, "patterns", 37)
149
+ SN_FIELD(PREFIX_ITEMS, "prefix_items", 38)
150
+ SN_FIELD(PROPERTIES, "properties", 39)
151
+ SN_FIELD(PROPERTY_NAMES, "property_names", 40)
152
+ SN_FIELD(RECURSIVE_ANCHOR_P, "recursive_anchor?", 41)
153
+ SN_FIELD(REQUIRED, "required", 42)
154
+ SN_FIELD(RESOURCE, "resource", 43)
155
+ SN_FIELD(THEN_BRANCH, "then_branch", 44)
156
+ SN_FIELD(TO_F, "to_f", 45)
157
+ SN_FIELD(UNEVALUATED, "unevaluated", 46)
158
+ SN_FIELD(UNIQUE, "unique", 47)
159
+ SN_FIELD(VALUE, "value", 48)
160
+ SN_FIELD(DECODE_BASE64, "decode_base64", 49)
@@ -3,8 +3,10 @@
3
3
  enum {
4
4
  #define SN_OPCODE(name, symbol, value) SN_OP_##name = value,
5
5
  #define SN_CONSTANT(name, value) SN_##name = value,
6
+ #define SN_FIELD(name, symbol, value) SN_FIELD_##name = value,
6
7
  #include "protocol.def"
7
8
  #undef SN_OPCODE
8
9
  #undef SN_CONSTANT
10
+ #undef SN_FIELD
9
11
  };
10
12
  #endif
@@ -305,8 +305,8 @@ impl Engine {
305
305
  pub(super) fn container_header(
306
306
  &mut self,
307
307
  source: Value,
308
- maximum: &'static [u8],
309
- minimum: &'static [u8],
308
+ maximum: i32,
309
+ minimum: i32,
310
310
  object: bool,
311
311
  tail: u8,
312
312
  ) -> Result<Header, Value> {
@@ -314,7 +314,7 @@ impl Engine {
314
314
  let minimum = self.field(source, minimum)?;
315
315
  let native_bounds = self.native_count(maximum)? && self.native_count(minimum)?;
316
316
  let required = if object {
317
- self.field(source, b"required\0")?
317
+ self.field(source, FIELD_REQUIRED)?
318
318
  } else {
319
319
  unsafe { sn_nil() }
320
320
  };
@@ -324,8 +324,8 @@ impl Engine {
324
324
  let mut native_dependencies = true;
325
325
  if object {
326
326
  for (field, keyword) in [
327
- (b"dependencies\0".as_slice(), "dependencies"),
328
- (b"dependent_required\0", "dependentRequired"),
327
+ (FIELD_DEPENDENCIES, "dependencies"),
328
+ (FIELD_DEPENDENT_REQUIRED, "dependentRequired"),
329
329
  ] {
330
330
  let entries = self.field(source, field)?;
331
331
  for pair in self.values(entries) {
@@ -375,7 +375,7 @@ impl Engine {
375
375
  if unsafe { sn_is_float(value) } != 0 {
376
376
  return Ok(unsafe { sn_float(value) } >= 0.0);
377
377
  }
378
- let negative = self.field(value, b"negative?\0")?;
378
+ let negative = self.field(value, FIELD_NEGATIVE_P)?;
379
379
  Ok(unsafe { sn_true(negative) } == 0)
380
380
  }
381
381
  }
@@ -1,14 +1,71 @@
1
1
  //! Content validation borrows Ruby string storage. Base64 decoding uses bounded
2
- //! stack scratch; native JSON scanning never constructs a tree. Other source
2
+ //! scratch; native JSON scanning never constructs a tree. Other source
3
3
  //! encodings request Ruby parsing after Rust evaluation returns.
4
4
  use super::*;
5
5
  use std::mem::MaybeUninit;
6
6
 
7
- const STACK_BYTES: usize = 384;
7
+ const STACK_BYTES: usize = 256;
8
8
  // Full refills must end on a three-byte Base64 group boundary.
9
- const STREAM_BYTES: usize = 1536;
9
+ const STREAM_BYTES: usize = 8190;
10
10
  const DIRECT_BYTES: usize = 8192;
11
11
 
12
+ // Runner-local derived content storage. The decoder overwrites the requested
13
+ // prefix before parsing; previous decoded bytes are never used as input.
14
+ #[derive(Default)]
15
+ pub(super) struct Scratch(std::cell::RefCell<Vec<u8>>);
16
+ impl Scratch {
17
+ fn decode(&self, bytes: &[u8], length: usize) -> Verdict {
18
+ let mut scratch = self.0.borrow_mut();
19
+ if scratch.is_empty() {
20
+ scratch.resize(DIRECT_BYTES + 2, 0);
21
+ }
22
+ if length <= DIRECT_BYTES {
23
+ decode_json_turbo(bytes, length, &mut scratch)
24
+ } else {
25
+ let mut input = BufferedBase64 {
26
+ bytes,
27
+ decoded: &mut scratch,
28
+ at: 0,
29
+ len: 0,
30
+ remaining: length,
31
+ valid: true,
32
+ };
33
+ input.fill();
34
+ json(input)
35
+ }
36
+ }
37
+ pub(super) fn memory_usage(&self) -> usize {
38
+ self.0.try_borrow().map_or(0, |scratch| scratch.capacity())
39
+ }
40
+ }
41
+
42
+ #[cfg(test)]
43
+ mod scratch_tests {
44
+ use super::*;
45
+
46
+ #[test]
47
+ fn reused_decoding_ignores_old_bytes_and_reports_bounded_memory() {
48
+ let caches = runtime::Caches::default();
49
+ let before = unsafe { runtime::sn_caches_size(&caches) };
50
+ assert_eq!(caches.content.memory_usage(), 0);
51
+ for length in [8192, 385, 32768, 4096, 386, 8191, 387] {
52
+ let json = format!("\"{}\"", "x".repeat(length - 2));
53
+ let encoded = base64_simd::STANDARD.encode_to_string(&json);
54
+ assert_eq!(caches.content.decode(encoded.as_bytes(), length), Valid);
55
+ let mut malformed = encoded.into_bytes();
56
+ malformed[0] = b'!';
57
+ assert_eq!(caches.content.decode(&malformed, length), Invalid);
58
+ let bad_json = base64_simd::STANDARD.encode_to_string("x".repeat(length));
59
+ assert_eq!(caches.content.decode(bad_json.as_bytes(), length), Invalid);
60
+ assert_eq!(caches.content.memory_usage(), DIRECT_BYTES + 2);
61
+ }
62
+ assert_eq!(
63
+ unsafe { runtime::sn_caches_size(&caches) } - before,
64
+ DIRECT_BYTES + 2
65
+ );
66
+ }
67
+ }
68
+
12
69
  #[derive(Debug)]
13
70
  pub(super) struct Content {
14
71
  pub(super) base64: bool,
@@ -156,10 +213,7 @@ impl Content {
156
213
  if length <= STACK_BYTES {
157
214
  return decode_json_medium(bytes, length);
158
215
  }
159
- if length <= DIRECT_BYTES {
160
- return decode_json_direct(bytes, length);
161
- }
162
- decode_json_stream(bytes, length)
216
+ caches.content.decode(bytes, length)
163
217
  } else {
164
218
  if JSON {
165
219
  let encoding = match (BORROWED, encoding) {
@@ -187,13 +241,6 @@ fn decode_json_medium(bytes: &[u8], length: usize) -> Verdict {
187
241
  decode_json_turbo(bytes, length, &mut scratch)
188
242
  }
189
243
 
190
- // Keep the 8 KiB scratch frame out of shorter decoded-content paths.
191
- #[inline(never)]
192
- fn decode_json_direct(bytes: &[u8], length: usize) -> Verdict {
193
- let mut scratch = [0; DIRECT_BYTES + 2];
194
- decode_json_turbo(bytes, length, &mut scratch)
195
- }
196
-
197
244
  #[cfg(target_arch = "aarch64")]
198
245
  fn decode_json_turbo(bytes: &[u8], length: usize, scratch: &mut [u8]) -> Verdict {
199
246
  let capacity = base64_turbo::STANDARD.decoded_len_estimate(bytes.len());
@@ -216,22 +263,7 @@ fn decode_json_turbo(bytes: &[u8], length: usize, scratch: &mut [u8]) -> Verdict
216
263
  }
217
264
  }
218
265
 
219
- // Isolate the streaming frame from both direct-decoding paths.
220
- #[inline(never)]
221
- fn decode_json_stream(bytes: &[u8], length: usize) -> Verdict {
222
- let mut input = BufferedBase64 {
223
- bytes,
224
- decoded: [0; STREAM_BYTES + 2],
225
- at: 0,
226
- len: 0,
227
- remaining: length,
228
- valid: true,
229
- };
230
- input.fill();
231
- json(input)
232
- }
233
-
234
- // Decode borrowed bytes into the caller's fixed stack scratch. The STANDARD
266
+ // Decode borrowed bytes into the caller's bounded scratch. The STANDARD
235
267
  // variant also checks each chunk's alphabet, padding and unused trailing bits.
236
268
  #[cfg(target_arch = "aarch64")]
237
269
  fn decode_groups(bytes: &[u8], output: &mut [u8], expected: usize) -> bool {
@@ -398,7 +430,7 @@ unsafe fn string_run_avx2(bytes: &[u8]) -> (usize, bool) {
398
430
  }
399
431
  struct BufferedBase64<'a> {
400
432
  bytes: &'a [u8],
401
- decoded: [u8; STREAM_BYTES + 2],
433
+ decoded: &'a mut [u8],
402
434
  at: usize,
403
435
  len: usize,
404
436
  remaining: usize,
@@ -1680,8 +1680,8 @@ pub unsafe extern "C" fn sn_diagnose(
1680
1680
  ) -> *mut Report {
1681
1681
  let result = catch_unwind(AssertUnwindSafe(|| {
1682
1682
  let engine = unsafe { &*engine };
1683
- let local = runtime::Caches::default();
1684
- let caches = unsafe { caches.as_ref() }.unwrap_or(&local);
1683
+ let mut local = None;
1684
+ let caches = runtime::Caches::supplied_or_default(unsafe { caches.as_ref() }, &mut local);
1685
1685
  let execution = Execution {
1686
1686
  engine,
1687
1687
  caches,
@@ -1718,8 +1718,8 @@ pub unsafe extern "C" fn sn_diagnose_with_limits(
1718
1718
  outcome: *mut budget::Outcome,
1719
1719
  ) -> *mut Report {
1720
1720
  let result = catch_unwind(AssertUnwindSafe(|| {
1721
- let local = runtime::Caches::default();
1722
- let caches = unsafe { caches.as_ref() }.unwrap_or(&local);
1721
+ let mut local = None;
1722
+ let caches = runtime::Caches::supplied_or_default(unsafe { caches.as_ref() }, &mut local);
1723
1723
  let limits = unsafe { *limits };
1724
1724
  let execution = Execution {
1725
1725
  engine: unsafe { &*engine },
@@ -2,7 +2,7 @@
2
2
  //! Ruby runs with the GVL held. Evaluation calls only non-allocating C adapters;
3
3
  //! Ruby callbacks during compilation are protected on the C side.
4
4
  use std::collections::HashMap;
5
- use std::ffi::{c_char, c_void};
5
+ use std::ffi::c_void;
6
6
  use std::hash::{BuildHasherDefault, Hasher};
7
7
  use std::panic::{AssertUnwindSafe, catch_unwind};
8
8
  mod budget;
@@ -108,7 +108,7 @@ unsafe extern "C" {
108
108
  context: *mut c_void,
109
109
  callback: unsafe extern "C" fn(*mut c_void, *const u8, usize, Value) -> i32,
110
110
  ) -> i32;
111
- fn sn_field(object: Value, name: *const c_char, value: *mut Value) -> i32;
111
+ fn sn_field(object: Value, name: i32, value: *mut Value) -> i32;
112
112
  fn sn_pairs(hash: Value, value: *mut Value) -> i32;
113
113
  fn sn_key_copy(key: Value, value: *mut Value) -> i32;
114
114
  fn sn_opcode(symbol: Value) -> i32;
@@ -733,9 +733,9 @@ impl Engine {
733
733
  unsafe { &mut *self.roots }.values.push(value);
734
734
  value
735
735
  }
736
- fn field(&mut self, value: Value, name: &'static [u8]) -> Result<Value, Value> {
736
+ fn field(&mut self, value: Value, name: i32) -> Result<Value, Value> {
737
737
  let mut result = 0;
738
- if unsafe { sn_field(value, name.as_ptr().cast(), &mut result) } != 0 {
738
+ if unsafe { sn_field(value, name, &mut result) } != 0 {
739
739
  return Err(result);
740
740
  }
741
741
  Ok(self.retain(result))
@@ -786,7 +786,7 @@ impl Engine {
786
786
  .map(|p| (p[0], p[1]))
787
787
  .collect())
788
788
  }
789
- fn size_field(&mut self, value: Value, name: &'static [u8]) -> Result<Option<usize>, Value> {
789
+ fn size_field(&mut self, value: Value, name: i32) -> Result<Option<usize>, Value> {
790
790
  let n = self.field(value, name)?;
791
791
  Ok(if unsafe { sn_same(n, sn_nil()) } != 0 {
792
792
  None
@@ -802,7 +802,7 @@ impl Engine {
802
802
  None // The only non-integral bound is maxContains' +Infinity.
803
803
  })
804
804
  }
805
- fn child(&mut self, value: Value, name: &'static [u8]) -> Result<Option<usize>, Value> {
805
+ fn child(&mut self, value: Value, name: i32) -> Result<Option<usize>, Value> {
806
806
  let child = self.field(value, name)?;
807
807
  if unsafe { sn_same(child, sn_nil()) } != 0 {
808
808
  Ok(None)
@@ -886,7 +886,7 @@ impl Engine {
886
886
  self.tracks.push(false);
887
887
  self.kind_demand.push(KindDemand::None);
888
888
  self.metadata.push(scope::Metadata::default());
889
- let code = self.field(program, b"code\0")?;
889
+ let code = self.field(program, FIELD_CODE)?;
890
890
  let flags = unsafe { sn_integer(sn_entry(code, 0)) };
891
891
  self.tracks[index] = flags & i64::from(TRACKS_EVALUATION) != 0;
892
892
  self.compile_scope(program, index, flags)?;
@@ -906,7 +906,7 @@ impl Engine {
906
906
  OP_TYPE_INTEGER => Op::Type(TYPE_INTEGER, false),
907
907
  OP_TYPE_STRING => Op::Type(TYPE_STRING, false),
908
908
  OP_TYPES => {
909
- let mask = self.field(operand, b"mask\0")?;
909
+ let mask = self.field(operand, FIELD_MASK)?;
910
910
  Op::Type(unsafe { sn_integer(mask) } as i32, true)
911
911
  }
912
912
  OP_CONST => Op::Const(operand),
@@ -920,11 +920,11 @@ impl Engine {
920
920
  OP_ONE_OF => Op::One(self.branches(operand)?),
921
921
  OP_NOT => Op::Not(self.compile(operand)?),
922
922
  OP_CONDITIONAL => {
923
- let condition = self.field(operand, b"condition\0")?;
923
+ let condition = self.field(operand, FIELD_CONDITION)?;
924
924
  Op::Conditional(
925
925
  self.compile(condition)?,
926
- self.child(operand, b"then_branch\0")?,
927
- self.child(operand, b"else_branch\0")?,
926
+ self.child(operand, FIELD_THEN_BRANCH)?,
927
+ self.child(operand, FIELD_ELSE_BRANCH)?,
928
928
  )
929
929
  }
930
930
  OP_NUMBER | OP_TYPED_NUMBER | OP_TYPED_INTEGER => {
@@ -944,10 +944,10 @@ impl Engine {
944
944
  }
945
945
  }
946
946
  OP_STRING | OP_TYPED_STRING => {
947
- let pattern = self.field(operand, b"pattern\0")?;
948
- let format = self.field(operand, b"format\0")?;
949
- let base64 = self.field(operand, b"decode_base64\0")?;
950
- let json = self.field(operand, b"parse_json\0")?;
947
+ let pattern = self.field(operand, FIELD_PATTERN)?;
948
+ let format = self.field(operand, FIELD_FORMAT)?;
949
+ let base64 = self.field(operand, FIELD_DECODE_BASE64)?;
950
+ let json = self.field(operand, FIELD_PARSE_JSON)?;
951
951
  {
952
952
  let content = if self.validate_content
953
953
  && unsafe { sn_true(base64) | sn_true(json) } != 0
@@ -959,12 +959,12 @@ impl Engine {
959
959
  } else {
960
960
  None
961
961
  };
962
- let assertion = self.field(operand, b"format_assertion\0")?;
962
+ let assertion = self.field(operand, FIELD_FORMAT_ASSERTION)?;
963
963
  let runtime_format = (self.validate_format
964
964
  || unsafe { sn_true(assertion) } != 0)
965
965
  && unsafe { sn_same(format, sn_nil()) } == 0;
966
966
  let format = if runtime_format {
967
- let name = self.field(format, b"name\0")?;
967
+ let name = self.field(format, FIELD_NAME)?;
968
968
  if unsafe { sn_same(name, sn_nil()) } != 0 {
969
969
  None
970
970
  } else {
@@ -973,8 +973,8 @@ impl Engine {
973
973
  } else {
974
974
  None
975
975
  };
976
- let max_length = self.field(operand, b"max_length\0")?;
977
- let min_length = self.field(operand, b"min_length\0")?;
976
+ let max_length = self.field(operand, FIELD_MAX_LENGTH)?;
977
+ let min_length = self.field(operand, FIELD_MIN_LENGTH)?;
978
978
  let length_flags = (unsafe { sn_truthy(max_length) } << 2)
979
979
  | (unsafe { sn_truthy(min_length) } << 3);
980
980
  Op::String(
@@ -991,8 +991,8 @@ impl Engine {
991
991
  runtime_format,
992
992
  format,
993
993
  content,
994
- min: self.size_field(operand, b"min_length\0")?,
995
- max: self.size_field(operand, b"max_length\0")?,
994
+ min: self.size_field(operand, FIELD_MIN_LENGTH)?,
995
+ max: self.size_field(operand, FIELD_MAX_LENGTH)?,
996
996
  pattern: if unsafe { sn_truthy(pattern) } == 0 {
997
997
  None
998
998
  } else {
@@ -1006,11 +1006,11 @@ impl Engine {
1006
1006
  }
1007
1007
  }
1008
1008
  OP_ARRAY | OP_TYPED_ARRAY => {
1009
- let unique = self.field(operand, b"unique\0")?;
1010
- let prefix = self.field(operand, b"prefix_items\0")?;
1009
+ let unique = self.field(operand, FIELD_UNIQUE)?;
1010
+ let prefix = self.field(operand, FIELD_PREFIX_ITEMS)?;
1011
1011
  let prefix = self.children(prefix)?;
1012
- let items = self.field(operand, b"items\0")?;
1013
- let list = self.field(operand, b"items_list\0")?;
1012
+ let items = self.field(operand, FIELD_ITEMS)?;
1013
+ let list = self.field(operand, FIELD_ITEMS_LIST)?;
1014
1014
  let list = unsafe { sn_true(list) } != 0;
1015
1015
  let items = if list {
1016
1016
  self.children(items)?
@@ -1028,8 +1028,8 @@ impl Engine {
1028
1028
  Box::new(Array {
1029
1029
  header: self.container_header(
1030
1030
  operand,
1031
- b"max_items\0",
1032
- b"min_items\0",
1031
+ FIELD_MAX_ITEMS,
1032
+ FIELD_MIN_ITEMS,
1033
1033
  false,
1034
1034
  (if unsafe { sn_truthy(unique) } != 0 {
1035
1035
  4
@@ -1038,25 +1038,27 @@ impl Engine {
1038
1038
  }) | (if list { 8 } else { 0 }),
1039
1039
  )?,
1040
1040
  unique: unsafe { sn_truthy(unique) } != 0,
1041
- min: self.size_field(operand, b"min_items\0")?,
1042
- max: self.size_field(operand, b"max_items\0")?,
1041
+ min: self.size_field(operand, FIELD_MIN_ITEMS)?,
1042
+ max: self.size_field(operand, FIELD_MAX_ITEMS)?,
1043
1043
  prefix,
1044
1044
  items,
1045
1045
  list,
1046
- additional: self.child(operand, b"additional\0")?,
1047
- contains: self.child(operand, b"contains\0")?,
1048
- min_contains: self.size_field(operand, b"min_contains\0")?.unwrap_or(1),
1049
- max_contains: self.size_field(operand, b"max_contains\0")?,
1046
+ additional: self.child(operand, FIELD_ADDITIONAL)?,
1047
+ contains: self.child(operand, FIELD_CONTAINS)?,
1048
+ min_contains: self
1049
+ .size_field(operand, FIELD_MIN_CONTAINS)?
1050
+ .unwrap_or(1),
1051
+ max_contains: self.size_field(operand, FIELD_MAX_CONTAINS)?,
1050
1052
  count_contains: unsafe {
1051
- sn_true(self.field(operand, b"count_contains\0")?)
1053
+ sn_true(self.field(operand, FIELD_COUNT_CONTAINS)?)
1052
1054
  } != 0,
1053
- unevaluated: self.child(operand, b"unevaluated\0")?,
1055
+ unevaluated: self.child(operand, FIELD_UNEVALUATED)?,
1054
1056
  }),
1055
1057
  )
1056
1058
  }
1057
1059
  OP_OBJECT | OP_TYPED_OBJECT => {
1058
- let patterns = self.field(operand, b"patterns\0")?;
1059
- let dependencies = self.field(operand, b"dependencies\0")?;
1060
+ let patterns = self.field(operand, FIELD_PATTERNS)?;
1061
+ let dependencies = self.field(operand, FIELD_DEPENDENCIES)?;
1060
1062
  {
1061
1063
  let patterns = self.compile_map(patterns)?;
1062
1064
  let pattern_names: Vec<_> = patterns.iter().map(|&(key, _)| key).collect();
@@ -1071,7 +1073,7 @@ impl Engine {
1071
1073
  .collect();
1072
1074
  let pattern_group =
1073
1075
  pattern::PatternGroup::compile(&pattern_names, &patterns);
1074
- let required = self.field(operand, b"required\0")?;
1076
+ let required = self.field(operand, FIELD_REQUIRED)?;
1075
1077
  let inspect_required = unsafe { sn_truthy(required) } != 0
1076
1078
  && !(unsafe { sn_kind(required) } == TYPE_ARRAY
1077
1079
  && unsafe { sn_len(required) } == 0);
@@ -1082,7 +1084,7 @@ impl Engine {
1082
1084
  .filter(|key| key.ordinary_string)
1083
1085
  .map(|key| key.name)
1084
1086
  .collect();
1085
- let properties = self.field(operand, b"properties\0")?;
1087
+ let properties = self.field(operand, FIELD_PROPERTIES)?;
1086
1088
  let properties = self.compile_map(properties)?;
1087
1089
  if properties
1088
1090
  .iter()
@@ -1157,7 +1159,7 @@ impl Engine {
1157
1159
  }
1158
1160
  })
1159
1161
  .collect();
1160
- let dependent = self.field(operand, b"dependent_required\0")?;
1162
+ let dependent = self.field(operand, FIELD_DEPENDENT_REQUIRED)?;
1161
1163
  let mut dependent_required = vec![];
1162
1164
  let mut dependent_schemas = vec![];
1163
1165
  let mut diagnostic_dependencies = vec![];
@@ -1192,7 +1194,7 @@ impl Engine {
1192
1194
  ));
1193
1195
  dependent_required.push((name, names));
1194
1196
  }
1195
- let dependent = self.field(operand, b"dependent_schemas\0")?;
1197
+ let dependent = self.field(operand, FIELD_DEPENDENT_SCHEMAS)?;
1196
1198
  let schemas = self.compile_map(dependent)?;
1197
1199
  diagnostic_dependencies.extend(schemas.iter().map(|&(name, child)| {
1198
1200
  DiagnosticDependency::Schema(name, child, "dependentSchemas")
@@ -1211,15 +1213,15 @@ impl Engine {
1211
1213
  let mut object = Object {
1212
1214
  header: self.container_header(
1213
1215
  operand,
1214
- b"max_properties\0",
1215
- b"min_properties\0",
1216
+ FIELD_MAX_PROPERTIES,
1217
+ FIELD_MIN_PROPERTIES,
1216
1218
  true,
1217
1219
  0,
1218
1220
  )?,
1219
1221
  inspect_required,
1220
1222
  record: None,
1221
- min: self.size_field(operand, b"min_properties\0")?,
1222
- max: self.size_field(operand, b"max_properties\0")?,
1223
+ min: self.size_field(operand, FIELD_MIN_PROPERTIES)?,
1224
+ max: self.size_field(operand, FIELD_MAX_PROPERTIES)?,
1223
1225
  required,
1224
1226
  required_order,
1225
1227
  properties,
@@ -1231,12 +1233,12 @@ impl Engine {
1231
1233
  required_property_indices,
1232
1234
  property_keys,
1233
1235
  property_indices,
1234
- additional: self.child(operand, b"additional\0")?,
1235
- names: self.child(operand, b"property_names\0")?,
1236
+ additional: self.child(operand, FIELD_ADDITIONAL)?,
1237
+ names: self.child(operand, FIELD_PROPERTY_NAMES)?,
1236
1238
  dependent_required,
1237
1239
  dependent_schemas,
1238
1240
  diagnostic_dependencies,
1239
- unevaluated: self.child(operand, b"unevaluated\0")?,
1241
+ unevaluated: self.child(operand, FIELD_UNEVALUATED)?,
1240
1242
  }
1241
1243
  .with_record();
1242
1244
  if let Some(record) = &mut object.record {
@@ -1289,11 +1291,11 @@ impl Engine {
1289
1291
  }
1290
1292
  }
1291
1293
  OP_REF | OP_RECURSIVE_REF | OP_DYNAMIC_REF => {
1292
- let reference = self.field(operand, b"value\0")?;
1294
+ let reference = self.field(operand, FIELD_VALUE)?;
1293
1295
  let mut target = 0;
1294
1296
  if unsafe { sn_resolve(self.compiler, program, reference, &mut target) } != 0 {
1295
1297
  if unsafe { sn_resolution_error(target) } != 0 {
1296
- let message = self.field(target, b"message\0")?;
1298
+ let message = self.field(target, FIELD_MESSAGE)?;
1297
1299
  Op::UnresolvedRef(message)
1298
1300
  } else {
1299
1301
  return Err(target);
@@ -3586,8 +3588,8 @@ pub unsafe extern "C" fn sn_valid(
3586
3588
  ) -> i32 {
3587
3589
  catch_unwind(AssertUnwindSafe(|| {
3588
3590
  let e = unsafe { &*engine };
3589
- let local = runtime::Caches::default();
3590
- let caches = unsafe { caches.as_ref() }.unwrap_or(&local);
3591
+ let mut local = None;
3592
+ let caches = runtime::Caches::supplied_or_default(unsafe { caches.as_ref() }, &mut local);
3591
3593
  caches.admit(e, root);
3592
3594
  let execution = Execution {
3593
3595
  engine: e,
@@ -3614,8 +3616,8 @@ pub unsafe extern "C" fn sn_valid_with_limits(
3614
3616
  outcome: *mut budget::Outcome,
3615
3617
  ) -> i32 {
3616
3618
  let result = catch_unwind(AssertUnwindSafe(|| {
3617
- let local = runtime::Caches::default();
3618
- let caches = unsafe { caches.as_ref() }.unwrap_or(&local);
3619
+ let mut local = None;
3620
+ let caches = runtime::Caches::supplied_or_default(unsafe { caches.as_ref() }, &mut local);
3619
3621
  let e = unsafe { &*engine };
3620
3622
  caches.admit(e, root);
3621
3623
  let execution = Execution {
@@ -810,6 +810,35 @@ mod tests {
810
810
  }
811
811
 
812
812
  impl Engine {
813
+ fn clear_memo_guards(&mut self) {
814
+ for flags in [
815
+ &mut self.memo_guarded,
816
+ &mut self.memo_guard_sources,
817
+ &mut self.memo_guard_same_sources,
818
+ &mut self.memo_guard_scalar_sources,
819
+ &mut self.memo_guard_scalar_amplification,
820
+ ] {
821
+ *flags = vec![false; self.nodes.len()];
822
+ }
823
+ }
824
+
825
+ fn clear_memo_analysis(&mut self) {
826
+ self.memoizable = vec![false; self.nodes.len()];
827
+ self.memo_contextual = vec![false; self.nodes.len()];
828
+ self.clear_memo_guards();
829
+ self.memo_scope_projection = memo_scope::Projection::default();
830
+ }
831
+
832
+ fn compile_memo_projection(&mut self, edges: &[Vec<usize>], parents: &[Vec<usize>]) {
833
+ self.memo_scope_projection = if self.memo_guarded.iter().any(|&guarded| guarded)
834
+ || self.anchor_tables.len() > 1 && self.memo_contextual.iter().any(|&scoped| scoped)
835
+ {
836
+ memo_scope::Projection::compile(edges, parents, self.nodes.len())
837
+ } else {
838
+ memo_scope::Projection::default()
839
+ };
840
+ }
841
+
813
842
  pub(super) fn compile_memoization(&mut self) {
814
843
  let mut incoming = vec![0_usize; self.nodes.len()];
815
844
  let mut repeated_position = vec![false; self.nodes.len()];
@@ -909,6 +938,27 @@ impl Engine {
909
938
  }
910
939
  }
911
940
  }
941
+ // Every admission requires multiple incoming references or repeated
942
+ // positions. If neither exists, skip guard/cycle analysis and retain
943
+ // indexed false flags for the runtime's ordinary uncached path.
944
+ if incoming.iter().all(|&count| count <= 1)
945
+ && !repeated_position.iter().any(|&repeated| repeated)
946
+ {
947
+ self.clear_memo_analysis();
948
+ return;
949
+ }
950
+ let parents = graph::parents(&edges);
951
+ // Compute iteration reachability once, before either admission policy.
952
+ graph::propagate(&mut repeated_subtree, &parents);
953
+ // A position-only candidate needs nested iteration to amplify sharing.
954
+ // Share this necessary condition with both admission policies below.
955
+ let candidates: Vec<_> = (0..count)
956
+ .map(|node| incoming[node] > 1 || repeated_position[node] && repeated_subtree[node])
957
+ .collect();
958
+ if !candidates.iter().any(|&candidate| candidate) {
959
+ self.clear_memo_analysis();
960
+ return;
961
+ }
912
962
  // Reject cycles through any possible binding, plus recursive refs.
913
963
  // States 2 and 4 distinguish scope-independent and scoped successes.
914
964
  // Bound analysis depth just as evaluation is bounded; conservatively
@@ -920,7 +970,6 @@ impl Engine {
920
970
  eligible: &[bool],
921
971
  states: &mut [u8],
922
972
  count: usize,
923
- repeated_subtree: &mut [bool],
924
973
  ) -> bool {
925
974
  match states[node] {
926
975
  1 | 3 => return false,
@@ -934,16 +983,7 @@ impl Engine {
934
983
  states[node] = 1;
935
984
  let mut scoped = node >= count;
936
985
  let safe = edges[node].iter().all(|&child| {
937
- let safe = reusable(
938
- child,
939
- depth + 1,
940
- edges,
941
- eligible,
942
- states,
943
- count,
944
- repeated_subtree,
945
- );
946
- repeated_subtree[node] |= repeated_subtree[child];
986
+ let safe = reusable(child, depth + 1, edges, eligible, states, count);
947
987
  scoped |= states[child] == 4;
948
988
  safe
949
989
  });
@@ -959,20 +999,21 @@ impl Engine {
959
999
  let mut states = vec![0; edges.len()];
960
1000
  self.memoizable = (0..self.nodes.len())
961
1001
  .map(|node| {
962
- (incoming[node] > 1 || repeated_position[node])
963
- && reusable(node, 0, &edges, &eligible, &mut states, count, &mut repeated_subtree)
964
- // Position-only candidates need nested iteration to amplify
965
- // sharing. Simple records/scalars keep their cheap scans.
966
- && (incoming[node] > 1 || repeated_subtree[node])
1002
+ candidates[node] && reusable(node, 0, &edges, &eligible, &mut states, count)
967
1003
  })
968
1004
  .collect();
969
1005
  self.memo_contextual = states[..count].iter().map(|&state| state == 4).collect();
1006
+ // Proven acyclic candidates do not need any guard signatures, even if
1007
+ // unrelated non-candidates lie on cycles elsewhere in the program.
1008
+ if (0..count).all(|node| !candidates[node] || self.memoizable[node]) {
1009
+ self.clear_memo_guards();
1010
+ self.compile_memo_projection(&edges, &parents);
1011
+ return;
1012
+ }
970
1013
  // Cyclic candidates can reuse results only under an exact guard/scope
971
1014
  // context. Propagate unsupported instructions and nested iteration
972
1015
  // through reverse edges without a per-candidate graph walk.
973
- let parents = graph::parents(&edges);
974
1016
  graph::propagate(&mut unsupported, &parents);
975
- graph::propagate(&mut repeated_subtree, &parents);
976
1017
  // Iterative Kosaraju analysis identifies the only active sources
977
1018
  // whose guards can affect descendants: sources lying on a schema cycle.
978
1019
  fn cycle_nodes(edges: &[Vec<usize>], parents: &[Vec<usize>]) -> Vec<bool> {
@@ -1083,22 +1124,27 @@ impl Engine {
1083
1124
  self.memo_guard_scalar_amplification = scalar_amplification[..count].to_vec();
1084
1125
  // Object dependencies keep the same value; all other container child
1085
1126
  // edges change positions. Scalar/array inputs cannot take dependencies.
1127
+ let mut has_dependencies = false;
1086
1128
  for (node, ops) in self.nodes.iter().enumerate() {
1087
1129
  for op in ops {
1088
1130
  if let Op::Object(_, rules) = op {
1131
+ has_dependencies |= !rules.dependent_schemas.is_empty();
1089
1132
  same_edges[node]
1090
1133
  .extend(rules.dependent_schemas.iter().map(|&(_, child)| child));
1091
1134
  }
1092
1135
  }
1093
1136
  }
1094
- self.memo_guard_same_sources =
1095
- cycle_nodes(&same_edges, &graph::parents(&same_edges))[..count].to_vec();
1137
+ self.memo_guard_same_sources = if has_dependencies {
1138
+ cycle_nodes(&same_edges, &graph::parents(&same_edges))[..count].to_vec()
1139
+ } else {
1140
+ self.memo_guard_scalar_sources.clone()
1141
+ };
1096
1142
  self.memo_guard_sources = cyclic[..count].to_vec();
1097
1143
  self.memo_guarded = vec![false; count];
1098
1144
  for node in 0..count {
1099
1145
  let amplified_cycle = incoming[node] > 2 || cyclic_incoming[node] > 1;
1100
1146
  if !self.memoizable[node] && !unsupported[node]
1101
- && (incoming[node] > 1 || repeated_position[node] && repeated_subtree[node])
1147
+ && candidates[node]
1102
1148
  // A linear recursive property chain has no amplification.
1103
1149
  // Avoid signatures unless a cycle has repeated input positions
1104
1150
  // or multiple recursive edges, with or without an entry ref.
@@ -1111,12 +1157,6 @@ impl Engine {
1111
1157
  self.memo_guarded[node] = true;
1112
1158
  }
1113
1159
  }
1114
- self.memo_scope_projection = if self.memo_guarded.iter().any(|&guarded| guarded)
1115
- || self.anchor_tables.len() > 1 && self.memo_contextual.iter().any(|&scoped| scoped)
1116
- {
1117
- memo_scope::Projection::compile(&edges, &parents, count)
1118
- } else {
1119
- memo_scope::Projection::default()
1120
- };
1160
+ self.compile_memo_projection(&edges, &parents);
1121
1161
  }
1122
1162
  }
@@ -18,6 +18,14 @@ impl Projection {
18
18
  Self::bounded(edges, parents, count, MAX_NAMES)
19
19
  }
20
20
  fn bounded(edges: &[Vec<usize>], parents: &[Vec<usize>], count: usize, limit: usize) -> Self {
21
+ // With no incoming edges to any dispatch vertex, every real node's
22
+ // reachable name set is empty, including nodes on schema cycles.
23
+ if parents[count..].iter().all(Vec::is_empty) {
24
+ return Self {
25
+ names: vec![Some(Arc::from([])); count],
26
+ graph: None,
27
+ };
28
+ }
21
29
  let mut seen = vec![false; edges.len()];
22
30
  let order = graph::postorder(edges, &mut seen);
23
31
  let mut component = vec![usize::MAX; edges.len()];
@@ -179,6 +187,28 @@ mod tests {
179
187
  parents
180
188
  }
181
189
  #[test]
190
+ fn static_cycles_share_empty_projections_without_default_scope_names() {
191
+ // The unused dispatch vertex points into a real cycle, but no real
192
+ // node can reach it. Even a zero closure budget needs no graph fallback.
193
+ let edges = vec![vec![1], vec![0], vec![], vec![0]];
194
+ for limit in [0, 1, MAX_NAMES] {
195
+ let projection = Projection::bounded(&edges, &parents(&edges), 3, limit);
196
+ assert!(projection.graph.is_none());
197
+ for node in 0..3 {
198
+ assert!(
199
+ projection
200
+ .names(node, 3, 0, true, &budget::Budget::default())
201
+ .unwrap()
202
+ .is_empty()
203
+ );
204
+ assert!(Arc::ptr_eq(
205
+ projection.names[0].as_ref().unwrap(),
206
+ projection.names[node].as_ref().unwrap()
207
+ ));
208
+ }
209
+ }
210
+ }
211
+ #[test]
182
212
  fn projects_cycles_and_transitive_dynamic_dispatch_targets() {
183
213
  // Four nodes, two dynamic names and the recursive dispatch vertex.
184
214
  let edges = vec![
@@ -440,14 +440,14 @@ impl FastMultiple {
440
440
 
441
441
  impl Engine {
442
442
  pub(super) fn compile_number(&mut self, operand: Value) -> Result<Option<Rules>, Value> {
443
- let mask = self.field(operand, b"mask\0")?;
443
+ let mask = self.field(operand, FIELD_MASK)?;
444
444
  let mask = unsafe { sn_integer(mask) } as i32;
445
445
  let mut limits = vec![];
446
446
  for (bit, name) in [
447
- (MAXIMUM, b"maximum\0".as_slice()),
448
- (MINIMUM, b"minimum\0"),
449
- (EXCLUSIVE_MAXIMUM, b"exclusive_maximum\0"),
450
- (EXCLUSIVE_MINIMUM, b"exclusive_minimum\0"),
447
+ (MAXIMUM, FIELD_MAXIMUM),
448
+ (MINIMUM, FIELD_MINIMUM),
449
+ (EXCLUSIVE_MAXIMUM, FIELD_EXCLUSIVE_MAXIMUM),
450
+ (EXCLUSIVE_MINIMUM, FIELD_EXCLUSIVE_MINIMUM),
451
451
  ] {
452
452
  if mask & bit != 0 {
453
453
  let value = self.field(operand, name)?;
@@ -460,7 +460,7 @@ impl Engine {
460
460
  let multiple = if mask & MULTIPLE_OF == 0 {
461
461
  None
462
462
  } else {
463
- let value = self.field(operand, b"multiple_of\0")?;
463
+ let value = self.field(operand, FIELD_MULTIPLE_OF)?;
464
464
  let Some(limit) = self.numeric_limit(value)? else {
465
465
  return Ok(None);
466
466
  };
@@ -537,8 +537,8 @@ impl Engine {
537
537
  let (numerator, denominator) = if integer {
538
538
  (value, None)
539
539
  } else {
540
- let numerator = self.field(value, b"numerator\0")?;
541
- let denominator = self.field(value, b"denominator\0")?;
540
+ let numerator = self.field(value, FIELD_NUMERATOR)?;
541
+ let denominator = self.field(value, FIELD_DENOMINATOR)?;
542
542
  (numerator, Some(denominator))
543
543
  };
544
544
  let exact = read_integer(numerator)
@@ -550,7 +550,7 @@ impl Engine {
550
550
  let arbitrary = exact
551
551
  .is_none()
552
552
  .then(|| Box::new(arbitrary::fraction(numerator, denominator)));
553
- let rounded = self.field(value, b"to_f\0")?;
553
+ let rounded = self.field(value, FIELD_TO_F)?;
554
554
  let rounded = unsafe { sn_float(rounded) };
555
555
  let wide_rounded_order = if integer && exact.is_none() {
556
556
  wide_float_tie(value, rounded)
@@ -32,14 +32,27 @@ impl Path {
32
32
  let bytes = unsafe { byte_view(data, len) };
33
33
  if matches!(encoding, ENCODING_UTF8 | ENCODING_ASCII)
34
34
  && (encoding != ENCODING_ASCII || bytes.is_ascii())
35
- && let Ok(name) = std::str::from_utf8(bytes)
35
+ && let Some(name) = if bytes.len() < 64 {
36
+ std::str::from_utf8(bytes).ok()
37
+ } else {
38
+ simdutf8::compat::from_utf8(bytes).ok()
39
+ }
36
40
  {
37
- for character in name.chars() {
38
- match character {
39
- '~' => self.text.push_str("~0"),
40
- '/' => self.text.push_str("~1"),
41
- _ => self.text.push(character),
41
+ // ASCII escape bytes are UTF-8 boundaries. Copy unchanged ends
42
+ // together; retain the scalar loop for densely escaped names.
43
+ if let Some(first) = memchr::memchr2(b'~', b'/', bytes) {
44
+ let end = memchr::memrchr2(b'~', b'/', bytes).unwrap() + 1;
45
+ self.text.push_str(&name[..first]);
46
+ for character in name[first..end].chars() {
47
+ match character {
48
+ '~' => self.text.push_str("~0"),
49
+ '/' => self.text.push_str("~1"),
50
+ _ => self.text.push(character),
51
+ }
42
52
  }
53
+ self.text.push_str(&name[end..]);
54
+ } else {
55
+ self.text.push_str(name);
43
56
  }
44
57
  } else {
45
58
  self.names.push((self.text.len(), value));
@@ -37,6 +37,7 @@ pub struct Caches {
37
37
  reports: reports::Reports,
38
38
  properties: properties::Properties,
39
39
  report_continuations_allowed: std::cell::Cell<bool>,
40
+ pub(super) content: content::Scratch,
40
41
  }
41
42
  #[repr(C)]
42
43
  #[derive(Clone, Copy)]
@@ -95,6 +96,15 @@ unsafe extern "C" {
95
96
  ) -> i32;
96
97
  }
97
98
  impl Caches {
99
+ // FFI callers normally provide runner-owned state. Only null-cache calls
100
+ // need a temporary owner, whose lifetime stays in the adapter's scope.
101
+ pub(super) fn supplied_or_default<'a>(
102
+ supplied: Option<&'a Self>,
103
+ fallback: &'a mut Option<Self>,
104
+ ) -> &'a Self {
105
+ supplied.unwrap_or_else(|| fallback.get_or_insert_with(Self::default))
106
+ }
107
+
98
108
  fn reset_pass(&self) {
99
109
  self.scopes.reset();
100
110
  self.reports.reset();
@@ -692,6 +702,7 @@ pub unsafe extern "C" fn sn_caches_size(caches_ptr: *const Caches) -> usize {
692
702
  + messages
693
703
  + unsafe { &*caches_ptr }.reports.memory_usage()
694
704
  + unsafe { &*caches_ptr }.properties.memory_usage()
705
+ + unsafe { &*caches_ptr }.content.memory_usage()
695
706
  + pointers
696
707
  + caches.capacity() * std::mem::size_of::<Option<Box<Cache>>>()
697
708
  + caches
@@ -978,6 +989,22 @@ pub unsafe extern "C" fn sn_scalar_arguments(caches: *const Caches, output: *mut
978
989
  #[cfg(test)]
979
990
  mod tests {
980
991
  use super::*;
992
+ #[test]
993
+ fn ffi_fallback_is_created_only_without_supplied_state() {
994
+ let supplied = Caches::default();
995
+ supplied.dynamic_calls.set(true);
996
+ let mut fallback = None;
997
+ let selected = Caches::supplied_or_default(Some(&supplied), &mut fallback);
998
+ assert!(std::ptr::eq(selected, &supplied));
999
+ assert!(selected.dynamic_calls());
1000
+ assert!(fallback.is_none());
1001
+
1002
+ let selected = Caches::supplied_or_default(None, &mut fallback);
1003
+ assert!(!selected.dynamic_calls());
1004
+ selected.dynamic_calls.set(true);
1005
+ assert!(fallback.as_ref().unwrap().dynamic_calls());
1006
+ }
1007
+
981
1008
  #[test]
982
1009
  fn child_modes_inherit_deadlines_without_adding_normal_scalar_checkpoints() {
983
1010
  let parent = Caches::default();
@@ -15,6 +15,53 @@ pub(super) struct Metadata {
15
15
  mod tests {
16
16
  use super::*;
17
17
 
18
+ #[test]
19
+ fn acyclic_candidates_do_not_require_guards_for_unrelated_cycles() {
20
+ let reference = |target| {
21
+ Op::Ref(Reference {
22
+ target,
23
+ kind: ReferenceKind::Static,
24
+ })
25
+ };
26
+ let mut engine = Engine {
27
+ nodes: vec![
28
+ vec![Op::Boolean(true)],
29
+ vec![reference(1)],
30
+ vec![reference(0)],
31
+ vec![reference(0)],
32
+ ],
33
+ ..Engine::default()
34
+ };
35
+ engine.compile_memoization();
36
+ assert_eq!(engine.memoizable, [true, false, false, false]);
37
+ assert_eq!(engine.memo_guarded, [false; 4]);
38
+ assert_eq!(engine.memo_guard_sources, [false; 4]);
39
+ }
40
+
41
+ #[test]
42
+ fn clears_prior_guard_analysis_when_no_reuse_positions_remain() {
43
+ let mut engine = Engine {
44
+ nodes: (0..4)
45
+ .map(|_| {
46
+ vec![Op::Ref(Reference {
47
+ target: 0,
48
+ kind: ReferenceKind::Static,
49
+ })]
50
+ })
51
+ .collect(),
52
+ ..Engine::default()
53
+ };
54
+ engine.compile_memoization();
55
+ assert!(engine.memo_guarded[0]);
56
+ assert!(engine.memo_scope_projection.memory_usage() > 0);
57
+ engine.nodes = vec![vec![Op::Boolean(true)], vec![Op::Boolean(false)]];
58
+ engine.compile_memoization();
59
+ assert_eq!(engine.memoizable, [false, false]);
60
+ assert_eq!(engine.memo_guarded, [false, false]);
61
+ assert_eq!(engine.memo_guard_sources, [false, false]);
62
+ assert_eq!(engine.memo_scope_projection.memory_usage(), 0);
63
+ }
64
+
18
65
  #[test]
19
66
  fn requires_guard_histories_for_cycles_reached_through_dynamic_overrides() {
20
67
  let reference = |target, kind| Op::Ref(Reference { target, kind });
@@ -173,10 +220,10 @@ impl Engine {
173
220
  index: usize,
174
221
  flags: i64,
175
222
  ) -> Result<(), Value> {
176
- let node = self.field(program, b"node\0")?;
177
- let resource = self.field(node, b"resource\0")?;
178
- let recursive = self.field(program, b"recursive_anchor?\0")?;
179
- let dynamic = self.field(program, b"dynamic_anchor\0")?;
223
+ let node = self.field(program, FIELD_NODE)?;
224
+ let resource = self.field(node, FIELD_RESOURCE)?;
225
+ let recursive = self.field(program, FIELD_RECURSIVE_ANCHOR_P)?;
226
+ let dynamic = self.field(program, FIELD_DYNAMIC_ANCHOR)?;
180
227
  self.metadata[index] = Metadata {
181
228
  resource,
182
229
  enters_scope: flags & i64::from(TRACKS_DYNAMIC_SCOPE) != 0,
@@ -204,7 +251,7 @@ impl Engine {
204
251
  opcode: i32,
205
252
  target: usize,
206
253
  ) -> Result<Op, Value> {
207
- let fragment = self.field(operand, b"fragment\0")?;
254
+ let fragment = self.field(operand, FIELD_FRAGMENT)?;
208
255
  let kind = if opcode == OP_RECURSIVE_REF
209
256
  && unsafe { sn_same(fragment, sn_nil()) } == 0
210
257
  && unsafe { sn_string_bytes(fragment) } == 0
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Schemurai
4
4
  # The installed Schemurai version.
5
- VERSION = "3.1.0"
5
+ VERSION = "3.1.1"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schemurai
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kuya KOHARA (oakcask)
@@ -118,7 +118,7 @@ licenses:
118
118
  metadata:
119
119
  homepage_uri: https://github.com/oakcask/schemurai
120
120
  source_code_uri: https://github.com/oakcask/schemurai/tree/main
121
- documentation_uri: https://rubydoc.info/gems/schemurai/3.1.0
121
+ documentation_uri: https://rubydoc.info/gems/schemurai/3.1.1
122
122
  rubygems_mfa_required: 'true'
123
123
  rdoc_options:
124
124
  - "--main"