@native-sdk/cli 0.4.4 → 0.5.0

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 (83) hide show
  1. package/build/app.zig +187 -6
  2. package/build/ts_run.mjs +63 -0
  3. package/build.zig +213 -1
  4. package/build.zig.zon +1 -0
  5. package/package.json +15 -10
  6. package/packages/core/package-lock.json +43 -0
  7. package/packages/core/package.json +37 -0
  8. package/packages/core/rt/rt.zig +1579 -0
  9. package/packages/core/sdk/bytes_text_methods.d.ts +88 -0
  10. package/packages/core/sdk/core.ts +694 -0
  11. package/packages/core/sdk/events.ts +137 -0
  12. package/packages/core/sdk/text.ts +519 -0
  13. package/packages/core/src/checker.ts +1869 -0
  14. package/packages/core/src/cli.ts +68 -0
  15. package/packages/core/src/devhost.ts +291 -0
  16. package/packages/core/src/devhost_resolver.mjs +22 -0
  17. package/packages/core/src/diagnostics.ts +417 -0
  18. package/packages/core/src/emitter.ts +8815 -0
  19. package/packages/core/src/infer.ts +1515 -0
  20. package/packages/core/src/modules.ts +173 -0
  21. package/packages/core/src/ownership.ts +740 -0
  22. package/packages/core/src/text_polyfill.ts +337 -0
  23. package/packages/core/src/text_tables.ts +408 -0
  24. package/packages/core/src/transpile.ts +146 -0
  25. package/packages/core/src/typed_ast.ts +536 -0
  26. package/packages/core/src/types.ts +815 -0
  27. package/skill-data/native-ui/SKILL.md +14 -0
  28. package/skill-data/ts-core/SKILL.md +493 -0
  29. package/skill-data/zig/SKILL.md +10 -0
  30. package/src/app_runner/root.zig +24 -0
  31. package/src/app_runner/ts_core_main.zig +231 -0
  32. package/src/primitives/app_manifest/comptime_scene.zig +341 -0
  33. package/src/primitives/app_manifest/root.zig +5 -0
  34. package/src/primitives/canvas/commands.zig +14 -7
  35. package/src/primitives/canvas/lazy_tls.zig +73 -0
  36. package/src/primitives/canvas/render_generic_resources.zig +22 -15
  37. package/src/primitives/canvas/root.zig +6 -0
  38. package/src/primitives/canvas/tests.zig +1 -0
  39. package/src/primitives/canvas/text_atlas.zig +40 -32
  40. package/src/primitives/canvas/text_layout_cache.zig +29 -22
  41. package/src/primitives/canvas/text_measure_cache.zig +56 -41
  42. package/src/primitives/canvas/text_spans.zig +39 -26
  43. package/src/primitives/canvas/tokens.zig +54 -0
  44. package/src/primitives/canvas/ui.zig +125 -0
  45. package/src/primitives/canvas/ui_markup.zig +22 -3
  46. package/src/primitives/canvas/ui_markup_compiled.zig +97 -7
  47. package/src/primitives/canvas/ui_markup_compiled_tests.zig +275 -0
  48. package/src/primitives/canvas/ui_markup_contract.zig +68 -14
  49. package/src/primitives/canvas/ui_markup_contract_tests.zig +257 -2
  50. package/src/primitives/canvas/ui_markup_reflect.zig +214 -0
  51. package/src/primitives/canvas/ui_markup_view.zig +105 -1
  52. package/src/primitives/canvas/ui_markup_view_tests.zig +336 -0
  53. package/src/primitives/canvas/ui_schema.zig +8 -0
  54. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  55. package/src/primitives/canvas/widget_invalidation.zig +14 -7
  56. package/src/primitives/canvas/widget_render.zig +25 -15
  57. package/src/primitives/canvas/widget_render_controls.zig +15 -9
  58. package/src/primitives/canvas/widget_runtime_tests.zig +44 -0
  59. package/src/root.zig +7 -0
  60. package/src/runtime/canvas_frame.zig +57 -36
  61. package/src/runtime/canvas_images.zig +9 -3
  62. package/src/runtime/canvas_widget_control_tests.zig +5 -6
  63. package/src/runtime/canvas_widget_runtime.zig +7 -4
  64. package/src/runtime/effects.zig +490 -2
  65. package/src/runtime/effects_host_tests.zig +594 -0
  66. package/src/runtime/root.zig +18 -0
  67. package/src/runtime/session_replay.zig +7 -1
  68. package/src/runtime/tests.zig +2 -0
  69. package/src/runtime/ts_core_host.zig +1505 -0
  70. package/src/runtime/ts_core_host_tests.zig +1408 -0
  71. package/src/runtime/ts_ui_app.zig +469 -0
  72. package/src/runtime/ui_app.zig +58 -0
  73. package/src/runtime/view_canvas.zig +24 -17
  74. package/src/runtime/view_widget_tree.zig +2 -2
  75. package/src/tooling/codesign.zig +212 -105
  76. package/src/tooling/doctor.zig +78 -0
  77. package/src/tooling/manifest.zig +47 -0
  78. package/src/tooling/package.zig +124 -12
  79. package/src/tooling/raw_manifest.zig +15 -0
  80. package/src/tooling/root.zig +1 -0
  81. package/src/tooling/templates.zig +683 -28
  82. package/src/tooling/ts_core.zig +486 -0
  83. package/src/tooling/verbs.zig +17 -0
