lutaml-model 0.8.158 → 0.8.160

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.
@@ -0,0 +1,328 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Model
5
+ module Lml
6
+ # Reads a model class's declaration surfaces — the Attribute
7
+ # registry and the TransformationRegistry-resolved per-format
8
+ # mappings — and projects them into the neutral Document
9
+ # (lutaml-model#1033 Phase 1).
10
+ #
11
+ # Only public introspection is used: `Klass.attributes(register)`,
12
+ # `Klass.mappings_for(format, register)`, `Klass.mappings` (the
13
+ # declared-format keys), `Lutaml::Model::Type`'s builtin registry.
14
+ class Exporter
15
+ SERIALIZABLE_BASE = Lutaml::Model::Serialize
16
+
17
+ def self.export(klass, register: nil)
18
+ new(klass, register: register).export
19
+ end
20
+
21
+ def initialize(klass, register: nil)
22
+ @klass = klass
23
+ # A nil caller register resolves through the model's own
24
+ # declared default register (lutaml_default_register) — the
25
+ # same precedence mappings_for applies internally, needed
26
+ # explicitly here because attributes/type resolution takes
27
+ # the register literally.
28
+ @register = register ||
29
+ (klass.lutaml_default_register if klass.respond_to?(:lutaml_default_register))
30
+ end
31
+
32
+ def export
33
+ reject_non_serializable!
34
+ reject_custom_initialize!
35
+ Document.new(
36
+ model_name: @klass.name,
37
+ register: @register,
38
+ attributes: @klass.attributes(@register).map { |_, attr| project_attribute(attr) },
39
+ mappings: project_mappings,
40
+ )
41
+ end
42
+
43
+ private
44
+
45
+ def reject_non_serializable!
46
+ return if @klass.is_a?(Class) && @klass.include?(SERIALIZABLE_BASE)
47
+
48
+ raise RejectedError.new(:not_serializable, @klass,
49
+ "the class does not include Lutaml::Model::Serializable")
50
+ end
51
+
52
+ # A custom `initialize` is constructor-contract: its body is not
53
+ # a declaration. The exported document records nothing about it;
54
+ # the model must be rejected so the schema stays honest.
55
+ #
56
+ # The framework prepends Serialize::Builder onto Serializable at
57
+ # boot, so every plain model's initialize is owned by Builder.
58
+ # The ownership check therefore walks the ancestry: the owner is
59
+ # custom only when it sits between the model and its
60
+ # Serializable boundary (the class itself or a module it
61
+ # included).
62
+ def reject_custom_initialize!
63
+ owner = @klass.instance_method(:initialize).owner
64
+ ancestors = @klass.ancestors
65
+ boundary = ancestors.index(Lutaml::Model::Serializable)
66
+ owner_index = owner && ancestors.index(owner)
67
+ return if boundary.nil? || owner_index.nil? || owner_index >= boundary
68
+
69
+ raise RejectedError.new(:custom_initialize, @klass,
70
+ "initialize is owned by #{owner}; the body stays hand-written")
71
+ end
72
+
73
+ def project_attribute(attr)
74
+ {
75
+ "name" => attr.name.to_s,
76
+ "type" => type_name(attr),
77
+ "collection" => collection_field(attr),
78
+ "default" => static_default(attr),
79
+ "required" => true_or_nil(attr.options[:required]),
80
+ "values" => enum_field(attr),
81
+ "initialize_empty" => true_or_nil(attr.initialize_empty?),
82
+ "polymorphic_class" => class_path(attr.options[:polymorphic_class]),
83
+ "reference" => reference_field(attr),
84
+ }.compact
85
+ end
86
+
87
+ def enum_field(attr)
88
+ values = attr.enum_values
89
+ return nil if values.nil? || values.empty?
90
+
91
+ values.map(&:to_s)
92
+ end
93
+
94
+ def type_name(attr)
95
+ resolved =
96
+ begin
97
+ attr.type(@register)
98
+ rescue UnknownTypeError
99
+ # Deferred model references: consumers that declare
100
+ # `attribute :x, "Ns::Model"` by string path resolve
101
+ # through autoload, which the type resolver refuses to
102
+ # materialize mid-finalization. Exporting happens
103
+ # post-definition — materialize here.
104
+ const_get_or_nil(attr.unresolved_type.to_s)
105
+ end
106
+ if resolved.nil?
107
+ raise RejectedError.new(:unknown_type, @klass,
108
+ "attribute #{attr.name} has an unresolved type")
109
+ end
110
+
111
+ builtin_name(resolved) || model_name_for(resolved, attr)
112
+ end
113
+
114
+ def const_get_or_nil(path)
115
+ Object.const_get(path)
116
+ rescue NameError
117
+ nil
118
+ end
119
+
120
+ def builtin_name(resolved)
121
+ builtin_registry.find { |_, klass| klass == resolved }&.first&.to_s
122
+ end
123
+
124
+ def builtin_registry
125
+ @builtin_registry ||= begin
126
+ registry = TypeRegistry.new
127
+ Type.register_builtin_types_in(registry)
128
+ registry.types
129
+ end
130
+ end
131
+
132
+ def model_name_for(resolved, attr)
133
+ return resolved.name if resolved.is_a?(Class) &&
134
+ resolved.include?(SERIALIZABLE_BASE) && resolved.name
135
+
136
+ raise RejectedError.new(:unknown_type, @klass,
137
+ "attribute #{attr.name}: #{resolved} has no LML name")
138
+ end
139
+
140
+ def collection_field(attr)
141
+ return nil unless attr.collection?
142
+
143
+ range = attr.resolved_collection
144
+ return true if range.nil?
145
+
146
+ max = range.max
147
+ max = nil if max.respond_to?(:infinite?) && max.infinite?
148
+ { "min" => range.min, "max" => max }.compact
149
+ end
150
+
151
+ def static_default(attr)
152
+ case attr.default_kind
153
+ when :none
154
+ nil
155
+ when :static
156
+ default = attr.options[:default]
157
+ if default.is_a?(Float) && !default.finite?
158
+ # JSON's canonical form cannot carry Infinity/NaN.
159
+ raise RejectedError.new(:non_serializable_default, @klass,
160
+ "attribute #{attr.name}: default #{default.inspect}")
161
+ end
162
+ default
163
+ when :delegate
164
+ raise RejectedError.new(:lambda_default, @klass,
165
+ "attribute #{attr.name}: delegated defaults are not declarative")
166
+ else # :proc
167
+ raise RejectedError.new(:lambda_default, @klass,
168
+ "attribute #{attr.name}: defaults must be constants in the schema")
169
+ end
170
+ end
171
+
172
+ def reference_field(attr)
173
+ ref = attr.options[:ref_model_class]
174
+ return nil unless ref
175
+
176
+ { "model" => class_path(ref) }.compact
177
+ end
178
+
179
+ def class_path(value)
180
+ return nil if value.nil?
181
+ return value.name if value.is_a?(Class)
182
+
183
+ value.to_s
184
+ end
185
+
186
+ def true_or_nil(value)
187
+ value ? true : nil
188
+ end
189
+
190
+ def project_mappings
191
+ # Export per RESOLVED format, not per DSL block: the
192
+ # key_value/rdf DSL fan one block out across the registered
193
+ # formats, and each resolved format carries its own
194
+ # projection.
195
+ @klass.mappings.each_with_object({}) do |(format, _), out|
196
+ out[format.to_s] = project_format(format)
197
+ end
198
+ end
199
+
200
+ def project_format(format)
201
+ mapping = @klass.mappings_for(format, @register)
202
+ if mapping.respond_to?(:root_element)
203
+ project_xml_mapping(mapping)
204
+ else
205
+ project_key_value_mapping(mapping)
206
+ end
207
+ end
208
+
209
+ def project_xml_mapping(mapping)
210
+ reject_imperative_element_order!(mapping)
211
+ sequence = mapping.element_sequence
212
+ sequence = nil if sequence.is_a?(Array) && sequence.empty?
213
+ {
214
+ "root" => root_field(mapping),
215
+ "ordered" => true_or_nil(mapping.ordered?),
216
+ "mixed_content" => true_or_nil(mapping.mixed_content?),
217
+ "sequence" => sequence&.map(&:to_s),
218
+ "namespace_uri" => mapping.namespace_uri,
219
+ "namespace_prefix" => mapping.namespace_prefix,
220
+ "rules" => mapping.mappings.map { |rule| project_xml_rule(rule) },
221
+ }.compact
222
+ end
223
+
224
+ def reject_imperative_element_order!(mapping)
225
+ sequence = mapping.element_sequence
226
+ return if !mapping.ordered? || (sequence.is_a?(Array) && !sequence.empty?)
227
+
228
+ raise RejectedError.new(:imperative_element_order, @klass,
229
+ "ordered content without a declared sequence is instance-driven")
230
+ end
231
+
232
+ def root_field(mapping)
233
+ return nil if mapping.root_element.nil?
234
+
235
+ {
236
+ "element" => mapping.root_element,
237
+ "mixed" => true_or_nil(mapping.mixed_content?),
238
+ }.compact
239
+ end
240
+
241
+ def project_xml_rule(rule)
242
+ if rule.name.nil?
243
+ # map_content: the content catch-all carries no name.
244
+ return {
245
+ "kind" => "content",
246
+ "to" => rule.to&.to_s,
247
+ "cdata" => true_or_nil(rule.cdata),
248
+ "render_nil" => render_nil_field(rule),
249
+ }.compact
250
+ end
251
+
252
+ {
253
+ "kind" => rule.attribute ? "attribute" : "element",
254
+ "name" => Array(rule.name).map(&:to_s),
255
+ "to" => rule.to&.to_s,
256
+ "namespace_uri" => xml_rule_namespace(rule),
257
+ "prefix" => rule.prefix&.to_s,
258
+ "cdata" => true_or_nil(rule.cdata),
259
+ "render_nil" => render_nil_field(rule),
260
+ "polymorphic_map" => polymorphic_map_field(rule),
261
+ }.compact
262
+ end
263
+
264
+ def xml_rule_namespace(rule)
265
+ namespace = rule.namespace
266
+ return nil unless namespace.is_a?(String) || namespace.is_a?(Symbol)
267
+
268
+ namespace.to_s
269
+ end
270
+
271
+ def render_nil_field(rule)
272
+ value = rule.render_nil
273
+ return nil unless value && value != false
274
+
275
+ value == true ? true : value.to_s
276
+ end
277
+
278
+ def polymorphic_map_field(rule)
279
+ return nil unless rule.polymorphic_map.is_a?(Hash) && rule.polymorphic_map.any?
280
+
281
+ rule.polymorphic_map.to_h { |key, value| [class_path(key), value.to_s] }
282
+ end
283
+
284
+ def project_key_value_mapping(mapping)
285
+ rules = mapping.mappings.map { |rule| project_key_value_rule(rule) }
286
+ {
287
+ "rules" => rules,
288
+ "root_mappings" => root_mappings_field(mapping),
289
+ }.compact
290
+ end
291
+
292
+ def root_mappings_field(mapping)
293
+ return nil unless mapping.respond_to?(:root_mappings)
294
+
295
+ root_mappings = mapping.root_mappings
296
+ return nil unless root_mappings.is_a?(Hash) && root_mappings.any?
297
+
298
+ root_mappings.to_h { |key, value| [key.to_s, value.to_s] }
299
+ end
300
+
301
+ def project_key_value_rule(rule)
302
+ {
303
+ "key" => Array(rule.name).map(&:to_s),
304
+ "to" => rule.to&.to_s,
305
+ "render_nil" => render_nil_field(rule),
306
+ "child_mappings" => child_mappings_field(rule),
307
+ }.compact
308
+ end
309
+
310
+ def child_mappings_field(rule)
311
+ child_mappings = rule.child_mappings
312
+ return nil unless child_mappings.is_a?(Hash) && child_mappings.any?
313
+
314
+ child_mappings.to_h { |key, value| [key.to_s, canonicalize_value(value)] }
315
+ end
316
+
317
+ def canonicalize_value(value)
318
+ case value
319
+ when Hash then value.to_h { |key, item| [key.to_s, canonicalize_value(item)] }
320
+ when Array then value.map { |item| canonicalize_value(item) }
321
+ when Symbol then value.to_s
322
+ else value
323
+ end
324
+ end
325
+ end
326
+ end
327
+ end
328
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Model
5
+ module Lml
6
+ # A named rejection from the exporter: the construct is
7
+ # constructor-contract, not schema, and has no declarative
8
+ # equivalent in the LML document (lutaml-model#1033 design
9
+ # decision #3).
10
+ class RejectedError < StandardError
11
+ attr_reader :reason, :klass, :detail
12
+
13
+ # +reason+ is one of:
14
+ # :lambda_default — a default given as a Proc
15
+ # :custom_initialize — the model overrides `initialize`
16
+ # :imperative_element_order — ordered content with no
17
+ # declarative sequence
18
+ # :unknown_type — a type with no registry name
19
+ # :not_serializable — the class does not include
20
+ # Serializable
21
+ def initialize(reason, klass, detail = nil)
22
+ @reason = reason
23
+ @klass = klass
24
+ @detail = detail
25
+ message = "cannot export #{klass} to LML: #{reason}"
26
+ message += " (#{detail})" if detail
27
+ super(message)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Model
5
+ module Lml
6
+ # The round-trip gate: the document must re-derive the model it
7
+ # came from. Phase 1's importer-side is a re-export comparison —
8
+ # the recorded model is re-read and re-projected, and the
9
+ # canonical forms must be identical (equal checksums). 1:1-ness
10
+ # is the design's spine; a drift here means the document lost a
11
+ # declaration on the way out.
12
+ class RoundTrip
13
+ def self.verify(document, register: nil)
14
+ new(document, register: register).verify
15
+ end
16
+
17
+ def initialize(document, register: nil)
18
+ @document = document
19
+ @register = register
20
+ end
21
+
22
+ def verify
23
+ klass = resolve_model!
24
+ reexported = Exporter.export(klass, register: @register)
25
+ return reexported if reexported.canonical_json == @document.canonical_json
26
+
27
+ raise RejectedError.new(:round_trip_mismatch, klass,
28
+ "the document does not re-derive the model " \
29
+ "(#{short(@document.checksum)} vs #{short(reexported.checksum)})")
30
+ end
31
+
32
+ private
33
+
34
+ def resolve_model!
35
+ name = @document.model_name
36
+ begin
37
+ Object.const_get(name)
38
+ rescue NameError
39
+ raise RejectedError.new(:unknown_type, name, "the recorded model is not loadable")
40
+ end
41
+ end
42
+
43
+ def short(checksum)
44
+ checksum.to_s.slice(0, 20)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Model
5
+ module Lml
6
+ autoload :Document, "#{__dir__}/lml/document"
7
+ autoload :Exporter, "#{__dir__}/lml/exporter"
8
+ autoload :RejectedError, "#{__dir__}/lml/rejected_error"
9
+ autoload :RoundTrip, "#{__dir__}/lml/round_trip"
10
+
11
+ module_function
12
+
13
+ # Export a model class's declarations to a neutral LML document
14
+ # (lutaml-model#1033 Phase 1).
15
+ #
16
+ # The document is the model's `attribute` + per-format mapping
17
+ # vocabulary, serialized with sorted keys and pinned by a sha256
18
+ # checksum over the canonical form — the same
19
+ # "declare once, verify everywhere" move PARG made for grammars.
20
+ #
21
+ # Constructor-contract constructs are rejected with named errors
22
+ # (RejectedError): lambda defaults, custom `initialize` bodies,
23
+ # imperative element-order mixes.
24
+ #
25
+ # @param klass [Class] a Serializable model class
26
+ # @param register [Symbol, nil] the register context
27
+ # @return [Lml::Document]
28
+ def export(klass, register: nil)
29
+ Exporter.export(klass, register: register)
30
+ end
31
+
32
+ # The round-trip gate: re-export the model from the document's
33
+ # recorded model name and compare canonical forms — identical
34
+ # declarations, identical checksum.
35
+ def verify(document, register: nil)
36
+ RoundTrip.verify(document, register: register)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -48,9 +48,15 @@ module Lutaml
48
48
  # Cache for resolve_for_child results.
