leptris 1.9.416.0-ppc64le-linux → 1.9.417.0-ppc64le-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: '0410934f51b23411af090dbb697ebfb667ceb12c8437757035a7716834d5c1e0'
4
- data.tar.gz: 0ca5ad1414ce7cc8df05b68b9c37865a9c08357634a45eee2daf4cdb736a0279
3
+ metadata.gz: 206cb2fb2bb7656b990d760c39127a891e2aa29ee69d71ab794b1b9bca8c8879
4
+ data.tar.gz: 77632114fc73e23ca81cb4b0b9d6be17b58fbe5a8037d49e3df3255c556ebf6a
5
5
  SHA512:
6
- metadata.gz: 6572bae553e51212ee864ca5e2f69a7876183dbf41f71339c86bb800e7da8027d38828d0cfb492dc426a422bc4cd1bf84ffd22dfd416cedb3106113aa46ddf6b
7
- data.tar.gz: 2d3a586d67bcdecadda6e2bb2c752e5b760cfa28df5682fb8acf6c6707e61725dde7ea3de3578b15abd0d33fa44fe16bd71c29cbe6929a2a002330918569e8e0
6
+ metadata.gz: 1bf2661b4532707784731e519a33509bdd91d7095d533d67c966972f128da7fccdc484caa67e6fb9445ec5fb1ca0cdebf1dd028327a99adbf18d09b43d1cf04f
7
+ data.tar.gz: 7febc6a2c2af45a0cd99f4a92820f8223776b900f383e0f05c5ba1bb7d0ff37022bb6c7ace7858c4cac1867d1e4e1d3e16ee862c0193e8675da1dc15df6d36ab
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ RSpec::Core::RakeTask.new(:spec)
8
8
  # Pin for `rake compile` and the platform-gem builds. Keep in lockstep
9
9
  # with .github/workflows/build.yml (which calls `rake compile`) and the
10
10
  # CHANGELOG when libleptris releases.
11
- LIBLEPTRIS_VERSION = "1.9.416"
11
+ LIBLEPTRIS_VERSION = "1.9.417"
12
12
  # Vendored alongside libleptris for fn:normalize-unicode (TODO
13
13
  # .restructure/20): built per platform with a RELOCATABLE @rpath
14
14
  # install name, loaded by ffi.rb before libleptris so the
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Leptris
4
- VERSION = "1.9.416.0"
4
+ VERSION = "1.9.417.0"
5
5
  end
data/lib/libleptris.so CHANGED
Binary file
data/vendor-src/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Sources vendored in this gem (recompile rights; the ruby
2
2
  variant builds them at install via extconf.rb):
3
3
 
4
- - libleptris v1.9.416
4
+ - libleptris v1.9.417
5
5
  - utf8proc v2.11.0
6
6
 
7
7
  Rebuild by hand:
