moxml 0.6.2 → 0.6.4

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.
@@ -33,6 +33,75 @@ module Moxml
33
33
  # the README's "older bindings do not load" claim is now true).
34
34
  MINIMUM_BINDING_VERSION = "1.9.386.0"
35
35
 
36
+ # Snapshot-backed attribute listing native (moxml#520): the
37
+ # listing face serves reads from the binding's flat packet face —
38
+ # no Attr mint, no FFI::Pointer, no per-attribute C crossing on
39
+ # the read path (the hydration-walk shape). The node held here is
40
+ # whatever the listing read (a native node on the native layer, a
41
+ # binding element otherwise); the binding element is resolved on
42
+ # demand for the mutation and namespace-scope faces.
43
+ class SnapshotAttribute
44
+ attr_reader :name, :qualified_name
45
+
46
+ def initialize(name, qualified_name, value, node)
47
+ @name = name
48
+ @qualified_name = qualified_name
49
+ @value = value
50
+ @node = node
51
+ end
52
+
53
+ def value
54
+ @value
55
+ end
56
+
57
+ def value=(new_value)
58
+ @value = new_value
59
+ end
60
+
61
+ # Binding element for adapter faces that need binding-side APIs
62
+ # (mutations, namespace scope walks); reads never convert.
63
+ def element
64
+ @element ||= ::Moxml::Adapter::Leptris.to_binding(@node)
65
+ end
66
+
67
+ # Adopt a real binding Attr for mutation-contract returns
68
+ # (name=/namespace= renames go through the element and yield
69
+ # a fresh native).
70
+ def resolve!
71
+ element.attribute_nodes
72
+ .reverse.find { |candidate| candidate.name == @qualified_name }
73
+ end
74
+
75
+ # Shape parity with the binding Attr the snapshot stands for:
76
+ # consumers duck-check to_xml; delegate to the resolved Attr
77
+ # so the engine's own escaping defines the output.
78
+ def to_xml
79
+ resolve!&.to_xml
80
+ end
81
+ end
82
+
83
+ # The address-native lane's native (leptris-ruby#587/#589/#591):
84
+ # a node as (binding document, raw C address, parent-reported
85
+ # kind). Plain Ruby — one allocation per node, no TypedData
86
+ # wrap, no binding identity-cache crossing. Reads resolve
87
+ # through the address-keyed C faces; writes bridge through
88
+ # #to_binding (address + document answer the bridge's
89
+ # questions). The per-document token cache (#address_native)
90
+ # hands the SAME token back per address while the document is
91
+ # unmutated, so wrapper identity rides the context registry
92
+ # unchanged. +kind+ is the children_addrs integer (0 element,
93
+ # 1 text, 2 comment, 3 cdata, 4 pi) — VISIT_NODE_TYPES'
94
+ # order.
95
+ class AddressNode
96
+ attr_reader :document, :address, :kind
97
+
98
+ def initialize(document, address, kind)
99
+ @document = document
100
+ @address = address
101
+ @kind = kind
102
+ end
103
+ end
104
+
36
105
  if Gem::Version.new(::Leptris::VERSION.to_s) <
37
106
  Gem::Version.new(MINIMUM_BINDING_VERSION)
