Skip to content

Commit 4527fc7

Browse files
authored
Merge pull request DeusData#2702 from DeusData/fix/php-single-file-registry-finalize
fix(php): index the single-file LSP type registry
2 parents c06465b + 4641e16 commit 4527fc7

4 files changed

Lines changed: 103 additions & 0 deletions

File tree

‎internal/cbm/lsp/lsp_work.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ extern _Thread_local uint64_t cbm_lsp_work_steps;
1717
#define CBM_LSP_WORK(n) (cbm_lsp_work_steps += (uint64_t)(n))
1818
/* Read and reset the calling thread's counter. */
1919
uint64_t cbm_lsp_work_take(void);
20+
/* Test-only work ceiling (0 = none). A regression test that reverts a bound
21+
* sets it so the reverted, unbounded loop stops deterministically and the test
22+
* reports RED instead of hanging the runner. Seam builds only: a product build
23+
* has no ceiling, so it can never decide graph content. */
24+
extern _Thread_local uint64_t cbm_lsp_work_ceiling;
25+
#define CBM_LSP_WORK_CEILING_HIT() \
26+
(cbm_lsp_work_ceiling != 0 && cbm_lsp_work_steps > cbm_lsp_work_ceiling)
2027
#else
2128
#define CBM_LSP_WORK(n) ((void)0)
29+
#define CBM_LSP_WORK_CEILING_HIT() (0)
2230
#endif
2331

2432
#endif /* CBM_LSP_WORK_H */

‎internal/cbm/lsp/php_lsp.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4372,6 +4372,17 @@ void cbm_run_php_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
43724372
}
43734373
}
43744374

4375+
/* Index the registry (F12). Unindexed, every lookup below was a linear
4376+
* scan over the stdlib plus every definition of the file: O(lookups x
4377+
* definitions), quadratic in the size of one file. First-registered
4378+
* answers keep the result of that scan (the first registration of a
4379+
* duplicate QN wins), so the output does not change. Index memory goes to
4380+
* a scratch arena that dies with this call, as in cbm_run_php_lsp_cross. */
4381+
reg.index_first_registered = true;
4382+
CBMArena idx_arena;
4383+
cbm_arena_init(&idx_arena);
4384+
cbm_registry_finalize_into(&reg, &idx_arena);
4385+
43754386
/* Phase C: run the resolver. */
43764387
PHPLSPContext ctx;
43774388
php_lsp_init(&ctx, arena, source, source_len, &reg, module_qn, &result->resolved_calls);
@@ -4401,6 +4412,11 @@ void cbm_run_php_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
44014412
ctx.use_count = 0;
44024413
}
44034414

4415+
/* Trait flattening registered methods after the index was built; lookups
4416+
* would tail-scan them. Re-index once, first-registered as above, so the
4417+
* answers equal those of the indexed part followed by the tail scan. */
4418+
cbm_registry_finalize_into(&reg, &idx_arena);
4419+
44044420
php_lsp_process_file(&ctx, root);
44054421

44064422
if (ctx.debug) {
@@ -4414,6 +4430,7 @@ void cbm_run_php_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
44144430
rc->confidence);
44154431
}
44164432
}
4433+
cbm_arena_destroy(&idx_arena);
44174434
}
44184435

44194436
/* ── Cross-file + batch ───────────────────────────────────────── */