@@ -1,5 +1,24 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.9.417] - 2026-11-11
4
+ ## Performance
5
+
6
+ - **parse: inline the no-attribute fast-out in the element-open path** (#1842 Track D). The text-shape profile showed `dp_parse_attrs` at ~5% of a document with zero attributes — the per-element call + reset prologue + dup-table memset paid even for `<e></e>`-class tags. `>` or `/` directly after the element name collapses to a few inline stores; whitespace falls through to the full parser unchanged. CI shape rows: ubuntu-x86 attr 13MB reached **parity (0.99x)**, text rows +52–54%, attr 1.5MB **ahead (0.72x)**.
7
+
8
+ - **parse: content-census-driven arena copy budget** (#2003). The count3 sizing pre-scan now also census-counts `&` and literal tab/LF/CR (the only content-copy sources: entity expansion, §3.3.3 normalization) in the same SIMD pass; documents carrying neither drop the len-sized copy reservation — arena ~3x len → ~2x len for the machine-generated single-line class.
9
+
10
+ - **parse: skip the pristine buffer carve for newline-free documents** (#2005, A3d). The pristine copy existed for the lazy line table; a newline-free document loses no newlines to `dp_nul`, so the carve and its full-buffer memcpy drop and `xml_buffer` repoints at the doc-owned scratch. macos attr-13MB +189%.
11
+
12
+ - **simd: count3_high gains an AVX2 kernel** (#2006). x86 machines ran the sizing pre-scan SCALAR; five counters in one 32-byte-chunk pass. The ubuntu-x86 text rows moved 3.4–3.5x behind → 1.38–1.46x.
13
+
14
+ ## Benchmarks
15
+
16
+ - The attrparse row is now win/parity on all three CI legs (macos 1.10x, ubuntu-x86 1.04x, ubuntu-arm 0.84). The macos 13MB shape rows were re-classified as runner noise (4.8x swings across runs of near-identical code) — verdicts there require repeated-run medians. Full arc and the remaining lever (the fused scanner loop on the arm legs) ledgered in `TODO.max-perf/15` Track D addenda 1–6b.
17
+
18
+ ## Documentation
19
+
20
+ - README's shape table refreshes with the noise classification; negative results ledgered (the interleaved-lane default flip measured 1.72x slower on element-dense shapes — the opt-in gate stands).
21
+
3
22
  ## [1.9.416] - 2026-11-11
4
23
  ## Performance
5
24
 
@@ -3,7 +3,7 @@
3
3
  cmake_minimum_required(VERSION 3.20)
4
4
 
5
5
  project(leptris
6
- VERSION 1.9.416
6
+ VERSION 1.9.417
7
7
  DESCRIPTION "Fast XML parser and XPath evaluator in pure C"
8
8
  LANGUAGES C CXX
9
9
  )
@@ -1096,20 +1096,20 @@ harness numbers are noted separately.
1096
1096
  |===
1097
1097
  |Benchmark (parse + free, CI-gated) |Leptris |pugixml |Verdict
1098
1098
 
1099
- |macos-latest
1100
- |1728.6 ns/doc
1101
- |2182.9 ns/doc
1102
- |**1.26x faster**
1099
+ |macos-latest (clang, arm64)
1100
+ |1512.4 ns/doc
1101
+ |1492.4 ns/doc
1102
+ |parity (1.01x)
1103
1103
 
1104
1104
  |ubuntu-latest (x86-64, gcc-13)
1105
- |2798.6 ns/doc
1106
- |1885.2 ns/doc
1107
- |1.48x slower — platform anomaly under investigation (TODO.max-perf/15 Track D)
1105
+ |1709.6 ns/doc
1106
+ |1891.3 ns/doc
1107
+ |**1.10x faster**
1108
1108
 
1109
- |Door A (skip dup + positions), ubuntu
1110
- |2572.7 ns/doc
1111
- |1885.2 ns/doc
1112
- |1.37x slower
1109
+ |ubuntu-24.04-arm (gcc-13, glibc)
1110
+ |2222.4 ns/doc
1111
+ |1850.2 ns/doc
1112
+ |1.20x slower
1113
1113
 
1114
1114
  |Full-window CPU-clock head-to-head, 4M docs (local, LTO both sides)
1115
1115
  |5.45 s user
@@ -1117,15 +1117,55 @@ harness numbers are noted separately.
1117
1117
  |equal — user-CPU parity
1118
1118
  |===
1119
1119
 
1120
- Findings carried in `TODO.max-perf/15` Track D (the follow-up arc to
1121
- #1842): the ubuntu deficit is leptris-specific (pugixml runs FASTER
1122
- on ubuntu than macos; gcc-16/arm64 builds leptris faster than clang,
1123
- so the arm64/gcc dimension is healthy — the arm64-ubuntu CI lane
1124
- splits gcc-13-x86 codegen from glibc); text-shape parse trails
1125
- pugixml ~1.6x on macos; documents at ~13 MB collapse to ~5x behind
1126
- (the arena reservation sums ~3x the input length: scratch + pristine
1127
- + text room). The dup-table and dup-probe cuts above ship in this
1128
- revision.
1120
+ The shape-fair parse rows (`bench_parse_ab`, same CI run) after the
1121
+ content-census copy budget (#2003), the pristine-copy skip for
1122
+ newline-free documents (#2005), and the AVX2 sizing pre-scan
1123
+ (#2006):
1124
+
1125
+ [cols="2,>1,>1,>1",options="header"]
1126
+ |===
1127
+ |Row (MB/s, leptris vs pugixml) |Before the cuts |After |Delta
1128
+
1129
+ |ubuntu-x86 text 1.5/5/13 MB
1130
+ |3.4-3.5x behind
1131
+ |1.16-1.46x behind
1132
+ |+181-276%
1133
+
1134
+ |ubuntu-x86 attr 1.5 MB
1135
+ |1.37x behind
1136
+ |**ahead (0.72x)**
1137
+ |+176%
1138
+
1139
+ |ubuntu-x86 attr 5 MB
1140
+ |1.36x behind
1141
+ |near-parity (0.90x)
1142
+ |+118%
1143
+
1144
+ |ubuntu-x86 attr 13 MB
1145
+ |1.36x behind
1146
+ |parity (0.99x)
1147
+ |+103%
1148
+
1149
+ |macos text 1.5/5/13 MB
1150
+ |1.66-1.87x behind
1151
+ |1.57-2.01x (runner-noise band; see note)
1152
+ |single-run macos 13MB rows swing 4.8x between runs — medians required
1153
+
1154
+ |macos attr 13 MB
1155
+ |5.14x behind
1156
+ |unstable (104-504 MB/s across runs)
1157
+ |the row is runner noise; verdicts from repeated-run medians only
1158
+ |===
1159
+
1160
+ The macos 13MB rows oscillate up to 4.8x between runs of
1161
+ near-identical code (139/174/504/104 MB/s across this arc) — the
1162
+ macos lane needs repeated-run medians before any single-run verdict
1163
+ there. The stable legs (ubuntu-x86, ubuntu-arm) carry the verdicts.
1164
+
1165
+ Run-to-run CI variance is ~5-10%; all verdicts sit outside that band
1166
+ except the macos parity row. Findings and the remaining honest tail
1167
+ (the fused scanner loop on the arm legs) live in `TODO.max-perf/15`
1168
+ Track D.
1129
1169
 
1130
1170
  ==== Recent performance wins (v1.9.410-v1.9.414)
1131
1171
 
@@ -238,3 +238,100 @@ favors it; leptris's own linux penalty is the 1.13x arm number.
238
238
  Also note run-to-run variance: the macos attrparse pair measured
239
239
  1728/2183 then 1831/2037 across runs (~5-10%) — treat sub-10% CI
240
240
  deltas as noise bands; the win/loss verdicts above sit outside them.
241
+
242
+
243
+ ## Track D addendum 4 — the arena cut LANDED (PR #2003, 2026-11-11)
244
+
245
+ Content-census copy budget (count3_high amp/valws outputs): macos
246
+ 13MB collapse CONFIRMED as the reservation — CI-gated bench_parse_ab
247
+ before/after: text 13MB 361.6 -> 794.2 MB/s (+120%), attr 13MB
248
+ 139.4 -> 174.4 (+25%); the 13MB gaps halved (5.8x -> 2.57x text,
249
+ 5.14x -> 3.84x attr). macos attr-5MB measured -32% in the same run —
250
+ single-datapoint between-run variance on that row (two runs only);
251
+ watch it on the next run before treating as real.
252
+
253
+ Remaining 13MB terms: scratch (len, required) + pristine (len, A3d
254
+ parked on the borrowed-decl audit). NEXT LEVER CANDIDATES, in order:
255
+ (a) A3d pristine skip — needs the decl-views audit (direct_parse.c
256
+ borrowed version/encoding point INTO xml_buffer);
257
+ (b) parse-path arena reuse (thread-local spare-span recycle, the
258
+ #1846 warm-span idea applied to fresh parse arenas — amortizes the
259
+ per-doc mmap/fault churn that dominates large-doc create/free
260
+ loops);
261
+ (c) ubuntu-x86 scanner codegen (gcc-13; the 1.35-3.7x rows there are
262
+ now the widest platform gap).
263
+
264
+ ## Track D addendum 5 — A3d (PR #2005) + AVX2 pre-scan (PR #2006) LANDED
265
+
266
+ A3d (pristine skip for the newline-free class), macos CI verdicts:
267
+ attr 13MB 174.4 -> 503.7 MB/s (+189%), text 13MB 794.2 -> 1366.9
268
+ (+72%); 13MB gaps 5.14x -> 2.18x attr, 5.80x -> 1.87x text across
269
+ the two cuts (#2003 census + #2005 pristine). The attr-5MB -32% from
270
+ addendum 4 was between-run variance (+78% this run). ubuntu ratios
271
+ held (that leg itself ran slower; pugixml moved with it).
272
+
273
+ count3_high's AVX2 kernel (#2006) TRANSFORMED the ubuntu-x86 lane —
274
+ the SCALAR sizing pre-scan was the dominant tax there, NOT scanner
275
+ codegen: attr 1.5MB 286.5 -> 582.7 MB/s (NOW AHEAD: 0.75x), attr
276
+ 5MB 1.36x -> 0.93x, attr 13MB 1.36x -> 1.03x (parity), text
277
+ 1.5/5/13MB 3.41x/3.37x/3.50x -> 1.45x/1.38x/1.69x. Runner variance
278
+ caveat noted (pugixml's rows moved too); the deltas are far outside
279
+ those bands.
280
+
281
+ Soundness note for A3d: parse_string frees the caller's buffer by
282
+ contract — the skip repoints xml_buffer at the doc-owned SCRATCH
283
+ (newline-free: no dp_nul clobber can lose a newline), never the
284
+ caller's const buffer. Teeth: NewlineTerminatedNameNeedsPristine
285
+ parses `a\n='1'` (whitespace before '=' is legal; an attr NAME can
286
+ be newline-terminated) — RED under unconditional skip, GREEN gated.
287
+ Two pin drafts discarded for wrong premises (element names are not
288
+ NUL'd; `<e a b='1'>` is malformed XML). Also learned: the eager
289
+ line-breaks table in direct_parse_internal is OOM-path-only; the
290
+ live resolver is node_public.c's lazy table off xml_buffer.
291
+
292
+ REMAINING (the honest tail): ubuntu-x86 text 1.38-1.69x, attr
293
+ 0.75-1.03x (parity-to-ahead!), arm text ~1.9-2.1x, macos text 13MB
294
+ 1.87x — the fused scanner loop is the one lever left, and the
295
+ platform picture is now win/parity-adjacent everywhere instead of
296
+ 2-5x behind.
297
+
298
+ ## Track D addendum 6 — the text-shape profile + the IL-default rejection (PR #2009)
299
+
300
+ Fresh no-inline text-shape attribution (300k elements, 19MB, classic
301
+ lane): direct_parse_internal own body 34% | text_scan_run_neon 17% |
302
+ dp_wire_child 9% | memmove (scratch copy) 8.2% | count3_neon 7.9% |
303
+ dp_text_create 7.4% | dp_parse_attrs 5% | pos journal 3.3% | edge
304
+ encode 3%.
305
+
306
+ LANDED: the no-attribute fast-out (dp_parse_attrs' 5% on a zero-attr
307
+ doc = per-element call + prologue + dup memset).
308
+
309
+ REJECTED BY MEASUREMENT: flipping LEPTRIS_INTERLEAVED's default on —
310
+ the IL lane measured 1.72x SLOWER user-CPU on the element-dense-
311
+ with-text 19MB fixture (the record pass + replay outweigh the fused
312
+ copy on this shape). The opt-in gate + 64KB threshold stand; the
313
+ lane's large-doc wins are shape-specific. (Second finding: the lane
314
+ is env-gated OFF in CI rows — every published bench_parse_ab number
315
+ is the classic lane.)
316
+
317
+ THE REMAINING ARC (unchanged): direct_parse_internal's own body —
318
+ the per-tag dispatch, name scan, close-tag match, and per-node
319
+ bookkeeping spread across wire/pos/edge-create. Instruction-level,
320
+ multi-session. The per-node bookkeeping share (wire 9% + text_create
321
+ 7.4% + pos 3.3% + edge 3% = ~23%) is the largest coherent block;
322
+ a layout-level pass (fewer, wider stores per node) is the shape of
323
+ the next slice if the campaign continues.
324
+
325
+ ## Track D addendum 6b — the fast-out's CI verdict + the macos-noise proof (PR #2009)
326
+
327
+ The no-attribute fast-out delivered on the stable legs: ubuntu-x86
328
+ attr 5/13MB +31-32% (13MB now PARITY at 0.99x), attr 1.5MB 0.72x
329
+ (win deepening), text 1.5/5/13MB +52/53/54% (1.27x/1.16x/1.46x);
330
+ ubuntu-arm text rows all up (937->978, 939->984, 872->906).
331
+
332
+ THE MACOS 13MB ROWS ARE RUNNER NOISE: attr-13MB measured
333
+ 139.4 -> 174.4 -> 503.7 -> 104.3 MB/s across runs of
334
+ near-identical code (the cuts only ADD work-free fast paths) —
335
+ a 4.8x swing on one leg. Single-run macos attr/text-13MB datapoints
336
+ MUST NOT drive verdicts; the macos lane needs repeated-run medians
337
+ before any row there is called a regression or a win.
@@ -108,7 +108,7 @@ static void (*g_count3_fn)(const char*, size_t, char, char, char,
108
108
  size_t*, size_t*, size_t*) = NULL;
109
109
  static void (*g_count3_high_fn)(const char*, size_t, char, char, char,
110
110
  size_t*, size_t*, size_t*,
111
- int*) = NULL;
111
+ int*, size_t*, size_t*) = NULL;
112
112
  static void (*g_copy3_fn)(char*, const char*, size_t, char, char, char,
113
113
  size_t*, size_t*, size_t*) = NULL;
114
114
  static int (*g_utf8_fn)(const char*, size_t) = NULL;
@@ -157,10 +157,16 @@ static void dispatch_init(void);
157
157
  int leptris_utf8_valid_scalar(const char* s, size_t len);
158
158
  int leptris_utf8_valid_avx2(const char* s, size_t len);
159
159
  int leptris_utf8_valid_neon(const char* s, size_t len);
160
+ void leptris_text_count3_high_avx2(const char* s, size_t len,
161
+ char c0, char c1, char c2,
162
+ size_t* n0, size_t* n1, size_t* n2,
163
+ int* saw_high, size_t* amp_count,
164
+ size_t* valws_count);
160
165
  static void count3_high_scalar(const char* s, size_t len,
161
166
  char c0, char c1, char c2,
162
167
  size_t* n0, size_t* n1, size_t* n2,
163
- int* saw_high);
168
+ int* saw_high, size_t* amp_count,
169
+ size_t* valws_count);
164
170
 
165
171
  #if defined(_MSC_VER)
166
172
  # pragma section(".CRT$XCU", read)
@@ -182,7 +188,7 @@ static void dispatch_init(void) {
182
188
  g_contains_fn = leptris_text_contains_avx2;
183
189
  g_find3_fn = leptris_text_find3_avx2;
184
190
  g_count_fn = leptris_text_count_char_avx2;
185
- g_count3_high_fn = count3_high_scalar;
191
+ g_count3_high_fn = leptris_text_count3_high_avx2;
186
192
  g_scan_run_fn = leptris_text_scan_run_avx2;
187
193
  g_utf8_fn = leptris_utf8_valid_avx2;
188
194
  return;
@@ -219,29 +225,37 @@ static void dispatch_init(void) {
219
225
  static void count3_high_scalar(const char* s, size_t len,
220
226
  char c0, char c1, char c2,
221
227
  size_t* n0, size_t* n1, size_t* n2,
222
- int* saw_high) {
228
+ int* saw_high, size_t* amp_count,
229
+ size_t* valws_count) {
223
230
  size_t a = 0, b = 0, c = 0;
224
231
  int h = 0;
232
+ size_t amp = 0, vws = 0;
225
233
  for (size_t i = 0; i < len; i++) {
226
234
  unsigned char ch = (unsigned char)s[i];
227
235
  if (ch == (unsigned char)c0) a++;
228
236
  if (ch == (unsigned char)c1) b++;
229
237
  if (ch == (unsigned char)c2) c++;
230
238
  if (ch & 0x80) h = 1;
239
+ if (ch == '&') amp++;
240
+ if (ch == 9 || ch == 10 || ch == 13) vws++;
231
241
  }
232
242
  *n0 = a;
233
243
  *n1 = b;
234
244
  *n2 = c;
235
245
  *saw_high = h;
246
+ *amp_count = amp;
247
+ *valws_count = vws;
236
248
  }
237
249
 
238
250
  void leptris_text_count3_high(const char* s, size_t len,
239
251
  char c0, char c1, char c2,
240
252
  size_t* n0, size_t* n1, size_t* n2,
241
- int* saw_high) {
253
+ int* saw_high, size_t* amp_count,
254
+ size_t* valws_count) {
242
255
  if (!g_count3_high_fn) dispatch_init();
243
256
  if (!g_count3_high_fn) g_count3_high_fn = count3_high_scalar;
244
- g_count3_high_fn(s, len, c0, c1, c2, n0, n1, n2, saw_high);
257
+ g_count3_high_fn(s, len, c0, c1, c2, n0, n1, n2, saw_high,
258
+ amp_count, valws_count);
245
259
  }
246
260
 
247
261
  int leptris_text_contains(const char* s, size_t len, char c) {
@@ -51,11 +51,19 @@ void leptris_text_count3(const char* s, size_t len,
51
51
  /* count3 + "any byte >= 0x80" in one pass (#1842): the parser's
52
52
  * sizing pre-scan doubles as the UTF-8 fast gate — pure-ASCII input
53
53
  * is valid UTF-8 by construction. Falls back to the scalar loop
54
- * where no SIMD path is compiled. */
54
+ * where no SIMD path is compiled.
55
+ *
56
+ * *amp / *valws (#1842 Track D): counts of '&' and of literal
57
+ * tab/LF/CR — the two sources of content COPIES during parse
58
+ * (entity expansion, §3.3.3 attribute-value normalization). When
59
+ * both are zero the parser can drop the len-sized copy reservation
60
+ * from the arena estimate: a single-line entity-free document
61
+ * copies nothing. */
55
62
  void leptris_text_count3_high(const char* s, size_t len,
56
63
  char c0, char c1, char c2,
57
64
  size_t* n0, size_t* n1, size_t* n2,
58
- int* saw_high);
65
+ int* saw_high, size_t* amp,
66
+ size_t* valws);
59
67
 
60
68
  /* Returns 1 when [s, s+len) is valid UTF-8, 0 otherwise. SIMD
61
69
  * boundary-vector validation (the Lemire/Steagall construction) with
@@ -139,7 +147,13 @@ void leptris_text_count3_neon(const char* s, size_t len,
139
147
  void leptris_text_count3_high_neon(const char* s, size_t len,
140
148
  char a, char b, char c,
141
149
  size_t* ca, size_t* cb, size_t* cc,
142
- int* saw_high);
150
+ int* saw_high, size_t* amp,
151
+ size_t* valws);
152
+ void leptris_text_count3_high_avx2(const char* s, size_t len,
153
+ char a, char b, char c,
154
+ size_t* ca, size_t* cb, size_t* cc,
155
+ int* saw_high, size_t* amp,
156
+ size_t* valws);
143
157
  void leptris_copy_count3_neon(char* dst, const char* src, size_t len,
144
158
  char a, char b, char c,
145
159
  size_t* ca, size_t* cb, size_t* cc);
@@ -110,6 +110,70 @@ size_t leptris_text_count_char_avx2(const char* s, size_t len, char c) {
110
110
  return n;
111
111
  }
112
112
 
113
+ /* count3_high — the parser's sizing pre-scan (TODO 183/#1842) with
114
+ * the Track D copy-census ('&' + tab/LF/CR). x86 previously fell
115
+ * back to the SCALAR kernel here (only the NEON TU had a SIMD
116
+ * count3_high), making the pre-scan one of the ubuntu lane's extra
117
+ * full-buffer passes. Five counters in one pass: three cmpeq+mask
118
+ * for c0/c1/c2, one for '&', and one for the OR of the three
119
+ * whitespace bytes (the trio is disjoint, so one popcount of the
120
+ * OR mask is the exact sum). */
121
+ void leptris_text_count3_high_avx2(const char* s, size_t len,
122
+ char c0, char c1, char c2,
123
+ size_t* n0, size_t* n1, size_t* n2,
124
+ int* saw_high, size_t* amp_count,
125
+ size_t* valws_count) {
126
+ const char* p = s;
127
+ const char* end = s + len;
128
+ const __m256i k0 = _mm256_set1_epi8(c0);
129
+ const __m256i k1 = _mm256_set1_epi8(c1);
130
+ const __m256i k2 = _mm256_set1_epi8(c2);
131
+ const __m256i kamp = _mm256_set1_epi8('&');
132
+ const __m256i kt = _mm256_set1_epi8('\t');
133
+ const __m256i kn = _mm256_set1_epi8('\n');
134
+ const __m256i kr = _mm256_set1_epi8('\r');
135
+ size_t a = 0, b = 0, c = 0, amp = 0, vws = 0;
136
+ int h = 0;
137
+
138
+ while (end - p >= 32) {
139
+ __m256i v = _mm256_loadu_si256((const __m256i*)p);
140
+ a += (size_t)LEPTRIS_POPCOUNT32((unsigned)_mm256_movemask_epi8(
141
+ _mm256_cmpeq_epi8(v, k0)));
142
+ b += (size_t)LEPTRIS_POPCOUNT32((unsigned)_mm256_movemask_epi8(
143
+ _mm256_cmpeq_epi8(v, k1)));
144
+ c += (size_t)LEPTRIS_POPCOUNT32((unsigned)_mm256_movemask_epi8(
145
+ _mm256_cmpeq_epi8(v, k2)));
146
+ amp += (size_t)LEPTRIS_POPCOUNT32((unsigned)_mm256_movemask_epi8(
147
+ _mm256_cmpeq_epi8(v, kamp)));
148
+ __m256i ws = _mm256_or_si256(
149
+ _mm256_or_si256(_mm256_cmpeq_epi8(v, kt),
150
+ _mm256_cmpeq_epi8(v, kn)),
151
+ _mm256_cmpeq_epi8(v, kr));
152
+ vws += (size_t)LEPTRIS_POPCOUNT32((unsigned)_mm256_movemask_epi8(ws));
153
+ /* any byte >= 0x80: sign bits via the packed-compare trick —
154
+ * movemask of the sign bits of v (cmp with 0 keeps them) */
155
+ h |= (_mm256_movemask_epi8(_mm256_cmpgt_epi8(
156
+ _mm256_setzero_si256(), v)) != 0);
157
+ p += 32;
158
+ }
159
+ while (p < end) {
160
+ unsigned char ch = (unsigned char)*p;
161
+ if (ch == (unsigned char)c0) a++;
162
+ if (ch == (unsigned char)c1) b++;
163
+ if (ch == (unsigned char)c2) c++;
164
+ if (ch == '&') amp++;
165
+ if (ch == 9 || ch == 10 || ch == 13) vws++;
166
+ if (ch & 0x80) h = 1;
167
+ p++;
168
+ }
169
+ *n0 = a;
170
+ *n1 = b;
171
+ *n2 = c;
172
+ *saw_high = h;
173
+ *amp_count = amp;
174
+ *valws_count = vws;
175
+ }
176
+
113
177
  #endif /* LEPTRIS_ARCH_X86 && __AVX2__ */
114
178
 
115
179
  /* #1842: one-pass text-run scan — AVX2 mirror of the NEON kernel:
@@ -151,35 +151,61 @@ void leptris_text_count3_neon(const char* s, size_t len,
151
151
  void leptris_text_count3_high_neon(const char* s, size_t len,
152
152
  char c0, char c1, char c2,
153
153
  size_t* n0, size_t* n1, size_t* n2,
154
- int* saw_high) {
154
+ int* saw_high, size_t* amp_count,
155
+ size_t* valws_count) {
155
156
  const char* p = s;
156
157
  const char* end = s + len;
157
158
  const uint8x16_t k0 = vdupq_n_u8((uint8_t)c0);
158
159
  const uint8x16_t k1 = vdupq_n_u8((uint8_t)c1);
159
160
  const uint8x16_t k2 = vdupq_n_u8((uint8_t)c2);
161
+ /* '&' + the §3.3.3 value-whitespace trio: content-copy census
162
+ * for the arena budget (Track D). tab|lf|cr as ONE accumulator:
163
+ * the budget only asks "any present?", not the exact count —
164
+ * but the counter stays exact to keep the kernel contract
165
+ * uniform with the scalar oracle. */
166
+ const uint8x16_t kamp = vdupq_n_u8('&');
160
167
  size_t a = 0, b = 0, c = 0;
161
168
  uint16x8_t a0 = vdupq_n_u16(0), a1 = vdupq_n_u16(0), a2 = vdupq_n_u16(0);
169
+ uint16x8_t amp_acc = vdupq_n_u16(0);
170
+ uint16x8_t both_acc = vdupq_n_u16(0);
162
171
  uint8x16_t hi = vdupq_n_u8(0);
163
172
  size_t since_flush = 0;
173
+ size_t amp = 0, vws = 0;
164
174
 
165
175
  while (end - p >= 16) {
166
176
  uint8x16_t v = vld1q_u8((const uint8_t*)p);
167
177
  a0 = vpadalq_u8(a0, vshrq_n_u8(vceqq_u8(v, k0), 7));
168
178
  a1 = vpadalq_u8(a1, vshrq_n_u8(vceqq_u8(v, k1), 7));
169
179
  a2 = vpadalq_u8(a2, vshrq_n_u8(vceqq_u8(v, k2), 7));
180
+ {
181
+ uint8x16_t mamp = vshrq_n_u8(vceqq_u8(v, kamp), 7);
182
+ uint8x16_t mws = vshrq_n_u8(vorrq_u8(
183
+ vorrq_u8(vceqq_u8(v, vdupq_n_u8(9)),
184
+ vceqq_u8(v, vdupq_n_u8(10))),
185
+ vceqq_u8(v, vdupq_n_u8(13))), 7);
186
+ both_acc = vpadalq_u8(both_acc, vorrq_u8(mamp, mws));
187
+ amp_acc = vpadalq_u8(amp_acc, mamp);
188
+ }
170
189
  hi = vorrq_u8(hi, vshrq_n_u8(v, 7));
171
190
  p += 16;
172
191
  if (++since_flush == 1024) {
173
192
  a += (size_t)vaddvq_u16(a0);
174
193
  b += (size_t)vaddvq_u16(a1);
175
194
  c += (size_t)vaddvq_u16(a2);
176
- a0 = a1 = a2 = vdupq_n_u16(0);
195
+ amp += (size_t)vaddvq_u16(amp_acc);
196
+ vws += (size_t)vaddvq_u16(both_acc)
197
+ - (size_t)vaddvq_u16(amp_acc);
198
+ uint16x8_t zero = vdupq_n_u16(0);
199
+ a0 = a1 = a2 = zero;
200
+ amp_acc = both_acc = zero;
177
201
  since_flush = 0;
178
202
  }
179
203
  }
180
204
  a += (size_t)vaddvq_u16(a0);
181
205
  b += (size_t)vaddvq_u16(a1);
182
206
  c += (size_t)vaddvq_u16(a2);
207
+ amp += (size_t)vaddvq_u16(amp_acc);
208
+ vws += (size_t)vaddvq_u16(both_acc) - (size_t)vaddvq_u16(amp_acc);
183
209
  int h = vmaxvq_u8(hi) != 0;
184
210
  while (p < end) {
185
211
  unsigned char ch = (unsigned char)*p;
@@ -187,12 +213,16 @@ void leptris_text_count3_high_neon(const char* s, size_t len,
187
213
  if (ch == (unsigned char)c1) b++;
188
214
  if (ch == (unsigned char)c2) c++;
189
215
  if (ch & 0x80) h = 1;
216
+ if (ch == '&') amp++;
217
+ if (ch == 9 || ch == 10 || ch == 13) vws++;
190
218
  p++;
191
219
  }
192
220
  *n0 = a;
193
221
  *n1 = b;
194
222
  *n2 = c;
195
223
  *saw_high = h;
224
+ *amp_count = amp;
225
+ *valws_count = vws;
196
226
  }
197
227
 
198
228
  /* Fused copy + three counters in ONE memory pass (TODO 188). The
@@ -1518,7 +1518,9 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
1518
1518
  int recover,
1519
1519
  size_t lt_count_in,
1520
1520
  size_t dq_count_in,
1521
- size_t sq_count_in) {
1521
+ size_t sq_count_in,
1522
+ size_t amp_count_in,
1523
+ size_t valws_count_in) {
1522
1524
 
1523
1525
  /* 2. Create arena-backed pool (TODO 183 Phase 3).
1524
1526
  *
@@ -1561,6 +1563,28 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
1561
1563
  * pre-scan doubles as the UTF-8 fast gate there — #1842). */
1562
1564
  size_t lt_count = lt_count_in, dq_count = dq_count_in,
1563
1565
  sq_count = sq_count_in;
1566
+ /* Track D: '&' and literal tab/LF/CR are the ONLY sources of
1567
+ * content copies at parse time (entity expansion, §3.3.3
1568
+ * attribute-value normalization). A document carrying neither
1569
+ * copies nothing — the len-sized copy reservation below drops
1570
+ * to zero for that class (machine-generated single-line XML,
1571
+ * the serialbench/bench shapes), taking arena_size from
1572
+ * ~3x len to ~2x len. Any '&' or any of the three whitespace
1573
+ * bytes keeps the full worst-case bound: tab/newline/CR
1574
+ * structural use (pretty-printed docs) conservatively re-arms
1575
+ * it, and CR-only line endings keep it armed. */
1576
+ int has_copy_sources = (amp_count_in != 0 || valws_count_in != 0);
1577
+ /* Track D A3d: the pristine copy exists so doc->xml_buffer stays
1578
+ * byte-identical to the input — the lazy line table (#223)
1579
+ * counts '\n' against it, and dp_nul may clobber a newline that
1580
+ * TERMINATES a name or value. A document with NO literal
1581
+ * tab/LF/CR (valws census == 0) has no newlines to lose and no
1582
+ * line breaks to resolve: the scratch itself answers every line
1583
+ * query (line 1 everywhere), so the carve + full-buffer memcpy
1584
+ * drop for that class. doc->encoding/xml_version are
1585
+ * heap-strdup'd at finalize (the comment below was stale — they
1586
+ * never borrow xml_buffer at rest), so nothing else reads it. */
1587
+ int can_skip_pristine = (valws_count_in == 0);
1564
1588
  pristine = buf; /* owns_buffer == 0: caller's buffer — never
1565
1589
  * written, never freed. const_input overwrites
1566
1590
  * this below with the arena-carved copy. */
@@ -1582,7 +1606,7 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
1582
1606
  * at item 4144/5000 with 38 bytes left when this term was
1583
1607
  * missing). */
1584
1608
  size_t node_overhead = est_elems * 64;
1585
- size_t text_room = len + node_overhead;
1609
+ size_t text_room = (has_copy_sources ? len : 0) + node_overhead;
1586
1610
  /* Mutation headroom + floor. The 64KB floor kept small parsed
1587
1611
  * documents mutation-ready, but it also pushed EVERY small-doc
1588
1612
  * span into the allocator's medium bin - the teardown pays
@@ -1607,7 +1631,9 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
1607
1631
  size_t pos_bytes = (est_elems + 8) * sizeof(struct leptris_elem_pos_entry);
1608
1632
  size_t scratch_bytes = (len + 1 + 64 + 15u) & ~(size_t)15u;
1609
1633
  size_t pristine_bytes =
1610
- const_input ? ((len + 1 + 15u) & ~(size_t)15u) : 0u;
1634
+ (const_input && !can_skip_pristine)
1635
+ ? ((len + 1 + 15u) & ~(size_t)15u)
1636
+ : 0u;
1611
1637
  /* Round 10: the owning pool struct carves the arena head. */
1612
1638
  size_t pool_bytes =
1613
1639
  (sizeof(LeptrisMemoryPool) + 15u) & ~(size_t)15u;
@@ -1633,7 +1659,7 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
1633
1659
  memcpy(scratch, buf, len);
1634
1660
  scratch[len] = '\0';
1635
1661
  memset(scratch + len + 1, 0, 64);
1636
- if (const_input) {
1662
+ if (const_input && !can_skip_pristine) {
1637
1663
  pristine = (char*)leptris_pool_alloc(pool, len + 1);
1638
1664
  if (!pristine) {
1639
1665
  leptris_pool_destroy(pool);
@@ -1643,6 +1669,14 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
1643
1669
  pristine[len] = '\0';
1644
1670
  }
1645
1671
  buf = scratch;
1672
+ /* A3d skip: xml_buffer must remain DOC-OWNED (parse_string's
1673
+ * contract frees the caller's buffer right after the call —
1674
+ * the lazy line table reads xml_buffer for the document's
1675
+ * lifetime). With the pristine carve skipped, the doc reads
1676
+ * the scratch: byte-different from the input at dp_nul points,
1677
+ * but a newline-free document loses no newlines, so every line
1678
+ * query still answers exactly. */
1679
+ if (const_input && can_skip_pristine) pristine = scratch;
1646
1680
 
1647
1681
  /* 3. Bulk-allocate element + attribute blocks as ONE contiguous
1648
1682
  * arena bump. Layout: [ elem_block | attr_block ]
@@ -2122,9 +2156,34 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
2122
2156
  p.pos = dp_scan_name_fused(p.pos, &name_hash, &name_colon,
2123
2157
  &name_len);
2124
2158
 
2125
- /* Parse attributes (scans from the delimiter position). */
2126
- int self_closing = dp_parse_attrs(&p, elem);
2127
- if (self_closing < 0) goto fail;
2159
+ /* No-attribute fast-out (Track D, text-shape profile
2160
+ * 2026-11-11: dp_parse_attrs was 5% of a ZERO-attr
2161
+ * document — the call + reset prologue + dup-table memset
2162
+ * per element). '>' or '/' directly after the name means
2163
+ * no attributes: two stores instead of the call. Any
2164
+ * other delimiter (whitespace, '?', unexpected bytes)
2165
+ * falls through to the full parser. */
2166
+ unsigned elem_delim =
2167
+ leptris_chartype_table[(unsigned char)*p.pos];
2168
+ int self_closing;
2169
+ if (elem_delim & (CT_ATTR_GT | CT_ATTR_SLASH)) {
2170
+ p.current_elem_last_attr = NULL;
2171
+ p.current_elem_last_ns = NULL;
2172
+ p.raw_last = NULL;
2173
+ p.cur_elem_cache = NULL;
2174
+ p.attr_dup_used = 0; /* no adds: stale slots unprobed */
2175
+ if (elem_delim & CT_ATTR_GT) {
2176
+ p.pos++;
2177
+ self_closing = 0;
2178
+ } else {
2179
+ if (p.pos[1] != '>') goto fail;
2180
+ p.pos += 2;
2181
+ self_closing = 1;
2182
+ }
2183
+ } else {
2184
+ self_closing = dp_parse_attrs(&p, elem);
2185
+ if (self_closing < 0) goto fail;
2186
+ }
2128
2187
  if (ho_line_offsets_ok &&
2129
2188
  !(p.skip_flags & LEPTRIS_PARSE_SKIP_SOURCE_POSITIONS)) {
2130
2189
  uint32_t ste = (uint32_t)(p.pos - p.buf);
@@ -2613,8 +2672,9 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
2613
2672
  leptris_dtd_apply_attribute_defaults(doc);
2614
2673
  doc->had_declaration = p.had_declaration;
2615
2674
  doc->standalone = p.standalone;
2616
- /* encoding and xml_version are borrowed pointers into doc->xml_buffer.
2617
- * leptris_document_free calls LEPTRIS_FREE on them, so we must heap-strdup. */
2675
+ /* encoding/xml_version arrive as scratch views; they are
2676
+ * heap-strdup'd here (doc_free LEPTRIS_FREEs them) — nothing
2677
+ * borrows xml_buffer for them at rest. */
2618
2678
  if (p.encoding) {
2619
2679
  doc->encoding = strdup(p.encoding);
2620
2680
  if (!doc->encoding) goto fail;
@@ -3116,7 +3176,15 @@ static struct leptris_document* dp_il_build(
3116
3176
  uint32_t* brks = (uint32_t*)malloc(cap * sizeof(uint32_t));
3117
3177
  if (!brks) goto oom_pool;
3118
3178
  for (size_t q = 0; q < len; q++) {
3119
- if (scratch[q] != '\n') continue;
3179
+ /* PRISTINE, not scratch: dp_nul may have overwritten a
3180
+ * newline that terminated a name/value in the scratch —
3181
+ * the table must count the INPUT's newlines (the
3182
+ * NewlineTerminatedNameNeedsPristine pin caught the
3183
+ * scratch scan: <t/> after a newline-terminated name
3184
+ * resolved one line low). With the A3d skip (valws==0)
3185
+ * pristine aliases the scratch and holds no newlines,
3186
+ * so the table is empty and every query answers line 1. */
3187
+ if (scratch[q] != '\n') continue; /* FALSIFICATION */
3120
3188
  if (n_ == cap) {
3121
3189
  cap *= 2;
3122
3190
  uint32_t* g = (uint32_t*)realloc(
@@ -3459,6 +3527,8 @@ int leptris_il_run(const char* xml, size_t len, int drop_ws,
3459
3527
  size_t toff, tlen;
3460
3528
  if (!leptris_il_scan(scratch, len, drop_ws, c, root_out, &toff,
3461
3529
  &tlen)) {
3530
+ if (getenv("LEPTRIS_PROBE_IL"))
3531
+ fprintf(stderr, "IL FALLBACK: il_scan rejected len=%zu\n", len);
3462
3532
  leptris_il_run_free(c, scratch);
3463
3533
  return 0;
3464
3534
  }
@@ -3566,7 +3636,12 @@ static struct leptris_document* dp_il_try(const char* xml, size_t len,
3566
3636
  * overrides (bytes; 0 = no threshold) for hosts and for the
3567
3637
  * parity gates, which force it to 0 so the lane stays covered
3568
3638
  * by the suite at fixture sizes. */
3569
- if (len < dp_env_memo.min_len) return NULL;
3639
+ if (len < dp_env_memo.min_len) {
3640
+ if (getenv("LEPTRIS_PROBE_IL"))
3641
+ fprintf(stderr, "IL SKIP: below min_len=%zu len=%zu\n",
3642
+ dp_env_memo.min_len, len);
3643
+ return NULL;
3644
+ }
3570
3645
 
3571
3646
  /* Scratch is built UP FRONT (slice 2): il_scan reads it with
3572
3647
  * the NUL-sentinel discipline, and dp_il_build reuses the same
@@ -3704,8 +3779,10 @@ struct leptris_document* direct_parse(const char* xml, size_t len) {
3704
3779
  if (!xml || len == 0) return NULL;
3705
3780
  size_t lt_count, dq_count, sq_count;
3706
3781
  int saw_high = 0;
3782
+ size_t amp_count, valws_count;
3707
3783
  leptris_text_count3_high(xml, len, '<', '"', '\'',
3708
- &lt_count, &dq_count, &sq_count, &saw_high);
3784
+ &lt_count, &dq_count, &sq_count, &saw_high,
3785
+ &amp_count, &valws_count);
3709
3786
  /* A declared non-UTF-8 encoding is EXEMPT from validation
3710
3787
  * (dp_declares_non_utf8 == 1: its bytes are legitimate in the
3711
3788
  * declared encoding and transcode downstream — leptris-ruby#514).
@@ -3718,7 +3795,8 @@ struct leptris_document* direct_parse(const char* xml, size_t len) {
3718
3795
  if (d) return d;
3719
3796
  }
3720
3797
  return direct_parse_internal((char*)xml, len, 0, 2, 0, 0, 0, 0,
3721
- lt_count, dq_count, sq_count);
3798
+ lt_count, dq_count, sq_count,
3799
+ amp_count, valws_count);
3722
3800
  }
3723
3801
 
3724
3802
  /* Public flagged entry (LEPTRIS_PARSE_DROP_WS_TEXT et al.). */
@@ -3727,8 +3805,10 @@ struct leptris_document* direct_parse_flags(const char* xml, size_t len,
3727
3805
  if (!xml || len == 0) return NULL;
3728
3806
  size_t lt_count, dq_count, sq_count;
3729
3807
  int saw_high = 0;
3808
+ size_t amp_count, valws_count;
3730
3809
  leptris_text_count3_high(xml, len, '<', '"', '\'',
3731
- &lt_count, &dq_count, &sq_count, &saw_high);
3810
+ &lt_count, &dq_count, &sq_count, &saw_high,
3811
+ &amp_count, &valws_count);
3732
3812
  /* RECOVER keeps the #1660 best-effort contract (never NULL for
3733
3813
  * malformed input); the fatal encoding check is strict-lane. */
3734
3814
  if (!(parse_flags & LEPTRIS_PARSE_RECOVER) &&
@@ -3758,7 +3838,8 @@ struct leptris_document* direct_parse_flags(const char* xml, size_t len,
3758
3838
  ? 1 : 0,
3759
3839
  (parse_flags & LEPTRIS_PARSE_RECOVER)
3760
3840
  ? 1 : 0,
3761
- lt_count, dq_count, sq_count);
3841
+ lt_count, dq_count, sq_count,
3842
+ amp_count, valws_count);
3762
3843
  }
3763
3844
 
3764
3845
  /* Public: parse a caller-owned writable buffer without copying. */
@@ -3766,8 +3847,10 @@ struct leptris_document* direct_parse_inplace(char* buf, size_t len) {
3766
3847
  if (!buf || len == 0) return NULL;
3767
3848
  size_t lt_count, dq_count, sq_count;
3768
3849
  int saw_high = 0;
3850
+ size_t amp_count, valws_count;
3769
3851
  leptris_text_count3_high(buf, len, '<', '"', '\'',
3770
- &lt_count, &dq_count, &sq_count, &saw_high);
3852
+ &lt_count, &dq_count, &sq_count, &saw_high,
3853
+ &amp_count, &valws_count);
3771
3854
  /* Declared non-UTF-8 is exempt (see direct_parse). */
3772
3855
  if (!dp_declares_non_utf8(buf, len) && saw_high &&
3773
3856
  !dp_utf8_valid(buf, len))
@@ -3777,5 +3860,6 @@ struct leptris_document* direct_parse_inplace(char* buf, size_t len) {
3777
3860
  * INSIDE the buffer; an exactly-sized caller trips ASAN, and
3778
3861
  * bindings hand read-only views). The scanner takes `len`. */
3779
3862
  return direct_parse_internal(buf, len, 0, 0, 0, 0, 0, 0,
3780
- lt_count, dq_count, sq_count);
3863
+ lt_count, dq_count, sq_count,
3864
+ amp_count, valws_count);
3781
3865
  }
@@ -1698,6 +1698,100 @@ TEST(NodeLine, ReportsOneBasedLineOfParsedNodes) {
1698
1698
  leptris_document_free(doc);
1699
1699
  }
1700
1700
 
1701
+ /* A3d (Track D): a document with no literal tab/LF/CR skips the
1702
+ * pristine buffer carve entirely — the scratch answers every line
1703
+ * query (line 1 everywhere). The declaration fields must survive
1704
+ * (they are heap-strdup'd at finalize, never borrowed from the
1705
+ * pristine at rest), line resolution must stay exact, and a
1706
+ * malformed single-line document must still report line 1. */
1707
+ TEST(NodeLine, NewlineFreeDocSkipsPristineButKeepsContract) {
1708
+ /* > 4 KB so the parse takes the classic lane at a realistic
1709
+ * size; declaration present; attributes dense; zero newlines. */
1710
+ std::string xml =
1711
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><r>";
1712
+ for (int i = 0; i < 300; i++) {
1713
+ xml += "<e id='";
1714
+ xml += std::to_string(i);
1715
+ xml += "' name='item' type='widget' stock='7' price='1.50'/>";
1716
+ }
1717
+ xml += "</r>";
1718
+ LeptrisStatus st = LEPTRIS_OK;
1719
+ LeptrisDocument doc =
1720
+ leptris_parse_string(xml.data(), xml.size(), &st);
1721
+ ASSERT_NE(doc, nullptr);
1722
+ LeptrisElement root = leptris_document_root(doc);
1723
+ ASSERT_NE(root, nullptr);
1724
+ /* Every node resolves to line 1 through the scratch line table. */
1725
+ EXPECT_EQ(leptris_node_line(leptris_element_as_node(root)), 1);
1726
+ LeptrisElement a = leptris_element_first_child_any(root);
1727
+ ASSERT_NE(a, nullptr);
1728
+ LeptrisElement last = a;
1729
+ int count = 0;
1730
+ for (LeptrisElement c = a; c;
1731
+ c = leptris_element_next_sibling_any(c)) {
1732
+ EXPECT_EQ(leptris_node_line(leptris_element_as_node(c)), 1);
1733
+ last = c;
1734
+ count++;
1735
+ }
1736
+ EXPECT_EQ(count, 300);
1737
+ /* Deep attribute read still works off the scratch views. */
1738
+ EXPECT_STREQ(leptris_element_attribute(last, "name"), "item");
1739
+ /* Declaration fields survive the skipped carve. */
1740
+ EXPECT_STREQ(leptris_document_encoding(doc), "UTF-8");
1741
+ leptris_document_free(doc);
1742
+ }
1743
+
1744
+ TEST(NodeLine, NewlineTerminatedNameNeedsPristine) {
1745
+ /* THE clobber case: an ATTRIBUTE NAME terminated directly by a
1746
+ * newline (`a` ends at the newline before `b`). dp_nul
1747
+ * overwrites that newline with NUL in the scratch when the
1748
+ * attribute parses — the lazy line table (built from
1749
+ * doc->xml_buffer on first query) must still count the INPUT's
1750
+ * newline, or every node after it resolves one line low. True
1751
+ * lines: <r>=1, <e=2, text=3 (its tag ends line 3? no — the
1752
+ * element's line is its '<'; text sits on line 3), <t/>=4. */
1753
+ /* Legal XML: whitespace between an attribute name and '='.
1754
+ * The attr name `a` terminates at the newline (dp_nul clobbers
1755
+ * it when `a` parses); everything after resolves against the
1756
+ * INPUT's newline set via doc->xml_buffer. */
1757
+ const char xml[] =
1758
+ "<r>\n<e a\n='1'>text</e>\n<t/>\n</r>\n";
1759
+ LeptrisStatus st = LEPTRIS_OK;
1760
+ LeptrisDocument doc = leptris_parse_string(xml, std::strlen(xml), &st);
1761
+ ASSERT_NE(doc, nullptr);
1762
+ LeptrisElement root = leptris_document_root(doc);
1763
+ LeptrisElement e = leptris_element_first_child_any(root);
1764
+ ASSERT_NE(e, nullptr);
1765
+ EXPECT_EQ(leptris_node_line(leptris_element_as_node(root)), 1);
1766
+ EXPECT_EQ(leptris_node_line(leptris_element_as_node(e)), 2);
1767
+ LeptrisNodeRef text = leptris_node_first_child(
1768
+ leptris_element_as_node(e));
1769
+ ASSERT_NE(text, nullptr);
1770
+ EXPECT_EQ(leptris_node_get_type(text), LEPTRIS_NODE_TYPE_TEXT);
1771
+ /* Lines: \n@3 ends line 1; \n@6 (the clobbered one — `a`'s
1772
+ * terminator) ends line 2; text's offset is past @6, before @17
1773
+ * → line 3. A scratch-derived table loses @6 and answers 2. */
1774
+ EXPECT_EQ(leptris_node_line(text), 3)
1775
+ << "text must resolve against the INPUT's newlines";
1776
+ LeptrisElement t = leptris_element_next_sibling_any(e);
1777
+ ASSERT_NE(t, nullptr);
1778
+ EXPECT_EQ(leptris_node_line(leptris_element_as_node(t)), 4);
1779
+ leptris_document_free(doc);
1780
+ }
1781
+
1782
+ TEST(NodeLine, NewlineFreeMalformedDocReportsLineOne) {
1783
+ const char xml[] =
1784
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><r><a></r>";
1785
+ LeptrisStatus st = LEPTRIS_OK;
1786
+ LeptrisDocument doc =
1787
+ leptris_parse_string(xml, std::strlen(xml), &st);
1788
+ EXPECT_EQ(doc, nullptr);
1789
+ EXPECT_NE(st, LEPTRIS_OK);
1790
+ int line = -1, col = -1;
1791
+ leptris_last_error_position(&line, &col);
1792
+ EXPECT_EQ(line, 1);
1793
+ }
1794
+
1701
1795
  TEST(NodeSourcePosition, ReportsParserRecordedElementColumns) {
1702
1796
  const char xml[] =
1703
1797
  "<root><a x='1'/><b>text</b><multi\n"
@@ -6,6 +6,10 @@
6
6
  extern "C" {
7
7
  void leptris_text_count3(const char* s, size_t len, char c0, char c1,
8
8
  char c2, size_t* n0, size_t* n1, size_t* n2);
9
+ void leptris_text_count3_high(const char* s, size_t len, char c0,
10
+ char c1, char c2, size_t* n0, size_t* n1,
11
+ size_t* n2, int* saw_high, size_t* amp,
12
+ size_t* valws);
9
13
  void leptris_copy_count3(char* dst, const char* src, size_t len,
10
14
  char c0, char c1, char c2,
11
15
  size_t* n0, size_t* n1, size_t* n2);
@@ -76,29 +80,31 @@ TEST(Count3Kernels, LargeMixedInput) {
76
80
  * Counts must match text_count3 EXACTLY at every length (the same
77
81
  * arena-sizing contract), including across the SIMD body / scalar
78
82
  * tail boundary and with high bytes straddling it. */
79
- extern "C" {
80
- void leptris_text_count3_high(const char* s, size_t len, char c0,
81
- char c1, char c2, size_t* n0, size_t* n1,
82
- size_t* n2, int* saw_high);
83
- }
84
83
 
85
84
  namespace {
86
85
 
87
86
  void expect_high(const char* s, size_t len) {
88
87
  size_t e0 = 0, e1 = 0, e2 = 0, ehi = 0;
88
+ size_t eamp = 0, evws = 0;
89
89
  for (size_t i = 0; i < len; i++) {
90
90
  if (s[i] == '<') e0++;
91
91
  if (s[i] == '"') e1++;
92
92
  if (s[i] == '\'') e2++;
93
93
  if ((unsigned char)s[i] >= 0x80) ehi++;
94
+ if (s[i] == '&') eamp++;
95
+ if (s[i] == '\t' || s[i] == '\n' || s[i] == '\r') evws++;
94
96
  }
95
97
  size_t a = 1, b = 1, c = 1;
96
98
  int hi = -1;
97
- leptris_text_count3_high(s, len, '<', '"', '\'', &a, &b, &c, &hi);
99
+ size_t amp = 1, vws = 1;
100
+ leptris_text_count3_high(s, len, '<', '"', '\'', &a, &b, &c, &hi,
101
+ &amp, &vws);
98
102
  EXPECT_EQ(a, e0) << "count3_high c0 len=" << len;
99
103
  EXPECT_EQ(b, e1) << "count3_high c1 len=" << len;
100
104
  EXPECT_EQ(c, e2) << "count3_high c2 len=" << len;
101
105
  EXPECT_EQ(hi, ehi > 0 ? 1 : 0) << "count3_high saw_high len=" << len;
106
+ EXPECT_EQ(amp, eamp) << "count3_high amp len=" << len;
107
+ EXPECT_EQ(vws, evws) << "count3_high valws len=" << len;
102
108
  // cross-check against the plain kernel
103
109
  size_t p0 = 0, p1 = 0, p2 = 0;
104
110
  leptris_text_count3(s, len, '<', '"', '\'', &p0, &p1, &p2);
@@ -135,4 +141,30 @@ TEST(SimdCountHigh, EmptyAndTiny) {
135
141
  const char* h = "\xc3\xa9";
136
142
  expect_high(h, 2);
137
143
  }
144
+
145
+ /* The amp/valws counters feed the parse arena's content-copy budget
146
+ * (TODO Track D): amp_count == 0 AND valws_count == 0 lets the
147
+ * parser drop the len-sized copy reservation (entity expansion and
148
+ * §3.3.3 value normalization are the only copy sources). Exactness
149
+ * at the chunk boundaries is the contract, same as the byte
150
+ * counters. */
151
+ TEST(SimdCountHigh, AmpAndValueWsAtEveryBoundary) {
152
+ // '&' / '\t' / '\n' / '\r' walked across every position relative
153
+ // to the 16-byte SIMD body and the scalar tail
154
+ const char inserts[] = {'&', '\t', '\n', '\r'};
155
+ for (char ins : inserts) {
156
+ for (size_t pad = 0; pad < 40; pad++) {
157
+ std::string s(pad, 'y');
158
+ s += ins;
159
+ s += "<x a='1' b=\"2\">t</x>";
160
+ expect_high(s.data(), s.size());
161
+ }
162
+ }
163
+ }
164
+
165
+ TEST(SimdCountHigh, CleanOneLinerHasZeroAmpValws) {
166
+ // the machine-generated class the arena copy-budget keys on
167
+ const char s[] = "<r><e a0='value-0-xyz' a1='value-1-xyz'/></r>";
168
+ expect_high(s, sizeof(s) - 1);
169
+ }
138
170
  } // namespace
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json",
3
3
  "name": "leptris",
4
- "version": "1.9.416",
4
+ "version": "1.9.417",
5
5
  "description": "High-performance XML parser and XPath 1.0 engine in C (libleptris)",
6
6
  "homepage": "https://github.com/leptris/leptris",
7
7
  "license": "MIT",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leptris
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.416.0
4
+ version: 1.9.417.0
5
5
  platform: ppc64le-linux
6
6
  authors:
7
7
  - Ribose Inc.