38
107
  raise Moxml::AdapterError.new(
@@ -49,12 +118,10 @@ module Moxml
49
118
  # Attribute-node xpath results carry proper wrappers since
50
119
  # 1.9.105 (leptris-ruby#153: ResultAttr with name/value);
51
120
  # before that the native gate routed them to the Ruby engine.
52
- ATTR_RESULT_NATIVE = true
53
121
 
54
122
  # leptris_node_digest shipped in bindings 1.9.99 (libleptris
55
123
  # 1.9.99, engine #869): on-demand Merkle subtree hash, zero
56
124
  # cost when unused. Below it, Node#digest answers nil.
57
- DIGEST_SUPPORTED = true
58
125
 
59
126
  # 1.9.163.7 added the XPath seams (TODO.perf/15+16):
60
127
  # Searchable#at_xpath materializes nodeset entry 0 and frees
@@ -81,6 +148,21 @@ module Moxml
81
148
  NATIVE_READ_LAYER =
82
149
  defined?(::Leptris::XML::NativeNode)
83
150
 
151
+ # The flat packet face (leptris-ruby#586, binding 1.9.406.1):
152
+ # Native.attribute_packet(document, address) returns
153
+ # [name1, value1, name2, value2, ...] — interned names, fresh
154
+ # values, no per-element memo, no pair arrays, no Attr
155
+ # objects, no FFI::Pointer. Supersedes the snapshot face for
156
+ # the one-pass hydration walks the XMI lanes are (the binding
157
+ # memo is a retention penalty there). Probed on the compiled
158
+ # ext; the version floor pairs it (misrelease races — the
159
+ # NATIVE_PLAN_STRUCTS lesson).
160
+ ELEMENT_ATTR_PACKET =
161
+ NATIVE_READ_LAYER &&
162
+ Gem::Version.new(::Leptris::VERSION) >=
163
+ Gem::Version.new("1.9.406.1") &&
164
+ ::Leptris::XML::Native.respond_to?(:attribute_packet)
165
+
84
166
  # leptris-ruby#529: the whole XPath node-set result as an Array
85
167
  # of registered natives in ONE C crossing (Document#
86
168
  # xpath_result_rows_bulk). The eager lane below crosses once and
@@ -124,6 +206,20 @@ module Moxml
124
206
  defined?(::Leptris::XML::Native) &&
125
207
  ::Leptris::XML::Native.respond_to?(:walk_columnar)
126
208
 
209
+ # The address-keyed shadow faces (leptris-ruby#587/#589/#591):
210
+ # children_addrs(parent) => [addrs, kinds] accepts a raw
211
+ # address Integer, and the fast_* read faces (name, element
212
+ # text, comment/cdata/pi payloads) read by address. The
213
+ # address-native lane mints a plain AddressNode per node
214
+ # instead of the TypedData wrap + identity-cache crossing the
215
+ # columnar lane pays in C. Face-probed, not version-gated.
216
+ ADDRESS_NATIVE =
217
+ defined?(::Leptris::XML::Native) &&
218
+ ::Leptris::XML::Native.respond_to?(:children_addrs) &&
219
+ ::Leptris::XML::Native.respond_to?(:walk_addresses) &&
220
+ ::Leptris::XML::Native.respond_to?(:fast_name) &&
221
+ ::Leptris::XML::Native.respond_to?(:fast_text_content)
222
+
127
223
  # 1.9.163.2's native layer returns UTF-8, unfrozen strings
128
224
  # (1.9.162.x answered ASCII-8BIT — the reads retagged with a
129
225
  # dup+force_encoding per call).
@@ -178,7 +274,6 @@ module Moxml
178
274
  # 1.9.181 (lockstep): Node#source_position — {line, col_start,
179
275
  # col_end} from the engine's source-position API (upstream
180
276
  # #1124); created nodes report zeros.
181
- NATIVE_SOURCE_POSITION = true
182
277
 
183
278
  # Entity-reference preservation (leptris-ruby#212 / upstream
184
279
  # #1094): 1.9.177 ships ParseOptions.keep_entity_refs plus
@@ -206,7 +301,6 @@ module Moxml
206
301
  # so the gate names the first release where the drain is
207
302
  # CORRECT, not merely present. Dev floor pins ~> 1.9.238;
208
303
  # older bindings take the callback Parser below.
209
- NATIVE_SAX_RECORDS = true
210
304
 
211
305
  # Document#absorb (engine #1548): one call transfers a source
212
306
  # document's pool ownership into the destination, so
@@ -367,7 +461,7 @@ module Moxml
367
461
  native_bulk_fixed: !NATIVE_BULK_FIXED.nil?,
368
462
  native_plan_structs: NATIVE_PLAN_STRUCTS,
369
463
  native_plan_typed: NATIVE_PLAN_TYPED,
370
- digest: DIGEST_SUPPORTED && engine_serves?(:digest),
464
+ digest: engine_serves?(:digest),
371
465
  xsd: XSD_SUPPORTED && engine_serves?(:xsd),
372
466
  relaxng: RELAXNG_SUPPORTED && engine_serves?(:relaxng),
373
467
  xslt: XSLT_SUPPORTED && engine_serves?(:xslt),
@@ -602,12 +696,13 @@ module Moxml
602
696
 
603
697
  def to_binding(node)
604
698
  return node unless NATIVE_READ_LAYER &&
605
- node.is_a?(::Leptris::XML::NativeNode)
699
+ (node.is_a?(::Leptris::XML::NativeNode) ||
700
+ node.is_a?(AddressNode))
606
701
 
607
702
  bridged = @binding_of[node]
608
703
  return bridged if bridged
609
704
 
610
- doc = doc_for(node)
705
+ doc = node.is_a?(AddressNode) ? node.document : doc_for(node)
611
706
  ptr = ::FFI::Pointer.new(node.address)
612
707
  bridged = ::Leptris::XML::Node.wrap(ptr, doc)
613
708
  @binding_of = fresh_node_registry if @binding_of.size >= BINDING_OF_CAP
@@ -856,6 +951,15 @@ module Moxml
856
951
  # already-minted wrappers, so the adapter crossing
857
952
  # (bind_call + to_a + keep-refs probe) drops out, and
858
953
  # entity-marker documents take super for the split.
954
+ # NOTE (address-lane verdict, #531): the token arm was
955
+ # tried HERE and measured — tokens cannot serve the
956
+ # wrapper walk without breaking the klass-propagating
957
+ # identity contract (children vs element_children mint
958
+ # different wrapper classes for the same C node) and the
959
+ # wrapper lifecycle (tokens pin the binding document, so
960
+ # dropped documents stop releasing wrappers). The C-side
961
+ # klass-injected native cache is the walk lane; the
962
+ # address faces serve direct adapter consumers.
859
963
  if NN.method_defined?(:children_read)
860
964
  module ChildReads
861
965
  def children
@@ -1018,17 +1122,23 @@ module Moxml
1018
1122
  def same_node?(one, other)
1019
1123
  return true if one.equal?(other)
1020
1124
 
1021
- if one.is_a?(::Leptris::XML::Attr) ||
1022
- other.is_a?(::Leptris::XML::Attr)
1023
- return one.is_a?(::Leptris::XML::Attr) &&
1024
- other.is_a?(::Leptris::XML::Attr) &&
1025
- one.name == other.name &&
1125
+ attr_like = ->(node) do
1126
+ node.is_a?(::Leptris::XML::Attr) || node.is_a?(SnapshotAttribute)
1127
+ end
1128
+ if attr_like.call(one) || attr_like.call(other)
1129
+ return false unless attr_like.call(one) && attr_like.call(other)
1130
+
1131
+ name_one = one.is_a?(SnapshotAttribute) ? one.qualified_name : one.name
1132
+ name_other = other.is_a?(SnapshotAttribute) ? other.qualified_name : other.name
1133
+ return name_one == name_other &&
1026
1134
  same_node?(one.element, other.element)
1027
1135
  end
1028
1136
 
1029
1137
  if NATIVE_READ_LAYER
1030
- native_one = one.is_a?(::Leptris::XML::NativeNode)
1031
- native_other = other.is_a?(::Leptris::XML::NativeNode)
1138
+ native_one = one.is_a?(::Leptris::XML::NativeNode) ||
1139
+ one.is_a?(AddressNode)
1140
+ native_other = other.is_a?(::Leptris::XML::NativeNode) ||
1141
+ other.is_a?(AddressNode)
1032
1142
  if native_one || native_other
1033
1143
  address_one = native_one ? one.address : one.c_ptr.address
1034
1144
  address_other = native_other ? other.address : other.c_ptr.address
@@ -1043,6 +1153,9 @@ module Moxml
1043
1153
  # to the root (C-bound reads) and look the doc up in the
1044
1154
  # root registry. Detached subtrees answer nil.
1045
1155
  def doc_for(node)
1156
+ # Address tokens carry their document (set at mint).
1157
+ return node.document if node.is_a?(AddressNode)
1158
+
1046
1159
  # The 1.9.163.5 native layer exposes the owning document
1047
1160
  # directly; the root climb serves older bindings.
1048
1161
  return NN_DOCUMENT.bind_call(node) if NATIVE_MUTATIONS_COHERENT
@@ -1506,8 +1619,13 @@ module Moxml
1506
1619
  def bare_attr_value(element, name)
1507
1620
  # Exact-name C read: bare names target no-namespace
1508
1621
  # attributes (the documented contract — a bare "y" never
1509
- # resolves a "p:y" sibling).
1510
- value = if NATIVE_READ_LAYER && element.is_a?(NN)
1622
+ # resolves a "p:y" sibling). Address tokens take the
1623
+ # address-keyed attribute face.
1624
+ value = if element.is_a?(AddressNode)
1625
+ ::Leptris::XML::Native.fast_attribute(
1626
+ element.address, name.to_s
1627
+ )
1628
+ elsif NATIVE_READ_LAYER && element.is_a?(NN)
1511
1629
  NN_ATTRIBUTE.bind_call(element, name.to_s)
1512
1630
  else
1513
1631
  element[name.to_s]
@@ -2081,7 +2199,9 @@ module Moxml
2081
2199
 
2082
2200
  def set_namespace(node, namespace)
2083
2201
  node = to_binding(node) if NATIVE_READ_LAYER
2084
- return set_attribute_namespace(node, namespace) if node.is_a?(::Leptris::XML::Attr)
2202
+ if node.is_a?(::Leptris::XML::Attr) || node.is_a?(SnapshotAttribute)
2203
+ return set_attribute_namespace(node, namespace)
2204
+ end
2085
2205
 
2086
2206
  element = node
2087
2207
  if namespace.nil?
@@ -2139,7 +2259,8 @@ module Moxml
2139
2259
  element = attr.element
2140
2260
  local = attr.name.include?(":") ? attr.name.split(":", 2)[1] : attr.name
2141
2261
  value = attr.value
2142
- element.remove_attribute(attr.name)
2262
+ qualified_old = attr.is_a?(SnapshotAttribute) ? attr.qualified_name : attr.name
2263
+ element.remove_attribute(qualified_old)
2143
2264
  prefix = namespace.is_a?(String) ? nil : namespace.prefix
2144
2265
  uri = namespace.is_a?(String) ? namespace : namespace.href
2145
2266
  qualified = prefix.nil? || prefix.empty? ? local : "#{prefix}:#{local}"
@@ -2147,12 +2268,35 @@ module Moxml
2147
2268
  if prefix && !prefix.empty? && !uri.to_s.empty? && !resolve_prefix_ns(element, prefix)
2148
2269
  element.add_namespace_definition(prefix, uri.to_s)
2149
2270
  end
2271
+ if attr.is_a?(SnapshotAttribute)
2272
+ # Mutation-return contract: hand back the same native
2273
+ # CLASS the caller tracks, refreshed for the new qname.
2274
+ return SnapshotAttribute.new(local, qualified, value, element)
2275
+ end
2276
+
2150
2277
  element.attribute_nodes.reverse.find { |candidate| candidate.name == qualified }
2151
2278
  end
2152
2279
 
2153
2280
  def namespace(node)
2154
2281
  node = to_binding(node) if NATIVE_READ_LAYER
2155
2282
 
2283
+ # SnapshotAttribute: resolve the prefix against the owning
2284
+ # element's scope (same contract as the Attr branch below);
2285
+ # unprefixed names (plain attributes, xmlns declarations)
2286
+ # resolve the real binding Attr for the declaration walk.
2287
+ if node.is_a?(SnapshotAttribute)
2288
+ if (prefix = prefix_part(node.qualified_name))
2289
+ resolved = resolve_prefix_ns(node.element, prefix)
2290
+ return resolved if resolved
2291
+ return implicit_xml_namespace(node) if prefix == "xml"
2292
+
2293
+ return nil
2294
+ end
2295
+
2296
+ real = node.resolve!
2297
+ return real && namespace(real)
2298
+ end
2299
+
2156
2300
  # The binding resolves Attr#namespace to the declaration but
2157
2301
  # without the prefix; when the qualified name carries one,
2158
2302
  # prefer the in-scope declaration that binds it so wrappers
@@ -2218,7 +2362,12 @@ module Moxml
2218
2362
  end
2219
2363
 
2220
2364
  def implicit_xml_namespace(node)
2221
- owner = node.is_a?(::Leptris::XML::Attr) ? node.element : node
2365
+ owner = if node.is_a?(::Leptris::XML::Attr) ||
2366
+ node.is_a?(SnapshotAttribute)
2367
+ node.element
2368
+ else
2369
+ node
2370
+ end
2222
2371
  return nil unless owner.is_a?(::Leptris::XML::Element)
2223
2372
 
2224
2373
  ::Leptris::XML::Namespace.new(owner, RESERVED_XML_URI, prefix: "xml")
@@ -2229,7 +2378,10 @@ module Moxml
2229
2378
  end
2230
2379
 
2231
2380
  def processing_instruction_target(node)
2232
- return to_binding(node).target if NATIVE_READ_LAYER && node.is_a?(NN)
2381
+ if NATIVE_READ_LAYER &&
2382
+ (node.is_a?(NN) || node.is_a?(AddressNode))
2383
+ return to_binding(node).target
2384
+ end
2233
2385
 
2234
2386
  node.target
2235
2387
  end
@@ -2241,6 +2393,18 @@ module Moxml
2241
2393
  processing_instruction doctype].freeze