49
49
  # Key: [child_class.object_id, parent_register] → Value: resolved register Symbol or nil.
50
50
  # Classes are singletons so object_id is stable for the process lifetime.
51
+ # Concurrent is required here (not at boot): requiring it while a
52
+ # require is still in flight makes concurrent-ruby's optional
53
+ # native-extension requires raise inside RubyGems' unresolved-tree
54
+ # resolution, which walks every installed gem on gem-heavy machines.
51
55
  RESOLVE_CACHE = if Lutaml::Model.opal?
52
56
  {}
53
57
  else
58
+ Lutaml::Model::RuntimeCompatibility
59
+ .require_native("concurrent")
54
60
  Concurrent::Map.new
55
61
  end
56
62
 
@@ -134,6 +134,10 @@ module Lutaml
134
134
  # @param collection [Boolean] Whether the enum is a collection
135
135
  def add_enum_setter_if_not_defined(klass, enum_name, _values,
136
136
  collection)
137
+ # Static per-definition symbols close over instead of being
138
+ # minted per setter call (11k+ per corpus parse).
139
+ ivar = :"@#{enum_name}"
140
+ reader = enum_name.to_sym
137
141
  Utils.add_method_if_not_defined(klass, "#{enum_name}=") do |value|
138
142
  value = [] if value.nil?
