|
19 | 19 | #include "cbm.h" |
20 | 20 | #include "../src/pipeline/lsp_resolve.h" |
21 | 21 | #include "lsp/php_lsp.h" |
| 22 | +#include "lsp/lsp_work.h" |
| 23 | +#include <stdio.h> |
| 24 | +#include <stdlib.h> |
22 | 25 | #include <string.h> |
23 | 26 |
|
24 | 27 | /* ── Helpers ───────────────────────────────────────────────────── */ |
@@ -625,6 +628,79 @@ TEST(phplsp_class_aliased_trait_name_collision_terminates) { |
625 | 628 | PASS(); |
626 | 629 | } |
627 | 630 |
|
| 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 | + |
628 | 704 | /* ── 24. Match expression result type ──────────────────────────── */ |
629 | 705 |
|
630 | 706 | TEST(phplsp_match_result_type) { |
@@ -5671,6 +5747,7 @@ SUITE(php_lsp) { |
5671 | 5747 | RUN_TEST(phplsp_trait_self_use_terminates); |
5672 | 5748 | RUN_TEST(phplsp_trait_aliased_import_name_collision_terminates); |
5673 | 5749 | RUN_TEST(phplsp_class_aliased_trait_name_collision_terminates); |
| 5750 | + RUN_TEST(phplsp_single_file_lookup_work_is_linear_f12); |
5674 | 5751 | RUN_TEST(phplsp_match_result_type); |
5675 | 5752 | RUN_TEST(phplsp_ternary_result_type); |
5676 | 5753 | RUN_TEST(phplsp_method_chain_depth_three); |
|
0 commit comments