2242
2394
 
2243
2395
  def node_type(node)
2396
+ # SnapshotAttribute natives are first-class attributes: the
2397
+ # listing face and the pure-Ruby XPath engine's attribute
2398
+ # steps both produce them; reads (name/value/element) are
2399
+ # complete on the snapshot itself.
2400
+ return :attribute if node.is_a?(SnapshotAttribute)
2401
+
2402
+ # Address tokens carry the parent-reported WS kind.
2403
+ if node.is_a?(AddressNode)
2404
+ mapped = VISIT_NODE_TYPES[node.kind]
2405
+ return mapped if mapped
2406
+ end
2407
+
2244
2408
  if node.is_a?(::Leptris::XML::Node) &&
2245
2409
  node.instance_variable_defined?(:@node_type)
2246
2410
  kind = node.instance_variable_get(:@node_type)
@@ -2285,8 +2449,9 @@ module Moxml
2285
2449
  # wrappers (declarations, doctypes, entity markers) and the
2286
2450
  # lightweight Attr triples answer nil.
2287
2451
  def digest(node, drop_ws_text: false)
2288
- return nil unless DIGEST_SUPPORTED
2289
- if NATIVE_READ_LAYER && node.is_a?(::Leptris::XML::NativeNode)
2452
+ if NATIVE_READ_LAYER &&
2453
+ (node.is_a?(::Leptris::XML::NativeNode) ||
2454
+ node.is_a?(AddressNode))
2290
2455
  return to_binding(node).digest(drop_ws: drop_ws_text)