139
143
  value = [value] if !value.is_a?(Array)
@@ -141,16 +145,16 @@ module Lutaml
141
145
  value_set_for(enum_name)
142
146
 
143
147
  if collection
144
- curr_value = public_send(:"#{enum_name}")
148
+ curr_value = public_send(reader)
145
149
 
146
150
  # Build a new Array rather than appending into the stored one.
147
151
  # The reader hands back the real Array now, so `curr_value` may
148
152
  # be this model's own collection — or, when the model defines
149
153
  # its own reader, something shared between instances or not an
150
154
  # Array at all. Duplicates are the reader's job either way.
151
- instance_variable_set(:"@#{enum_name}", curr_value + value)
155
+ instance_variable_set(ivar, curr_value + value)
152
156
  else
153
- instance_variable_set(:"@#{enum_name}", value)
157
+ instance_variable_set(ivar, value)
154
158
  end
155
159
  end
156
160
  end
@@ -111,6 +111,12 @@ module Lutaml
111
111
  # Opal strings are JS strings with no per-string encoding;
112
112
  # String#encode there doesn't accept MRI's keyword options.
113
113
  return value if Lutaml::Model.opal?
114
+ # The encode-with-options forces a fresh copy per value even
115
+ # when the string is already valid UTF-8 — the overwhelming
116
+ # case on parsed documents. Scan instead: no allocation on
117
+ # the happy path; encode still runs for genuinely invalid
118
+ # input.
119
+ return value if value.encoding == Encoding::UTF_8 && value.valid_encoding?
114
120
 
