lutaml-model 0.8.161 → 0.8.163

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: 40f892c8db0278f7a1dd5b88220134e6c42f0c14fa5371073ebd416be45f63d1
4
- data.tar.gz: 00ddc4bdb2656c805708036ac712a82375add79b1b93286f08d24ff34d526944
3
+ metadata.gz: d813c0b0e6ab90b0b51fa88e5fd6720d7bfa0f02adad8f4e3b4ec89d77c06fc5
4
+ data.tar.gz: 1bdc413c72e3a4c0ce4f25df8c58bbf6949a735a7bf8f06a59e3f20e40371008
5
5
  SHA512:
6
- metadata.gz: caa94d3c42642ad8e70569b85170b452e45323fd7411cb61594d5b185e772ae387d7d799835ac0250f6efa26666caf6bbb92c60cd1edb9ecd6eadaf5fce29cf2
7
- data.tar.gz: 36ab095bed9f7ef4b83679641f0ce47c98fe6b9517a3aa5e0a5ec0f8db7208a532e03e339ca9e8ba0dcd2759933c2232787080de69aa848031b6be2ce47ab897
6
+ metadata.gz: 5e473c2c1e9bbb3af04dcabaa2b3403af9a36dfdb57263b6bac055aa8958b16efcd665878096a64bfcdd850b24e241e3f60c6dc716da2a9438f89046eb3ee15f
7
+ data.tar.gz: 68d6a01a9afa35a963c91611dc0c44dfe179ca18a9f0187fd0c83a31b706ac322a4e7f553370545557b2775a70b9edb7107fa5c03a5e3be88ca45f43de6b0056
data/Gemfile CHANGED
@@ -49,7 +49,7 @@ gem "rexml"
49
49
  # exact-URI attribute rows, nested-attribute capture, wildcard rows,
50
50
  # walk-side ns retention and leniency, :unqualified rows, and the
51
51
  # address-based text attach all ride it.
52
- gem "leptris", ">= 1.9.351.1"
52
+ gem "leptris", ">= 1.9.406.0" # RAW op (leptris#1941): the packet fast path needs PLAN_OP_RAW + row identity
53
53
  gem "teptris"
54
54
  gem "yeptris" unless Gem.win_platform?