@@ -191,8 +191,14 @@ pub const DisplayList = struct {
191
191
  /// half-full bound; small or oversized lists keep the linear scans.
192
192
  const diff_id_index_slots = 4096;
193
193
  const DiffIdIndex = plan_key_index.HashSlots(diff_id_index_slots);
194
- threadlocal var diff_previous_id_index: DiffIdIndex = .{};
195
- threadlocal var diff_next_id_index: DiffIdIndex = .{};
194
+ // Lazily heap-allocated per thread (32 KiB of probe tables): reset per
195
+ // diff, so first-use init on the diffing thread is the only contract —
196
+ // threads that never diff never allocate it.
197
+ const DiffIdScratch = struct {
198
+ previous: DiffIdIndex = .{},
199
+ next: DiffIdIndex = .{},
200
+ };
201
+ const diff_id_scratch = @import("lazy_tls.zig").LazyTls(DiffIdScratch);
196
202
 
197
203
  /// Fill `table` with the keyed commands' id->index mapping, erroring on
198
204
  /// the duplicate ids `validateUniqueObjectIds` rejects — one pass does
@@ -229,9 +235,10 @@ fn diffDisplayLists(previous: DisplayList, next: DisplayList, output: []DiffChan
229
235
  next.commands.len >= plan_key_index.min_entries_for_index) and
230
236
  plan_key_index.fitsHashSlots(diff_id_index_slots, previous.commands.len) and
231
237
  plan_key_index.fitsHashSlots(diff_id_index_slots, next.commands.len);
232
- if (use_index) {
233
- try buildDiffIdIndex(previous, &diff_previous_id_index);
234
- try buildDiffIdIndex(next, &diff_next_id_index);
238
+ const id_scratch: ?*DiffIdScratch = if (use_index) diff_id_scratch.get() else null;
239
+ if (id_scratch) |scratch| {
240
+ try buildDiffIdIndex(previous, &scratch.previous);
241
+ try buildDiffIdIndex(next, &scratch.next);
235
242
  } else {
236
243
  try validateUniqueObjectIds(previous);
237
244
  try validateUniqueObjectIds(next);
@@ -260,7 +267,7 @@ fn diffDisplayLists(previous: DisplayList, next: DisplayList, output: []DiffChan
260
267
 
261
268
  for (previous.commands, 0..) |previous_command, previous_index| {
262
269
  const id = previous_command.objectId() orelse continue;
263
- const next_lookup = if (use_index) findCommandByIdIndexed(next, &diff_next_id_index, id) else next.findCommandById(id);
270
+ const next_lookup = if (id_scratch) |scratch| findCommandByIdIndexed(next, &scratch.next, id) else next.findCommandById(id);
264
271
  const next_ref = next_lookup orelse {
265
272
  try appendDiffChange(output, &len, .{
266
273
  .kind = .removed,
@@ -284,7 +291,7 @@ fn diffDisplayLists(previous: DisplayList, next: DisplayList, output: []DiffChan
284
291
 
285
292
  for (next.commands, 0..) |next_command, next_index| {
286
293
  const id = next_command.objectId() orelse continue;
287
- const previous_lookup = if (use_index) findCommandByIdIndexed(previous, &diff_previous_id_index, id) else previous.findCommandById(id);
294
+ const previous_lookup = if (id_scratch) |scratch| findCommandByIdIndexed(previous, &scratch.previous, id) else previous.findCommandById(id);
288
295
  if (previous_lookup == null) {
289
296
  try appendDiffChange(output, &len, .{
290
297
  .kind = .added,
@@ -0,0 +1,73 @@
1
+ //! Lazily heap-allocated per-thread scratch.
2
+ //!
3
+ //! Large `threadlocal` arrays land in the executable's static TLS
4
+ //! template, and the OS loader materializes that whole template for
5
+ //! EVERY thread of the process — window host, COM, accessibility, and
6
+ //! worker threads all pay for the full multi-megabyte canvas planner
7
+ //! scratch even though only a runtime loop thread ever touches it
8
+ //! (measured on Windows as ~6.5 MiB of heap-backed private working set
9
+ //! per thread). `LazyTls` keeps only one pointer in static TLS: the
10
+ //! backing storage is heap-allocated the first time a thread actually
11
+ //! asks for it, so threads that never plan a frame pay eight bytes
12
+ //! instead of megabytes.
13
+ //!
14
+ //! Semantics match the `threadlocal var scratch: T = .{}` it replaces:
15
+ //! each thread gets its own instance, initialized to the struct's field
16
+ //! defaults on that thread's first access. Fields declared WITHOUT a
17
+ //! default stay uninitialized, matching the `= undefined` statics they
18
+ //! replace. The instance lives until process exit — one long-lived
19
+ //! runtime loop thread per process is the designed shape, and a static
20
+ //! TLS block was process-lifetime address space per thread too.
21
+ //!
22
+ //! Allocation failure panics: this is the render path's fixed scratch,
23
+ //! sized at compile time, and a process that cannot commit it cannot
24
+ //! render at all — the old static-TLS commit would have failed thread
25
+ //! creation under the same pressure.
26
+
27
+ const std = @import("std");
28
+
29
+ pub fn LazyTls(comptime T: type) type {
30
+ return struct {
31
+ threadlocal var instance: ?*T = null;
32
+
33
+ /// This thread's instance, allocated and default-initialized on
34
+ /// first use. The pointer is stable for the thread's lifetime,
35
+ /// so hot loops may hoist it once per operation.
36
+ pub fn get() *T {
37
+ return instance orelse create();
38
+ }
39
+
40
+ /// This thread's instance only if something already used it —
41
+ /// for stats accessors that must observe without allocating.
42
+ pub fn peek() ?*T {
43
+ return instance;
44
+ }
45
+
46
+ fn create() *T {
47
+ const ptr = std.heap.page_allocator.create(T) catch
48
+ @panic("out of memory allocating per-thread canvas scratch");
49
+ inline for (@typeInfo(T).@"struct".fields) |field| {
50
+ if (comptime field.defaultValue()) |value| @field(ptr, field.name) = value;
51
+ }
52
+ instance = ptr;
53
+ return ptr;
54
+ }
55
+ };
56
+ }
57
+
58
+ test "lazy tls initializes defaults once per access pattern" {
59
+ const Scratch = struct {
60
+ counter: u64 = 7,
61
+ buffer: [32]u8, // no default: stays uninitialized, like `= undefined`
62
+ };
63
+ const tls = LazyTls(Scratch);
64
+ try std.testing.expectEqual(@as(?*Scratch, null), tls.peek());
65
+ const first = tls.get();
66
+ try std.testing.expectEqual(@as(u64, 7), first.counter);
67
+ first.counter += 1;
68
+ first.buffer[0] = 42;
69
+ const second = tls.get();
70
+ try std.testing.expectEqual(first, second);
71
+ try std.testing.expectEqual(@as(u64, 8), second.counter);
72
+ try std.testing.expectEqual(@as(?*Scratch, first), tls.peek());
73
+ }
@@ -231,30 +231,31 @@ pub const RenderResourceCachePlanner = struct {
231
231
  previous.len >= plan_key_index.min_entries_for_index) and
232
232
  plan_key_index.fitsHashSlots(resource_cache_index_slots, previous.len) and
233
233
  plan_key_index.fitsHashSlots(resource_cache_index_slots, resource_plan.resources.len);
234
- if (use_index) {
235
- resource_cache_previous_index.reset();
234
+ const index_scratch: ?*ResourceCacheIndexScratch = if (use_index) resource_cache_index_scratch.get() else null;
235
+ if (index_scratch) |scratch| {
236
+ scratch.previous.reset();
236
237
  for (previous, 0..) |entry, index| {
237
238
  var p = ResourceCacheIndex.probe(renderResourceKeyHash(entry.key));
238
- while (resource_cache_previous_index.next(&p)) |_| {}
239
- resource_cache_previous_index.insert(p, @intCast(index));
239
+ while (scratch.previous.next(&p)) |_| {}
240
+ scratch.previous.insert(p, @intCast(index));
240
241
  }
241
- resource_cache_entry_index.reset();
242
+ scratch.entry.reset();
242
243
  }
243
244
 
244
245
  for (resource_plan.resources, 0..) |resource, resource_index| {
245
246
  const key = renderResourceKey(resource);
246
247
  const key_hash = if (use_index) renderResourceKeyHash(key) else 0;
247
- if (use_index) {
248
+ if (index_scratch) |scratch| {
248
249
  var p = ResourceCacheIndex.probe(key_hash);
249
250
  var duplicate = false;
250
- while (resource_cache_entry_index.next(&p)) |candidate| {
251
+ while (scratch.entry.next(&p)) |candidate| {
251
252
  if (renderResourceKeysEqual(self.entries[candidate].key, key)) {
252
253
  duplicate = true;
253
254
  break;
254
255
  }
255
256
  }
256
257
  if (duplicate) continue;
257
- const previous_index = findRenderResourceCacheEntryIndexed(previous, key, key_hash);
258
+ const previous_index = findRenderResourceCacheEntryIndexed(&scratch.previous, previous, key, key_hash);
258
259
  try self.appendAction(.{
259
260
  .kind = if (previous_index == null) .upload else .retain,
260
261
  .key = key,
@@ -265,7 +266,7 @@ pub const RenderResourceCachePlanner = struct {
265
266
  .key = key,
266
267
  .last_used_frame = frame_index,
267
268
  });
268
- resource_cache_entry_index.insert(p, @intCast(self.entry_len - 1));
269
+ scratch.entry.insert(p, @intCast(self.entry_len - 1));
269
270
  continue;
270
271
  }
271
272
  if (findRenderResourceCacheEntry(self.entries[0..self.entry_len], key) != null) continue;
@@ -284,10 +285,10 @@ pub const RenderResourceCachePlanner = struct {
284
285
  }
285
286
 
286
287
  for (previous, 0..) |entry, cache_index| {
287
- if (use_index) {
288
+ if (index_scratch) |scratch| {
288
289
  var p = ResourceCacheIndex.probe(renderResourceKeyHash(entry.key));
289
290
  var kept = false;
290
- while (resource_cache_entry_index.next(&p)) |candidate| {
291
+ while (scratch.entry.next(&p)) |candidate| {
291
292
  if (renderResourceKeysEqual(self.entries[candidate].key, entry.key)) {
292
293
  kept = true;
293
294
  break;
@@ -346,14 +347,20 @@ fn findRenderResourceCacheEntry(entries: []const RenderResourceCacheEntry, key:
346
347
  /// half-full bound; bigger inputs fall back to the linear scans.
347
348
  const resource_cache_index_slots = 4096;
348
349
  const ResourceCacheIndex = plan_key_index.HashSlots(resource_cache_index_slots);
349
- threadlocal var resource_cache_previous_index: ResourceCacheIndex = .{};
350
- threadlocal var resource_cache_entry_index: ResourceCacheIndex = .{};
350
+ // Lazily heap-allocated per thread (32 KiB of probe tables): reset per
351
+ // build, so first-use init on the planning thread is the only contract —
352
+ // threads that never plan resources never allocate it.
353
+ const ResourceCacheIndexScratch = struct {
354
+ previous: ResourceCacheIndex = .{},
355
+ entry: ResourceCacheIndex = .{},
356
+ };
357
+ const resource_cache_index_scratch = @import("lazy_tls.zig").LazyTls(ResourceCacheIndexScratch);
351
358
 
352
359
  /// The chain's first equal candidate is the lowest-index equal entry —
353
360
  /// the exact value the linear scan returned.
354
- fn findRenderResourceCacheEntryIndexed(previous: []const RenderResourceCacheEntry, key: RenderResourceKey, key_hash: u64) ?usize {
361
+ fn findRenderResourceCacheEntryIndexed(previous_index: *const ResourceCacheIndex, previous: []const RenderResourceCacheEntry, key: RenderResourceKey, key_hash: u64) ?usize {
355
362
  var p = ResourceCacheIndex.probe(key_hash);
356
- while (resource_cache_previous_index.next(&p)) |candidate| {
363
+ while (previous_index.next(&p)) |candidate| {
357
364
  if (renderResourceKeysEqual(previous[candidate].key, key)) return candidate;
358
365
  }
359
366
  return null;
@@ -370,6 +370,7 @@ pub const PixelSnapTokenOverrides = token_model.PixelSnapTokenOverrides;
370
370
  pub const ControlVisualTokenOverrides = token_model.ControlVisualTokenOverrides;
371
371
  pub const ControlTokenOverrides = token_model.ControlTokenOverrides;
372
372
  pub const DesignTokenOverrides = token_model.DesignTokenOverrides;
373
+ pub const accentOverrides = token_model.accentOverrides;
373
374
  pub const DesignTokens = token_model.DesignTokens;
374
375
 
375
376
  // Canvas widget model and built-in factories live in `widgets.zig`; root keeps the public API stable.
@@ -469,6 +470,11 @@ pub const markdown = @import("markdown.zig");
469
470
  // the runtime's keyed diffs (see plan_key_index.zig).
470
471
  pub const plan_key_index = @import("plan_key_index.zig");
471
472
 
473
+ // Lazily heap-allocated per-thread scratch: keeps the large planner
474
+ // buffers out of the static TLS template every OS thread must clone
475
+ // (see lazy_tls.zig for the working-set numbers).
476
+ pub const lazy_tls = @import("lazy_tls.zig");
477
+
472
478
  // Experimental markup front-end lives in `ui_markup.zig` / `ui_markup_view.zig`
473
479
  // (runtime parse + interpret: the dev/hot-reload engine) and
474
480
  // `ui_markup_compiled.zig` (comptime parse: the release engine, no parser in
@@ -27,4 +27,5 @@ test {
27
27
  _ = @import("markdown_hostile_tests.zig");
28
28
  _ = @import("layout_audit_tests.zig");
29
29
  _ = @import("a11y_audit_tests.zig");
30
+ _ = @import("lazy_tls.zig");
30
31
  }
@@ -79,17 +79,18 @@ pub const GlyphAtlasPlanner = struct {
79
79
  }
80
80
  const use_index = estimated_glyphs >= plan_key_index.min_entries_for_index and
81
81
  plan_key_index.fitsHashSlots(glyph_atlas_index_slots, self.entries.len);
82
- if (use_index) glyph_atlas_plan_index.reset();
82
+ const plan_index: ?*GlyphAtlasIndex = if (use_index) &glyph_atlas_index_scratch.get().plan else null;
83
+ if (plan_index) |index| index.reset();
83
84
  for (display_list.commands, 0..) |command, command_index| {
84
85
  switch (command) {
85
- .draw_text => |value| try self.consumeText(value, command_index, use_index),
86
+ .draw_text => |value| try self.consumeText(value, command_index, plan_index),
86
87
  else => {},
87
88
  }
88
89
  }
89
90
  return .{ .entries = self.entries[0..self.len] };
90
91
  }
91
92
 
92
- fn consumeText(self: *GlyphAtlasPlanner, text: anytype, command_index: usize, use_index: bool) Error!void {
93
+ fn consumeText(self: *GlyphAtlasPlanner, text: anytype, command_index: usize, plan_index: ?*GlyphAtlasIndex) Error!void {
93
94
  if (text.glyphs.len > 0) {
94
95
  for (text.glyphs, 0..) |glyph, glyph_index| {
95
96
  const key = GlyphAtlasKey{
@@ -99,7 +100,7 @@ pub const GlyphAtlasPlanner = struct {
99
100
  .subpixel_x = subpixelBucket(text.origin.x + glyph.x),
100
101
  .subpixel_y = subpixelBucket(text.origin.y + glyph.y),
101
102
  };
102
- try self.appendUnique(key, command_index, glyph_index, use_index);
103
+ try self.appendUnique(key, command_index, glyph_index, plan_index);
103
104
  }
104
105
  return;
105
106
  }
@@ -121,15 +122,15 @@ pub const GlyphAtlasPlanner = struct {
121
122
  .subpixel_x = subpixelBucket(text.origin.x + @as(f32, @floatFromInt(scalar_index)) * text.size * 0.5),
122
123
  .subpixel_y = subpixelBucket(text.origin.y),
123
124
  };
124
- try self.appendUnique(key, command_index, scalar_index, use_index);
125
+ try self.appendUnique(key, command_index, scalar_index, plan_index);
125
126
  }
126
127
  }
127
128
 
128
- fn appendUnique(self: *GlyphAtlasPlanner, key: GlyphAtlasKey, command_index: usize, glyph_index: usize, use_index: bool) Error!void {
129
+ fn appendUnique(self: *GlyphAtlasPlanner, key: GlyphAtlasKey, command_index: usize, glyph_index: usize, plan_index: ?*GlyphAtlasIndex) Error!void {
129
130
  var probe: GlyphAtlasIndex.Probe = undefined;
130
- if (use_index) {
131
+ if (plan_index) |index| {
131
132
  probe = GlyphAtlasIndex.probe(glyphAtlasKeyHash(key));
132
- while (glyph_atlas_plan_index.next(&probe)) |candidate| {
133
+ while (index.next(&probe)) |candidate| {
133
134
  if (glyphAtlasKeysEqual(self.entries[candidate].key, key)) return;
134
135
  }
135
136
  } else {
@@ -144,7 +145,7 @@ pub const GlyphAtlasPlanner = struct {
144
145
  .glyph_index = glyph_index,
145
146
  };
146
147
  self.len += 1;
147
- if (use_index) glyph_atlas_plan_index.insert(probe, @intCast(self.len - 1));
148
+ if (plan_index) |index| index.insert(probe, @intCast(self.len - 1));
148
149
  }
149
150
  };
150
151
 
@@ -225,22 +226,23 @@ pub const GlyphAtlasCachePlanner = struct {
225
226
  previous.len >= plan_key_index.min_entries_for_index) and
226
227
  plan_key_index.fitsHashSlots(glyph_atlas_cache_index_slots, previous.len) and
227
228
  plan_key_index.fitsHashSlots(glyph_atlas_cache_index_slots, plan.entries.len + previous.len);
228
- if (use_index) {
229
- glyph_atlas_cache_previous_index.reset();
229
+ const index_scratch: ?*GlyphAtlasIndexScratch = if (use_index) glyph_atlas_index_scratch.get() else null;
230
+ if (index_scratch) |scratch| {
231
+ scratch.cache_previous.reset();
230
232
  for (previous, 0..) |entry, index| {
231
233
  var p = GlyphAtlasCacheIndex.probe(glyphAtlasKeyHash(entry.key));
232
- while (glyph_atlas_cache_previous_index.next(&p)) |_| {}
233
- glyph_atlas_cache_previous_index.insert(p, @intCast(index));
234
+ while (scratch.cache_previous.next(&p)) |_| {}
235
+ scratch.cache_previous.insert(p, @intCast(index));
234
236
  }
235
- glyph_atlas_cache_entry_index.reset();
237
+ scratch.cache_entry.reset();
236
238
  }
237
239
 
238
240
  for (plan.entries, 0..) |entry, atlas_index| {
239
241
  const previous_index = blk: {
240
- if (use_index) {
242
+ if (index_scratch) |scratch| {
241
243
  const key_hash = glyphAtlasKeyHash(entry.key);
242
- if (self.entryIndexProbe(entry.key, key_hash)) |_| continue;
243
- break :blk findGlyphAtlasCacheEntryIndexed(previous, entry.key, key_hash);
244
+ if (self.entryIndexProbe(&scratch.cache_entry, entry.key, key_hash)) |_| continue;
245
+ break :blk findGlyphAtlasCacheEntryIndexed(&scratch.cache_previous, previous, entry.key, key_hash);
244
246
  }
245
247
  if (findGlyphAtlasCacheEntry(self.entries[0..self.entry_len], entry.key) != null) continue;
246
248
  break :blk findGlyphAtlasCacheEntry(previous, entry.key);
@@ -248,7 +250,7 @@ pub const GlyphAtlasCachePlanner = struct {
248
250
  try self.appendEntryMaybeIndexed(.{
249
251
  .key = entry.key,
250
252
  .last_used_frame = frame_index,
251
- }, use_index);
253
+ }, if (index_scratch) |scratch| &scratch.cache_entry else null);
252
254
  try self.appendAction(.{
253
255
  .kind = if (previous_index == null) .upload else .retain,
254
256
  .key = entry.key,
@@ -258,13 +260,13 @@ pub const GlyphAtlasCachePlanner = struct {
258
260
  }
259
261
 
260
262
  for (previous, 0..) |entry, previous_index| {
261
- if (use_index) {
262
- if (self.entryIndexProbe(entry.key, glyphAtlasKeyHash(entry.key))) |_| continue;
263
+ if (index_scratch) |scratch| {
264
+ if (self.entryIndexProbe(&scratch.cache_entry, entry.key, glyphAtlasKeyHash(entry.key))) |_| continue;
263
265
  } else if (findGlyphAtlasCacheEntry(self.entries[0..self.entry_len], entry.key) != null) {
264
266
  continue;
265
267
  }
266
268
  if (shouldRetainUnusedCacheEntry(frame_index, entry.last_used_frame, retention_frames) and self.hasEntryCapacity()) {
267
- try self.appendEntryMaybeIndexed(entry, use_index);
269
+ try self.appendEntryMaybeIndexed(entry, if (index_scratch) |scratch| &scratch.cache_entry else null);
268
270
  try self.appendAction(.{
269
271
  .kind = .retain,
270
272
  .key = entry.key,
@@ -288,20 +290,20 @@ pub const GlyphAtlasCachePlanner = struct {
288
290
  /// First appended entry equal to `key`, walking the entry index's
289
291
  /// probe chain — the indexed equivalent of scanning
290
292
  /// `self.entries[0..self.entry_len]`.
291
- fn entryIndexProbe(self: *GlyphAtlasCachePlanner, key: GlyphAtlasKey, key_hash: u64) ?usize {
293
+ fn entryIndexProbe(self: *GlyphAtlasCachePlanner, entry_index: *const GlyphAtlasCacheIndex, key: GlyphAtlasKey, key_hash: u64) ?usize {
292
294
  var p = GlyphAtlasCacheIndex.probe(key_hash);
293
- while (glyph_atlas_cache_entry_index.next(&p)) |candidate| {
295
+ while (entry_index.next(&p)) |candidate| {
294
296
  if (glyphAtlasKeysEqual(self.entries[candidate].key, key)) return candidate;
295
297
  }
296
298
  return null;
297
299
  }
298
300
 
299
- fn appendEntryMaybeIndexed(self: *GlyphAtlasCachePlanner, entry: GlyphAtlasCacheEntry, use_index: bool) Error!void {
301
+ fn appendEntryMaybeIndexed(self: *GlyphAtlasCachePlanner, entry: GlyphAtlasCacheEntry, entry_index: ?*GlyphAtlasCacheIndex) Error!void {
300
302
  try self.appendEntry(entry);
301
- if (use_index) {
303
+ if (entry_index) |index| {
302
304
  var p = GlyphAtlasCacheIndex.probe(glyphAtlasKeyHash(entry.key));
303
- while (glyph_atlas_cache_entry_index.next(&p)) |_| {}
304
- glyph_atlas_cache_entry_index.insert(p, @intCast(self.entry_len - 1));
305
+ while (index.next(&p)) |_| {}
306
+ index.insert(p, @intCast(self.entry_len - 1));
305
307
  }
306
308
  }
307
309
 
@@ -366,13 +368,19 @@ const glyph_atlas_index_slots = 16384;
366
368
  const glyph_atlas_cache_index_slots = 32768;
367
369
  const GlyphAtlasIndex = plan_key_index.HashSlots(glyph_atlas_index_slots);
368
370
  const GlyphAtlasCacheIndex = plan_key_index.HashSlots(glyph_atlas_cache_index_slots);
369
- threadlocal var glyph_atlas_plan_index: GlyphAtlasIndex = .{};
370
- threadlocal var glyph_atlas_cache_previous_index: GlyphAtlasCacheIndex = .{};
371
- threadlocal var glyph_atlas_cache_entry_index: GlyphAtlasCacheIndex = .{};
371
+ // Lazily heap-allocated per thread (320 KiB of probe tables): reset per
372
+ // build, so first-use init on the planning thread is the only contract —
373
+ // threads that never plan glyphs never allocate it.
374
+ const GlyphAtlasIndexScratch = struct {
375
+ plan: GlyphAtlasIndex = .{},
376
+ cache_previous: GlyphAtlasCacheIndex = .{},
377
+ cache_entry: GlyphAtlasCacheIndex = .{},
378
+ };
379
+ const glyph_atlas_index_scratch = @import("lazy_tls.zig").LazyTls(GlyphAtlasIndexScratch);
372
380
 
373
- fn findGlyphAtlasCacheEntryIndexed(previous: []const GlyphAtlasCacheEntry, key: GlyphAtlasKey, key_hash: u64) ?usize {
381
+ fn findGlyphAtlasCacheEntryIndexed(previous_index: *const GlyphAtlasCacheIndex, previous: []const GlyphAtlasCacheEntry, key: GlyphAtlasKey, key_hash: u64) ?usize {
374
382
  var p = GlyphAtlasCacheIndex.probe(key_hash);
375
- while (glyph_atlas_cache_previous_index.next(&p)) |candidate| {
383
+ while (previous_index.next(&p)) |candidate| {
376
384
  if (glyphAtlasKeysEqual(previous[candidate].key, key)) return candidate;
377
385
  }
378
386
  return null;
@@ -136,22 +136,23 @@ pub const TextLayoutCachePlanner = struct {
136
136
  previous.len >= plan_key_index.min_entries_for_index) and
137
137
  plan_key_index.fitsHashSlots(text_layout_cache_index_slots, previous.len) and
138
138
  plan_key_index.fitsHashSlots(text_layout_cache_index_slots, plans.len + previous.len);
139
- if (use_index) {
140
- text_layout_cache_previous_index.reset();
139
+ const index_scratch: ?*TextLayoutCacheIndexScratch = if (use_index) text_layout_cache_index_scratch.get() else null;
140
+ if (index_scratch) |scratch| {
141
+ scratch.previous.reset();
141
142
  for (previous, 0..) |entry, index| {
142
143
  var p = TextLayoutCacheIndex.probe(textLayoutKeyHash(entry.key));
143
- while (text_layout_cache_previous_index.next(&p)) |_| {}
144
- text_layout_cache_previous_index.insert(p, @intCast(index));
144
+ while (scratch.previous.next(&p)) |_| {}
145
+ scratch.previous.insert(p, @intCast(index));
145
146
  }
146
- text_layout_cache_entry_index.reset();
147
+ scratch.entry.reset();
147
148
  }
148
149
 
149
150
  for (plans, 0..) |plan, layout_index| {
150
151
  const previous_index = blk: {
151
- if (use_index) {
152
+ if (index_scratch) |scratch| {
152
153
  const key_hash = textLayoutKeyHash(plan.key);
153
- if (self.entryIndexProbe(plan.key, key_hash)) |_| continue;
154
- break :blk findTextLayoutCacheEntryIndexed(previous, plan.key, key_hash);
154
+ if (self.entryIndexProbe(&scratch.entry, plan.key, key_hash)) |_| continue;
155
+ break :blk findTextLayoutCacheEntryIndexed(&scratch.previous, previous, plan.key, key_hash);
155
156
  }
156
157
  if (findTextLayoutCacheEntry(self.entries[0..self.entry_len], plan.key) != null) continue;
157
158
  break :blk findTextLayoutCacheEntry(previous, plan.key);
@@ -162,7 +163,7 @@ pub const TextLayoutCachePlanner = struct {
162
163
  .line_count = plan.lineCount(),
163
164
  .bounds = plan.layout.bounds,
164
165
  .last_used_frame = frame_index,
165
- }, use_index);
166
+ }, if (index_scratch) |scratch| &scratch.entry else null);
166
167
  try self.appendAction(.{
167
168
  .kind = if (previous_index == null) .upload else .retain,
168
169
  .key = plan.key,
@@ -172,13 +173,13 @@ pub const TextLayoutCachePlanner = struct {
172
173
  }
173
174
 
174
175
  for (previous, 0..) |entry, index| {
175
- if (use_index) {
176
- if (self.entryIndexProbe(entry.key, textLayoutKeyHash(entry.key))) |_| continue;
176
+ if (index_scratch) |scratch| {
177
+ if (self.entryIndexProbe(&scratch.entry, entry.key, textLayoutKeyHash(entry.key))) |_| continue;
177
178
  } else if (findTextLayoutCacheEntry(self.entries[0..self.entry_len], entry.key) != null) {
178
179
  continue;
179
180
  }
180
181
  if (shouldRetainUnusedCacheEntry(frame_index, entry.last_used_frame, retention_frames) and self.hasEntryCapacity()) {
181
- try self.appendEntryMaybeIndexed(entry, use_index);
182
+ try self.appendEntryMaybeIndexed(entry, if (index_scratch) |scratch| &scratch.entry else null);
182
183
  try self.appendAction(.{
183
184
  .kind = .retain,
184
185
  .key = entry.key,
@@ -202,20 +203,20 @@ pub const TextLayoutCachePlanner = struct {
202
203
  /// First appended entry equal to `key`, walking the entry index's
203
204
  /// probe chain — the indexed equivalent of scanning
204
205
  /// `self.entries[0..self.entry_len]`.
205
- fn entryIndexProbe(self: *TextLayoutCachePlanner, key: TextLayoutKey, key_hash: u64) ?usize {
206
+ fn entryIndexProbe(self: *TextLayoutCachePlanner, entry_index: *const TextLayoutCacheIndex, key: TextLayoutKey, key_hash: u64) ?usize {
206
207
  var p = TextLayoutCacheIndex.probe(key_hash);
207
- while (text_layout_cache_entry_index.next(&p)) |candidate| {
208
+ while (entry_index.next(&p)) |candidate| {
208
209
  if (textLayoutKeysEqual(self.entries[candidate].key, key)) return candidate;
209
210
  }
210
211
  return null;
211
212
  }
212
213
 
213
- fn appendEntryMaybeIndexed(self: *TextLayoutCachePlanner, entry: TextLayoutCacheEntry, use_index: bool) Error!void {
214
+ fn appendEntryMaybeIndexed(self: *TextLayoutCachePlanner, entry: TextLayoutCacheEntry, entry_index: ?*TextLayoutCacheIndex) Error!void {
214
215
  try self.appendEntry(entry);
215
- if (use_index) {
216
+ if (entry_index) |index| {
216
217
  var p = TextLayoutCacheIndex.probe(textLayoutKeyHash(entry.key));
217
- while (text_layout_cache_entry_index.next(&p)) |_| {}
218
- text_layout_cache_entry_index.insert(p, @intCast(self.entry_len - 1));
218
+ while (index.next(&p)) |_| {}
219
+ index.insert(p, @intCast(self.entry_len - 1));
219
220
  }
220
221
  }
221
222
 
@@ -249,12 +250,18 @@ fn findTextLayoutCacheEntry(entries: []const TextLayoutCacheEntry, key: TextLayo
249
250
  /// bound; bigger inputs fall back to the linear scans.
250
251
  const text_layout_cache_index_slots = 8192;
251
252
  const TextLayoutCacheIndex = plan_key_index.HashSlots(text_layout_cache_index_slots);
252
- threadlocal var text_layout_cache_previous_index: TextLayoutCacheIndex = .{};
253
- threadlocal var text_layout_cache_entry_index: TextLayoutCacheIndex = .{};
253
+ // Lazily heap-allocated per thread (64 KiB of probe tables): reset per
254
+ // build, so first-use init on the planning thread is the only contract —
255
+ // threads that never plan text layouts never allocate it.
256
+ const TextLayoutCacheIndexScratch = struct {
257
+ previous: TextLayoutCacheIndex = .{},
258
+ entry: TextLayoutCacheIndex = .{},
259
+ };
260
+ const text_layout_cache_index_scratch = @import("lazy_tls.zig").LazyTls(TextLayoutCacheIndexScratch);
254
261
 
255
- fn findTextLayoutCacheEntryIndexed(previous: []const TextLayoutCacheEntry, key: TextLayoutKey, key_hash: u64) ?usize {
262
+ fn findTextLayoutCacheEntryIndexed(previous_index: *const TextLayoutCacheIndex, previous: []const TextLayoutCacheEntry, key: TextLayoutKey, key_hash: u64) ?usize {
256
263
  var p = TextLayoutCacheIndex.probe(key_hash);
257
- while (text_layout_cache_previous_index.next(&p)) |candidate| {
264
+ while (previous_index.next(&p)) |candidate| {
258
265
  if (textLayoutKeysEqual(previous[candidate].key, key)) return candidate;
259
266
  }
260
267
  return null;