115
121
  value.encode("UTF-8", invalid: :replace, undef: :replace,
116
122
  replace: "")
@@ -2,8 +2,6 @@
2
2
 
3
3
  require_relative "runtime_compatibility"
4
4
 
5
- Lutaml::Model::RuntimeCompatibility.require_native("concurrent")
6
-
7
5
  module Lutaml
8
6
  module Model
9
7
  # Registry for managing transformation lifecycle and caching.
@@ -89,6 +87,12 @@ module Lutaml
89
87
  end
90
88
 
91
89
  def initialize
90
+ # Required lazily: requiring "concurrent" while a require is still
91
+ # in flight makes concurrent-ruby's optional native-extension
92
+ # requires raise inside RubyGems' unresolved-tree resolution,
93
+ # which walks every installed gem on gem-heavy machines and can
94
+ # add minutes to process boot
95
+ Lutaml::Model::RuntimeCompatibility.require_native("concurrent")
92
96
  @transformations = {} # Cache for transformation instances (mutex-protected for cycle detection)
93
97
  @mappings = if Lutaml::Model.opal?
94
98
  {}
@@ -197,6 +201,7 @@ module Lutaml
197
201
  # Inner cache level container matching the outer map's engine
198
202
  # (Concurrent::Map under GVL-threaded MRI, plain Hash under Opal).