2291
2456
  end
2292
2457
  return nil unless node.is_a?(::Leptris::XML::Node) &&
@@ -2298,6 +2463,18 @@ module Moxml
2298
2463
  def node_name(node)
2299
2464
  return node.root_name if node.is_a?(::Leptris::XML::DocType)
2300
2465
 
2466
+ # Address tokens: the fast_name face is element-only (nil
2467
+ # otherwise); PIs answer their target.
2468
+ if node.is_a?(AddressNode)
2469
+ if node.kind == 4
2470
+ return ::Leptris::XML::Native.fast_pi_target(
2471
+ node.address,
2472
+ ).to_s
2473
+ end
2474
+
2475
+ return ::Leptris::XML::Native.fast_name(node.address).to_s
2476
+ end
2477
+
2301
2478
  if NATIVE_READ_LAYER && node.is_a?(::Leptris::XML::NativeNode)
2302
2479
  # PIs expose no name through the native layer.
2303
2480
  return to_binding(node).target.to_s if NN_NODE_TYPE.bind_call(node) == :pi
@@ -2372,7 +2549,30 @@ module Moxml
2372
2549
  return to_binding(node).children.to_a
2373
2550
  end
2374
2551
 
2552
+ # Address tokens: element receivers take the token arm;
2553
+ # terminal kinds answer [] through the case chain below.
2554
+ if NATIVE_READ_LAYER && node.is_a?(AddressNode)
2555
+ doc = node.document
2556
+ return address_children(doc, node) if doc && node.kind.zero?
2557
+
2558
+ return []
2559
+ end
2560
+
2375
2561
  if NATIVE_READ_LAYER && node.is_a?(::Leptris::XML::NativeNode)