‎internal/cbm/lsp/scope.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#ifdef CBM_ENABLE_TEST_SEAMS
77
_Thread_local uint64_t cbm_lsp_work_steps = 0;
8+
_Thread_local uint64_t cbm_lsp_work_ceiling = 0;
89
uint64_t cbm_lsp_work_take(void) {
910
uint64_t v = cbm_lsp_work_steps;
1011
cbm_lsp_work_steps = 0;

‎tests/test_php_lsp.c‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include "cbm.h"
2020
#include "../src/pipeline/lsp_resolve.h"
2121
#include "lsp/php_lsp.h"
22+
#include "lsp/lsp_work.h"
23+
#include <stdio.h>
24+
#include <stdlib.h>
2225
#include <string.h>
2326

2427
/* ── Helpers ───────────────────────────────────────────────────── */
@@ -625,6 +628,79 @@ TEST(phplsp_class_aliased_trait_name_collision_terminates) {
625628
PASS();
626629
}
627630

631+
/* ── 23e. #765 / #1265: trait flattening work is counted, bounded, linear ──
632+
*
633+
* The guards above prove only that extraction returns; a regression would hang
634+
* the runner instead of failing it. These tests count the LSP work
635+
* (CBM_LSP_WORK, which flatten_trait_into_class adds to once per registry
636+
* function it visits) under a test-only ceiling (lsp_work.h): if a flatten
637+
* bound is reverted, the runaway loop stops at the ceiling and the test is RED
638+
* deterministically. No clock is read (O9). */
639+
enum { PHPLSP_FLATTEN_CEILING = 50000000 };
640+
641+
static uint64_t phplsp_counted_work(const char *src, int *ceiling_hit, CBMFileResult **out) {
642+
(void)cbm_lsp_work_take();
643+
cbm_lsp_work_ceiling = PHPLSP_FLATTEN_CEILING;
644+
CBMFileResult *r = extract_php(src);
645+
uint64_t work = cbm_lsp_work_take();
646+
cbm_lsp_work_ceiling = 0;
647+
*ceiling_hit = work > PHPLSP_FLATTEN_CEILING;
648+
if (out) {
649+
*out = r;
650+
} else if (r) {
651+
cbm_free_result(r);
652+
}
653+
return work;
654+
}
655+
656+
/* F12: the single-file PHP LSP (cbm_run_php_lsp) never indexed its registry,
657+
* so every type and method lookup was a linear scan over the stdlib plus every
658+
* definition of the file. Each class here looks up the previous class and its
659+
* method once: twice the classes must cost about twice the counted work; the
660+
* linear scans drift toward four. */
661+
static char *phplsp_chain_classes_source(int n) {
662+
size_t sz = (size_t)n * 96 + 256;
663+
char *src = malloc(sz);
664+
if (!src) {
665+
return NULL;
666+
}
667+
size_t pos = (size_t)snprintf(src, sz,
668+
"<?php\nnamespace App\\Chain;\n\n"
669+
"class C0 { public function m(): int { return 0; } }\n");
670+
for (int i = 1; i < n; i++) {
671+
pos += (size_t)snprintf(src + pos, sz - pos,
672+
"class C%d { public function m(): int { return (new C%d())->m(); } }\n",
673+
i, i - 1);
674+
}
675+
return src;
676+
}
677+
678+
TEST(phplsp_single_file_lookup_work_is_linear_f12) {
679+
enum { N1 = 1000, N2 = 2000 };
680+
char *s1 = phplsp_chain_classes_source(N1);
681+
char *s2 = phplsp_chain_classes_source(N2);
682+
ASSERT_NOT_NULL(s1);
683+
ASSERT_NOT_NULL(s2);
684+
int hit1 = 0;
685+
int hit2 = 0;
686+
CBMFileResult *r2 = NULL;
687+
uint64_t w1 = phplsp_counted_work(s1, &hit1, NULL);
688+
uint64_t w2 = phplsp_counted_work(s2, &hit2, &r2);
689+
free(s1);
690+
free(s2);
691+
printf(" chained classes %d -> %d: work %llu -> %llu (ratio %.2f)\n", N1, N2,
692+
(unsigned long long)w1, (unsigned long long)w2, w1 ? (double)w2 / (double)w1 : 0.0);
693+
ASSERT_NOT_NULL(r2);
694+
ASSERT_FALSE(hit1);
695+
ASSERT_FALSE(hit2);
696+
ASSERT(w1 > 0);
697+
/* The last class's call still resolves to the previous class's method. */
698+
ASSERT(find_resolved(r2, "C1999.m", "C1998.m") >= 0);
699+
cbm_free_result(r2);
700+
ASSERT(w2 * 10 <= w1 * 25); /* ratio <= 2.5 */
701+
PASS();
702+
}
703+
628704
/* ── 24. Match expression result type ──────────────────────────── */
629705

630706
TEST(phplsp_match_result_type) {
@@ -5671,6 +5747,7 @@ SUITE(php_lsp) {
56715747
RUN_TEST(phplsp_trait_self_use_terminates);
56725748
RUN_TEST(phplsp_trait_aliased_import_name_collision_terminates);
56735749
RUN_TEST(phplsp_class_aliased_trait_name_collision_terminates);
5750+
RUN_TEST(phplsp_single_file_lookup_work_is_linear_f12);
56745751
RUN_TEST(phplsp_match_result_type);
56755752
RUN_TEST(phplsp_ternary_result_type);
56765753
RUN_TEST(phplsp_method_chain_depth_three);

0 commit comments

Comments
 (0)