Skip to content

Commit 5d2a30b

Browse files
committed
test(514): add a shared TS barrel to the complexity guard
One lib/index.ts star-exports k*20 modules, and k*20 importers each import their own name through it. Every importer must bind to its defining module, and the barrel registry's requests and walk steps must grow linearly. Answering each importer by scanning the barrel's star lines would grow at about 4 per doubling. Signed-off-by: chidev <[email protected]> (cherry picked from commit a16096e) Signed-off-by: chidev <[email protected]>
1 parent 679ea8d commit 5d2a30b

1 file changed

Lines changed: 137 additions & 4 deletions

File tree

‎tests/test_complexity.c‎

Lines changed: 137 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ enum {
9898
CX_PYEXT_FILES_PER_MOD = 20,
9999
CX_PYEXT_DEFS_PER_FILE = 120,
100100
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,
101104
};
102105

103106
/* 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) {
380383
return 0;
381384
}
382385

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+
383437
/* ── Metrics ─────────────────────────────────────────────────────────── */
384438

385439
/* Per-pass timing capture: a TEE log sink parses `pass.timing` lines during a
@@ -471,10 +525,47 @@ typedef struct {
471525
int pyrange_classes;
472526
int pyrange_peer_classes;
473527
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 */
474531
int errors;
475532
double wall_s;
476533
} CxMetrics;
477534

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+
478569
/* Count the intended corpus, then prove each use() kept its exact local LSP
479570
* call. Query errors are failures, never indistinguishable from zero rows. */
480571
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) {
705796
return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9;
706797
}
707798

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) {
709801
memset(out, 0, sizeof(*out));
802+
uint64_t br0;
803+
uint64_t bs0;
804+
cbm_pxc_ts_barrel_stats(&br0, &bs0);
710805

711806
uint64_t d0;
712807
uint64_t b0;
@@ -774,6 +869,11 @@ static int cx_run(const char *root, const char *db_path, CxMetrics *out, bool py
774869
out->tf_candidates = atomic_load_explicit(&g_tf_candidates, memory_order_relaxed) - fc0;
775870
out->dk_hub_edges = atomic_load_explicit(&g_dk_hub_edges, memory_order_relaxed) - dh0;
776871

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;
777877
out->py_probes = atomic_load_explicit(&cbm_py_submodule_probes, memory_order_relaxed) - pp0;
778878
out->py_probe_visits =
779879
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
801901
out->py_wildcard_visits =
802902
atomic_load_explicit(&cbm_py_wildcard_invalidation_visits, memory_order_relaxed) - pwv0;
803903
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));
805906
cbm_store_close(s);
806907
return extra_ok ? 0 : -1;
807908
}
@@ -845,8 +946,9 @@ static int cx_measure(CxCorpusBuilder build, const char *prefix_a, const char *p
845946
bool paths = da >= 0 && (size_t)da < sizeof(db_a) && db >= 0 && (size_t)db < sizeof(db_b);
846947
bool pyext = build == cx_build_pyext;
847948
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) {
850952
rc = 0;
851953
}
852954
}
@@ -1050,6 +1152,36 @@ TEST(complexity_shared_package_growth_stays_linear) {
10501152
PASS();
10511153
}
10521154

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+
10531185
TEST(complexity_py_submodule_probe_avoids_registry_scan) {
10541186
static const char *const keys[] = {"CBM_WORKERS", "CBM_INDEX_SINGLE_THREAD"};
10551187
char *saved[2] = {NULL, NULL};
@@ -1395,6 +1527,7 @@ SUITE(complexity) {
13951527
RUN_TEST(complexity_infra_resolution_is_linear);
13961528
RUN_TEST(complexity_importance_scoring_is_linear);
13971529
RUN_TEST(complexity_shared_package_growth_stays_linear);
1530+
RUN_TEST(complexity_ts_shared_barrel_stays_linear);
13981531
RUN_TEST(complexity_py_submodule_probe_avoids_registry_scan);
13991532
RUN_TEST(complexity_throughput_report_written);
14001533
}

0 commit comments

Comments
 (0)