2562
+ # The address-native arm outranks the columnar lane: the
2563
+ # same child lists, but the tokens are plain
2564
+ # (document, address, kind) AddressNodes — the columnar
2565
+ # crossing mints a TypedData wrap + identity-cache write
2566
+ # per node in C, this arm allocates one plain token per
2567
+ # node in Ruby (ADDRESS_NATIVE above). Element receivers
2568
+ # only: their children's kinds are the WS enum the token
2569
+ # carries; document receivers keep the assembly path
2570
+ # (declaration/doctype kinds are outside the enum).
2571
+ if ADDRESS_NATIVE && !entity_bearing && element_native?(node)
2572
+ doc = doc_for(node)
2573
+ return address_children(doc, node) if doc
2574
+ end
2575
+
2376
2576
  # Bulk C children through moxml's own scratch (see
2377
2577
  # NATIVE_READ_LAYER note above). Entity-marker documents
2378
2578
  # bridge each child for the marker split — the
@@ -2436,15 +2636,67 @@ module Moxml
2436
2636
  CustomizedLeptris::EntityReference, CustomizedLeptris::TextSegment,
2437
2637
  # Terminal node kinds pay an FFI round trip for an empty
2438
2638
  # list; unfiltered recursions visit every text node.
2639
+ # Address tokens of the terminal kinds (text, comment,
2640
+ # cdata, pi) have no children either.
2439
2641
  ::Leptris::XML::Text, ::Leptris::XML::Comment,
2440
2642
  ::Leptris::XML::CDATA, ::Leptris::XML::ProcessingInstruction,
2441
- ::Leptris::XML::Attr
2643
+ ::Leptris::XML::Attr, AddressNode
2442
2644
  []
2443
2645
  else
2444
2646
  node.children.to_a
2445
2647
  end
2446
2648
  end
2447
2649
 
2650
+ # The address-native arm's child list: one children_addrs
2651
+ # crossing, one token per child (ADDRESS_NATIVES cache for
2652
+ # identity stability).
2653
+ ADDRESS_NATIVES = :address_natives
2654
+
2655
+ def address_children(doc, node)
2656
+ addrs, kinds = ::Leptris::XML::Native.children_addrs(
2657
+ node.is_a?(AddressNode) ? node.address : node,
2658
+ )
2659
+ children = Array.new(addrs.size)
2660
+ i = 0
2661
+ while i < addrs.size
2662
+ children[i] = address_native(doc, addrs[i], kinds[i])
2663
+ i += 1
2664
+ end
2665
+ children
2666
+ end
2667
+
2668
+ # One address token per (document version, address): the
2669
+ # context registry keys wrapper identity on the token, so a
2670
+ # stable token per address keeps the children[0].equal?
2671
+ # semantics across traversals. The document version stamps
2672
+ # the map — any mutation invalidates wholesale, the same
2673
+ # discipline as the columnar map and the binding's own
2674
+ # children memo.
2675
+ ADDRESS_NATIVES_CAP = 1_048_576
2676
+
2677
+ def address_native(doc, address, kind)
2678
+ pair = attachments.get(doc, ADDRESS_NATIVES)
2679
+ unless pair.is_a?(Array) && pair[0] == doc.version
2680
+ pair = [doc.version, {}]
2681
+ attachments.set(doc, ADDRESS_NATIVES, pair)
2682
+ end
2683
+ map = pair[1]
2684
+ map.clear if map.size >= ADDRESS_NATIVES_CAP
2685
+ token = map[address]
2686
+ return token if token
2687
+
2688
+ token = AddressNode.new(doc, address, kind)
2689
+ map[address] = token
2690
+ token
2691
+ end
2692
+
2693
+ # Element-ness of a native receiver, for the address arm's
2694
+ # gate (one kind read; the token lane answers from the
2695
+ # parent-reported kind without a crossing).
2696
+ def element_native?(node)
2697
+ NN_NODE_TYPE.bind_call(node) == :element
2698
+ end
2699
+
2448
2700
  # Map hit for a primed node (see COLUMNAR_WALK). Served dup'd so