55
55
  # TODO: revert rng branch to main when lutaml/rng#32 is merged
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Deterministic KV materialization/serialization benchmark (campaign
4
+ # instrument; no external corpora). Measures from_yamls and to_yaml on
5
+ # a seeded synthetic corpus shaped like real registry documents:
6
+ # repeatable across machines and independent of downstream-gem drift.
7
+ #
8
+ # Usage: bundle exec ruby bench/bench_kv_materialization.rb [DOCS] [ITERATIONS]
9
+
10
+ require "bundler/setup"
11
+ require "lutaml/model"
12
+ require "lutaml/yaml"
13
+
14
+ Lutaml::Model::Config.configure do |config|
15
+ config.yaml_adapter_type = :standard
16
+ end
17
+
18
+ module KvBench
19
+ class Entry < Lutaml::Model::Serializable
20
+ attribute :name, :string
21
+ attribute :value, :integer
22
+ attribute :active, :boolean
23
+ attribute :tags, :string, collection: true
24
+
25
+ key_value do
26
+ map "name", to: :name
27
+ map "value", to: :value
28
+ map "active", to: :active
29
+ map "tags", to: :tags
30
+ end
31
+ end
32
+
33
+ class Meta < Lutaml::Model::Serializable
34
+ attribute :language, :string
35
+ attribute :source, :string
36
+ attribute :revision, :integer
37
+
38
+ key_value do
39
+ map "language", to: :language
40
+ map "source", to: :source
41
+ map "revision", to: :revision
42
+ end
43
+ end
44
+
45
+ class Doc < Lutaml::Model::Serializable
46
+ attribute :id, :string
47
+ attribute :status, :string, values: %w[draft published withdrawn]
48
+ attribute :score, :float
49
+ attribute :meta, Meta
50
+ attribute :entries, Entry, collection: true
51
+
52
+ key_value do
53
+ map "id", to: :id
54
+ map "status", to: :status
55
+ map "score", to: :score
56
+ map "meta", to: :meta
57
+ map "entries", to: :entries
58
+ end
59
+ end
60
+
61
+ module_function
62
+
63
+ def corpus(docs, seed: 42)
64
+ rng = Random.new(seed)
65
+ Array.new(docs) do |i|
66
+ {
67
+ "id" => "doc-#{i}",
68
+ "status" => %w[draft published withdrawn][rng.rand(3)],
69
+ "score" => (rng.rand * 100).round(3),
70
+ "meta" => {
71
+ "language" => %w[en fr ja][rng.rand(3)],
72
+ "source" => "src-#{rng.rand(20)}",
73
+ "revision" => rng.rand(50),
74
+ },
75
+ "entries" => Array.new(2 + rng.rand(4)) do |j|
76
+ {
77
+ "name" => "entry-#{i}-#{j}",
78
+ "value" => rng.rand(10_000),
79
+ "active" => rng.rand(2).zero?,
80
+ "tags" => Array.new(1 + rng.rand(3)) { |t| "tag-#{rng.rand(30)}-#{t}" },
81
+ }
82
+ end,
83
+ }
84
+ end
85
+ end
86
+
87
+ def measure(label, docs, iterations)
88
+ # Warm caches once (docs are already-serialized YAML strings for
89
+ # the from_yaml lane, model instances for the to_yaml lane).
90
+ docs.first(10).each { |d| d.is_a?(::String) ? Doc.from_yaml(d) : Doc.from_yaml(d.to_yaml) }
91
+
92
+ times = []
93
+ allocs = []
94
+ iterations.times do
95
+ GC.start
96
+ a0 = GC.stat[:total_allocated_objects]
97
+ t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
98
+ yield
99
+ t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
100
+ a1 = GC.stat[:total_allocated_objects]
101
+ times << (t1 - t0)
102
+ allocs << (a1 - a0)
103
+ end
104
+ ms = times.min * 1000 / docs.size
105
+ alloc = allocs.min.to_f / docs.size
106
+ puts format("%<label>-18s %<ms>7.3f ms/doc %<alloc>8.1f allocs/doc",
107
+ label: label, ms: ms, alloc: alloc)
108
+ end
109
+ end
110
+
111
+ if $PROGRAM_NAME == __FILE__
112
+ docs_n = (ARGV[0] || 400).to_i
113
+ iters = (ARGV[1] || 3).to_i
114
+ raws = KvBench.corpus(docs_n)
115
+ yamls = raws.map(&:to_yaml)
116
+ parsed = yamls.map { |y| KvBench::Doc.from_yaml(y) }
117
+
118
+ puts "kv materialization bench: #{docs_n} docs, #{iters} iters"
119
+ KvBench.measure("from_yaml", yamls, iters) do
120
+ yamls.each { |y| KvBench::Doc.from_yaml(y) }
121
+ end
122
+ KvBench.measure("to_yaml", parsed, iters) do
123
+ parsed.each(&:to_yaml)
124
+ end
125
+ end
@@ -195,6 +195,7 @@ require "lutaml/yaml/format"
195
195
  require "lutaml/yaml/schema"
196
196
  require "lutaml/yamlld/adapter"
197
197
  require "lutaml/yamls/adapter"
198
+ require "lutaml/yamls/adapter_loader"
198
199
  require "lutaml/yamls/format"
199
200
  require "lutaml/hash_format/adapter/document"
200
201
  require "lutaml/hash_format/adapter/mapping"
@@ -437,6 +438,7 @@ require "lutaml/yamls/adapter/standard_adapter"
437
438
  require "lutaml/yamls/adapter/transform"
438
439
  require "lutaml/yamls/adapter/yamls_sequence"
439
440
  require "lutaml/yamls/adapter/yamls_sequence_rule"
441
+ require "lutaml/yamls/adapter/yeptris_adapter"
440
442
  require "lutaml/key_value/adapter/hash/document"
441
443
  require "lutaml/key_value/adapter/hash/mapping"
442
444
  require "lutaml/key_value/adapter/hash/mapping_rule"
@@ -263,8 +263,19 @@ module Lutaml
263
263
  end
264
264
 
265
265
  def mappings(register_id = nil)
266
- ensure_mappings_imported!(register_id) if finalized?
267
- mappings_hash(register_id).values.flatten
266
+ unless finalized?
267
+ return mappings_hash(register_id).values.flatten
268
+ end
269
+
270
+ # Post-finalize the rule list is immutable: imports resolve
271
+ # before this memo is built (the guard runs first), the DSL only
272
+ # mutates during class definition, and the writer clears the
273
+ # memo. The per-call `.values.flatten` (plus the merged hash for
274
+ # non-default registers) was the serialize lane's top our-side
275
+ # allocation line on the synthetic KV corpus.
276
+ ensure_mappings_imported!(register_id)
277
+ (@flattened_mappings ||= {})[register_id] ||=
278
+ mappings_hash(register_id).values.flatten.freeze
268
279
  end
269
280
 
270
281
  def mappings_hash(register_id = nil)
@@ -351,7 +351,9 @@ child_mappings)
351
351
  # index and the pair arrays were the lane's biggest allocation
