yeptris 0.6.15.2-aarch64-linux → 0.6.16.1-aarch64-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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 419470a0e038bec65880a3d764da46f072d7993e5aca9de71825a8428d5b2f98
4
- data.tar.gz: 19cf714c7f80abc07d8729420700ef2c08bf1a140de03c3e11a8bb6ff177a4f4
3
+ metadata.gz: e2612a8d5390e6bf215a646fee2c582ff4aaa1091a65d26fb0f113220983741c
4
+ data.tar.gz: c706b95796ded1c06c4ea4a41415ee484c6f894d9fdbf8652f20d3f3adae5c9b
5
5
  SHA512:
6
- metadata.gz: 9d11c53e5305c76f780f431d73be9c62228e250ab164c4fd636429ad4fca4e3705b1d46b0e9cf7abed5d8a0662d0347a2399de0ce3a16f5eec987d05d0fb053f
7
- data.tar.gz: 5c24e95902b4ae44595710274ede3efbd605885d8cbc0747fc0965fcb8a2addb32ea8f18648606fef823d09a6f106d777d175f4e8ed0f1c8e74810f22d6cbee6
6
+ metadata.gz: 0ad96188aa09042b35024144cdb9b555abfc0bf5731d73f4d246b4d84cbd2b001080f769d3fa8a0f1488b3ce41da672881724fb21acb431a74ddbb43a468664f
7
+ data.tar.gz: 496b1c54f30d326cf2999fca95105c56756bf4addb32b32e4d46651a2930921973bda3c8bf488e23b7c63185be3223210cc1deeb2e2e9a1fc2dfd39e5163c185
@@ -17,6 +17,14 @@
17
17
  #include <stdlib.h>
18
18
  #include <string.h>
19
19
 
20
+ static rb_encoding* cr_utf8_enc;
21
+
22
+ /* Map keys repeat across records in CBOR corpora; interning shares one
23
+ * VALUE per distinct key (the json_ruby.c on_key pattern, #157). */
24
+ static VALUE cr_key_str(const char* p, size_t len) {
25
+ return rb_enc_interned_str(p, (long)len, cr_utf8_enc);
26
+ }
27
+
20
28
  #include "dom/dom.h"
21
29
  #include "doc.h" /* the public YeptrisDocument wrapper: ->dom */
22
30
  #include <yeptris/cbor.h>
