Skip to content

Commit f08dada

Browse files
committed
test(py-lsp): gate the shared cross-registry build on counted work
Replace the wall-clock ratio test pylsp_scale_cross_registry_build_linear (py_lsp_scale suite) with pylsp_cross_registry_build_work_is_linear in the py_lsp suite. The new test builds the same generated input (n classes, each followed by two methods, in extraction order) and reads the seam-build CBM_LSP_WORK counter instead of a clock, so its verdict does not depend on host load, and it runs in PR CI, which skips the perf suites. 4x the classes must cost at most ~4x the counted work. With the type-QN set the build examines no registry candidates (0 at 1000 and 4000 classes); with the scanning receiver probe the work grows from 2,809,000 to 23,236,000 and the test fails. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 895b451 commit f08dada

2 files changed

Lines changed: 54 additions & 66 deletions

File tree

‎tests/test_py_lsp.c‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,6 +2483,59 @@ TEST(pylsp_scale_work_is_linear) {
24832483
PASS();
24842484
}
24852485

2486+
/* #2413 (O9: no clock): the shared Tier-2 build registers one def at a time
2487+
* into a registry it finalizes only at the end, so a receiver probe that scans
2488+
* the types registered so far makes it O(methods x types). The registry
2489+
* primitives count every candidate they examine (CBM_LSP_WORK): 4x the classes
2490+
* must cost at most ~4x the work; the scanning probe lands at ~16x. */
2491+
static uint64_t cross_registry_work_for(int n_classes, int *out_types) {
2492+
CBMArena arena;
2493+
cbm_arena_init(&arena);
2494+
int n_defs = n_classes * 3;
2495+
CBMLSPDef *defs = (CBMLSPDef *)cbm_arena_calloc(&arena, (size_t)n_defs * sizeof(*defs));
2496+
if (!defs) {
2497+
cbm_arena_destroy(&arena);
2498+
*out_types = -1;
2499+
return 0;
2500+
}
2501+
/* Extraction order: each class followed by its two methods. */
2502+
for (int i = 0; i < n_classes; i++) {
2503+
CBMLSPDef *cls = &defs[i * 3];
2504+
cls->qualified_name = cbm_arena_sprintf(&arena, "scale.mod.Cls%d", i);
2505+
cls->short_name = cls->qualified_name + strlen("scale.mod.");
2506+
cls->label = "Class";
2507+
cls->def_module_qn = "scale.mod";
2508+
cls->lang = CBM_LANG_PYTHON;
2509+
for (int j = 1; j <= 2; j++) {
2510+
CBMLSPDef *m = &defs[i * 3 + j];
2511+
m->qualified_name = cbm_arena_sprintf(&arena, "%s.m%d", cls->qualified_name, j);
2512+
m->short_name = j == 1 ? "m1" : "m2";
2513+
m->label = "Method";
2514+
m->receiver_type = cls->qualified_name;
2515+
m->def_module_qn = "scale.mod";
2516+
m->lang = CBM_LANG_PYTHON;
2517+
}
2518+
}
2519+
(void)cbm_lsp_work_take();
2520+
CBMTypeRegistry *reg = cbm_py_build_cross_registry(&arena, defs, n_defs);
2521+
uint64_t work = cbm_lsp_work_take();
2522+
*out_types = reg ? reg->type_count : -1;
2523+
cbm_arena_destroy(&arena);
2524+
return work;
2525+
}
2526+
2527+
TEST(pylsp_cross_registry_build_work_is_linear) {
2528+
int t1 = 0, t4 = 0;
2529+
uint64_t w1 = cross_registry_work_for(1000, &t1);
2530+
uint64_t w4 = cross_registry_work_for(4000, &t4);
2531+
printf(" cross registry work: 1000=%llu (types=%d) 4000=%llu (types=%d)\n",
2532+
(unsigned long long)w1, t1, (unsigned long long)w4, t4);
2533+
/* Every class registered exactly once (no duplicate placeholder). */
2534+
ASSERT_EQ(t4 - t1, 3000);
2535+
ASSERT(w4 <= 5 * w1 + 1000);
2536+
PASS();
2537+
}
2538+
24862539
extern const TSLanguage *tree_sitter_python(void);
24872540