352
352
  # (~6 arrays per node on hydrate-heavy corpora).
353
353
  values = Array.new(rows.size, MISSING)
354
- children = []
354
+ # Lazy: the scalar hot path never pushes a child; the shared
355
+ # wiring pass tolerates nil.
356
+ children = nil
355
357
  # Lazy: the present-key hot path never pushes, and this runs
356
358
  # once per hydrated model instance.
357
359
  absent = nil
@@ -397,7 +399,7 @@ child_mappings)
397
399
  child_hash, format, { mappings: type.child_rules })
398
400
  end
399
401
  values[i] = built
400
- children.concat(built)
402
+ (children ||= []).concat(built)
401
403
  i += 1
402
404
  next
403
405
  end
@@ -427,7 +429,7 @@ child_hash, format, { mappings: type.child_rules })
427
429
  built << child
428
430
  end
429
431
  values[i] = built
430
- children.concat(built)
432
+ (children ||= []).concat(built)
431
433
  else
432
434
  return nil unless v.is_a?(::Hash)
433
435
 
@@ -437,23 +439,26 @@ child_hash, format, { mappings: type.child_rules })
437
439
  return nil if child.nil?
438
440
 
439
441
  values[i] = child
440
- children << child
442
+ (children ||= []) << child
441
443
  end
442
444
  i += 1
443
445
  end
444
446
  instance = model_class.allocate_for_walk(register)
445
447
  i = 0
446
- rows.each do |rule, _attr, kind, *_rest|
448
+ # Index access, not block-parameter splat: `*_rest` re-sliced a
449
+ # fresh Array per row per instance (the walk's largest our-side
450
+ # allocation line on the synthetic KV corpus).
451
+ rows.each do |row|
447
452
  v = values[i]
448
453
  i += 1
449
- next if v.equal?(MISSING) || kind == :unmapped
454
+ next if v.equal?(MISSING) || row[2] == :unmapped
450
455
 
451
- instance.public_send(rule.kv_setter, v)
456
+ instance.public_send(row[0].kv_setter, v)
452
457
  end
453
458
  absent&.each do |rule|
454
459
  process_mapping_rule(doc, instance, format, rule, options, nil)
455
460
  end
456
- children.each do |child|
461
+ children&.each do |child|
457
462
  child.lutaml_parent = instance
