leptris 1.9.458.0-arm-linux → 1.9.460.0-arm-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.
Files changed (123) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -0
  3. data/CONTEXT.md +9 -3
  4. data/README.adoc +28 -17
  5. data/Rakefile +1 -1
  6. data/lib/leptris/version.rb +1 -1
  7. data/lib/leptris/xml/document.rb +75 -48
  8. data/lib/leptris/xml/ffi.rb +8 -0
  9. data/lib/libleptris.so +0 -0
  10. data/vendor-src/README.md +1 -1
  11. data/vendor-src/libleptris/CHANGELOG.md +42 -0
  12. data/vendor-src/libleptris/CMakeLists.txt +1 -1
  13. data/vendor-src/libleptris/README.adoc +19 -0
  14. data/vendor-src/libleptris/benchmarks/CMakeLists.txt +23 -2
  15. data/vendor-src/libleptris/benchmarks/README.adoc +15 -0
  16. data/vendor-src/libleptris/benchmarks/README.md +17 -4
  17. data/vendor-src/libleptris/benchmarks/comprehensive/benchmark_decomp.cpp +2 -1
  18. data/vendor-src/libleptris/benchmarks/comprehensive/benchmark_first_access.cpp +2 -1
  19. data/vendor-src/libleptris/benchmarks/comprehensive/benchmark_many_attrs.cpp +2 -1
  20. data/vendor-src/libleptris/benchmarks/comprehensive/benchmark_memory.cpp +2 -1
  21. data/vendor-src/libleptris/benchmarks/comprehensive/benchmark_parse.cpp +2 -2
  22. data/vendor-src/libleptris/benchmarks/comprehensive/benchmark_parse_ops.cpp +8 -4
  23. data/vendor-src/libleptris/benchmarks/comprehensive/benchmark_write.cpp +4 -2
  24. data/vendor-src/libleptris/benchmarks/dom/dom_benchmark_v2.cpp +8 -4
  25. data/vendor-src/libleptris/benchmarks/fixtures/serialbench_users_medium.xml +13004 -0
  26. data/vendor-src/libleptris/benchmarks/matrix/bench_matrix.cpp +21 -76
  27. data/vendor-src/libleptris/benchmarks/matrix/bench_matrix_fused.cpp +31 -0
  28. data/vendor-src/libleptris/benchmarks/matrix/bench_rows.h +94 -0
  29. data/vendor-src/libleptris/benchmarks/modification/bench_append_attr_pugixml.cpp +2 -1
  30. data/vendor-src/libleptris/benchmarks/parse/bench_parse_ab.cpp +2 -1
  31. data/vendor-src/libleptris/benchmarks/twins/generation_users.sh +49 -0
  32. data/vendor-src/libleptris/benchmarks/twins/generation_users_fixture.h +78 -0
  33. data/vendor-src/libleptris/benchmarks/twins/generation_users_leptris.c +122 -0
  34. data/vendor-src/libleptris/benchmarks/twins/generation_users_pugixml.cpp +231 -0
  35. data/vendor-src/libleptris/benchmarks/twins/parse_pugixml.cpp +1 -1
  36. data/vendor-src/libleptris/benchmarks/twins/parse_users_ab.cpp +158 -0
  37. data/vendor-src/libleptris/benchmarks/workflows/bench_pipeline_shapes.cpp +12 -4
  38. data/vendor-src/libleptris/benchmarks/xpath/bench_pugixml.cpp +2 -1
  39. data/vendor-src/libleptris/cli/CLI_ARCHITECTURE.md +9 -1
  40. data/vendor-src/libleptris/cli/commands/diff.c +26 -28
  41. data/vendor-src/libleptris/cli/commands/format.c +3 -1
  42. data/vendor-src/libleptris/cli/commands/parse.c +3 -1
  43. data/vendor-src/libleptris/cli/commands/validate.c +22 -24
  44. data/vendor-src/libleptris/cli/commands/xpath.c +9 -1
  45. data/vendor-src/libleptris/cli/commands/xquery.c +12 -3
  46. data/vendor-src/libleptris/cli/commands/xslt.c +24 -8
  47. data/vendor-src/libleptris/cli/error.c +91 -0
  48. data/vendor-src/libleptris/cli/error.h +84 -0
  49. data/vendor-src/libleptris/cli/man/leptris-parse.1.adoc +21 -10
  50. data/vendor-src/libleptris/cli/man/leptris.1.adoc +19 -0
  51. data/vendor-src/libleptris/cli/output.c +18 -9
  52. data/vendor-src/libleptris/cli/output.h +17 -0
  53. data/vendor-src/libleptris/src/CMakeLists.txt +3 -0
  54. data/vendor-src/libleptris/src/include/leptris/types.h +25 -1
  55. data/vendor-src/libleptris/src/include/leptris/xquery/xquery.h +9 -1
  56. data/vendor-src/libleptris/src/include/leptris.h +48 -3
  57. data/vendor-src/libleptris/src/leptris/common/entities.c +2 -2
  58. data/vendor-src/libleptris/src/leptris/common/entities.h +8 -2
  59. data/vendor-src/libleptris/src/leptris/common/port.h +62 -0
  60. data/vendor-src/libleptris/src/leptris/common/uri_base.c +284 -0
  61. data/vendor-src/libleptris/src/leptris/common/uri_base.h +62 -0
  62. data/vendor-src/libleptris/src/leptris/core.c +33 -2
  63. data/vendor-src/libleptris/src/leptris/dom/compact.h +51 -0
  64. data/vendor-src/libleptris/src/leptris/dom/element.c +18 -0
  65. data/vendor-src/libleptris/src/leptris/dom/element.h +201 -12
  66. data/vendor-src/libleptris/src/leptris/dom/element_modify.c +611 -210
  67. data/vendor-src/libleptris/src/leptris/dom/element_query.c +72 -1
  68. data/vendor-src/libleptris/src/leptris/dom/name_intern.c +3 -1
  69. data/vendor-src/libleptris/src/leptris/dtd/model.c +13 -0
  70. data/vendor-src/libleptris/src/leptris/dtd/model.h +95 -6
  71. data/vendor-src/libleptris/src/leptris/dtd/resolver.c +302 -215
  72. data/vendor-src/libleptris/src/leptris/flat/direct_parse.c +801 -294
  73. data/vendor-src/libleptris/src/leptris/leptris.c +20 -5
  74. data/vendor-src/libleptris/src/leptris/leptris_internal.h +68 -22
  75. data/vendor-src/libleptris/src/leptris/serialize/serialize.c +289 -57
  76. data/vendor-src/libleptris/src/leptris/xinclude/xinclude.c +14 -125
  77. data/vendor-src/libleptris/src/leptris/xpath/evaluator.c +3 -0
  78. data/vendor-src/libleptris/src/leptris/xpath/evaluator_path.c +11 -8
  79. data/vendor-src/libleptris/src/leptris/xpath/functions_ext31.c +188 -24
  80. data/vendor-src/libleptris/src/leptris/xpath/vm.c +3 -1
  81. data/vendor-src/libleptris/src/leptris/xquery/xquery.c +5 -0
  82. data/vendor-src/libleptris/src/leptris/xsd/compile.c +59 -20
  83. data/vendor-src/libleptris/src/leptris/xsd/validate.c +291 -168
  84. data/vendor-src/libleptris/test/CMakeLists.txt +28 -0
  85. data/vendor-src/libleptris/test/abi/test_public_surface.cpp +37 -0
  86. data/vendor-src/libleptris/test/cli/test_cli.cpp +228 -0
  87. data/vendor-src/libleptris/test/common/hang_guard.h +179 -0
  88. data/vendor-src/libleptris/test/dom/test_compact.cpp +46 -0
  89. data/vendor-src/libleptris/test/dom/test_mut_append_contracts.cpp +173 -0
  90. data/vendor-src/libleptris/test/dom/test_mut_set_attr_contracts.cpp +771 -7
  91. data/vendor-src/libleptris/test/fixtures/entity_limits/good.xml +100 -0
  92. data/vendor-src/libleptris/test/fixtures/entity_limits/good_attr.xml +100 -0
  93. data/vendor-src/libleptris/test/fixtures/entity_limits/lol_classic.xml +51 -0
  94. data/vendor-src/libleptris/test/fixtures/entity_limits/lol_ig_attr.xml +23 -0
  95. data/vendor-src/libleptris/test/fixtures/entity_limits/lol_ig_content.xml +23 -0
  96. data/vendor-src/libleptris/test/fixtures/entity_limits/lol_long_name.xml +23 -0
  97. data/vendor-src/libleptris/test/memory/test_oom_injection.cpp +63 -0
  98. data/vendor-src/libleptris/test/parser/test_entity_limits.cpp +404 -0
  99. data/vendor-src/libleptris/test/parser/test_parser.cpp +126 -0
  100. data/vendor-src/libleptris/test/serializer/test_serialize.cpp +491 -0
  101. data/vendor-src/libleptris/test/xpath/test_base_uri.cpp +368 -0
  102. data/vendor-src/libleptris/test/xpath/test_xpath.cpp +86 -0
  103. data/vendor-src/libleptris/test/xsd/test_xsd11_base_uri.cpp +145 -0
  104. data/vendor-src/libleptris/test/xsd/test_xsd11_conformance.cpp +23 -5
  105. data/vendor-src/libleptris/test/xsd/test_xsd11_cta_context.cpp +385 -0
  106. data/vendor-src/libleptris/test/xsd/xsd11-all/all-restrict.xsd +3 -2
  107. data/vendor-src/libleptris/test/xsd/xsd11-all/all-wildcard.xsd +3 -2
  108. data/vendor-src/libleptris/test/xsd/xsd11-cases/MANIFEST.tsv +19 -2
  109. data/vendor-src/libleptris/test/xsd/xsd11-cases/saxonData/CTA/cta0003.n01.xml +5 -0
  110. data/vendor-src/libleptris/test/xsd/xsd11-cases/saxonData/CTA/cta0003.v01.xml +4 -0
  111. data/vendor-src/libleptris/test/xsd/xsd11-cases/saxonData/CTA/cta0003.xsd +46 -0
  112. data/vendor-src/libleptris/test/xsd/xsd11-cases/saxonData/CTA/cta0006.v01.xml +6 -0
  113. data/vendor-src/libleptris/test/xsd/xsd11-cases/saxonData/CTA/cta0006.xsd +83 -0
  114. data/vendor-src/libleptris/test/xsd/xsd11-cases/saxonData/CTA/cta0017.v01.xml +3 -0
  115. data/vendor-src/libleptris/test/xsd/xsd11-cases/saxonData/CTA/cta0017.xsd +36 -0
  116. data/vendor-src/libleptris/test/xsd/xsd11-cases/saxonData/CTA/cta0021.n01.xml +3 -0
  117. data/vendor-src/libleptris/test/xsd/xsd11-cases/saxonData/CTA/cta0021.v01.xml +3 -0
  118. data/vendor-src/libleptris/test/xsd/xsd11-cases/saxonData/CTA/cta0021.xsd +36 -0
  119. data/vendor-src/libleptris/test/xsd/xsd11-cases/saxonData/CTA/cta0023.v01.xml +3 -0
  120. data/vendor-src/libleptris/test/xsd/xsd11-cases/saxonData/CTA/cta0024.xsd +40 -0
  121. data/vendor-src/libleptris/test/xsd/xsd11-dispositions/README.md +67 -0
  122. data/vendor-src/libleptris/vcpkg.json +1 -1
  123. metadata +35 -2