2449
2701
  # no consumer can mutate the cached list in place.
2450
2702
  def columnar_children_hit(node)
@@ -2634,6 +2886,24 @@ module Moxml
2634
2886
  def root_parent(node)
2635
2887
  return node.document if node.is_a?(::Leptris::XML::DocType)
2636
2888
 
2889
+ # Address tokens carry no parent face: bridge once, then
2890
+ # answer through the binding parent — as a document-cache
2891
+ # twin so the answer stays in the native representation.
2892
+ # The binding reports the root element and document-level
2893
+ # parts parentless; the moxml contract roots them at the
2894
+ # document (same rule as the binding arm below).
2895
+ if node.is_a?(AddressNode)
2896
+ doc = node.document
2897
+ bound = to_binding(node)
2898
+ parent = bound&.parent
2899
+ return native_twin(doc, parent) if parent
2900
+
2901
+ return doc if doc && (doc.root&.c_address == node.address ||
2902
+ bound.is_a?(::Leptris::XML::ProcessingInstruction))
2903
+
2904
+ return nil
2905
+ end
2906
+
2637
2907
  parent = node.parent
2638
2908
  return parent if parent
2639
2909
 
@@ -2647,6 +2917,8 @@ module Moxml
2647
2917
  end
2648
2918
 
2649
2919
  def next_sibling(node)
2920
+ return next_sibling(to_binding(node)) if node.is_a?(AddressNode)
2921
+
2650
2922
  return NN_NEXT_SIBLING.bind_call(node) if NATIVE_READ_LAYER &&
2651
2923
  node.is_a?(::Leptris::XML::NativeNode)
2652
2924
 
@@ -2663,6 +2935,8 @@ module Moxml
2663
2935
  end
2664
2936
 
2665
2937
  def document(node)
2938
+ return node.document if node.is_a?(AddressNode)
2939
+
2666
2940
  return doc_for(node) if NATIVE_READ_LAYER &&
2667
2941
  node.is_a?(::Leptris::XML::NativeNode)
2668
2942
 
@@ -2689,8 +2963,6 @@ module Moxml
2689
2963
  end
2690
2964
 
2691
2965
  def source_position(node)
2692
- return nil unless NATIVE_SOURCE_POSITION
2693
-
2694
2966
  to_binding(node).source_position
2695
2967
  end
2696
2968
 
@@ -2788,20 +3060,74 @@ module Moxml
2788
3060
  end
2789
3061
 
2790
3062
  def attributes(element)
2791
- element = to_binding(element)
2792
- element.attribute_nodes
3063
+ # moxml#520: serve the listing from the binding's flat packet
3064
+ # face when the native layer exposes it — ONE flat array of
3065
+ # interned names + fresh values, no per-element memo, no
3066
+ # pair arrays, no Attr objects, no FFI::Pointer. The native
3067
+ # node's own (document, address) serve directly: to_binding
3068
+ # converts per call (a Node.wrap + registry hit, ~6s/3M
3069
+ # allocations per million-element walk), and only mutation
3070
+ # faces need the binding element.
3071
+ if ELEMENT_ATTR_PACKET
3072
+ doc, addr = attr_packet_target(element)
3073
+ if doc
3074
+ flat = ::Leptris::XML::Native.attribute_pairs(doc, addr)
3075
+ return packet_attributes(flat, element)
3076
+ end
3077
+ end
3078
+
3079
+ binding_element = to_binding(element)
3080
+ unless binding_element.respond_to?(:attribute_pairs)
3081
+ return binding_element.attribute_nodes
3082
+ end
3083
+
3084
+ binding_element.attribute_pairs.map do |qualified, value|
3085
+ SnapshotAttribute.new(local_name(qualified), qualified, value,
3086
+ binding_element)
3087
+ end
3088
+ end
3089
+
3090
+ def attr_packet_target(element)
3091
+ if element.is_a?(AddressNode)
3092
+ [element.document, element.address]
3093
+ elsif NATIVE_READ_LAYER && element.is_a?(NN)
3094
+ [NN_DOCUMENT.bind_call(element), element.address]
3095
+ elsif element.respond_to?(:c_address)
3096
+ [element.document, element.c_address]
3097
+ end
3098
+ end
3099
+
3100
+ def packet_attributes(flat, node)
3101
+ result = Array.new(flat.size / 2)
3102
+ i = 0
3103
+ j = 0
3104
+ while i < flat.size
3105
+ qualified = flat[i]
3106
+ result[j] = SnapshotAttribute.new(local_name(qualified), qualified,
3107
+ flat[i + 1], node)
3108
+ i += 2
3109
+ j += 1
3110
+ end
3111
+ result
2793
3112
  end
2794
3113
 
