@geastack/engine 0.1.6 → 0.1.7

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.
package/ui/tree_state.cpp CHANGED
@@ -6,6 +6,8 @@
6
6
  #include <cstring>
7
7
  #include <deque>
8
8
  #include <new>
9
+ #include <memory>
10
+ #include <vector>
9
11
  #include <utility>
10
12
 
11
13
  // TreeState is a single large struct (kMaxNodes-scaled per-node arrays: nodes,
@@ -20,7 +22,165 @@
20
22
  #include "esp_heap_caps.h"
21
23
  #endif
22
24
 
23
- namespace gea::embedded::ui {
25
+ namespace gea::embedded::ui
26
+ {
27
+
28
+ namespace
29
+ {
30
+ std::size_t g_nodeTextStorageBytes = 0;
31
+ template <class T> struct NodeTextAllocator {
32
+ using value_type = T;
33
+ NodeTextAllocator() = default;
34
+ template <class U> NodeTextAllocator(const NodeTextAllocator<U> &) {}
35
+ T *allocate(std::size_t n)
36
+ {
37
+ #if defined(ESP_PLATFORM)
38
+ // Match TreeState placement even when the board sends small ordinary
39
+ // allocations to SRAM. Node text must not steal the audio heap.
40
+ T *result = static_cast<T *>(heap_caps_malloc(n * sizeof(T), MALLOC_CAP_SPIRAM));
41
+ if (!result) result = std::allocator<T>{}.allocate(n);
42
+ #else
43
+ T *result = std::allocator<T>{}.allocate(n);
44
+ #endif
45
+ g_nodeTextStorageBytes += n * sizeof(T);
46
+ return result;
47
+ }
48
+ void deallocate(T *ptr, std::size_t n)
49
+ {
50
+ g_nodeTextStorageBytes -= n * sizeof(T);
51
+ #if defined(ESP_PLATFORM)
52
+ // ESP-IDF new/delete and capability allocations use the same heap.
53
+ heap_caps_free(ptr);
54
+ #else
55
+ std::allocator<T>{}.deallocate(ptr, n);
56
+ #endif
57
+ }
58
+ template <class U> bool operator==(const NodeTextAllocator<U> &) const
59
+ {
60
+ return true;
61
+ }
62
+ template <class U> bool operator!=(const NodeTextAllocator<U> &) const
63
+ {
64
+ return false;
65
+ }
66
+ };
67
+ union NodeTextEntry {
68
+ // A free entry has no live string, so its reuse link occupies the same
69
+ // bytes instead of adding a word to every nonempty label.
70
+ std::string text;
71
+ std::uint32_t nextFree;
72
+ NodeTextEntry() : nextFree(UINT32_MAX) {}
73
+ ~NodeTextEntry() {} // Live strings are destroyed when their owner clears.
74
+ };
75
+ struct NodeTextPool {
76
+ // Fixed small pages avoid libc++ deque's 4 KiB minimum block for a UI
77
+ // containing only a handful of labels. Page addresses never move.
78
+ static constexpr std::size_t kPageEntries = 16;
79
+ struct PageDeleter {
80
+ void operator()(NodeTextEntry *page) const {
81
+ for (std::size_t i = 0; i < kPageEntries; ++i) page[i].~NodeTextEntry();
82
+ NodeTextAllocator<NodeTextEntry>{}.deallocate(page, kPageEntries);
83
+ }
84
+ };
85
+ std::vector<NodeTextEntry *, NodeTextAllocator<NodeTextEntry *>> pages;
86
+ std::size_t size = 0;
87
+ std::uint32_t freeHead = UINT32_MAX;
88
+ NodeTextEntry &at(std::size_t index)
89
+ {
90
+ return pages[index / kPageEntries][index % kPageEntries];
91
+ }
92
+ void append()
93
+ {
94
+ if (size == pages.size() * kPageEntries) {
95
+ std::unique_ptr<NodeTextEntry[], PageDeleter> page(NodeTextAllocator<NodeTextEntry>{}.allocate(kPageEntries));
96
+ for (std::size_t i = 0; i < kPageEntries; ++i) new (&page[i]) NodeTextEntry;
97
+ pages.push_back(page.get());
98
+ page.release();
99
+ }
100
+ ++size;
101
+ }
102
+ };
103
+ NodeTextPool *g_nodeTextPool = nullptr;
104
+ const std::string g_emptyNodeText;
105
+ NodeTextPool &nodeTextPool()
106
+ {
107
+ if (!g_nodeTextPool)
108
+ g_nodeTextPool = new (NodeTextAllocator<NodeTextPool>{}.allocate(1)) NodeTextPool;
109
+ return *g_nodeTextPool;
110
+ }
111
+ } // namespace
112
+
113
+ std::size_t NodeText::storageBytes()
114
+ {
115
+ return g_nodeTextStorageBytes;
116
+ }
117
+
118
+ NodeText::NodeText(const NodeText &other)
119
+ {
120
+ if (!other.empty())
121
+ assign(other.c_str());
122
+ }
123
+ NodeText::NodeText(NodeText &&other) noexcept : handle_(other.handle_)
124
+ {
125
+ other.handle_ = kEmpty;
126
+ }
127
+ NodeText::~NodeText() { clear(); }
128
+ NodeText &NodeText::operator=(const NodeText &other)
129
+ {
130
+ if (this != &other) {
131
+ if (other.empty())
132
+ clear();
133
+ else
134
+ assign(other.c_str());
135
+ }
136
+ return *this;
137
+ }
138
+ NodeText &NodeText::operator=(NodeText &&other) noexcept
139
+ {
140
+ if (this != &other) {
141
+ clear();
142
+ handle_ = other.handle_;
143
+ other.handle_ = kEmpty;
144
+ }
145
+ return *this;
146
+ }
147
+ const std::string &NodeText::str() const
148
+ {
149
+ return empty() ? g_emptyNodeText : nodeTextPool().at(handle_).text;
150
+ }
151
+ void NodeText::clear()
152
+ {
153
+ if (empty())
154
+ return;
155
+ auto &pool = nodeTextPool();
156
+ auto &entry = pool.at(handle_);
157
+ // Release long text allocations on removal, rather than retaining the
158
+ // largest label ever seen in every reusable slot.
159
+ entry.text.~basic_string();
160
+ entry.nextFree = pool.freeHead;
161
+ pool.freeHead = handle_;
162
+ handle_ = kEmpty;
163
+ }
164
+ void NodeText::assign(const char *text)
165
+ {
166
+ if (!text || !*text) {
167
+ clear();
168
+ return;
169
+ }
170
+ auto &pool = nodeTextPool();
171
+ if (empty()) {
172
+ if (pool.freeHead == kEmpty) {
173
+ const auto index = pool.size;
174
+ pool.append();
175
+ handle_ = static_cast<std::uint32_t>(index);
176
+ } else {
177
+ handle_ = pool.freeHead;
178
+ pool.freeHead = pool.at(handle_).nextFree;
179
+ }
180
+ new (&pool.at(handle_).text) std::string;
181
+ }
182
+ pool.at(handle_).text.assign(text);
183
+ }
24
184
 
25
185
  namespace {
26
186
 
@@ -307,137 +467,118 @@ std::string NodeClassList::value() const
307
467
  return out;
308
468
  }
309
469
 
470
+ // Empty override stores own nothing. One allocation contains both metadata
471
+ // and values; computed styles remain ordinary aligned fields in Node.
472
+ struct NodeStyleOverrideStore::Block {
473
+ std::size_t count = 0, capacity = 0;
474
+ // Only CSS px overrides allocate unit metadata. Raw numeric styles keep
475
+ // their compact entries and do not lose precision by reverse-scaling ints.
476
+ std::unique_ptr<std::vector<std::pair<Property, float>>> cssPixels;
477
+ NodeStyleOverride *values() { return reinterpret_cast<NodeStyleOverride *>(this + 1); }
478
+ const NodeStyleOverride *values() const { return reinterpret_cast<const NodeStyleOverride *>(this + 1); }
479
+ };
310
480
  namespace {
311
-
312
- NodeStyleOverride &styleOverrideAt(NodeStyleOverrideStore &store, std::size_t index)
313
- {
314
- return store.spilled ? store.spillValues[index] : store.inlineValues[index];
315
- }
316
-
317
481
  void appendStyleOverride(NodeStyleOverrideStore &store, NodeStyleOverride entry)
318
482
  {
319
- if (!store.spilled && store.inlineCount < NodeStyleOverrideStore::kInlineCount) {
320
- store.inlineValues[store.inlineCount++] = entry;
321
- return;
322
- }
323
- if (!store.spilled) {
324
- store.spillCapacity = NodeStyleOverrideStore::kInlineCount * 2;
325
- store.spillValues = new NodeStyleOverride[store.spillCapacity];
326
- for (std::size_t i = 0; i < store.inlineCount; ++i)
327
- store.spillValues[i] = store.inlineValues[i];
328
- store.spillCount = store.inlineCount;
329
- store.spilled = true;
330
- }
331
- if (store.spillCount >= store.spillCapacity) {
332
- const std::size_t nextCapacity = store.spillCapacity ? store.spillCapacity * 2 : NodeStyleOverrideStore::kInlineCount * 2;
333
- auto *next = new NodeStyleOverride[nextCapacity];
334
- for (std::size_t i = 0; i < store.spillCount; ++i)
335
- next[i] = store.spillValues[i];
336
- delete[] store.spillValues;
337
- store.spillValues = next;
338
- store.spillCapacity = nextCapacity;
483
+ auto *old = store.block;
484
+ if (!old || old->count == old->capacity) {
485
+ const std::size_t capacity = old ? old->capacity * 2 : 2;
486
+ auto *next = new (::operator new(sizeof(NodeStyleOverrideStore::Block) + capacity * sizeof(NodeStyleOverride))) NodeStyleOverrideStore::Block;
487
+ next->capacity = capacity;
488
+ if (old) {
489
+ next->count = old->count;
490
+ next->cssPixels = std::move(old->cssPixels);
491
+ for (std::size_t i = 0; i < old->count; ++i) new (&next->values()[i]) NodeStyleOverride(old->values()[i]);
492
+ old->~Block();
493
+ ::operator delete(old);
494
+ }
495
+ store.block = next;
339
496
  }
340
- store.spillValues[store.spillCount++] = entry;
497
+ new (&store.block->values()[store.block->count++]) NodeStyleOverride(entry);
498
+ }
341
499
  }
342
-
343
- } // namespace
344
-
345
500
  NodeStyleOverrideStore::NodeStyleOverrideStore(const NodeStyleOverrideStore &other)
346
501
  {
347
- for (std::size_t i = 0, n = other.size(); i < n; ++i)
348
- appendStyleOverride(*this, other.at(i));
502
+ for (std::size_t i = 0; i < other.size(); ++i) appendStyleOverride(*this, other.at(i));
503
+ if (other.block && other.block->cssPixels)
504
+ block->cssPixels = std::make_unique<std::vector<std::pair<Property, float>>>(*other.block->cssPixels);
349
505
  }
350
-
351
506
  NodeStyleOverrideStore &NodeStyleOverrideStore::operator=(const NodeStyleOverrideStore &other)
352
507
  {
353
- if (this == &other) return *this;
354
- clear();
355
- for (std::size_t i = 0, n = other.size(); i < n; ++i)
356
- appendStyleOverride(*this, other.at(i));
508
+ if (this != &other) {
509
+ NodeStyleOverrideStore copy(other);
510
+ *this = std::move(copy);
511
+ }
357
512
  return *this;
358
513
  }
359
-
360
- NodeStyleOverrideStore::NodeStyleOverrideStore(NodeStyleOverrideStore &&other) noexcept
514
+ NodeStyleOverrideStore::NodeStyleOverrideStore(NodeStyleOverrideStore &&other) noexcept : block(other.block)
361
515
  {
362
- for (std::uint8_t i = 0; i < kInlineCount; ++i)
363
- inlineValues[i] = other.inlineValues[i];
364
- spillValues = other.spillValues;
365
- spillCount = other.spillCount;
366
- spillCapacity = other.spillCapacity;
367
- inlineCount = other.inlineCount;
368
- spilled = other.spilled;
369
- other.spillValues = nullptr;
370
- other.spillCount = 0;
371
- other.spillCapacity = 0;
372
- other.inlineCount = 0;
373
- other.spilled = false;
516
+ other.block = nullptr;
374
517
  }
375
-
376
518
  NodeStyleOverrideStore &NodeStyleOverrideStore::operator=(NodeStyleOverrideStore &&other) noexcept
377
519
  {
378
- if (this == &other) return *this;
379
- delete[] spillValues;
380
- for (std::uint8_t i = 0; i < kInlineCount; ++i)
381
- inlineValues[i] = other.inlineValues[i];
382
- spillValues = other.spillValues;
383
- spillCount = other.spillCount;
384
- spillCapacity = other.spillCapacity;
385
- inlineCount = other.inlineCount;
386
- spilled = other.spilled;
387
- other.spillValues = nullptr;
388
- other.spillCount = 0;
389
- other.spillCapacity = 0;
390
- other.inlineCount = 0;
391
- other.spilled = false;
520
+ if (this != &other) {
521
+ clear();
522
+ block = other.block;
523
+ other.block = nullptr;
524
+ }
392
525
  return *this;
393
526
  }
394
-
395
- NodeStyleOverrideStore::~NodeStyleOverrideStore()
396
- {
397
- delete[] spillValues;
398
- }
399
-
527
+ NodeStyleOverrideStore::~NodeStyleOverrideStore() { clear(); }
400
528
  void NodeStyleOverrideStore::clear()
401
529
  {
402
- if (spilled)
403
- spillCount = 0;
404
- else
405
- inlineCount = 0;
530
+ if (!block) return;
531
+ block->~Block();
532
+ ::operator delete(block);
533
+ block = nullptr;
406
534
  }
407
-
535
+ std::size_t NodeStyleOverrideStore::size() const { return block ? block->count : 0; }
408
536
  void NodeStyleOverrideStore::set(Property property, int value)
409
537
  {
410
- for (std::size_t i = 0, n = size(); i < n; ++i) {
411
- auto &entry = styleOverrideAt(*this, i);
412
- if (entry.property != property) continue;
413
- entry.value = value;
414
- return;
538
+ for (std::size_t i = 0; i < size(); ++i) {
539
+ auto &entry = block->values()[i];
540
+ if (entry.property == property) {
541
+ entry.value = value;
542
+ if (block->cssPixels) {
543
+ auto &units = *block->cssPixels;
544
+ units.erase(std::remove_if(units.begin(), units.end(), [property](const auto &item) { return item.first == property; }), units.end());
545
+ }
546
+ return;
547
+ }
415
548
  }
416
549
  appendStyleOverride(*this, {property, value});
417
550
  }
418
-
551
+ void NodeStyleOverrideStore::setCssPixels(Property property, float value)
552
+ {
553
+ if (!block) return;
554
+ bool present = false;
555
+ for (std::size_t i = 0; i < size(); ++i) if (at(i).property == property) { present = true; break; }
556
+ if (!present) return;
557
+ if (!block->cssPixels) block->cssPixels = std::make_unique<std::vector<std::pair<Property, float>>>();
558
+ for (auto &entry : *block->cssPixels) if (entry.first == property) { entry.second = value; return; }
559
+ block->cssPixels->emplace_back(property, value);
560
+ }
561
+ bool NodeStyleOverrideStore::getCssPixels(Property property, float &value) const
562
+ {
563
+ if (!block || !block->cssPixels) return false;
564
+ for (const auto &entry : *block->cssPixels) if (entry.first == property) { value = entry.second; return true; }
565
+ return false;
566
+ }
419
567
  bool NodeStyleOverrideStore::remove(Property property)
420
568
  {
421
- for (std::size_t i = 0, n = size(); i < n; ++i) {
569
+ for (std::size_t i = 0; i < size(); ++i) {
422
570
  if (at(i).property != property) continue;
423
- if (spilled) {
424
- for (std::size_t j = i + 1; j < spillCount; ++j)
425
- spillValues[j - 1] = spillValues[j];
426
- --spillCount;
427
- } else {
428
- for (std::size_t j = i + 1; j < inlineCount; ++j)
429
- inlineValues[j - 1] = inlineValues[j];
430
- --inlineCount;
571
+ if (block->cssPixels) {
572
+ auto &units = *block->cssPixels;
573
+ units.erase(std::remove_if(units.begin(), units.end(), [property](const auto &item) { return item.first == property; }), units.end());
431
574
  }
575
+ for (std::size_t j = i + 1; j < block->count; ++j) block->values()[j - 1] = block->values()[j];
576
+ if (--block->count == 0) clear();
432
577
  return true;
433
578
  }
434
579
  return false;
435
580
  }
436
-
437
- const NodeStyleOverride &NodeStyleOverrideStore::at(std::size_t index) const
438
- {
439
- return spilled ? spillValues[index] : inlineValues[index];
440
- }
581
+ const NodeStyleOverride &NodeStyleOverrideStore::at(std::size_t index) const { return block->values()[index]; }
441
582
 
442
583
  void NodeCustomPropertyStore::clear()
443
584
  {
@@ -450,9 +591,16 @@ void NodeCustomPropertyStore::set(const std::string &name, const std::string &va
450
591
  set(internCssAtom(name), value);
451
592
  }
452
593
 
453
- CssAtomId customPropertyValueAtom(const std::string &value)
594
+ void setCustomPropertyStorage(NodeCustomProperty &entry, const std::string &value)
454
595
  {
455
- return value.empty() ? kInvalidCssAtom : internCssAtom(value);
596
+ const CssAtomId atom = value.empty() ? kInvalidCssAtom : internCssAtom(value);
597
+ // Form the replacement before releasing the old string: callers may pass
598
+ // the value returned by get(), including an uninterned overflow value.
599
+ auto storage = atom != kInvalidCssAtom || value.empty()
600
+ ? std::shared_ptr<const std::string>(std::shared_ptr<const std::string>{}, &cssAtomString(atom))
601
+ : std::make_shared<const std::string>(value);
602
+ entry.value = std::move(storage);
603
+ entry.valueAtom = atom;
456
604
  }
457
605
 
458
606
  void NodeCustomPropertyStore::set(CssAtomId nameId, const std::string &value)
@@ -460,15 +608,13 @@ void NodeCustomPropertyStore::set(CssAtomId nameId, const std::string &value)
460
608
  if (nameId == kInvalidCssAtom) return;
461
609
  for (auto &entry : values) {
462
610
  if (entry.nameId != nameId) continue;
463
- entry.value = value;
464
- entry.valueAtom = customPropertyValueAtom(value);
611
+ setCustomPropertyStorage(entry, value);
465
612
  entry.flags = 0;
466
613
  return;
467
614
  }
468
615
  NodeCustomProperty entry;
469
616
  entry.nameId = nameId;
470
- entry.value = value;
471
- entry.valueAtom = customPropertyValueAtom(value);
617
+ setCustomPropertyStorage(entry, value);
472
618
  values.push_back(std::move(entry));
473
619
  }
474
620
 
@@ -481,8 +627,7 @@ void NodeCustomPropertyStore::setColor(CssAtomId nameId,
481
627
  if (nameId == kInvalidCssAtom) return;
482
628
  for (auto &entry : values) {
483
629
  if (entry.nameId != nameId) continue;
484
- entry.value = value;
485
- entry.valueAtom = customPropertyValueAtom(value);
630
+ setCustomPropertyStorage(entry, value);
486
631
  entry.colorStyle = styleColor;
487
632
  entry.colorNative = nativeColor;
488
633
  entry.colorAlpha = alpha;
@@ -491,8 +636,7 @@ void NodeCustomPropertyStore::setColor(CssAtomId nameId,
491
636
  }
492
637
  NodeCustomProperty entry;
493
638
  entry.nameId = nameId;
494
- entry.value = value;
495
- entry.valueAtom = customPropertyValueAtom(value);
639
+ setCustomPropertyStorage(entry, value);
496
640
  entry.colorStyle = styleColor;
497
641
  entry.colorNative = nativeColor;
498
642
  entry.colorAlpha = alpha;
@@ -505,11 +649,11 @@ void NodeCustomPropertyStore::setLength(CssAtomId nameId,
505
649
  float lengthValue,
506
650
  std::uint8_t lengthUnit)
507
651
  {
652
+ #if GEA_CSS_CUSTOM_PROPERTY_LENGTHS
508
653
  if (nameId == kInvalidCssAtom) return;
509
654
  for (auto &entry : values) {
510
655
  if (entry.nameId != nameId) continue;
511
- entry.value = value;
512
- entry.valueAtom = customPropertyValueAtom(value);
656
+ setCustomPropertyStorage(entry, value);
513
657
  entry.lengthValue = lengthValue;
514
658
  entry.lengthUnit = lengthUnit;
515
659
  entry.flags = static_cast<std::uint8_t>((entry.flags & ~1u) | 2u);
@@ -517,12 +661,15 @@ void NodeCustomPropertyStore::setLength(CssAtomId nameId,
517
661
  }
518
662
  NodeCustomProperty entry;
519
663
  entry.nameId = nameId;
520
- entry.value = value;
521
- entry.valueAtom = customPropertyValueAtom(value);
664
+ setCustomPropertyStorage(entry, value);
522
665
  entry.lengthValue = lengthValue;
523
666
  entry.lengthUnit = lengthUnit;
524
667
  entry.flags = 2;
525
668
  values.push_back(std::move(entry));
669
+ #else
670
+ (void)lengthValue; (void)lengthUnit;
671
+ set(nameId, value);
672
+ #endif
526
673
  }
527
674
 
528
675
  const std::string *NodeCustomPropertyStore::get(const std::string &name) const
@@ -534,7 +681,7 @@ const std::string *NodeCustomPropertyStore::get(const std::string &name) const
534
681
  const std::string *NodeCustomPropertyStore::get(CssAtomId nameId) const
535
682
  {
536
683
  if (nameId == kInvalidCssAtom) return nullptr;
537
- if (const NodeCustomProperty *entry = getEntry(nameId)) return &entry->value;
684
+ if (const NodeCustomProperty *entry = getEntry(nameId)) return entry->value.get();
538
685
  return nullptr;
539
686
  }
540
687
 
@@ -571,4 +718,4 @@ TreeState &treeState()
571
718
  return *g_treeState;
572
719
  }
573
720
 
574
- } // namespace gea::embedded::ui
721
+ } // namespace gea::embedded::ui
package/ui/tree_state.h CHANGED
@@ -16,7 +16,7 @@
16
16
  namespace gea::embedded::ui {
17
17
 
18
18
  struct NodeClassList {
19
- static constexpr std::uint8_t kInlineTokenCount = 4;
19
+ static constexpr std::uint8_t kInlineTokenCount = GEA_UI_CLASS_INLINE_TOKENS;
20
20
  static constexpr std::uint16_t kNoOverflow = 0xFFFFu;
21
21
 
22
22
  CssAtomId inlineTokens[kInlineTokenCount]{};
@@ -51,13 +51,8 @@ struct NodeStyleOverride {
51
51
  };
52
52
 
53
53
  struct NodeStyleOverrideStore {
54
- static constexpr std::uint8_t kInlineCount = 4;
55
- NodeStyleOverride inlineValues[kInlineCount]{};
56
- NodeStyleOverride *spillValues = nullptr;
57
- std::size_t spillCount = 0;
58
- std::size_t spillCapacity = 0;
59
- std::uint8_t inlineCount = 0;
60
- bool spilled = false;
54
+ struct Block;
55
+ Block *block = nullptr;
61
56
 
62
57
  NodeStyleOverrideStore() = default;
63
58
  NodeStyleOverrideStore(const NodeStyleOverrideStore &other);
@@ -68,8 +63,10 @@ struct NodeStyleOverrideStore {
68
63
 
69
64
  void clear();
70
65
  void set(Property property, int value);
66
+ void setCssPixels(Property property, float value);
67
+ bool getCssPixels(Property property, float &value) const;
71
68
  bool remove(Property property);
72
- std::size_t size() const { return spilled ? spillCount : inlineCount; }
69
+ std::size_t size() const;
73
70
  bool empty() const { return size() == 0; }
74
71
  const NodeStyleOverride &at(std::size_t index) const;
75
72
  };
@@ -105,17 +102,30 @@ struct InlineStaticPosition {
105
102
 
106
103
  struct NodeCustomProperty {
107
104
  CssAtomId nameId = kInvalidCssAtom;
108
- std::string value;
109
105
  CssAtomId valueAtom = kInvalidCssAtom;
110
- std::int32_t colorStyle = 0;
111
- std::int32_t colorNative = 0;
106
+ // Most values already live in the atom table: alias its stable immutable
107
+ // string without a control block. A full atom table uses owned storage.
108
+ std::shared_ptr<const std::string> value{std::shared_ptr<const std::string>{}, &cssAtomString(kInvalidCssAtom)};
109
+ // Both caches hold packed target colors: RGB565/gray fit 16 bits;
110
+ // full-color targets retain all 32 bits and their signed carrier.
111
+ using ColorValue = std::conditional_t<GEA_PIXEL_FORMAT_IS_8888, std::int32_t, std::uint16_t>;
112
+ ColorValue colorStyle = 0;
113
+ ColorValue colorNative = 0;
114
+ #if GEA_CSS_CUSTOM_PROPERTY_LENGTHS
112
115
  float lengthValue = 0.0f;
116
+ #else
117
+ static constexpr float lengthValue = 0.0f;
118
+ #endif
113
119
  std::uint8_t colorAlpha = 255;
120
+ #if GEA_CSS_CUSTOM_PROPERTY_LENGTHS
114
121
  std::uint8_t lengthUnit = 0;
122
+ #else
123
+ static constexpr std::uint8_t lengthUnit = 0;
124
+ #endif
115
125
  std::uint8_t flags = 0;
116
126
 
117
127
  bool hasColor() const { return (flags & 1u) != 0; }
118
- bool hasLength() const { return (flags & 2u) != 0; }
128
+ bool hasLength() const { return GEA_CSS_CUSTOM_PROPERTY_LENGTHS && (flags & 2u) != 0; }
119
129
  };
120
130
 
121
131
  struct NodeCustomPropertyStore {
@@ -162,20 +172,38 @@ struct GridTrackLayout {
162
172
  };
163
173
 
164
174
  struct NodeRareData {
175
+ // Reuse the listener record's tail padding for inline-position metadata.
176
+ // Both members remain ordinary embedded fields with fixed-offset access.
177
+ [[no_unique_address]] NodeEventListeners listeners;
178
+ InlineStaticPosition inlineStaticPosition;
165
179
  NodeAttributeStore attributes;
166
- NodeEventListeners listeners;
167
180
  NodeCustomPropertyStore customProperties;
168
181
  // Authored inline values survive rebuilding the computed custom-property map.
169
182
  NodeCustomPropertyStore inlineCustomProperties;
170
183
  NodeStyleOverrideStore defaultStyles;
171
184
  NodeStyleOverrideStore inlineStyles;
185
+ #if GEA_CSS_GRID
172
186
  CssAtomId inlineGridTemplates[2] = {kInvalidCssAtom, kInvalidCssAtom};
173
187
  uint8_t inlineGridShorthandMask = 0;
174
188
  std::unique_ptr<GridTrackLayout> gridLayout;
175
- VirtualListNodeState virtualList;
189
+ #endif
190
+ #if GEA_CSS_SCROLLING
191
+ std::unique_ptr<VirtualListNodeState> virtualList;
192
+ VirtualListNodeState &ensureVirtualList()
193
+ {
194
+ if (!virtualList) virtualList = std::make_unique<VirtualListNodeState>();
195
+ return *virtualList;
196
+ }
197
+ #else
198
+ static constexpr const VirtualListNodeState *virtualList = nullptr;
199
+ #endif
200
+ #if GEA_CSS_FIRST_LINE
176
201
  FirstLineBackground firstLineBackground;
177
202
  FirstLineFragment firstLineFragment;
178
- InlineStaticPosition inlineStaticPosition;
203
+ #else
204
+ inline static constexpr FirstLineBackground firstLineBackground{};
205
+ inline static constexpr FirstLineFragment firstLineFragment{};
206
+ #endif
179
207
 
180
208
  void clear()
181
209
  {
@@ -185,12 +213,18 @@ struct NodeRareData {
185
213
  inlineCustomProperties.clear();
186
214
  defaultStyles.clear();
187
215
  inlineStyles.clear();
216
+ #if GEA_CSS_GRID
188
217
  inlineGridTemplates[0] = inlineGridTemplates[1] = kInvalidCssAtom;
189
218
  inlineGridShorthandMask = 0;
190
219
  gridLayout.reset();
191
- virtualList = VirtualListNodeState{};
220
+ #endif
221
+ #if GEA_CSS_SCROLLING
222
+ virtualList.reset();
223
+ #endif
224
+ #if GEA_CSS_FIRST_LINE
192
225
  firstLineBackground = FirstLineBackground{};
193
226
  firstLineFragment = FirstLineFragment{};
227
+ #endif
194
228
  inlineStaticPosition = InlineStaticPosition{};
195
229
  }
196
230
  };
@@ -221,9 +255,14 @@ struct TreeState {
221
255
  // re-sync when the tree hasn't changed since the last frame, instead of
222
256
  // diffing the whole view tree at 60Hz while idle.
223
257
  uint64_t refreshSerial = 0;
258
+ #if GEA_CSS_SCROLLING
224
259
  uint64_t scrollDirtyNodes[kScrollDirtyWordCount]{};
225
260
  bool scrollDirtyAny = false;
226
261
  int pendingScrollIntoViewNode = -1;
262
+ #else
263
+ static constexpr bool scrollDirtyAny = false;
264
+ static constexpr int pendingScrollIntoViewNode = -1;
265
+ #endif
227
266
  bool inputTickRequired = false;
228
267
  bool displayListDirty = true;
229
268
  // When a rebuild is pending (displayListDirty), this says whether it may have
@@ -262,6 +301,7 @@ struct TreeState {
262
301
  // ViewGeometry::anyTransformPresent(). When false, the per-node ancestor-chain
263
302
  // transform walks short-circuit to O(1) — non-3D trees (the common case) pay
264
303
  // nothing for the 3D machinery. Defaults coax a recompute before first use.
304
+ #if GEA_CSS_TRANSFORMS
265
305
  uint64_t transformScanSerial = ~0ull;
266
306
  bool transformPresent = true;
267
307
  // Durable validity for a cached `transformPresent == false`. refreshSerial bumps
@@ -273,15 +313,26 @@ struct TreeState {
273
313
  // old mode-gate regression was that re-scan, not the gate itself). A cached `true`
274
314
  // is NOT held here — it still re-scans per refreshSerial so a removal is seen.
275
315
  bool transformScanValid = false;
316
+ #else
317
+ static constexpr uint64_t transformScanSerial = ~0ull;
318
+ static constexpr bool transformPresent = false;
319
+ static constexpr bool transformScanValid = true;
320
+ #endif
276
321
 
277
322
  // Focus + caret state for `<input>` elements. -1 means no input is
278
323
  // focused. caretLastFlipMs/caretVisible drive the blink animation —
279
324
  // consulted by InputRenderer::record on the pixel target. The macOS
280
325
  // renderer ignores these (NSResponder owns its own caret).
326
+ #if GEA_UI_INPUT_NODES
281
327
  int activeInputId = -1;
282
- int hoveredNodeId = -1;
283
328
  int caretLastFlipMs = 0;
284
329
  bool caretVisible = true;
330
+ #else
331
+ static constexpr int activeInputId = -1;
332
+ static constexpr int caretLastFlipMs = 0;
333
+ static constexpr bool caretVisible = false;
334
+ #endif
335
+ int hoveredNodeId = -1;
285
336
  };
286
337
 
287
338
  TreeState &treeState();