@@ -91,8 +99,17 @@ static VALUE cr_walk(const yep_dom* d, uint32_t id) {
91
99
  VALUE h = HASH_NEW_CAPA(pairs);
92
100
  uint32_t c = n->first_child;
93
101
  for (long i = 0; i < pairs && c != UINT32_MAX; i++) {
94
- VALUE k = cr_walk(d, c);
95
- c = d->nodes[c].next_sibling;
102
+ VALUE k;
103
+ const yep_dnode* kn = &d->nodes[c];
104
+ if (kn->kind == YEP_DOM_SCALAR && kn->tag_id == YEPTRIS_TAG_STR) {
105
+ uint32_t klen = 0;
106
+ const char* kp = cr_view(d, kn->value, &klen);
107
+ k = cr_key_str(kp, klen);
108
+ c = kn->next_sibling;
109
+ } else {
110
+ k = cr_walk(d, c);
111
+ c = d->nodes[c].next_sibling;
112
+ }
96
113
  VALUE v = (c != UINT32_MAX) ? cr_walk(d, c) : Qnil;
97
114
  if (c != UINT32_MAX) {
98
115
  c = d->nodes[c].next_sibling;
@@ -107,6 +124,9 @@ static VALUE cr_walk(const yep_dom* d, uint32_t id) {
107
124
  }
108
125
 
109
126
  VALUE yep_rb_cbor_load(const char* p, size_t len, int strict) {
127
+ if (cr_utf8_enc == NULL) {
128
+ cr_utf8_enc = rb_utf8_encoding();
129
+ }
110
130
  YeptrisStatus st = YEPTRIS_OK;
111
131
  /* the DOM borrows the input buffer zero-copy; the walk ALLOCATES,
112
132
  * and a GC compaction mid-walk moves the caller's String — the
@@ -8,6 +8,11 @@ class Yeptris::Document
8
8
  # explicit free path and the finalizer both flip the same flag.
9
9
  Freed = Struct.new(:state) # :alive | :freed
10
10
 
11
+ # The raw C handle — the Yeptris::Node wrappers cache by its
12
+ # address; stream-children wrappers (Document.without_finalizer)
13
+ # carry it without registering a finalizer (#182).
14
+ attr_reader :c_ptr
15
+
11
16
  # The schema this document was parsed with (a parse property).
12
17
  attr_reader :parse_schema
13
18
 
@@ -80,6 +85,18 @@ class Yeptris::Document
80
85
  new(c_ptr, Freed.new(:alive), schema)
81
86
  end
82
87
 
88
+ # #182: the stream-children wrapper owns NOTHING (the parent
89
+ # Document wrapper is the sole freer of the C memory). A plain
90
+ # Document.new would register a finalizer on a c_ptr we don't own;
91
+ # a parse_stream on a multi-doc stream would free the same pointer
92
+ # three times at GC → SIGABRT. This factory builds the wrapper
93
+ # WITHOUT a finalizer; the wrapper's lifetime follows the owner.
94
+ def self.without_finalizer(c_ptr)
95
+ doc = new(c_ptr)
96
+ ObjectSpace.undefine_finalizer(doc)
97
+ doc
98
+ end
99
+
83
100
  def ensure_alive!
84
101
  raise Yeptris::FreedError, "document is freed" if @freed.state == :freed
85
102
  end
Binary file
data/lib/yeptris/psych.rb CHANGED
@@ -291,7 +291,15 @@ module Yeptris
291
291
  raise SyntaxError.from_parse_error(e)
292
292
  end
293
293
 
294
- def parse_stream(yaml)
294
+ def parse_stream(yaml, &block)
295
+ # stdlib yields each document to a block if one was given (and
296
+ # the stream it returns holds the same children either way).
297
+ # #182: the wrapper is the sole owner of the C memory — the
298
+ # stream merely REFERENCES it; the wrapper's own finalizer
299
+ # handles GC-free. (The block-yielding form is tracked under
300
+ # #179: the current implementation materializes eagerly; the
301
+ # block is only honored as a no-op convenience.)
302
+ _ = block # reserved for the future yielding form (#179)
295
303
  doc = Yeptris::Document.parse(yaml, schema: :compat_11)
296
304
  return nil if doc.nil? || doc.document_count.zero?
297
305
 
@@ -308,6 +316,8 @@ module Yeptris
308
316
  rescue Yeptris::ParseError => e
309
317
  raise SyntaxError.from_parse_error(e)
310
318
  ensure
319
+ # the returned stream OWNS the doc's lifetime (its free delegates
320
+ # to the owner); only free here when the stream wasn't built
311
321
  doc&.free if doc && !stream
312
322
  end
313
323
 
@@ -522,8 +532,12 @@ module Yeptris
522
532
  # stream owns the yeptris document)
523
533
  def document_stream_child(doc, index)
524
534
  root = doc.root(index)
525
- d = Document.new
526
- d.handle = root&.document
535
+ # #182's root cause: three per-stream Document wrappers all
536
+ # capture the same c_ptr in their finalizers → three frees.
537
+ # The stream-children WRAPPER owns nothing (the owner wrapper
538
+ # is the real Document; this one just references it). Build
539
+ # a non-owning wrapper with no finalizer.
540
+ d = Document.send(:new, root&.document, false)
527
541
  d.children << node(root) if root
528
542
  d
529
543
  end
@@ -66,6 +66,92 @@ module Yeptris
66
66
  end
67
67
  end
68
68
 
69
+ # #184 (lutaml-model KV path): zip Schema columns into record
70
+ # hashes ready for Serializable.instantiate. Mapping root → one
71
+ # hash; sequence-of-mappings → one hash per element; nested
72
+ # mappings → nested hashes. Returns Array<Hash> always. when_attribute
73
+ # / polymorphic stay out of scope (interpretive fallback).
74
+ def load_records(source, schema: :core_12, desc:, capacity: 64)
75
+ cols = load(source, schema: schema, desc: desc, capacity: capacity)
76
+ zip_records(desc, cols)
77
+ end
78
+
79
+ def zip_records(desc, cols)
80
+ root = desc[0] || {}
81
+ case root[:kind]
82
+ when :sequence
83
+ child_start = root[:child_index] || 1
84
+ child_count = root[:child_count] || 0
85
+ return [] if child_count.zero?
86
+
87
+ child = desc[child_start]
88
+ if child && child[:kind] == :mapping
89
+ field_start = child[:child_index] || (child_start + 1)
90
+ field_count = child[:child_count] || 0
91
+ fields = desc[field_start, field_count] || []
92
+ field_cols = cols[field_start, field_count] || []
93
+ nrows = field_cols.map(&:length).max || 0
94
+ Array.new(nrows) do |r|
95
+ h = {}
96
+ fields.each_with_index do |f, i|
97
+ next unless f[:wire_name]
98
+ col = field_cols[i] || []
99
+ # ABSENT keys are OMITTED, not nil-filled (lutaml-model's
100
+ # load-bearing edge: absent seeds defaults +
101
+ # using_default?; explicit nil is a present value with
102
+ # different render semantics). The C columns fill in
103
+ # document order — col.length > r means present.
104
+ next if col.length <= r
105
+
106
+ h[f[:wire_name].to_sym] = materialize_field(f, desc, cols, col[r])
107
+ end
108
+ h
109
+ end
110
+ else
111
+ (cols[child_start] || []).map { |v| { value: v } }
112
+ end
113
+ when :mapping
114
+ field_start = root[:child_index] || 1
115
+ field_count = root[:child_count] || 0
116
+ fields = desc[field_start, field_count] || []
117
+ field_cols = cols[field_start, field_count] || []
118
+ h = {}
119
+ fields.each_with_index do |f, i|
120
+ next unless f[:wire_name]
121
+ col = field_cols[i] || []
122
+ next if col.empty? # absent: omitted, not nil-filled (#184)
123
+
124
+ h[f[:wire_name].to_sym] = materialize_field(f, desc, cols, col[0])
125
+ end
126
+ [h]
127
+ else
128
+ [{ value: cols[0]&.first }]
129
+ end
130
+ end
131
+ private_class_method :zip_records
132
+
133
+ def materialize_field(field, desc, cols, cell)
134
+ case field[:kind]
135
+ when :mapping
136
+ start = field[:child_index] || 0
137
+ count = field[:child_count] || 0
138
+ return nil if count.zero? || cell.nil?
139
+
140
+ nested = {}
141
+ desc[start, count]&.each_with_index do |nf, i|
142
+ next unless nf[:wire_name]
143
+ ncol = cols[start + i] || []
144
+ nested[nf[:wire_name].to_sym] = ncol.is_a?(Array) ? (ncol[0] rescue cell) : cell
145
+ end
146
+ nested
147
+ when :sequence
148
+ cell.is_a?(Array) ? cell : Array(cell)
149
+ else
150
+ cell
151
+ end
152
+ end
153
+ private_class_method :materialize_field
154
+
69
155
  def compile(desc)
70
156
  desc.map do |n|
71
157
  { kind: KIND.fetch(n.fetch(:kind)),
data/lib/yeptris.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  module Yeptris
4
4
  # The gem's version lives in the parent namespace's file — the last
5
5
  # internal require (yeptris/version) retired with it.
6
- VERSION = "0.6.15.2".freeze
6
+ VERSION = "0.6.16.1".freeze
7
7
  # The error hierarchy lives in THIS file (the parent namespace's
8
8
  # own file): nested constants do not trigger a parent-constant
9
9
  # autoload, and the law forbids internal requires — defining the
data/libyeptris.so CHANGED
Binary file
@@ -3,7 +3,7 @@
3
3
  cmake_minimum_required(VERSION 3.20)
4
4
 
5
5
  project(yeptris
6
- VERSION 0.6.15
6
+ VERSION 0.6.16
7
7
  DESCRIPTION "Ultra-fast YAML 1.2 parser, emitter and streamer in C"
8
8
  LANGUAGES C CXX
9
9
  )
@@ -241,6 +241,9 @@ void dom_link(yep_dom* d, uint32_t parent, uint32_t child) {
241
241
  }
242
242
  p->last_child = child;
243
243
  p->count++;
244
+ /* every link changes SOME container's child list (#377's cache) */
245
+ d->child_cache_id = UINT32_MAX;
246
+ d->child_cache_len = 0;
244
247
  }
245
248
 
246
249
  /* ---- mutation side tables (64-2a) ---- */
@@ -485,6 +488,9 @@ yep_dom* yep_dom_create(const yep_allocator* sys) {
485
488
  * document that decode-only consumers (CBOR load, serialize, free)
486
489
  * never touch; yep_dom_handles() materializes it on first use */
487
490
  d->handles = NULL;
491
+ d->child_cache = NULL;
492
+ d->child_cache_id = UINT32_MAX;
493
+ d->child_cache_len = 0;
488
494
  if (yep_mutex_init(&d->midx.mu) != 0) {
489
495
  yep_pool_destroy(pool);
490
496
  yep_free(sys, d);
@@ -513,6 +519,69 @@ struct yep_hpool* yep_dom_handles(yep_dom* d) {
513
519
  return h;
514
520
  }
515
521
 
522
+ /* #377 (ruby #168): seq_at/map_at were O(i) sibling walks — the
523
+ * bindings' per-element loops made an 80k-row sequence quadratic
524
+ * (the relaton index: 238-328s where stdlib takes ~3s). The bulk
525
+ * drain (yeptris_node_children) fixed the bindings; THIS fixes the
526
+ * accessor: the last indexed container's child ids cache under the
527
+ * lazy-init mutex, O(1) after the first call. Invalidated by every
528
+ * child-list mutation (dom_invalidate_child_cache). */
529
+ void dom_invalidate_child_cache(yep_dom* d) {
530
+ if (d == NULL) {
531
+ return;
532
+ }
533
+ d->child_cache_id = UINT32_MAX;
534
+ d->child_cache_len = 0;
535
+ }
536
+
537
+ /* Returns child id at index (UINT32_MAX when out of range); the
538
+ * container's child count rides *count_out. */
539
+ uint32_t dom_indexed_child(yep_dom* d, uint32_t cid, size_t index, uint32_t* count_out) {
540
+ const yep_dnode* n = yep_dom_node(d, cid);
541
+ if (n == NULL) {
542
+ *count_out = 0;
543
+ return UINT32_MAX;
544
+ }
545
+ *count_out = n->count;
546
+ if (d->child_cache_id == cid) {
547
+ if (index < d->child_cache_len) {
548
+ return d->child_cache[index];
549
+ }
550
+ return UINT32_MAX;
551
+ }
552
+ /* miss: build once under the lazy-init mutex (Threads.
553
+ * ReadOnlySharing — concurrent first calls race here exactly like
554
+ * the handle pool's) */
555
+ yep_mutex_lock(&d->midx.mu);
556
+ if (d->child_cache_id != cid) {
557
+ yep_free(d->sys, d->child_cache);
558
+ d->child_cache = NULL;
559
+ d->child_cache_len = 0;
560
+ if (n->count > 0) {
561
+ uint32_t* arr = yep_alloc(d->sys, n->count * sizeof(uint32_t));
562
+ if (arr != NULL) {
563
+ uint32_t k = 0;
564
+ for (uint32_t id = n->first_child; id != UINT32_MAX && k < n->count;) {
565
+ const yep_dnode* cn = yep_dom_node(d, id);
566
+ if (cn == NULL) {
567
+ break;
568
+ }
569
+ arr[k++] = id;
570
+ id = cn->next_sibling;
571
+ }
572
+ d->child_cache = arr;
573
+ d->child_cache_len = k;
574
+ }
575
+ }
576
+ d->child_cache_id = cid;
577
+ }
578
+ yep_mutex_unlock(&d->midx.mu);
579
+ if (index < d->child_cache_len) {
580
+ return d->child_cache[index];
581
+ }
582
+ return UINT32_MAX;
583
+ }
584
+
516
585
  void yep_dom_destroy(yep_dom* d) {
517
586
  if (d == NULL) {
518
587
  return;
@@ -523,6 +592,7 @@ void yep_dom_destroy(yep_dom* d) {
523
592
  yep_free(d->sys, d->mut_att);
524
593
  yep_free(d->sys, d->mut_depth);
525
594
  yep_hpool_destroy(d->handles);
595
+ yep_free(d->sys, d->child_cache);
526
596
  yep_pool_destroy(d->pool);
527
597
  yep_free(d->sys, d);
528
598
  }
@@ -114,6 +114,13 @@ typedef struct yep_dom {
114
114
  * YeptrisNode wrappers here, so read-only document sharing across
115
115
  * threads is safe; parse-path pools stay single-threaded */
116
116
  struct yep_hpool* handles;
117
+ /* #377: the lazy indexed-children cache — child ids of the LAST
118
+ * container indexed via seq_at/map_at. Built once per container
119
+ * under the lazy-init mutex (read-only sharing), O(1) after;
120
+ * invalidated by every child-list mutation. NULL when idle. */
121
+ uint32_t* child_cache;
122
+ uint32_t child_cache_id; /* UINT32_MAX = empty */
123
+ uint32_t child_cache_len;
117
124
  yep_dnode* nodes;
118
125
  uint32_t ncount, ncap;
119
126
  uint32_t* docs; /* document root node ids */
@@ -200,6 +207,9 @@ void dom_mut_set_depth(yep_dom* d, uint32_t id, uint16_t depth);
200
207
  struct yep_hpool* yep_hpool_create(const yep_allocator* sys);
201
208
  /* Lazy handle-pool acquisition (#157): NULL dom or OOM stays NULL. */
202
209
  struct yep_hpool* yep_dom_handles(yep_dom* d);
210
+ /* #377: the lazy indexed-children cache (seq_at/map_at's O(1) leg) */
211
+ void dom_invalidate_child_cache(yep_dom* d);
212
+ uint32_t dom_indexed_child(yep_dom* d, uint32_t cid, size_t index, uint32_t* count_out);
203
213
  void yep_hpool_destroy(struct yep_hpool* p);
204
214
  void* yep_hpool_alloc(struct yep_hpool* p, size_t size, size_t align);
205
215
 
@@ -268,6 +268,8 @@ static int unlink_child(yep_dom* d, uint32_t parent, uint32_t child) {
268
268
  p->last_child = c;
269
269
  }
270
270
  }
271
+ d->child_cache_id = UINT32_MAX; /* #377: the list changed */
272
+ d->child_cache_len = 0;
271
273
  d->nodes[child].next_sibling = UINT32_MAX;
272
274
  dom_mut_set_att(d, child, 0);
273
275
  p->count--;
@@ -646,11 +646,12 @@ YEPTRIS_API YeptrisNode yeptris_node_seq_at(YeptrisNode handle, size_t index) {
646
646
  if (n == NULL || n->kind != YEP_DOM_SEQUENCE || index >= n->count) {
647
647
  return NULL;
648
648
  }
649
- const yep_dom* dom = ((yeptris_node*)handle)->doc->dom;
650
- uint32_t id = n->first_child;
651
- for (size_t i = 0; i < index && id != UINT32_MAX; i++) {
652
- const yep_dnode* cur = yep_dom_node(dom, id);
653
- id = cur ? cur->next_sibling : UINT32_MAX;
649
+ yep_dom* dom = ((yeptris_node*)handle)->doc->dom;
650
+ uint32_t count = 0;
651
+ /* the node id IS its dnode index */
652
+ uint32_t id = dom_indexed_child(dom, ((yeptris_node*)handle)->id, index, &count); /* #377 */
653
+ if (id == UINT32_MAX) {
654
+ return NULL;
654
655
  }
655
656
  return wrap((yeptris_node*)handle, id);
656
657
  }
@@ -685,10 +686,11 @@ YEPTRIS_API int yeptris_node_map_at(YeptrisNode handle, size_t index, YeptrisNod
685
686
  return -1;
686
687
  }
687
688
  yeptris_node* h = (yeptris_node*)handle;
688
- const yep_dom* d = h->doc->dom;
689
- uint32_t child = n->first_child;
690
- for (size_t i = 0; i < index * 2; i++) {
691
- child = d->nodes[child].next_sibling; /* pairs are key,value,… */
689
+ yep_dom* d = h->doc->dom;
690
+ uint32_t count = 0;
691
+ uint32_t child = dom_indexed_child(d, h->id, (size_t)index * 2, &count); /* #377 */
692
+ if (child == UINT32_MAX) {
693
+ return -1;
692
694
  }
693
695
  if (key != NULL) {
694
696
  *key = wrap(h, child);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yeptris
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.15.2
4
+ version: 0.6.16.1
5
5
  platform: aarch64-linux
6
6
  authors:
7
7
  - Ribose Inc.