24882541
TEST(pylsp_issue1527_memo_allocation_failure_is_reported) {
@@ -4885,6 +4938,7 @@ SUITE(py_lsp) {
48854938
RUN_TEST(pylsp_issue1527_long_binary_chain_is_not_nesting);
48864939
RUN_TEST(pylsp_issue1527_long_receiver_chain_is_not_nesting);
48874940
RUN_TEST(pylsp_scale_work_is_linear);
4941+
RUN_TEST(pylsp_cross_registry_build_work_is_linear);
48884942
/* Phase 2 — smoke */
48894943
RUN_TEST(pylsp_smoke_empty);
48904944
RUN_TEST(pylsp_smoke_one_function);

‎tests/test_py_lsp_scale.c‎

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -111,72 +111,6 @@ TEST(pylsp_scale_linear_growth) {
111111
PASS();
112112
}
113113

114-
/* Time cbm_py_build_cross_registry over n classes, each followed by two
115-
* methods (extraction order): every method probes the registry for its
116-
* receiver while the registry is still being built. */
117-
static double measure_cross_registry(int n_classes, int *out_types) {
118-
CBMArena arena;
119-
cbm_arena_init(&arena);
120-
int n_defs = n_classes * 3;
121-
CBMLSPDef *defs = (CBMLSPDef *)cbm_arena_calloc(&arena, (size_t)n_defs * sizeof(*defs));
122-
if (!defs) {
123-
cbm_arena_destroy(&arena);
124-
return -1.0;
125-
}
126-
for (int i = 0; i < n_classes; i++) {
127-
CBMLSPDef *cls = &defs[i * 3];
128-
cls->qualified_name = cbm_arena_sprintf(&arena, "scale.mod.Cls%d", i);
129-
cls->short_name = cls->qualified_name + strlen("scale.mod.");
130-
cls->label = "Class";
131-
cls->def_module_qn = "scale.mod";
132-
cls->lang = CBM_LANG_PYTHON;
133-
for (int j = 1; j <= 2; j++) {
134-
CBMLSPDef *m = &defs[i * 3 + j];
135-
m->qualified_name = cbm_arena_sprintf(&arena, "%s.m%d", cls->qualified_name, j);
136-
m->short_name = j == 1 ? "m1" : "m2";
137-
m->label = "Method";
138-
m->receiver_type = cls->qualified_name;
139-
m->def_module_qn = "scale.mod";
140-
m->lang = CBM_LANG_PYTHON;
141-
}
142-
}
143-
struct timespec t0, t1;
144-
clock_gettime(CLOCK_MONOTONIC, &t0);
145-
CBMTypeRegistry *reg = cbm_py_build_cross_registry(&arena, defs, n_defs);
146-
clock_gettime(CLOCK_MONOTONIC, &t1);
147-
if (out_types)
148-
*out_types = reg ? reg->type_count : -1;
149-
cbm_arena_destroy(&arena);
150-
return elapsed_ms(t0, t1);
151-
}
152-
153-
/* The shared build registers defs one at a time into an unfinalized registry;
154-
* a receiver probe that scans every type registered so far makes the build
155-
* O(methods x types). 16x input: linear ~16x time, quadratic well over 100x. */
156-
TEST(pylsp_scale_cross_registry_build_linear) {
157-
int ty_small = 0, ty_large = 0;
158-
/* Best of three for the small size: it is short enough for host noise. */
159-
double t_small = measure_cross_registry(1000, &ty_small);
160-
for (int k = 0; k < 2; k++) {
161-
double t = measure_cross_registry(1000, NULL);
162-
if (t >= 0.0 && t < t_small)
163-
t_small = t;
164-
}
165-
double t_large = measure_cross_registry(16000, &ty_large);
166-
printf(" cross registry: 1000 classes=%.1fms (types=%d) 16000 classes=%.1fms "
167-
"(types=%d)\n",
168-
t_small, ty_small, t_large, ty_large);
169-
ASSERT(t_small >= 0.0 && t_large >= 0.0);
170-
ASSERT_EQ(ty_large - ty_small, 15000);
171-
if (t_small > 0.5) {
172-
double ratio = t_large / t_small;
173-
printf(" cross registry ratio 16000/1000: %.1fx (linear ~16x)\n", ratio);
174-
ASSERT(ratio < 48.0);
175-
}
176-
PASS();
177-
}
178-
179114
SUITE(py_lsp_scale) {
180115
RUN_TEST(pylsp_scale_linear_growth);
181-
RUN_TEST(pylsp_scale_cross_registry_build_linear);
182116
}

0 commit comments

Comments
 (0)