458
463
  child.lutaml_root ||= instance.lutaml_root || instance
459
464
  end
@@ -1265,8 +1265,16 @@ child_mappings, options)
1265
1265
  return value
1266
1266
  end
1267
1267
 
1268
- wrapped_value = type.new(value)
1269
- wrapped_value.public_send(:"to_#{format}")
1268
+ # Known KV formats resolve without allocating (the instance
1269
+ # freezes after compile, so no per-call ivar memo is possible;
1270
+ # the interpolated fallback covers exotic formats).
1271
+ reader = case format
1272
+ when :yaml then :to_yaml
1273
+ when :json then :to_json
1274
+ when :toml then :to_toml
1275
+ else :"to_#{format}"
1276
+ end
1277
+ type.new(value).public_send(reader)
1270
1278
  else
1271
1279
  value
1272
1280
  end
@@ -416,23 +416,35 @@ module Lutaml
416
416
  end
417
417
 
418
418
  # Native key-value engine preference: yeptris (libyeptris C11)
419
- # when its gem is in the bundle AND the native engine answers a
420
- # probe, else the format's standard adapter. Opt-in by bundle
421
- # contents — nothing is required of consumers that do not add
422
- # yeptris. The probe matters because a half-initialized Yeptris
423
- # constant (the poison-process state the gem documents) passes
424
- # const_defined? and require while every parse fails; without
425
- # it the lane selection flips with process load order and the
426
- # failure surfaces as a native parse error on valid input
427
- # instead of a clean standard-lane parse (#978).
419
+ # when the host explicitly opts in with FONTIST_YAML=yeptris
420
+ # (the switch fontist and lutaml-store share) and the native
421
+ # engine answers a probe, else the format's standard adapter.
422
+ # Detection used to flip the lane for any process whose bundle
423
+ # merely contained yeptris — typically through a transitive
424
+ # dependency — which silently switched YAML parsing and dumping
425
+ # (including line-wrap behavior) for hosts that never chose the
426
+ # engine, and the yeptris YAML build still mis-parses some plain
427
+ # scalars as single-element Arrays on several platforms
428
+ # (leptris/yeptris#863), so bundle-presence selection is not a
429
+ # safe default. The probe matters because a half-initialized
430
+ # Yeptris constant (the poison-process state the gem documents)
431
+ # passes const_defined? and require while every parse fails;
432
+ # without it the lane selection flips with process load order
433
+ # and the failure surfaces as a native parse error on valid
434
+ # input instead of a clean standard-lane parse (#978).
428
435
  def detect_yeptris_kv_adapter(format)
429
436
  default = metadata.dig(format, :default)
430
437
  return default if Lutaml::Model.opal?
438
+ return default unless yeptris_opted_in?
431
439
  return default unless Utils.safe_load("yeptris", :Yeptris)
432
440
 
433
441
  load_adapter(format, :yeptris).available? ? :yeptris : default
434
442
  end
435
443
 
444
+ def yeptris_opted_in?
445
+ ENV["FONTIST_YAML"] == "yeptris"
446
+ end
447
+
436
448
  # Detect available XML adapter.
437
449
  #
438
450
  # Delegates to moxml which is the authority on XML adapter
@@ -16,7 +16,14 @@ module Lutaml
16
16
  nil
17
17
  end
18
18
 
19
- attr_writer :mappings
19
+ # Assignments invalidate the per-register flattened-mapping memo
20
+ # (Key-Value mappings memoize `mappings`; the writer is the only
21
+ # post-construction mutation seam — deep_dup builds a fresh
22
+ # instance and reaches this writer).
23
+ def mappings=(value)
24
+ @flattened_mappings = nil
25
+ @mappings = value
26
+ end
20
27
 
21
28
  def initialize
22
29
  @mappings = []
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.161"
5
+ VERSION = "0.8.163"
6
6
  end
7
7
  end