199
203
  def new_inner_map
204
+ Lutaml::Model::RuntimeCompatibility.require_native("concurrent")
200
205
  Lutaml::Model.opal? ? {} : ::Concurrent::Map.new
201
206
  end
202
207
 
@@ -271,20 +271,25 @@ module Lutaml
271
271
  end
272
272
 
273
273
  def add_getter_if_not_defined(klass, attribute)
274
+ # The ivar symbol closes over once per definition instead of
275
+ # being minted per call (14k+ per corpus parse on the setter).
276
+ ivar = :"@__#{attribute}"
274
277
  add_method_if_not_defined(klass, attribute) do
275
- instance_variable_get(:"@__#{attribute}")
278
+ instance_variable_get(ivar)
276
279
  end
277
280
  end
278
281
 
279
282
  def add_boolean_getter_if_not_defined(klass, attribute)
283
+ ivar = :"@__#{attribute}"
280
284
  add_method_if_not_defined(klass, "#{attribute}?") do
281
- !!instance_variable_get(:"@__#{attribute}")
285
+ !!instance_variable_get(ivar)
282
286
  end
283
287
  end
284
288
 
285
289
  def add_setter_if_not_defined(klass, attribute)
290
+ ivar = :"@__#{attribute}"
286
291
  add_method_if_not_defined(klass, "#{attribute}=") do |value|