2795
3114
  # One C crossing per element on bindings with the bulk face
2796
3115
  # (leptris-ruby#278): interned names, fresh values, zero
2797
3116
  # Attr objects. Face-probed, not version-gated — the floor
2798
3117
  # (1.9.194.1) admits bindings without it, which take the
2799
- # base default over #attributes.
3118
+ # base default over #attributes. The packet face
3119
+ # (ELEMENT_ATTR_PACKET, 1.9.406.1) outranks the snapshot
3120
+ # face: same stride, no binding-side memo retention.
2800
3121
  def attribute_pairs(element)
2801
3122
  binding_element = to_binding(element)
2802
3123
  return super unless binding_element.respond_to?(:attribute_pairs)
2803
3124
 
2804
- pairs = if ELEMENT_ATTR_SNAPSHOT
3125
+ pairs = if ELEMENT_ATTR_PACKET
3126
+ ::Leptris::XML::Native
3127
+ .attribute_packet(binding_element.document,
3128
+ binding_element.c_address)
3129
+ .each_slice(2).to_a
3130
+ elsif ELEMENT_ATTR_SNAPSHOT
2805
3131
  binding_element.attributes_snapshot.to_a
2806
3132
  else
2807
3133
  binding_element.attribute_pairs
@@ -2811,11 +3137,33 @@ module Moxml
2811
3137
  pairs.map { |(name, value)| [local_name(name), value] }
2812
3138
  end
2813
3139
 
3140
+ # moxml#520: local-name splits are interned — element/attribute
3141
+ # vocabularies are bounded per document, so the same qualified
3142
+ # names recur; one frozen local string per name instead of an
3143
+ # allocation per read. Bounded so an adversarial document
3144
+ # cannot grow the table without limit.
3145
+ LOCAL_NAME_INTERN_MAX = 4096
3146
+
3147
+ def local_name_intern
3148
+ @local_name_intern ||= {}
3149
+ end
3150
+
2814
3151
  def local_name(name)
2815
- name.include?(":") ? name.split(":", 2)[1] : name
3152
+ return name unless name.include?(":")
3153
+
3154
+ cached = local_name_intern[name]
3155
+ return cached if cached
3156
+
3157
+ local = name.split(":", 2)[1].freeze
3158
+ if local_name_intern.size < LOCAL_NAME_INTERN_MAX
3159
+ local_name_intern[name] = local
3160
+ end
3161
+ local
2816
3162
  end
2817
3163
 
2818
3164
  def attribute_element(attr)
3165
+ return attr.element if attr.is_a?(SnapshotAttribute)
3166
+
2819
3167
  # XPath results mint ResultAttr (parent-backed); document
2820
3168
  # attributes are binding Attr (element-backed).
2821
3169
  if attr.is_a?(::Leptris::XML::ResultAttr)
@@ -2826,6 +3174,8 @@ module Moxml
2826
3174
  end
2827
3175
 
2828
3176
  def attribute_name(attr)
3177
+ return attr.name if attr.is_a?(SnapshotAttribute)
3178
+
2829
3179
  # leptris Attr names are qualified; the wrapper composes the
2830
3180
  # prefix, so expose the local part.
2831
3181
  return attr.name.split(":", 2)[1] if attr.name.include?(":")
@@ -2854,6 +3204,19 @@ module Moxml
2854
3204
  end
2855
3205
 
2856
3206
  def set_attribute_name(attr, name)
3207
+ if attr.is_a?(SnapshotAttribute)
3208
+ # Renames go through the owning element; the mutation
3209
+ # return contract hands back the same native CLASS the
3210
+ # caller tracks (a refreshed snapshot), not the binding
3211
+ # Attr the rename minted.
3212
+ element = attr.element
3213
+ value = attr.value
3214
+ element.remove_attribute(attr.qualified_name)
3215
+ element[name.to_s] = value
3216
+ return SnapshotAttribute.new(name.to_s, name.to_s, value,
3217
+ element)
3218
+ end
3219
+
2857
3220
  # Attr is an immutable value object; renames go through the
2858
3221
  # element and yield a fresh native, which the wrapper adopts.
2859
3222
  element = attr.element
@@ -2864,13 +3227,31 @@ module Moxml
2864
3227
  end
2865
3228
 
2866
3229
  def set_attribute_value(attr, value)
3230
+ if attr.is_a?(SnapshotAttribute)
3231
+ # Write through the element (the engine mutates in place);
3232
+ # refresh the snapshot so the wrapper's memo serves the
3233
+ # new value without a crossing.
3234
+ attr.element[attr.qualified_name] = value.to_s
3235
+ attr.value = value.to_s
3236
+ return attr
3237
+ end
3238
+
2867
3239
  attr.value = value.to_s
2868
3240
  attr
2869
3241
  end
2870
3242
 
2871
3243
  def get_attribute(element, name)
2872
3244
  element = to_binding(element)