@@ -20,6 +20,7 @@
20
20
  #include "cdata.h"
21
21
  #include "pi.h"
22
22
  #include "entity_ref.h"
23
+ #include "document_node.h"
23
24
  #include "mut_recycle.h"
24
25
  #include "name_intern.h"
25
26
  #include <stdio.h>
@@ -121,13 +122,15 @@ static struct leptris_attribute* mut_attr_carve(struct leptris_document* doc) {
121
122
  /* Round 22: short string carve for attr names/values — reuses the
122
123
  * name block (its 8-byte doc header is unused here; harmless).
123
124
  * Returns NULL for long strings — caller falls back to the pool. */
124
- static char* mut_str_carve(struct leptris_document* doc, const char* s,
125
- size_t len) {
125
+ static char* mut_str_slot(struct leptris_document* doc, size_t len) {
126
126
  if (len + 1 > MUT_NAME_BLOCK_BYTES / 4) return NULL;
127
127
  size_t need = 8 + len + 1;
128
128
  /* Round the stride like mut_name_carve: this carve shares the
129
129
  * name block's cursor, and an unrounded advance here misaligns
130
- * every later namebp slot (doc backpointer stores). */
130
+ * every later namebp slot (doc backpointer stores). The rounding
131
+ * also guarantees the slot's payload is >= 8 bytes for every
132
+ * len <= 8 (16 at len <= 7, 24 at len 8) — the word-store form
133
+ * below writes the whole 8-byte window. */
131
134
  need = (need + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
132
135
  if (!doc->mut_name_cursor ||
133
136
  doc->mut_name_cursor + need > doc->mut_name_end) {
@@ -144,11 +147,35 @@ static char* mut_str_carve(struct leptris_document* doc, const char* s,
144
147
  }
145
148
  char* p = doc->mut_name_cursor + 8;
146
149
  doc->mut_name_cursor += need;
150
+ return p;
151
+ }
152
+
153
+ static char* mut_str_carve(struct leptris_document* doc, const char* s,
154
+ size_t len) {
155
+ char* p = mut_str_slot(doc, len);
156
+ if (!p) return NULL;
147
157
  memcpy(p, s, len);
148
158
  p[len] = '\0';
149
159
  return p;
150
160
  }
151
161
 
162
+ /* #2274 set-attr: the scanned-word form — the caller holds the string's
163
+ * zero-padded word from leptris_cstr_word8 (len <= 7), so the copy is
164
+ * one store and the NUL rides along (the S8-store law of
165
+ * leptris_dom_name_slot_carve: a fixed-width copy beats a libc call).
166
+ * The word carries nothing but the name: the scan stops at the NUL.
167
+ * The store goes through leptris_word8_store — the word is LOGICAL and
168
+ * its memory image is the host's, so a raw memcpy reversed the name on
169
+ * big-endian (leptris-ruby#699). */
170
+ static char* mut_str_carve_word8(struct leptris_document* doc, uint64_t w,
171
+ size_t len) {
172
+ char* p = mut_str_slot(doc, len);
173
+ if (!p) return NULL;
174
+ leptris_word8_store(p, w);
175
+ p[len] = '\0';
176
+ return p;
177
+ }
178
+
152
179
  /* #2132 mutcycle: the field-init half of assign-instead-of-clear.
153
180
  * Assigns EVERY leptris_element field the create branches do not
154
181
  * themselves set, so a carved slot — recycled from a previous
@@ -346,8 +373,9 @@ LeptrisElement leptris_element_create(LeptrisDocument doc, const char* name) {
346
373
  elem->header.flags |= LEPTRIS_NAMEBP_FLAG;
347
374
  elem_set_name_doc(elem, name_copy, doc);
348
375
  /* The fused pass already hashed these exact bytes —
349
- * the recompute pass is gone (#2132 append-uniq). */
350
- elem->name_hash = scan_hash;
376
+ * the recompute pass is gone (#2132 append-uniq).
377
+ * #2255: 0 is the lazy-hash sentinel, never stored. */
378
+ elem->name_hash = scan_hash ? scan_hash : 1;
351
379
  elem->name_len = (name_len > 254) ? 0xFF : (uint8_t)name_len;
352
380
  /* Round 20 contract: create registers every new element
353
381
  * so pre-attach ops (set_root validation, get_document
@@ -474,6 +502,11 @@ struct leptris_document* leptris_leaf_owner_doc(LeptrisNode* n) {
474
502
  return ((LeptrisCommentNode*)n)->owner_doc;
475
503
  case LEPTRIS_NODE_TYPE_PI:
476
504
  return ((LeptrisPINode*)n)->owner_doc;
505
+ case LEPTRIS_NODE_TYPE_DOCUMENT:
506
+ /* #2278: the XPath document node carries its document
507
+ * right after the base header (document_node.h) — "/" and
508
+ * "//" results resolve to it for base-uri()/document-uri(). */
509
+ return ((LeptrisDocumentNode*)n)->doc;
477
510
  default:
478
511
  return NULL;
479
512
  }
@@ -1350,206 +1383,515 @@ LeptrisStatus leptris_element_set_text(LeptrisElement elem, const char* text) {
1350
1383
  * COMPACT MODE: Uses linked list attribute storage
1351
1384
  */
1352
1385
 
1353
- /* ---- Doc-level attribute-name index (mutation path) --------------------
1386
+ /* ---- Attribute index (mutation path) -------------------------------------
1354
1387
  *
1355
1388
  * leptris_element_set_attribute walks the attr list per call to
1356
1389
  * reject duplicates — O(N) per set, O(N^2) for programmatic builds
1357
- * (11 ms at 2000 attrs on one element). This open-addressed index
1358
- * keys (element pointer, 32-bit name hash) -> attribute so the
1359
- * duplicate check is O(1). Safety: nodes are arena-backed and
1360
- * element removal only unlinks (never frees), so raw attr pointers
1361
- * cannot dangle before leptris_document_free — which frees the
1362
- * index. Entries: attr != NULL live; attr == NULL with elem != NULL
1363
- * is a tombstone (probe continues); all-NULL slot ends the probe.
1364
- * The (elem, hash == 0) sentinel marks an element as registered:
1365
- * parse-created attrs are bulk-inserted on the element's first
1366
- * mutation so the index is authoritative without ever touching the
1367
- * parse path. ------------------------------------------------------------------ */
1368
-
1369
- /* struct leptris_attr_index_entry / _index: leptris_internal.h
1370
- * (document_free releases the table). */
1371
-
1372
- #define ATTR_INDEX_INIT_CAP 16u
1390
+ * (11 ms at 2000 attrs on one element). The index makes the duplicate
1391
+ * check O(1). Safety: nodes are arena-backed and element removal only
1392
+ * unlinks (never frees), so raw attr pointers cannot dangle before
1393
+ * leptris_document_free.
1394
+ *
1395
+ * #2274 locality lever — two open-addressed levels, one discipline:
1396
+ *
1397
+ * DIRECTORY (per document, doc->attr_index): element -> the element's
1398
+ * OWN table. One record per indexed element, so it stays a few KB; a
1399
+ * back-to-back element (the programmatic-builder shape, the whole
1400
+ * set-attr bench row) skips even that probe through the memo pair.
1401
+ *
1402
+ * TABLE (per element, struct leptris_attr_table): name hash -> attr,
1403
+ * keyed WITHOUT the element pointer, over PACKED 8-byte slot words
1404
+ * (key || dense index) so a probe and its insert touch ONE cache line
1405
+ * — the doc-level layout walked a 192 KB calloc'd table (24 B x 8192
1406
+ * slots shared by the whole document), and the first local cut's
1407
+ * split key/value arrays still paid a second, random line per insert.
1408
+ * Values append densely (prefetch-friendly) in the same carve as the
1409
+ * slots. Everything is POOL-CARVED with the document's own mutation
1410
+ * storage instead of a cold calloc per doc-cycle: the pool destroy
1411
+ * reclaims it (no free path of its own, and the recycled spans stay
1412
+ * warm across cycles).
1413
+ *
1414
+ * The PR #2274 profile put the doc-level probe at 48% of set_attribute
1415
+ * and its rehash at 21% on the row shape — a 192 KB table shared by the
1416
+ * whole document scattered every probe over a different line. Slot
1417
+ * states: EMPTY (a probe stops), live word, TOMBSTONE (a probe
1418
+ * continues; remove_attribute writes it). A table whose insert cannot
1419
+ * land (a full table whose rehash could not allocate) is POISONED and
1420
+ * rebuilt from the chain on the element's next touch — the chain is the
1421
+ * truth, so the index is never observably partial. Both levels share
1422
+ * the load gate and the growth law (attr_idx_grow_cap).
1423
+ *
1424
+ * Row arithmetic (row-isolated harness, engine-only share of 2000 sets,
1425
+ * 2026-12-03/04): the engine share was 57 us on the doc-level index —
1426
+ * 38 us of it the append path alone (three byte passes over the name
1427
+ * and value: len16's loop, the index FNV, memchr for ':'; two libc
1428
+ * copies), the rest the probe. The S9 word scan (leptris_cstr_word8)
1429
+ * answers length, ':' and the key in one pass and turns the copies into
1430
+ * stores: 38 -> 18 us with the first cut's 8-byte window, ~2 us more
1431
+ * for the byte scan that replaced it (a window reads past the caller's
1432
+ * object even inside the page — ASAN flags it; the scan never does).
1433
+ * The index then costs 11-12 us at the ×16 growth law below against
1434
+ * 29 us at ×2 — the move cycles, not the final table size, were the
1435
+ * price (skipping the moves entirely saved 1 us). Engine share 57 ->
1436
+ * 33 us; the paired bench_matrix row (snprintf included) 216-226 ->
1437
+ * 201-202 us against pugixml's 198-205.
1438
+ * ---------------------------------------------------------------------- */
1439
+
1440
+ /* struct leptris_attr_table / _index: leptris_internal.h (the pool owns
1441
+ * all of it; document_free only clears the pointer). The directory
1442
+ * record is exactly two pointers — no padding at either word size
1443
+ * (16 bytes on LP64, 8 on the ILP32 layouts the i686/armv7 legs pin). */
1444
+ LEPTRIS_STATIC_ASSERT(sizeof(struct leptris_attr_dir_entry) ==
1445
+ 2 * sizeof(void*),
1446
+ "attr index directory record is two pointers");
1447
+
1448
+ /* #2255 set-attr row measurement (paired bench_matrix, 2026-12-02):
1449
+ * the probe is load-sensitive — the 2000-distinct-names build shape
1450
+ * at load 0.24 probes ~1.0 slot, at load 0.49 ~1.5-2 (each visited
1451
+ * slot a random line in the table). The doc-level table started at
1452
+ * 8192 entries for that reason. The per-element table starts at the
1453
+ * threshold-crossing size (32 slots, 384 B — small elements pay almost
1454
+ * nothing) and grows under the law below. */
1455
+ #ifndef ATTR_TBL_INIT_CAP
1456
+ #define ATTR_TBL_INIT_CAP 32u
1457
+ #endif
1458
+ /* Tables jump ×16 while small (32 -> 512 -> 8192): a table that
1459
+ * outgrows its first 16 live entries belongs to an attr-heavy element,
1460
+ * and each skipped move cycle is worth more than the slack — the row
1461
+ * measures +11 us here against +18 us at ×4 and +29 us at ×2. Past
1462
+ * ATTR_TBL_BIG_CAP the slack is real memory (an element with thousands
1463
+ * of attrs), so growth settles to ×4: the caps an element of N attrs
1464
+ * lands on are 384 B / 6 KB / 96 KB / 384 KB / ..., at or under what
1465
+ * the doc-level table's 192 KB floor cost EVERY indexed document. */
1466
+ #define ATTR_TBL_GROW_FACTOR 16u
1467
+ #define ATTR_TBL_GROW_FACTOR_BIG 4u
1468
+ #define ATTR_TBL_BIG_CAP 8192u
1469
+
1470
+ /* The directory keys one record per indexed ELEMENT: a few dozen
1471
+ * entries cover every element of an ordinary tree. It grows ×4 — its
1472
+ * records are 16 B apiece and its move cycles are tiny, so a wide
1473
+ * jump would only buy slack (1000 indexed elements: 16 KB at ×4). */
1474
+ #define ATTR_DIR_INIT_CAP 64u
1475
+ #define ATTR_DIR_GROW_FACTOR 4u
1373
1476
 
1374
1477
  /* Walk-first threshold: elements at or below this attr count use the
1375
- * hash-prefiltered list walk for the duplicate check instead of the
1376
- * doc-level index. XSLT result elements typically carry 1–3 attrs —
1377
- * register + probe + put costs more than walking a handful of nodes,
1378
- * and the index never re-pays for a short-lived result tree. Elements
1379
- * that grow past the threshold lazily bulk-register on their next
1380
- * mutation (attr_index_register inserts every existing attr). */
1478
+ * hash-prefiltered list walk for the duplicate check instead of an
1479
+ * index table. XSLT result elements typically carry 1–3 attrs — a
1480
+ * table carve + directory probe costs more than walking a handful of
1481
+ * nodes, and the index never re-pays for a short-lived result tree.
1482
+ * Elements that grow past the threshold lazily build their table on
1483
+ * their next mutation (attr_tbl_for fills it from the chain). */
1381
1484
  #define ATTR_INDEX_WALK_MAX 8
1382
1485
 
1383
- /* Index keys use the FULL 32-bit FNV of the name — independent of
1384
- * the attr field's 15-bit lazy hash (round 20). The u15 field feeds
1385
- * per-element walk pre-filters; the doc-level open-addressed index
1386
- * needs full-width keys so distinct names never share a probe key
1387
- * (a u15 key space turns each hash collision into an O(N) fallback
1388
- * walk, re-quadraticizing programmatic builds). */
1486
+ /* Index keys are full 32-bit words — independent of the attr field's
1487
+ * 15-bit lazy hash (round 20). The u15 field feeds per-element walk
1488
+ * pre-filters; the open-addressed table needs full-width keys so
1489
+ * distinct names never share a probe key (a u15 key space turns each
1490
+ * hash collision into an O(N) fallback walk, re-quadraticizing
1491
+ * programmatic builds).
1492
+ *
1493
+ * A live slot word's key field never reads 0 or 1 — those are the
1494
+ * EMPTY and TOMBSTONE words — so a raw hash of 0/1 (one name in 2^32)
1495
+ * is nudged; the string compare behind every probe still settles it
1496
+ * exactly. */
1497
+ static inline uint32_t attr_tbl_key(uint32_t hash) {
1498
+ return hash < 2u ? hash + 2u : hash;
1499
+ }
1500
+
1501
+ /* Names past the scan: FNV-1a under the round-20 avalanche (a raw FNV
1502
+ * truncation maps digit-differing names onto arithmetic progressions
1503
+ * that pile onto single probe chains). */
1389
1504
  static uint32_t attr_index_hash(const char* s, size_t len) {
1390
1505
  uint32_t h = 2166136261u;
1391
1506
  for (size_t i = 0; i < len; i++) {
1392
1507
  h ^= (unsigned char)s[i];
1393
1508
  h *= 16777619u;
1394
1509
  }
1395
- return h;
1510
+ return attr_hash32_finish(h);
1511
+ }
1512
+
1513
+ /* Names inside the scan (the NUL is inside `w`, zero-padded past it, so
1514
+ * "ab" and "abc" never share a key): one multiply spreads the whole
1515
+ * word into the high half — the FNV loop costs a dependent multiply per
1516
+ * byte and bought nothing here. */
1517
+ static inline uint32_t attr_index_key_word(uint64_t w) {
1518
+ uint64_t h = w * 0x9E3779B97F4A7C15ULL;
1519
+ h ^= h >> 29;
1520
+ return attr_tbl_key((uint32_t)(h >> 32));
1521
+ }
1522
+
1523
+ /* The probe key for a name: the word form when the scan holds the
1524
+ * whole name (NUL inside), FNV otherwise. EVERY writer and reader of a
1525
+ * table derives its key through this one function — the insert path
1526
+ * (which already holds the word) and the remove / chain-fill paths
1527
+ * (which do not) must agree bit-for-bit, or a removed name's entry
1528
+ * stays live (the next set resurrects the detached attr) or a
1529
+ * chain-built table misses every short name (duplicates append). */
1530
+ static inline uint32_t attr_index_key(const char* name, size_t len) {
1531
+ uint64_t w;
1532
+ if (len < 8 && leptris_cstr_word8(name, &w) &&
1533
+ leptris_word8_len(w) == len) {
1534
+ return attr_index_key_word(w);
1535
+ }
1536
+ return attr_index_hash(name, len);
1396
1537
  }
1397
1538
 
1398
- static uint32_t attr_index_slot(LeptrisElement elem, uint32_t name_hash,
1399
- size_t cap) {
1539
+ /* Directory slot from the element pointer (round 20: fold after the
1540
+ * multiply — the old `h2 ^= h2 >> 15` was a no-op for 15-bit hashes,
1541
+ * and stride-256 progressions piled whole arithmetic sequences onto
1542
+ * single probe chains). */
1543
+ static size_t attr_dir_slot(LeptrisElement elem, size_t cap) {
1400
1544
  uint64_t h = (uint64_t)(uintptr_t)elem;
1401
1545
  h ^= h >> 33;
1402
1546
  h *= 0xff51afd7ed558ccdULL;
1403
- /* Round 20: fold AFTER the multiply. The old `h2 ^= h2 >> 15`
1404
- * was a no-op for the 15-bit attr hashes — with stride-256 hash
1405
- * progressions (pre-finalizer bug) that piled whole arithmetic
1406
- * sequences onto single probe chains. */
1407
- uint64_t h2 = (uint64_t)name_hash * 0x9E3779B97F4A7C15ULL;
1408
- h2 ^= h2 >> 32;
1409
- return (uint32_t)((h ^ h2) & (cap - 1));
1410
- }
1411
-
1412
- static struct leptris_attr_index* attr_index_get(struct leptris_document* doc) {
1413
- if (!doc->attr_index) {
1414
- doc->attr_index = (struct leptris_attr_index*)malloc(
1415
- sizeof(struct leptris_attr_index));
1416
- if (!doc->attr_index) return NULL;
1417
- doc->attr_index->slots = (struct leptris_attr_index_entry*)calloc(
1418
- ATTR_INDEX_INIT_CAP, sizeof(struct leptris_attr_index_entry));
1419
- if (!doc->attr_index->slots) {
1420
- free(doc->attr_index);
1421
- doc->attr_index = NULL;
1422
- return NULL;
1547
+ return (size_t)(h & (cap - 1));
1548
+ }
1549
+
1550
+ /* The growth law both levels share (#2255): grow PAST the doubling — to
1551
+ * the power of two holding ~`factor` x the occupancy — so a monotonic
1552
+ * build (the set-attr bench shape) crosses a fraction of the rehash
1553
+ * cycles; each cycle carves a fresh array and re-moves every entry.
1554
+ * ATTR_IDX_LOAD_PCT is the gate (percent of cap): with linear probing a
1555
+ * MISS costs ~1/2(1 + 1/(1-a)^2) probes — 2.5 at a = 0.5, 6 at 0.7 — so
1556
+ * the gate trades probe length against the number of move cycles
1557
+ * (measured on the row: 0.5 beat 0.4 and 0.7 at every factor). */
1558
+ #ifndef ATTR_IDX_LOAD_PCT
1559
+ #define ATTR_IDX_LOAD_PCT 50u
1560
+ #endif
1561
+ static size_t attr_idx_grow_cap(size_t cap, size_t used, size_t factor) {
1562
+ size_t want = cap * 2;
1563
+ while (want < (used + 1) * factor) want <<= 1;
1564
+ return want;
1565
+ }
1566
+ static inline int attr_idx_gate_hit(size_t cap, size_t used) {
1567
+ return used * 100u >= cap * ATTR_IDX_LOAD_PCT;
1568
+ }
1569
+
1570
+ /* ---- per-element table ------------------------------------------------- */
1571
+
1572
+ /* Slot word (leptris_internal.h): key in the high 32 bits, dense index
1573
+ * + 1 in the low, so a live word is never EMPTY (0) or TOMBSTONE (1). */
1574
+ #define ATTR_TBL_SLOT_EMPTY 0u
1575
+ #define ATTR_TBL_SLOT_TOMBSTONE 1u
1576
+ static inline uint64_t attr_slot_word(uint32_t key, size_t idx) {
1577
+ return ((uint64_t)key << 32) | (uint64_t)(idx + 1);
1578
+ }
1579
+ static inline uint32_t attr_slot_key(uint64_t w) {
1580
+ return (uint32_t)(w >> 32);
1581
+ }
1582
+ static inline size_t attr_slot_idx(uint64_t w) {
1583
+ return (size_t)(w & 0xFFFFFFFFu) - 1;
1584
+ }
1585
+
1586
+ /* Pool-carved slot array (`ncap` words) and dense value array (ncap/2
1587
+ * entries — the 50% load gate bounds nvals below it) in ONE carve. Only
1588
+ * the slots are cleared (they carry the state; values are written
1589
+ * before they are ever read). The clear is explicit — pool memory is
1590
+ * uninitialized (recycled spans carry the previous document's slots). */
1591
+ static int attr_tbl_arrays_new(struct leptris_document* doc, size_t ncap,
1592
+ uint64_t** slots_out,
1593
+ struct leptris_attribute*** vals_out) {
1594
+ uint64_t* slots = (uint64_t*)leptris_pool_alloc(
1595
+ doc->pool, ncap * (sizeof(uint64_t) + sizeof(struct leptris_attribute*) / 2));
1596
+ if (!slots) return 0;
1597
+ memset(slots, 0, ncap * sizeof(uint64_t));
1598
+ *slots_out = slots;
1599
+ *vals_out = (struct leptris_attribute**)(slots + ncap);
1600
+ return 1;
1601
+ }
1602
+
1603
+ /* A fresh table with `cap` slots — struct and arrays in ONE pool carve
1604
+ * (the threshold-crossing element pays a single alloc). The header is
1605
+ * rounded to 8 bytes: the slot words are uint64_t and sizeof(*t) is 20
1606
+ * on ILP32, where the carve's 8-byte alignment alone would leave them
1607
+ * misaligned. */
1608
+ static struct leptris_attr_table* attr_tbl_new(struct leptris_document* doc,
1609
+ size_t cap) {
1610
+ const size_t hdr = (sizeof(struct leptris_attr_table) + 7u) & ~(size_t)7u;
1611
+ struct leptris_attr_table* t = (struct leptris_attr_table*)
1612
+ leptris_pool_alloc(doc->pool, hdr + cap *
1613
+ (sizeof(uint64_t) +
1614
+ sizeof(struct leptris_attribute*) / 2));
1615
+ if (!t) return NULL;
1616
+ t->slots = (uint64_t*)((char*)t + hdr);
1617
+ memset(t->slots, 0, cap * sizeof(uint64_t));
1618
+ t->vals = (struct leptris_attribute**)(t->slots + cap);
1619
+ t->cap = cap;
1620
+ t->used = 0;
1621
+ t->nvals = 0;
1622
+ return t;
1623
+ }
1624
+
1625
+ /* Probe for `key` (a raw hash — normalized here so every caller is
1626
+ * covered). On hit returns the slot index with *attr_out set. On miss
1627
+ * returns the first EMPTY slot for insertion (tombstones are passed
1628
+ * over), or (size_t)-1 when the table holds no empty slot.
1629
+ *
1630
+ * The two hot outcomes — the home slot is EMPTY (a fresh name: every
1631
+ * set of the build shape) or already holds this key (a replace) —
1632
+ * share ONE branch, taken for ~95% of lookups at the row's load; the
1633
+ * chain walk is the rare path. Measured on the row: the per-slot test
1634
+ * of the first cut was a coin flip at load ~0.5 (a 4096-slot table ran
1635
+ * 4 us SLOWER than 8192 at 2000 names) — the probe's price is the
1636
+ * mispredict, not the line. */
1637
+ static size_t attr_tbl_probe(const struct leptris_attr_table* t,
1638
+ uint32_t hash,
1639
+ struct leptris_attribute** attr_out,
1640
+ int* found) {
1641
+ size_t cap = t->cap;
1642
+ const uint64_t* slots = t->slots;
1643
+ uint32_t key = attr_tbl_key(hash);
1644
+ size_t i = (size_t)key & (cap - 1);
1645
+ *found = 0;
1646
+ uint64_t w = slots[i];
1647
+ if (w == ATTR_TBL_SLOT_EMPTY || attr_slot_key(w) == key) {
1648
+ if (w != ATTR_TBL_SLOT_EMPTY) {
1649
+ *attr_out = t->vals[attr_slot_idx(w)];
1650
+ *found = 1;
1423
1651
  }
1424
- doc->attr_index->cap = ATTR_INDEX_INIT_CAP;
1425
- doc->attr_index->used = 0;
1426
- doc->attr_index->last_registered = NULL; /* malloc: not zeroed */
1652
+ return i;
1653
+ }
1654
+ for (size_t n_ = 1; n_ < cap; n_++) {
1655
+ i = (i + 1) & (cap - 1);
1656
+ w = slots[i];
1657
+ if (w == ATTR_TBL_SLOT_EMPTY) return i; /* never used: miss */
1658
+ if (attr_slot_key(w) == key) {
1659
+ *attr_out = t->vals[attr_slot_idx(w)];
1660
+ *found = 1;
1661
+ return i;
1662
+ }
1663
+ }
1664
+ return (size_t)-1;
1665
+ }
1666
+
1667
+ static void attr_tbl_rehash_to(struct leptris_document* doc,
1668
+ struct leptris_attr_table* t, size_t ncap) {
1669
+ uint64_t* nslots;
1670
+ struct leptris_attribute** nvals;
1671
+ if (!attr_tbl_arrays_new(doc, ncap, &nslots, &nvals))
1672
+ return; /* stay at old size; the probe bound tolerates it */
1673
+ /* Live entries re-land in slot order and the dense array compacts
1674
+ * (tombstones drop with their dead entries). */
1675
+ size_t moved = 0;
1676
+ for (size_t i = 0; i < t->cap; i++) {
1677
+ uint64_t w = t->slots[i];
1678
+ if (w < 2u) continue; /* empty or tombstone */
1679
+ nvals[moved] = t->vals[attr_slot_idx(w)];
1680
+ size_t j = (size_t)attr_slot_key(w) & (ncap - 1);
1681
+ while (nslots[j]) j = (j + 1) & (ncap - 1);
1682
+ nslots[j] = attr_slot_word(attr_slot_key(w), moved);
1683
+ moved++;
1684
+ }
1685
+ t->slots = nslots; /* the old arrays are abandoned to the pool */
1686
+ t->vals = nvals;
1687
+ t->cap = ncap;
1688
+ t->used = moved;
1689
+ t->nvals = moved;
1690
+ }
1691
+
1692
+ /* Record (key -> attr) at `slot` — the EMPTY slot the caller's probe
1693
+ * returned (re-probed if it is no longer empty). An insert that cannot
1694
+ * land POISONS the table (slots = NULL): the element's next touch
1695
+ * rebuilds it from the chain, which stays the truth throughout. The
1696
+ * dense array cannot overflow: nvals <= used, and a used past the gate
1697
+ * whose rehash could not allocate (the only way nvals reaches cap/2)
1698
+ * poisons here instead of writing past the carve. */
1699
+ static void attr_tbl_put(struct leptris_document* doc,
1700
+ struct leptris_attr_table* t, uint32_t hash,
1701
+ struct leptris_attribute* attr, size_t slot) {
1702
+ if (!t->slots) return; /* already poisoned */
1703
+ uint32_t key = attr_tbl_key(hash);
1704
+ if (slot >= t->cap || t->slots[slot] != ATTR_TBL_SLOT_EMPTY) {
1705
+ struct leptris_attribute* dummy;
1706
+ int f;
1707
+ slot = attr_tbl_probe(t, key, &dummy, &f);
1708
+ if (f) return; /* live duplicate: nothing to record */
1709
+ }
1710
+ if (slot == (size_t)-1 || t->nvals >= t->cap / 2) {
1711
+ t->slots = NULL;
1712
+ t->vals = NULL;
1713
+ t->cap = 0;
1714
+ t->used = 0;
1715
+ t->nvals = 0;
1716
+ return;
1717
+ }
1718
+ t->vals[t->nvals] = attr;
1719
+ t->slots[slot] = attr_slot_word(key, t->nvals); /* publish */
1720
+ t->nvals++;
1721
+ t->used++;
1722
+ if (attr_idx_gate_hit(t->cap, t->used)) {
1723
+ attr_tbl_rehash_to(doc, t, attr_idx_grow_cap(
1724
+ t->cap, t->used,
1725
+ t->cap >= ATTR_TBL_BIG_CAP ? ATTR_TBL_GROW_FACTOR_BIG
1726
+ : ATTR_TBL_GROW_FACTOR));
1427
1727
  }
1428
- return doc->attr_index;
1429
1728
  }
1430
1729
 
1431
- /* Probe for (elem, name_hash). On hit returns the slot index with
1432
- * *attr_out set (NULL when the slot is the registration sentinel).
1433
- * On miss returns the empty-slot index for insertion. */
1434
- static size_t attr_index_probe(struct leptris_attr_index* ix,
1435
- LeptrisElement elem, uint32_t name_hash,
1436
- struct leptris_attribute** attr_out,
1437
- int* found) {
1730
+ /* Tombstone the slot a remove hit (the chain unlink is the removal;
1731
+ * the index must not resurrect the detached storage on a later set).
1732
+ * `used` is unchanged: tombstones count toward the load so the next
1733
+ * rehash drops them (and compacts their dense entries away). */
1734
+ static void attr_tbl_tombstone(struct leptris_attr_table* t, size_t slot) {
1735
+ t->slots[slot] = ATTR_TBL_SLOT_TOMBSTONE;
1736
+ }
1737
+
1738
+ /* ---- per-document directory -------------------------------------------- */
1739
+
1740
+ static struct leptris_attr_index* attr_dir_get(struct leptris_document* doc) {
1741
+ if (doc->attr_index) return doc->attr_index;
1742
+ if (!doc->pool) return NULL;
1743
+ struct leptris_attr_index* ix = (struct leptris_attr_index*)
1744
+ leptris_pool_alloc(doc->pool, sizeof(*ix) +
1745
+ ATTR_DIR_INIT_CAP *
1746
+ sizeof(struct leptris_attr_dir_entry));
1747
+ if (!ix) return NULL;
1748
+ ix->slots = (struct leptris_attr_dir_entry*)(ix + 1);
1749
+ memset(ix->slots, 0,
1750
+ ATTR_DIR_INIT_CAP * sizeof(struct leptris_attr_dir_entry));
1751
+ ix->cap = ATTR_DIR_INIT_CAP;
1752
+ ix->used = 0;
1753
+ ix->memo_elem = NULL;
1754
+ ix->memo_tbl = NULL;
1755
+ doc->attr_index = ix;
1756
+ return ix;
1757
+ }
1758
+
1759
+ /* Probe for `elem`'s record. On hit returns the slot with *tbl_out;
1760
+ * on miss the empty slot for insertion (the directory never
1761
+ * tombstones — records live as long as the document). */
1762
+ static size_t attr_dir_probe(struct leptris_attr_index* ix,
1763
+ LeptrisElement elem,
1764
+ struct leptris_attr_table** tbl_out,
1765
+ int* found) {
1438
1766
  size_t cap = ix->cap;
1439
- size_t i = attr_index_slot(elem, name_hash, cap);
1767
+ size_t i = attr_dir_slot(elem, cap);
1440
1768
  *found = 0;
1441
1769
  for (size_t n_ = 0; n_ < cap; n_++) {
1442
- struct leptris_attr_index_entry* e = &ix->slots[i];
1443
- if (!e->elem && !e->attr) return i; /* never used: miss */
1444
- if (e->elem == elem && e->name_hash == name_hash) {
1445
- if (e->attr) { *attr_out = e->attr; *found = 1; return i; }
1446
- if (name_hash == 0) { *attr_out = NULL; *found = 1; return i; }
1447
- }
1770
+ struct leptris_attr_dir_entry* e = &ix->slots[i];
1771
+ if (!e->elem) return i; /* never used: miss */
1772
+ if (e->elem == elem) { *tbl_out = e->tbl; *found = 1; return i; }
1448
1773
  i = (i + 1) & (cap - 1);
1449
1774
  }
1450
- return (size_t)-1; /* full (cannot happen: rehash keeps load < 0.7) */
1775
+ return (size_t)-1; /* full (cannot happen: the gate keeps load < 1/2) */
1451
1776
  }
1452
1777
 
1453
- static void attr_index_rehash(struct leptris_attr_index* ix) {
1454
- size_t ncap = ix->cap * 2;
1455
- struct leptris_attr_index_entry* ns = (struct leptris_attr_index_entry*)
1456
- calloc(ncap, sizeof(struct leptris_attr_index_entry));
1457
- if (!ns) return; /* stay at old size; probe loop tolerates it */
1458
- /* #2255: the occupancy count falls out of the move loop — the
1459
- * old post-pass re-scanned every slot of the new table (O(cap)
1460
- * over freshly calloc'd, cache-cold memory) to recompute a
1461
- * number the loop already knows. */
1778
+ static void attr_dir_rehash_to(struct leptris_document* doc,
1779
+ struct leptris_attr_index* ix, size_t ncap) {
1780
+ struct leptris_attr_dir_entry* ns = (struct leptris_attr_dir_entry*)
1781
+ leptris_pool_alloc(doc->pool, ncap * sizeof(*ns));
1782
+ if (!ns) return; /* stay at old size; the probe loop tolerates it */
1783
+ memset(ns, 0, ncap * sizeof(*ns));
1462
1784
  size_t moved = 0;
1463
1785
  for (size_t i = 0; i < ix->cap; i++) {
1464
- struct leptris_attr_index_entry e = ix->slots[i];
1465
- if (!e.elem && !e.attr) continue;
1466
- if (!e.attr && e.name_hash != 0) continue; /* drop tombstones */
1467
- size_t j = attr_index_slot(e.elem, e.name_hash, ncap);
1468
- while (ns[j].elem || ns[j].attr) j = (j + 1) & (ncap - 1);
1786
+ struct leptris_attr_dir_entry e = ix->slots[i];
1787
+ if (!e.elem) continue;
1788
+ size_t j = attr_dir_slot(e.elem, ncap);
1789
+ while (ns[j].elem) j = (j + 1) & (ncap - 1);
1469
1790
  ns[j] = e;
1470
1791
  moved++;
1471
1792
  }
1472
- free(ix->slots);
1473
- ix->slots = ns;
1793
+ ix->slots = ns; /* the old array is abandoned to the pool */
1474
1794
  ix->cap = ncap;
1475
1795
  ix->used = moved;
1476
1796
  }
1477
1797
 
1478
- static void attr_index_put(struct leptris_attr_index* ix, LeptrisElement elem,
1479
- uint32_t name_hash,
1480
- struct leptris_attribute* attr, size_t slot) {
1481
- if (slot >= ix->cap) {
1482
- attr_index_rehash(ix);
1483
- struct leptris_attribute* dummy;
1484
- int f;
1485
- slot = attr_index_probe(ix, elem, name_hash, &dummy, &f);
1486
- if (slot == (size_t)-1) return;
1487
- }
1488
- struct leptris_attr_index_entry* e = &ix->slots[slot];
1489
- if (!e->elem && !e->attr) ix->used++;
1798
+ /* Publish `tbl` as `elem`'s record at `slot` (a found slot overwrites
1799
+ * — the rebuilt-table path replaces a poisoned record in place). */
1800
+ static void attr_dir_put(struct leptris_document* doc,
1801
+ struct leptris_attr_index* ix, LeptrisElement elem,
1802
+ struct leptris_attr_table* tbl, size_t slot) {
1803
+ if (slot == (size_t)-1) return; /* full and could not grow: unindexed */
1804
+ struct leptris_attr_dir_entry* e = &ix->slots[slot];
1805
+ if (!e->elem) ix->used++;
1490
1806
  e->elem = elem;
1491
- e->name_hash = name_hash;
1492
- e->attr = attr;
1493
- if (ix->used * 10 >= ix->cap * 7) attr_index_rehash(ix);
1494
- }
1495
-
1496
- /* Bulk-register every attr of `elem` plus the (elem, 0) sentinel so
1497
- * later probes are authoritative. Falls back silently (index stays
1498
- * partial) only on allocation failure — callers must then use the
1499
- * list walk. Returns 1 when the element is registered (the sentinel
1500
- * is in place), 0 when the registration could not land. */
1501
- static int attr_index_register(struct leptris_document* doc,
1502
- struct leptris_attr_index* ix,
1503
- LeptrisElement elem) {
1504
- struct leptris_attribute* dummy;
1807
+ e->tbl = tbl;
1808
+ if (attr_idx_gate_hit(ix->cap, ix->used)) {
1809
+ attr_dir_rehash_to(doc, ix, attr_idx_grow_cap(ix->cap, ix->used,
1810
+ ATTR_DIR_GROW_FACTOR));
1811
+ }
1812
+ }
1813
+
1814
+ /* A table is sound to serve only while its slot array exists (a
1815
+ * poisoned one is rebuilt, never probed). */
1816
+ static inline struct leptris_attr_table* attr_tbl_live(
1817
+ struct leptris_attr_table* t) {
1818
+ return (t && t->slots) ? t : NULL;
1819
+ }
1820
+
1821
+ /* The element's existing table — memo first, then the directory —
1822
+ * WITHOUT building one. NULL when the element has none (walk tier) or
1823
+ * its table is poisoned. remove_attribute is the caller. */
1824
+ static struct leptris_attr_table* attr_tbl_existing(
1825
+ struct leptris_attr_index* ix, LeptrisElement elem) {
1826
+ struct leptris_attr_table* tbl = NULL;
1827
+ if (ix->memo_elem == elem) {
1828
+ tbl = ix->memo_tbl;
1829
+ } else {
1830
+ int found;
1831
+ attr_dir_probe(ix, elem, &tbl, &found);
1832
+ if (!found) tbl = NULL;
1833
+ }
1834
+ return attr_tbl_live(tbl);
1835
+ }
1836
+
1837
+ /* The element's table, built on first touch: pre-sized once from the
1838
+ * chain length (threshold-crossing and parse-built chains never pay
1839
+ * the grow-by-carving chain on the fill pass) and filled with every
1840
+ * existing attr, so later probes are authoritative. Returns NULL when
1841
+ * the table cannot land (allocation failure) — the caller walks, and
1842
+ * nothing is memoized. */
1843
+ static struct leptris_attr_table* attr_tbl_for(struct leptris_document* doc,
1844
+ struct leptris_attr_index* ix,
1845
+ LeptrisElement elem) {
1846
+ if (ix->memo_elem == elem && attr_tbl_live(ix->memo_tbl))
1847
+ return ix->memo_tbl;
1848
+ struct leptris_attr_table* tbl = NULL;
1505
1849
  int found;
1506
- size_t slot = attr_index_probe(ix, elem, 0, &dummy, &found);
1507
- if (found || slot == (size_t)-1) return found;
1508
- attr_index_put(ix, elem, 0, NULL, slot);
1509
- struct leptris_attribute* a = leptris_element_get_first_attribute(elem);
1510
- while (a) {
1511
- uint32_t h = attr_index_hash(attr_cname(a), a->name_view.length);
1512
- slot = attr_index_probe(ix, elem, h, &dummy, &found);
1513
- if (!found && slot != (size_t)-1) {
1514
- attr_index_put(ix, elem, h, a, slot);
1515
- }
1516
- a = leptris_attr_next(a);
1850
+ size_t slot = attr_dir_probe(ix, elem, &tbl, &found);
1851
+ if (found && attr_tbl_live(tbl)) {
1852
+ ix->memo_elem = elem;
1853
+ ix->memo_tbl = tbl;
1854
+ return tbl;
1517
1855
  }
1518
- return 1;
1856
+ /* Fresh table: (n entries) at the 0.7 load gate need cap > 1.4n —
1857
+ * the 2x round-up covers it. */
1858
+ size_t n = 0;
1859
+ for (struct leptris_attribute* a = leptris_element_get_first_attribute(elem);
1860
+ a; a = leptris_attr_next(a)) n++;
1861
+ size_t want = ATTR_TBL_INIT_CAP;
1862
+ while (want < (n + 1) * 2) want <<= 1;
1863
+ struct leptris_attr_table* nt = attr_tbl_new(doc, want);
1864
+ if (!nt) return NULL;
1865
+ for (struct leptris_attribute* a = leptris_element_get_first_attribute(elem);
1866
+ a; a = leptris_attr_next(a)) {
1867
+ uint32_t key = attr_index_key(attr_cname(a), a->name_view.length);
1868
+ struct leptris_attribute* dummy;
1869
+ int f;
1870
+ size_t s = attr_tbl_probe(nt, key, &dummy, &f);
1871
+ if (!f) attr_tbl_put(doc, nt, key, a, s);
1872
+ }
1873
+ if (!attr_tbl_live(nt)) return NULL; /* the fill could not land: walk */
1874
+ attr_dir_put(doc, ix, elem, nt, slot);
1875
+ ix->memo_elem = elem;
1876
+ ix->memo_tbl = nt;
1877
+ return nt;
1519
1878
  }
1520
1879
 
1521
- /* O(1) duplicate check with lazy registration. Returns the existing
1522
- * attr or NULL; *ix_out/registered bookkeeping lets the caller
1523
- * insert the new attr without re-probing. */
1524
- static struct leptris_attribute* attr_index_lookup(
1525
- struct leptris_document* doc, LeptrisElement elem, const char* name,
1526
- size_t name_len, uint32_t name_hash,
1527
- struct leptris_attr_index** ix_out, size_t* insert_slot) {
1528
- *ix_out = NULL;
1880
+ /* O(1) duplicate check against the element's table. Returns the
1881
+ * existing attr or NULL; *insert_slot lets the caller record the new
1882
+ * attr without re-probing. A key collision with a different name falls
1883
+ * back to the chain walk, which is authoritative. */
1884
+ static struct leptris_attribute* attr_tbl_lookup(
1885
+ struct leptris_attr_table* tbl, LeptrisElement elem, const char* name,
1886
+ size_t name_len, uint32_t key, size_t* insert_slot) {
1529
1887
  *insert_slot = (size_t)-1;
1530
- struct leptris_attr_index* ix = attr_index_get(doc);
1531
- if (!ix) return NULL;
1532
- /* #2255 set-attr: registration is permanent per element (removal
1533
- * tombstones leave the (elem,0) sentinel), so the same element
1534
- * back-to-back — the programmatic-builder shape, and the whole
1535
- * set-attr bench row — skips the register re-probe entirely.
1536
- * A registration that failed to land (allocation failure) must
1537
- * not memoize: the index stays partial and the next call has to
1538
- * retry the sentinel probe (and the caller keeps walking). */
1539
- if (ix->last_registered != elem) {
1540
- if (attr_index_register(doc, ix, elem))
1541
- ix->last_registered = elem;
1542
- }
1543
1888
  struct leptris_attribute* hit = NULL;
1544
1889
  int found;
1545
- size_t slot = attr_index_probe(ix, elem, name_hash, &hit, &found);
1546
- if (slot == (size_t)-1) return NULL;
1890
+ size_t slot = attr_tbl_probe(tbl, key, &hit, &found);
1547
1891
  if (!found) {
1548
- *ix_out = ix;
1549
- *insert_slot = slot;
1892
+ if (slot != (size_t)-1) *insert_slot = slot;
1550
1893
  return NULL;
1551
1894
  }
1552
- if (!hit) return NULL; /* sentinel-only element */
1553
1895
  /* Confirm with the string (hash collisions). */
1554
1896
  if (hit->name_view.length == name_len &&
1555
1897
  memcmp(attr_cname(hit), name, name_len) == 0) {
@@ -1559,6 +1901,21 @@ static struct leptris_attribute* attr_index_lookup(
1559
1901
  return leptris_element_get_attribute_by_name(elem, name);
1560
1902
  }
1561
1903
 
1904
+ /* #2274: the inline-value decision through the word scan — 1 when the
1905
+ * value was stored INLINE (len <= MAX_INLINE, NUL inside the scan,
1906
+ * which is exactly the inline-able range + 1), 0 when the caller takes
1907
+ * the byte-pass / long-value path and computes vlen itself. */
1908
+ static inline int attr_value_try_inline_word(struct leptris_attribute* a,
1909
+ const char* value) {
1910
+ uint64_t vw;
1911
+ if (!LEPTRIS_ATTR_VALUE_CAN_INLINE) return 0;
1912
+ if (!leptris_cstr_word8(value, &vw)) return 0; /* 8+ bytes: not inline */
1913
+ size_t vlen = leptris_word8_len(vw);
1914
+ if (vlen > LEPTRIS_ATTR_VALUE_MAX_INLINE) return 0;
1915
+ leptris_attr_value_set_inline_word(a, vw, vlen);
1916
+ return 1;
1917
+ }
1918
+
1562
1919
  LeptrisStatus leptris_element_set_attribute(LeptrisElement elem, const char* name, const char* value) {
1563
1920
  if (!elem || !name) return LEPTRIS_ERROR_NULL_ARG;
1564
1921
 
@@ -1569,17 +1926,28 @@ LeptrisStatus leptris_element_set_attribute(LeptrisElement elem, const char* nam
1569
1926
  * is pure overhead — ~200ns × N attrs. Inline pool_alloc + memcpy
1570
1927
  * is faster than leptris_sv_to_cstr_pooled for this case. */
1571
1928
 
1572
- /* Check if attribute already exists — O(1) via the doc-level
1573
- * attr-name index (lazy registration covers parse-created attrs);
1574
- * NULL index (alloc failure) falls back to the list walk. */
1575
- size_t set_name_len = leptris_cstr_len16(name);
1576
- uint32_t set_name_hash = 0;
1929
+ /* Check if attribute already exists — O(1) via the element's own
1930
+ * attr index table (lazy build from the chain covers parse-created
1931
+ * attrs); no table (allocation failure) falls back to the list
1932
+ * walk. */
1933
+ /* #2274: one scan over the name answers its length, its ':' (the
1934
+ * ns side-cache gate below) and the index key — the three byte
1935
+ * passes this paid (len16's loop, the FNV loop, memchr) were most
1936
+ * of the insert path's remaining cost. A name of 8+ bytes takes the
1937
+ * byte-pass forms (set_short == 0). */
1938
+ uint64_t set_nw = 0;
1939
+ size_t set_name_len;
1940
+ const int set_short =
1941
+ leptris_cstr_word8(name, &set_nw) &&
1942
+ (set_name_len = leptris_word8_len(set_nw)) != (size_t)-1;
1943
+ if (!set_short) set_name_len = leptris_cstr_len16(name);
1944
+ uint32_t set_key = 0;
1577
1945
  /* #2255: the mutation tier — identical answers to get_document,
1578
1946
  * and the builder shape (the bench row's parse-built root
1579
1947
  * terminus) answers inline from the packed memo. */
1580
1948
  struct leptris_document* set_doc =
1581
1949
  leptris_element_get_document_mutation(elem);
1582
- struct leptris_attr_index* set_ix = NULL;
1950
+ struct leptris_attr_table* set_tbl = NULL;
1583
1951
  size_t set_slot = (size_t)-1;
1584
1952
  struct leptris_attribute* existing;
1585
1953
  if (set_doc &&
@@ -1588,11 +1956,23 @@ LeptrisStatus leptris_element_set_attribute(LeptrisElement elem, const char* nam
1588
1956
  /* Lane 18: the flag latches — attr_count wraps at 255 and
1589
1957
  * must never flip a high-attr element back to the walk. */
1590
1958
  elem->header.flags |= LEPTRIS_ATTR_INDEXED_FLAG;
1591
- set_name_hash = attr_index_hash(name, set_name_len);
1592
- existing = attr_index_lookup(set_doc, elem, name, set_name_len,
1593
- set_name_hash, &set_ix, &set_slot);
1959
+ /* #2274: the element's own table — NULL when it cannot be
1960
+ * built (allocation failure), and the chain walk answers. */
1961
+ if (set_doc->pool) {
1962
+ struct leptris_attr_index* set_dir = attr_dir_get(set_doc);
1963
+ if (set_dir) set_tbl = attr_tbl_for(set_doc, set_dir, elem);
1964
+ }
1965
+ if (set_tbl) {
1966
+ /* attr_index_key with the scanned word already in hand. */
1967
+ set_key = set_short ? attr_index_key_word(set_nw)
1968
+ : attr_index_hash(name, set_name_len);
1969
+ existing = attr_tbl_lookup(set_tbl, elem, name, set_name_len,
1970
+ set_key, &set_slot);
1971
+ } else {
1972
+ existing = leptris_dom_attr_find(elem, name, set_name_len);
1973
+ }
1594
1974
  } else {
1595
- /* Walk-first (small elements): set_ix stays NULL so the
1975
+ /* Walk-first (small elements): set_tbl stays NULL so the
1596
1976
  * insert below skips the index put too. */
1597
1977
  existing = leptris_dom_attr_find(elem, name, set_name_len);
1598
1978
  }
@@ -1602,15 +1982,13 @@ LeptrisStatus leptris_element_set_attribute(LeptrisElement elem, const char* nam
1602
1982
  * re-climbed get_document/get_pool twice per overwrite. */
1603
1983
  LeptrisMemoryPool* pool = set_doc ? set_doc->pool : NULL;
1604
1984
 
1605
- if (value) {
1606
- size_t vlen = leptris_cstr_len16(value);
1607
- if (LEPTRIS_ATTR_VALUE_CAN_INLINE && vlen <= LEPTRIS_ATTR_VALUE_MAX_INLINE) {
1608
- /* lane18 S1: small values store INLINE in the slot —
1609
- * zero allocation per overwrite. */
1610
- leptris_attr_value_set_inline(existing, value, vlen);
1611
- attr_set_entities(existing, 0);
1612
- return LEPTRIS_OK;
1613
- }
1985
+ if (value && attr_value_try_inline_word(existing, value)) {
1986
+ /* lane18 S1 through the word scan: small values store
1987
+ * INLINE in the slot — zero allocation per overwrite. Every
1988
+ * inline-able value ends inside the scan, so the long-value
1989
+ * paths below are 8+ bytes (or ILP32, which has no inline). */
1990
+ attr_set_entities(existing, 0);
1991
+ return LEPTRIS_OK;
1614
1992
  }
1615
1993
  if (pool) {
1616
1994
  /* Pool-allocated document: pool_strdup the new value (no
@@ -1658,8 +2036,14 @@ LeptrisStatus leptris_element_set_attribute(LeptrisElement elem, const char* nam
1658
2036
  return LEPTRIS_ERROR_MEMORY;
1659
2037
  }
1660
2038
 
1661
- size_t nlen = strlen(name);
1662
- char* name_storage = mut_str_carve(set_doc, name, nlen);
2039
+ /* set_name_len (resolved above for the dup probe) IS this
2040
+ * name's length — no second strlen on the insert path. The
2041
+ * word scan, when it holds the whole name, is also the
2042
+ * copy: one store, NUL included. */
2043
+ size_t nlen = set_name_len;
2044
+ char* name_storage = set_short
2045
+ ? mut_str_carve_word8(set_doc, set_nw, nlen)
2046
+ : mut_str_carve(set_doc, name, nlen);
1663
2047
  if (!name_storage) {
1664
2048
  name_storage = (char*)leptris_pool_alloc(pool, nlen + 1);
1665
2049
  if (!name_storage) return LEPTRIS_ERROR_MEMORY;
@@ -1668,27 +2052,31 @@ LeptrisStatus leptris_element_set_attribute(LeptrisElement elem, const char* nam
1668
2052
  }
1669
2053
  attr->name_view = leptris_sv_from_ptr(name_storage, nlen);
1670
2054
 
1671
- /* Pre-compute name hash for O(1) lookup filtering (TODO 113).
1672
- * Round 19: 15-bit + entity flag share the field. */
1673
- attr->name_hash = attr_hash15(name_storage, nlen);
1674
-
1675
- if (value) {
2055
+ /* Name hash for O(1) lookup filtering (TODO 113; round 19:
2056
+ * 15-bit + entity flag share the field). LAZY — the parse
2057
+ * path's convention (direct_parse's lazy-hash sentinel): the
2058
+ * index key above is no longer an FNV whose low 15 bits could
2059
+ * be reused, and readers compute the cut once on first use via
2060
+ * attr_name_hash. Bit 15 clear = no entities, which is what
2061
+ * the old eager store left behind too. */
2062
+ attr->name_hash = 0;
2063
+
2064
+ if (value && attr_value_try_inline_word(attr, value)) {
2065
+ /* lane18 S1 through the word scan: small new values inline
2066
+ * in the slot — no carve, no pool round-trip. */
2067
+ } else if (value) {
2068
+ /* 8+ bytes (every shorter value ended inside the scan), or
2069
+ * ILP32 where inline storage is unavailable. */
1676
2070
  size_t vlen = leptris_cstr_len16(value);
1677
- if (LEPTRIS_ATTR_VALUE_CAN_INLINE && vlen <= LEPTRIS_ATTR_VALUE_MAX_INLINE) {
1678
- /* lane18 S1: small new values inline in the slot —
1679
- * no carve, no pool round-trip. */
1680
- leptris_attr_value_set_inline(attr, value, vlen);
1681
- } else {
1682
- char* value_storage = mut_str_carve(set_doc, value, vlen);
1683
- if (!value_storage) {
1684
- value_storage = (char*)leptris_pool_alloc(pool, vlen + 1);
1685
- if (!value_storage) return LEPTRIS_ERROR_MEMORY;
1686
- memcpy(value_storage, value, vlen);
1687
- value_storage[vlen] = '\0';
1688
- }
1689
- leptris_attr_value_set_heap(
1690
- attr, leptris_sv_from_ptr(value_storage, vlen));
2071
+ char* value_storage = mut_str_carve(set_doc, value, vlen);
2072
+ if (!value_storage) {
2073
+ value_storage = (char*)leptris_pool_alloc(pool, vlen + 1);
2074
+ if (!value_storage) return LEPTRIS_ERROR_MEMORY;
2075
+ memcpy(value_storage, value, vlen);
2076
+ value_storage[vlen] = '\0';
1691
2077
  }
2078
+ leptris_attr_value_set_heap(
2079
+ attr, leptris_sv_from_ptr(value_storage, vlen));
1692
2080
  } else {
1693
2081
  leptris_attr_value_set_heap(attr, leptris_sv_empty());
1694
2082
  }
@@ -1698,10 +2086,15 @@ LeptrisStatus leptris_element_set_attribute(LeptrisElement elem, const char* nam
1698
2086
 
1699
2087
  /* leptris#1664: prefixed names get their ns side-cache at
1700
2088
  * creation (reserved prefixes then resolve on
1701
- * declaration-free/mutation trees). */
2089
+ * declaration-free/mutation trees). #2274: the word scan
2090
+ * already knows whether a short name carries a ':' — memchr
2091
+ * runs only when it does. */
1702
2092
  {
1703
- const char* colon = (const char*)memchr(
1704
- name_storage, ':', nlen);
2093
+ const char* colon = set_short
2094
+ ? (leptris_word8_has(set_nw, ':')
2095
+ ? (const char*)memchr(name_storage, ':', nlen)
2096
+ : NULL)
2097
+ : (const char*)memchr(name_storage, ':', nlen);
1705
2098
  if (colon) {
1706
2099
  struct leptris_memory_pool* spool =
1707
2100
  leptris_element_get_pool(elem);
@@ -1747,8 +2140,8 @@ LeptrisStatus leptris_element_set_attribute(LeptrisElement elem, const char* nam
1747
2140
 
1748
2141
  elem->attr_count++;
1749
2142
 
1750
- if (set_ix && set_slot != (size_t)-1) {
1751
- attr_index_put(set_ix, elem, set_name_hash, attr, set_slot);
2143
+ if (set_tbl && set_slot != (size_t)-1) {
2144
+ attr_tbl_put(set_doc, set_tbl, set_key, attr, set_slot);
1752
2145
  }
1753
2146
  }
1754
2147
 
@@ -1765,7 +2158,6 @@ LeptrisStatus leptris_element_remove_attribute(LeptrisElement elem, const char*
1765
2158
  if (!elem || !name) return LEPTRIS_ERROR_NULL_ARG;
1766
2159
 
1767
2160
  struct leptris_document* rm_doc = leptris_element_get_document(elem);
1768
- uint32_t rm_hash = attr_index_hash(name, strlen(name));
1769
2161
 
1770
2162
  struct leptris_attribute* attr = leptris_element_get_first_attribute(elem);
1771
2163
  struct leptris_attribute* prev = NULL;
@@ -1803,21 +2195,27 @@ LeptrisStatus leptris_element_remove_attribute(LeptrisElement elem, const char*
1803
2195
  * reclaimed when the entire document/pool is freed.
1804
2196
  * Trying to free() them here causes undefined behavior (Abort trap). */
1805
2197
 
1806
- /* Tombstone the index entry: the attr memory stays valid
2198
+ /* Tombstone the index slot: the attr memory stays valid
1807
2199
  * (pool-backed) but is detached — a later set_attribute
1808
- * must not resurrect it. */
2200
+ * must not resurrect it. The element's table only exists
2201
+ * past the walk threshold; a walk-tier element has none. */
1809
2202
  if (rm_doc && rm_doc->mut_attr_elem == elem &&
1810
2203
  rm_doc->mut_attr_tail == attr) {
1811
2204
  rm_doc->mut_attr_elem = NULL;
1812
2205
  rm_doc->mut_attr_tail = NULL;
1813
2206
  }
1814
2207
  if (rm_doc && rm_doc->attr_index) {
1815
- struct leptris_attribute* dummy;
1816
- int found;
1817
- size_t slot = attr_index_probe(rm_doc->attr_index, elem,
1818
- rm_hash, &dummy, &found);
1819
- if (found && rm_doc->attr_index->slots[slot].attr == attr) {
1820
- rm_doc->attr_index->slots[slot].attr = NULL;
2208
+ struct leptris_attr_table* rm_tbl =
2209
+ attr_tbl_existing(rm_doc->attr_index, elem);
2210
+ if (rm_tbl) {
2211
+ struct leptris_attribute* hit = NULL;
2212
+ int found;
2213
+ size_t slot = attr_tbl_probe(
2214
+ rm_tbl, attr_index_key(name, strlen(name)),
2215
+ &hit, &found);
2216
+ /* Only the removed attr's own slot: a key collision
2217
+ * with a different name owns its entry. */
2218
+ if (found && hit == attr) attr_tbl_tombstone(rm_tbl, slot);
1821
2219
  }
1822
2220
  }
1823
2221
 
@@ -1856,6 +2254,9 @@ static int copy_element_namespaces_pooled(LeptrisElement copy,
1856
2254
  LeptrisMemoryPool* pool) {
1857
2255
  int any_decl = 0;
1858
2256
  if (!pool) return 0;
2257
+ /* #2294: the explicit-detach intent survives an adoption copy. */
2258
+ if (leptris_elem_ns_cleared(source))
2259
+ leptris_elem_set_ns_cleared(copy, 1);
1859
2260
  char* pfx = leptris_elem_prefix(source);
1860
2261
  if (pfx) {
1861
2262
  LeptrisStringView pv = leptris_sv_from_cstr(pfx);