287
- instance_variable_set(:"@__#{attribute}", value)
292
+ instance_variable_set(ivar, value)
288
293
  end
289
294
  end
290
295
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.158"
5
+ VERSION = "0.8.160"
6
6
  end
7
7
  end
data/lib/lutaml/model.rb CHANGED
@@ -78,6 +78,7 @@ module Lutaml
78
78
  autoload :JsonAdapter, "#{__dir__}/model/json_adapter"
79
79
  autoload :SchemaLocation, "#{__dir__}/model/schema_location"
80
80
  autoload :Validation, "#{__dir__}/model/validation"
81
+ autoload :Lml, "#{__dir__}/model/lml"
81
82
  autoload :Choice, "#{__dir__}/model/choice"
82
83
  autoload :Sequence, "#{__dir__}/model/sequence"
83
84
  autoload :Organization, "#{__dir__}/model/organization"
@@ -133,26 +133,6 @@ module Lutaml
133
133
  raise NotImplementedError, "#{self.class} must implement #adapter_class"
134
134
  end
135
135
 
136
- def node_attributes(node)
137
- return {} unless node.is_a?(Moxml::Element)
138
-
139
- node.attributes.each_with_object({}) do |attr, hash|
140
- next if attr_is_namespace?(attr)
141
-
142
- ns_prefix = attr.namespace&.prefix
143
- ns_prefix = nil if ns_prefix && ns_prefix.empty?
144
-
145
- attr_name = ns_prefix ? "#{ns_prefix}:#{attr.name}" : attr.name
146
-
147
- hash[attr_name] = XmlAttribute.new(
148
- attr_name,
149
- attribute_value_for_build(attr),
150
- namespace: ns_prefix ? attr.namespace&.uri : nil,
151
- namespace_prefix: ns_prefix,
152
- )
153
- end
154
- end
155
-
156
136
  # Shared frozen empties: the parse path hands these straight into
157
137
  # XmlElement (which never mutates the containers it is given), so
158
138
  # attribute-less elements — the majority in real documents — build
@@ -161,6 +141,21 @@ module Lutaml
161
141
  NO_CHILDREN = [].freeze # rubocop:todo Lint/UselessConstantScoping
162
142
  EMPTY_ATTRIBUTES = [{}, nil].freeze # rubocop:todo Lint/UselessConstantScoping
163
143
 
144
+ # Interned "prefix:name" attribute spellings. Attribute names
145
+ # repeat across every same-shaped node in a document (hundreds of
146
+ # distinct spellings against millions of nodes), so one cached
147
+ # composite string replaces one interpolated string per attribute
148
+ # per node. Guarded: parses may run on parallel threads; the
149
+ # cache is bounded by the distinct spellings.
150
+ ATTR_NAME_LOCK = Mutex.new # rubocop:todo Lint/UselessConstantScoping
151
+ ATTR_NAME_INTERN = {} # rubocop:todo Style/MutableConstant, Lint/UselessConstantScoping
152
+
153
+ def prefixed_attr_name(ns_prefix, name)
154
+ ATTR_NAME_LOCK.synchronize do
155
+ (ATTR_NAME_INTERN[ns_prefix] ||= {})[name] ||= "#{ns_prefix}:#{name}"
156
+ end
157
+ end
158
+
164
159
  def node_attributes_with_order(node)
165
160
  return EMPTY_ATTRIBUTES unless node.is_a?(Moxml::Element)
166
161
 
@@ -174,7 +169,7 @@ module Lutaml
174
169
  ns_prefix = attr.namespace&.prefix
175
170
  ns_prefix = nil if ns_prefix && ns_prefix.empty?
176
171
 
177
- attr_name = ns_prefix ? "#{ns_prefix}:#{attr.name}" : attr.name
172
+ attr_name = ns_prefix ? prefixed_attr_name(ns_prefix, attr.name) : attr.name
178
173
  order << attr_name
179
174
 
180
175
  h[attr_name] = XmlAttribute.new(