2873
- element.attribute_nodes.find { |attr| attr.name == name.to_s }
3245
+ return element.attribute_nodes.find { |attr| attr.name == name.to_s } unless
3246
+ element.respond_to?(:attribute_pairs)
3247
+
3248
+ # moxml#520: locate through the one-crossing pairs face —
3249
+ # attribute_nodes mints a binding Attr + FFI::Pointer per
3250
+ # attribute just to find one.
3251
+ pair = element.attribute_pairs.find { |qualified, _| qualified == name.to_s }
3252
+ return nil unless pair
3253
+
3254
+ SnapshotAttribute.new(local_name(pair[0]), pair[0], pair[1], element)
2874
3255
  end
2875
3256
 
2876
3257
  def get_attribute_value(element, name)
@@ -2884,6 +3265,11 @@ module Moxml
2884
3265
  end
2885
3266
 
2886
3267
  def remove_attribute_native(attr)
3268
+ if attr.is_a?(SnapshotAttribute)
3269
+ attr.element.remove_attribute(attr.qualified_name)
3270
+ return attr
3271
+ end
3272
+
2887
3273
  attr.element.remove_attribute(attr.name)
2888
3274
  attr
2889
3275
  end
@@ -3523,6 +3909,22 @@ module Moxml
3523
3909
  end
3524
3910
 
3525
3911
  def text_content(node)
3912
+ # Address tokens dispatch the same kind table the native
3913
+ # layer's #content uses (elements aggregate via
3914
+ # fast_element_text; text/comment/cdata/pi carry their
3915
+ # payload faces).
3916
+ if node.is_a?(AddressNode)
3917
+ native = ::Leptris::XML::Native
3918
+ content = case node.kind
3919
+ when 0 then native.fast_element_text(node.address)
3920
+ when 1 then native.fast_text_content(node.address)
3921
+ when 2 then native.fast_comment_content(node.address)
3922
+ when 3 then native.fast_cdata_content(node.address)
3923
+ when 4 then native.fast_pi_data(node.address)
3924
+ end
3925
+ return content.to_s
3926
+ end
3927
+
3526
3928
  if NATIVE_READ_LAYER && node.is_a?(::Leptris::XML::NativeNode)
3527
3929
  content = NN_CONTENT.bind_call(node)
3528
3930
  return content if NATIVE_STRINGS_UTF8
@@ -3618,6 +4020,10 @@ module Moxml
3618
4020
  end
3619
4021
 
3620
4022
  def cdata_content(node)
4023
+ if node.is_a?(AddressNode)
4024
+ return ::Leptris::XML::Native.fast_cdata_content(node.address)
4025
+ end
4026
+
3621
4027
  return NN_CONTENT.bind_call(node) if NATIVE_READ_LAYER && node.is_a?(NN)
3622
4028
 
3623
4029
  node.content
@@ -3629,6 +4035,10 @@ module Moxml
3629
4035
  end
3630
4036
 
3631
4037
  def comment_content(node)
4038
+ if node.is_a?(AddressNode)
4039
+ return ::Leptris::XML::Native.fast_comment_content(node.address)
4040
+ end
4041
+
3632
4042
  if NATIVE_READ_LAYER && node.is_a?(NN)
3633
4043
  content = NN_CONTENT.bind_call(node)
3634
4044
  # NativeNode#content answers nil for comment-kind nodes
@@ -4005,7 +4415,7 @@ module Moxml
4005
4415
  # (leptris-ruby#153: ResultAttr wrappers with name/value);
4006
4416
  # older bindings returned generic Node wrappers whose
4007
4417
  # #name raised, so the Ruby engine owned them.
4008
- next true if selects_attribute_results?(ast) && ATTR_RESULT_NATIVE
4418
+ next true if selects_attribute_results?(ast)
4009
4419
 
4010
4420
  !selects_attribute_results?(ast)
4011
4421
  end
@@ -4116,14 +4526,14 @@ module Moxml
4116
4526
  def sax_parse(xml, handler)
4117
4527
  bridge = LeptrisSAXBridge.new(handler)
4118
4528
  xml_string = xml.is_a?(IO) || xml.is_a?(::StringIO) ? xml.read : xml.to_s
4119
- # Bulk record drain (leptris#1298, NATIVE_SAX_RECORDS):
4529
+ # Bulk record drain (leptris#1298):
4120
4530
  # one crossing per document. The handler decides the shape
4121
4531
  # polymorphically — Handler#on_sax_records answers
4122
4532
  # :unhandled, so classic handlers get the exact callback
4123
4533
  # stream replayed from the table; hot loops override and
4124
4534
  # walk the records (nil table = outside the drain subset
4125
4535
  # or pre-1.9.238 binding — callback path).
4126
- table = NATIVE_SAX_RECORDS ? ::Leptris::XML::SAX::Records.open(xml_string) : nil
4536
+ table = ::Leptris::XML::SAX::Records.open(xml_string)
4127
4537
  if table
4128
4538
  begin
4129
4539
  return if handler.on_sax_records(table) != :unhandled