|
98 | 98 | CX_PYEXT_FILES_PER_MOD = 20, |
99 | 99 | CX_PYEXT_DEFS_PER_FILE = 120, |
100 | 100 | CX_PYEXT_PROBES_PER_FILE = 3, |
| 101 | + /* One shared TS barrel: per k, this many star-exported modules and as |
| 102 | + * many importers (each imports a different name through the barrel). */ |
| 103 | + CX_TSBARREL_MODULES_PER_K = 20, |
101 | 104 | }; |
102 | 105 |
|
103 | 106 | /* Linear growth bounds for a 2x input doubling. Fixed structural overhead |
@@ -380,6 +383,57 @@ static int cx_build_pyext(const char *root, int k) { |
380 | 383 | return 0; |
381 | 384 | } |
382 | 385 |
|
| 386 | +/* Corpus shape #3 — one shared TypeScript barrel. lib/index.ts star-exports |
| 387 | + * every module, and every importer imports its own name through the barrel: |
| 388 | + * the barrel grows with k, and so do its importers. Answering each importer |
| 389 | + * by scanning the barrel's star lines is importers x barrel size (~4 per |
| 390 | + * doubling); the per-pass barrel registry answers each (barrel, name) once |
| 391 | + * from a per-barrel index, so its work stays linear. */ |
| 392 | +static int cx_build_tsbarrel(const char *root, int k) { |
| 393 | + char dir[1024]; |
| 394 | + char path[1200]; |
| 395 | + int n = snprintf(dir, sizeof(dir), "%s/tsbarrel/lib", root); |
| 396 | + if (n < 0 || (size_t)n >= sizeof(dir) || th_mkdir_p(dir) != 0) |
| 397 | + return -1; |
| 398 | + int modules = k * CX_TSBARREL_MODULES_PER_K; |
| 399 | + n = snprintf(path, sizeof(path), "%s/index.ts", dir); |
| 400 | + if (n < 0 || (size_t)n >= sizeof(path)) |
| 401 | + return -1; |
| 402 | + FILE *barrel = fopen(path, "w"); |
| 403 | + if (!barrel) |
| 404 | + return -1; |
| 405 | + bool written = true; |
| 406 | + for (int j = 0; written && j < modules; j++) |
| 407 | + written = fprintf(barrel, "export * from './m%d';\n", j) >= 0; |
| 408 | + if (fclose(barrel) != 0 || !written) |
| 409 | + return -1; |
| 410 | + for (int j = 0; j < modules; j++) { |
| 411 | + n = snprintf(path, sizeof(path), "%s/m%d.ts", dir, j); |
| 412 | + if (n < 0 || (size_t)n >= sizeof(path)) |
| 413 | + return -1; |
| 414 | + FILE *f = fopen(path, "w"); |
| 415 | + if (!f) |
| 416 | + return -1; |
| 417 | + written = |
| 418 | + fprintf(f, "export function barrelFn%d(): number {\n return %d;\n}\n", j, j) >= 0; |
| 419 | + if (fclose(f) != 0 || !written) |
| 420 | + return -1; |
| 421 | + n = snprintf(path, sizeof(path), "%s/tsbarrel/u%d.ts", root, j); |
| 422 | + if (n < 0 || (size_t)n >= sizeof(path)) |
| 423 | + return -1; |
| 424 | + f = fopen(path, "w"); |
| 425 | + if (!f) |
| 426 | + return -1; |
| 427 | + written = fprintf(f, |
| 428 | + "import { barrelFn%d } from './lib';\n" |
| 429 | + "export function useBarrel%d(): number {\n return barrelFn%d();\n}\n", |
| 430 | + j, j, j) >= 0; |
| 431 | + if (fclose(f) != 0 || !written) |
| 432 | + return -1; |
| 433 | + } |
| 434 | + return 0; |
| 435 | +} |
| 436 | + |
383 | 437 | /* ── Metrics ─────────────────────────────────────────────────────────── */ |
384 | 438 |
|
385 | 439 | /* Per-pass timing capture: a TEE log sink parses `pass.timing` lines during a |
@@ -471,10 +525,47 @@ typedef struct { |
471 | 525 | int pyrange_classes; |
472 | 526 | int pyrange_peer_classes; |
473 | 527 | int pyrange_controls; |
| 528 | + uint64_t barrel_requests; /* (barrel, name) requests answered */ |
| 529 | + uint64_t barrel_steps; /* barrel walk work: items, index entries, edges */ |
| 530 | + int tsbarrel_calls; /* importer calls bound to the defining module */ |
474 | 531 | int errors; |
475 | 532 | double wall_s; |
476 | 533 | } CxMetrics; |
477 | 534 |
|
| 535 | +/* Count the importer calls that bound to the defining module tsbarrel/lib/mJ.ts. */ |
| 536 | +static bool cx_tsbarrel_population(cbm_store_t *store, const char *project, CxMetrics *out) { |
| 537 | + cbm_node_t *nodes = NULL; |
| 538 | + int count = 0; |
| 539 | + if (cbm_store_find_nodes_by_label(store, project, "Function", &nodes, &count) != CBM_STORE_OK) |
| 540 | + return false; |
| 541 | + for (int i = 0; i < count; i++) { |
| 542 | + const cbm_node_t *caller = &nodes[i]; |
| 543 | + int j = -1; |
| 544 | + if (!caller->name || sscanf(caller->name, "useBarrel%d", &j) != 1 || j < 0) |
| 545 | + continue; |
| 546 | + char want_name[64]; |
| 547 | + char want_path[96]; |
| 548 | + snprintf(want_name, sizeof(want_name), "barrelFn%d", j); |
| 549 | + snprintf(want_path, sizeof(want_path), "tsbarrel/lib/m%d.ts", j); |
| 550 | + cbm_edge_t *edges = NULL; |
| 551 | + int edge_count = 0; |
| 552 | + if (cbm_store_find_edges_by_source_type(store, caller->id, "CALLS", &edges, &edge_count) != |
| 553 | + CBM_STORE_OK) |
| 554 | + continue; |
| 555 | + for (int e = 0; e < edge_count; e++) { |
| 556 | + cbm_node_t target = {0}; |
| 557 | + if (cbm_store_find_node_by_id(store, edges[e].target_id, &target) == CBM_STORE_OK && |
| 558 | + target.name && target.file_path && strcmp(target.name, want_name) == 0 && |
| 559 | + strcmp(target.file_path, want_path) == 0) |
| 560 | + out->tsbarrel_calls++; |
| 561 | + cbm_node_free_fields(&target); |
| 562 | + } |
| 563 | + cbm_store_free_edges(edges, edge_count); |
| 564 | + } |
| 565 | + cbm_store_free_nodes(nodes, count); |
| 566 | + return true; |
| 567 | +} |
| 568 | + |
478 | 569 | /* Count the intended corpus, then prove each use() kept its exact local LSP |
479 | 570 | * call. Query errors are failures, never indistinguishable from zero rows. */ |
480 | 571 | static bool cx_pyext_control(cbm_store_t *store, const char *project, const cbm_node_t *caller) { |
@@ -705,8 +796,12 @@ static double cx_now_s(void) { |
705 | 796 | return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9; |
706 | 797 | } |
707 | 798 |
|
708 | | -static int cx_run(const char *root, const char *db_path, CxMetrics *out, bool pyext, bool pyrange) { |
| 799 | +static int cx_run(const char *root, const char *db_path, CxMetrics *out, bool pyext, bool pyrange, |
| 800 | + bool tsbarrel) { |
709 | 801 | memset(out, 0, sizeof(*out)); |
| 802 | + uint64_t br0; |
| 803 | + uint64_t bs0; |
| 804 | + cbm_pxc_ts_barrel_stats(&br0, &bs0); |
710 | 805 |
|
711 | 806 | uint64_t d0; |
712 | 807 | uint64_t b0; |
@@ -774,6 +869,11 @@ static int cx_run(const char *root, const char *db_path, CxMetrics *out, bool py |
774 | 869 | out->tf_candidates = atomic_load_explicit(&g_tf_candidates, memory_order_relaxed) - fc0; |
775 | 870 | out->dk_hub_edges = atomic_load_explicit(&g_dk_hub_edges, memory_order_relaxed) - dh0; |
776 | 871 |
|
| 872 | + uint64_t br1; |
| 873 | + uint64_t bs1; |
| 874 | + cbm_pxc_ts_barrel_stats(&br1, &bs1); |
| 875 | + out->barrel_requests = br1 - br0; |
| 876 | + out->barrel_steps = bs1 - bs0; |
777 | 877 | out->py_probes = atomic_load_explicit(&cbm_py_submodule_probes, memory_order_relaxed) - pp0; |
778 | 878 | out->py_probe_visits = |
779 | 879 | atomic_load_explicit(&cbm_py_submodule_probe_visits, memory_order_relaxed) - pv0; |
@@ -801,7 +901,8 @@ static int cx_run(const char *root, const char *db_path, CxMetrics *out, bool py |
801 | 901 | out->py_wildcard_visits = |
802 | 902 | atomic_load_explicit(&cbm_py_wildcard_invalidation_visits, memory_order_relaxed) - pwv0; |
803 | 903 | bool extra_ok = (!pyext || cx_pyext_population(s, project, out)) && |
804 | | - (!pyrange || cx_pyrange_population(s, project, out)); |
| 904 | + (!pyrange || cx_pyrange_population(s, project, out)) && |
| 905 | + (!tsbarrel || cx_tsbarrel_population(s, project, out)); |
805 | 906 | cbm_store_close(s); |
806 | 907 | return extra_ok ? 0 : -1; |
807 | 908 | } |
@@ -845,8 +946,9 @@ static int cx_measure(CxCorpusBuilder build, const char *prefix_a, const char *p |
845 | 946 | bool paths = da >= 0 && (size_t)da < sizeof(db_a) && db >= 0 && (size_t)db < sizeof(db_b); |
846 | 947 | bool pyext = build == cx_build_pyext; |
847 | 948 | bool pyrange = build == cx_build_pyrange; |
848 | | - if (paths && cx_run(root_a, db_a, out_a, pyext, pyrange) == 0 && |
849 | | - cx_run(root_b, db_b, out_b, pyext, pyrange) == 0) { |
| 949 | + bool tsbarrel = build == cx_build_tsbarrel; |
| 950 | + if (paths && cx_run(root_a, db_a, out_a, pyext, pyrange, tsbarrel) == 0 && |
| 951 | + cx_run(root_b, db_b, out_b, pyext, pyrange, tsbarrel) == 0) { |
850 | 952 | rc = 0; |
851 | 953 | } |
852 | 954 | } |
@@ -1050,6 +1152,36 @@ TEST(complexity_shared_package_growth_stays_linear) { |
1050 | 1152 | PASS(); |
1051 | 1153 | } |
1052 | 1154 |
|
| 1155 | +/* One shared TS barrel that grows with k, imported through by importers that |
| 1156 | + * grow with k (shape #3). Every importer must bind to its defining module, and |
| 1157 | + * the barrel walk work must stay linear: answering each importer by scanning |
| 1158 | + * the barrel's star lines would be ~4 per doubling. */ |
| 1159 | +TEST(complexity_ts_shared_barrel_stays_linear) { |
| 1160 | + CxMetrics a = {0}; |
| 1161 | + CxMetrics b = {0}; |
| 1162 | + ASSERT_EQ(cx_measure(cx_build_tsbarrel, "cbm_cxbarrel_a", "cbm_cxbarrel_b", &a, &b), 0); |
| 1163 | + const int modules = CX_K_BASE * CX_TSBARREL_MODULES_PER_K; |
| 1164 | + /* both legs take the parallel driver (2 files per module + the barrel) */ |
| 1165 | + ASSERT_GT(2 * modules + 1, 50); |
| 1166 | + printf(" barrel requests %llu -> %llu steps %llu -> %llu calls %d -> %d\n", |
| 1167 | + (unsigned long long)a.barrel_requests, (unsigned long long)b.barrel_requests, |
| 1168 | + (unsigned long long)a.barrel_steps, (unsigned long long)b.barrel_steps, a.tsbarrel_calls, |
| 1169 | + b.tsbarrel_calls); |
| 1170 | + /* Non-vacuous: every importer bound through the barrel, and the requests |
| 1171 | + * were really answered by the registry. */ |
| 1172 | + ASSERT_EQ(a.tsbarrel_calls, modules); |
| 1173 | + ASSERT_EQ(b.tsbarrel_calls, modules * 2); |
| 1174 | + ASSERT_GTE(a.barrel_requests, (uint64_t)modules); |
| 1175 | + ASSERT_GT((long long)a.barrel_steps, (long long)CX_MIN_BASE_WORK); |
| 1176 | + double req_r = cx_ratio((double)b.barrel_requests, (double)a.barrel_requests); |
| 1177 | + double steps_r = cx_ratio((double)b.barrel_steps, (double)a.barrel_steps); |
| 1178 | + printf(" barrel requests ratio %.2f steps ratio %.2f (linear ~2, importers x barrel ~4)\n", |
| 1179 | + req_r, steps_r); |
| 1180 | + ASSERT_TRUE(req_r >= CX_RATIO_LO && req_r <= CX_RATIO_HI); |
| 1181 | + ASSERT_TRUE(steps_r >= CX_RATIO_LO && steps_r <= CX_RATIO_HI); |
| 1182 | + PASS(); |
| 1183 | +} |
| 1184 | + |
1053 | 1185 | TEST(complexity_py_submodule_probe_avoids_registry_scan) { |
1054 | 1186 | static const char *const keys[] = {"CBM_WORKERS", "CBM_INDEX_SINGLE_THREAD"}; |
1055 | 1187 | char *saved[2] = {NULL, NULL}; |
@@ -1395,6 +1527,7 @@ SUITE(complexity) { |
1395 | 1527 | RUN_TEST(complexity_infra_resolution_is_linear); |
1396 | 1528 | RUN_TEST(complexity_importance_scoring_is_linear); |
1397 | 1529 | RUN_TEST(complexity_shared_package_growth_stays_linear); |
| 1530 | + RUN_TEST(complexity_ts_shared_barrel_stays_linear); |
1398 | 1531 | RUN_TEST(complexity_py_submodule_probe_avoids_registry_scan); |
1399 | 1532 | RUN_TEST(complexity_throughput_report_written); |
1400 | 1533 | } |
0 commit comments