Skip to content

Commit ba4122d

Browse files
authored
Merge pull request DeusData#2418 from Fieldnote-Echo/fix/sequential-definitions-retention
fix(pipeline): free trees and compact results in sequential definitions pass
2 parents 006101a + e1405ea commit ba4122d

3 files changed

Lines changed: 176 additions & 0 deletions

File tree

‎src/pipeline/pass_definitions.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
10001000
}
10011001
cbm_free(CBM_MEM_CLASS_EXTRACT, local_cache);
10021002
}
1003+
cbm_work_arena_release();
10031004
return CBM_NOT_FOUND;
10041005
}
10051006

@@ -1133,6 +1134,16 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
11331134
total_calls += result->calls.count;
11341135

11351136
if (local_cache) {
1137+
/* The cache lives through every later pass: hold the result as the
1138+
* parallel extract does (pass_parallel.c), without its tree (the
1139+
* cross-file LSP re-parses) and compacted. A PHP result keeps its
1140+
* tree: the vendor-import check reads each call and usage site's
1141+
* syntax from it (php_import_kind_at_site, pass_pkgmap.c), and this
1142+
* route keeps no source to fall back on. */
1143+
if (lang != CBM_LANG_PHP) {
1144+
cbm_free_tree(result);
1145+
}
1146+
cbm_result_compact(result);
11361147
local_cache[i] = result;
11371148
} else {
11381149
/* Cache unavailable: imports for this file can still only
@@ -1146,6 +1157,7 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
11461157
cbm_free_result(result);
11471158
}
11481159
}
1160+
cbm_work_arena_release(); /* the working arena compaction kept between files */
11491161

11501162
/* Every file's nodes exist now: link methods and diagram elements to
11511163
* their parents (without the cache each file linked its own above). */
@@ -1209,5 +1221,6 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
12091221
}
12101222
cbm_free(CBM_MEM_CLASS_EXTRACT, local_cache);
12111223
}
1224+
cbm_work_arena_release(); /* compaction keeps it between files: release on every exit */
12121225
return CBM_PIPELINE_ABORT_PRESERVE_DB;
12131226
}

‎tests/test_edge_imports.c‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,6 +2150,45 @@ TEST(ei_php_vendor_types_never_bind_by_name_issue1186) {
21502150
PASS();
21512151
}
21522152

2153+
/* A vendor `use function` import shares its local name with a project class the
2154+
* file also imports. `new Thing()`, a `Thing $t` parameter and `extends Thing`
2155+
* name the class, so the function import must not veto their edges. The check
2156+
* reads each site's syntax from the parse tree, which the sequential
2157+
* definitions pass (a repo of 50 files or fewer) keeps for PHP results. */
2158+
TEST(ei_php_vendor_function_import_keeps_same_named_class_sites) {
2159+
static const EILangFile f[] = {
2160+
{"composer.json", "{\"autoload\":{\"psr-4\":{\"App\\\\\":\"app/\"}}}\n"},
2161+
{"app/Models/Thing.php", "<?php\nnamespace App\\Models;\n\nclass Thing {\n"
2162+
" public function __construct() {}\n"
2163+
" public function go() { return 1; }\n}\n"},
2164+
{"app/Http/UsesThing.php", "<?php\nnamespace App\\Http;\n\n"
2165+
"use App\\Models\\Thing;\nuse function Vendor\\Lib\\Thing;\n\n"
2166+
"class UsesThing extends Thing {\n"
2167+
" public function run(Thing $t) {\n"
2168+
" $made = new Thing();\n"
2169+
" return $made->go();\n"
2170+
" }\n}\n"}};
2171+
static const char *const uses = "app/Http/UsesThing.php";
2172+
static const char *const thing = "app/Models/Thing.php";
2173+
EILangProj lp;
2174+
cbm_store_t *store = ei_index_files(&lp, f, 3);
2175+
int calls = store ? ei_php_checked_file_edges(store, lp.project, uses, "CALLS", thing) : -1;
2176+
int usages = store ? ei_php_checked_file_edges(store, lp.project, uses, "USAGE", thing) : -1;
2177+
int inherits =
2178+
store ? ei_php_checked_file_edges(store, lp.project, uses, "INHERITS", thing) : -1;
2179+
ei_cleanup(&lp, store);
2180+
if (calls != 2 || usages != 2 || inherits != 1) {
2181+
fprintf(stderr,
2182+
" UsesThing.php -> Thing.php: CALLS %d (want 2), USAGE %d (want 2), "
2183+
"INHERITS %d (want 1)\n",
2184+
calls, usages, inherits);
2185+
}
2186+
ASSERT_EQ(calls, 2); /* new Thing() and $made->go() */
2187+
ASSERT_EQ(usages, 2); /* extends Thing and the Thing $t parameter */
2188+
ASSERT_EQ(inherits, 1); /* never vetoed: control */
2189+
PASS();
2190+
}
2191+
21532192
/* Build the metadata through the production writer, then challenge its
21542193
* readback. A File node or an empty edge query alone is never completeness. */
21552194
static bool ei_php_seed_result(cbm_gbuf_t *gb, const char *project, const char *rel,
@@ -2538,6 +2577,7 @@ SUITE(edge_imports) {
25382577
RUN_TEST(ei_php_null_namespace_map_keeps_php_candidate_issue1186);
25392578
RUN_TEST(ei_php_require_path_keeps_generic_resolution_issue1186);
25402579
RUN_TEST(ei_php_vendor_types_never_bind_by_name_issue1186);
2580+
RUN_TEST(ei_php_vendor_function_import_keeps_same_named_class_sites);
25412581
RUN_TEST(ei_php_binding_snapshot_requires_current_complete_evidence);
25422582
RUN_TEST(ei_php_binding_snapshot_groups_kinds_and_invalidates_failed_rebuild);
25432583
RUN_TEST(ei_php_psr4_missing_class_leaves_type_evidence_unknown);

‎tests/test_parallel.c‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,127 @@ TEST(parallel_post_extract_projection_spills_before_resolve) {
770770
PASS();
771771
}
772772

773+
/* ── Sequential cache: held like the parallel one ─────────────────── */
774+
775+
/* The parallel extract caches each result without its parse tree and
776+
* compacted (pass_parallel.c). The sequential definitions pass cached the raw
777+
* result, tree and working arena included, and that cache lives through every
778+
* later pass: about 5x the memory per file, for every file of the repo. PHP
779+
* keeps its tree: the vendor-import check reads its sites' syntax from it. */
780+
TEST(sequential_definitions_cache_drops_trees_and_compacts) {
781+
if (ensure_parity_setup() != 0)
782+
FAIL("setup failed");
783+
cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL};
784+
cbm_file_info_t *files = NULL;
785+
int file_count = 0;
786+
ASSERT_EQ(cbm_discover(g_par_tmpdir, &opts, &files, &file_count), 0);
787+
ASSERT_GT(file_count, 0);
788+
789+
cbm_gbuf_t *gbuf = cbm_gbuf_new("par-test", g_par_tmpdir);
790+
cbm_registry_t *reg = cbm_registry_new();
791+
CBMFileResult **cache = (CBMFileResult **)calloc((size_t)file_count, sizeof(CBMFileResult *));
792+
ASSERT_NOT_NULL(cache);
793+
atomic_int cancelled;
794+
atomic_init(&cancelled, 0);
795+
cbm_pipeline_ctx_t ctx = {
796+
.project_name = "par-test",
797+
.repo_path = g_par_tmpdir,
798+
.gbuf = gbuf,
799+
.registry = reg,
800+
.cancelled = &cancelled,
801+
.result_cache = cache,
802+
};
803+
804+
cbm_init();
805+
cbm_work_arena_release(); /* start from a thread that keeps nothing */
806+
ASSERT_EQ(cbm_pipeline_pass_definitions(&ctx, files, file_count), 0);
807+
ASSERT_FALSE(cbm_work_arena_keeping()); /* ...and end the pass that way */
808+
809+
int cached = 0;
810+
int php_trees = 0;
811+
for (int i = 0; i < file_count; i++) {
812+
const CBMFileResult *r = cache[i];
813+
if (!r) {
814+
continue;
815+
}
816+
cached++;
817+
if (files[i].language == CBM_LANG_PHP) {
818+
ASSERT_NOT_NULL(r->cached_tree);
819+
php_trees++;
820+
} else {
821+
ASSERT_NULL(r->cached_tree);
822+
}
823+
/* One exact block: capacity == bytes used, no dead headroom. */
824+
ASSERT_EQ(r->arena.nblocks, 1);
825+
ASSERT_EQ(cbm_arena_capacity(&r->arena), cbm_arena_total(&r->arena));
826+
}
827+
ASSERT_EQ(cached, file_count);
828+
ASSERT_GT(php_trees, 0); /* app/Models.php and app/Services.php */
829+
830+
for (int i = 0; i < file_count; i++) {
831+
cbm_free_result(cache[i]);
832+
}
833+
free(cache);
834+
cbm_registry_free(reg);
835+
cbm_gbuf_free(gbuf);
836+
cbm_discover_free(files, file_count);
837+
PASS();
838+
}
839+
840+
/* A configured C test convention whose file cannot be extracted aborts the
841+
* sequential pass (CBM_PIPELINE_ABORT_PRESERVE_DB). The Python file compacted
842+
* before it handed its working arena back to this thread, so that exit has to
843+
* release it like the others. */
844+
TEST(sequential_definitions_configured_failure_releases_work_arena) {
845+
char dir[256];
846+
snprintf(dir, sizeof(dir), "/tmp/cbm_seqcf_XXXXXX");
847+
ASSERT_NOT_NULL(cbm_mkdtemp(dir));
848+
char py_path[320];
849+
char c_path[320];
850+
snprintf(py_path, sizeof(py_path), "%s/a.py", dir);
851+
snprintf(c_path, sizeof(c_path), "%s/z.c", dir);
852+
ASSERT_EQ(th_write_file(py_path, "def first():\n return 1\n"), 0);
853+
ASSERT_EQ(th_write_file(c_path, "void second(void) {}\n"), 0);
854+
cbm_file_info_t files[] = {
855+
{.path = py_path, .rel_path = "a.py", .language = CBM_LANG_PYTHON},
856+
{.path = c_path, .rel_path = "z.c", .language = CBM_LANG_C},
857+
};
858+
static const char config[] =
859+
"{\"test_impact\":{\"version\":1,\"tests\":{\"conventions\":[{\"language\":\"c\","
860+
"\"role\":\"case\",\"define_macro\":\"CHECK\",\"name_args\":[0]}]}}}";
861+
cbm_test_declarations_t *declarations =
862+
cbm_test_declarations_parse(config, sizeof(config) - 1, false);
863+
ASSERT_NOT_NULL(declarations);
864+
865+
cbm_gbuf_t *gbuf = cbm_gbuf_new("seq-cf", dir);
866+
cbm_registry_t *reg = cbm_registry_new();
867+
atomic_int cancelled;
868+
atomic_init(&cancelled, 0);
869+
cbm_pipeline_ctx_t ctx = {
870+
.project_name = "seq-cf",
871+
.repo_path = dir,
872+
.gbuf = gbuf,
873+
.registry = reg,
874+
.cancelled = &cancelled,
875+
.test_declarations = declarations,
876+
.test_fault_mask = CBM_PIPELINE_TEST_FAULT_EXTRACT_NULL, /* z.c extracts to NULL */
877+
};
878+
879+
cbm_init();
880+
cbm_work_arena_release(); /* start from a thread that keeps nothing */
881+
int rc = cbm_pipeline_pass_definitions(&ctx, files, 2);
882+
bool keeping = cbm_work_arena_keeping();
883+
cbm_work_arena_release();
884+
885+
cbm_registry_free(reg);
886+
cbm_gbuf_free(gbuf);
887+
cbm_test_declarations_free(declarations);
888+
rm_rf(dir);
889+
ASSERT_EQ(rc, CBM_PIPELINE_ABORT_PRESERVE_DB);
890+
ASSERT_FALSE(keeping);
891+
PASS();
892+
}
893+
773894
/* ── Empty file list ──────────────────────────────────────────────── */
774895

775896
TEST(parallel_empty_files) {
@@ -5599,6 +5720,8 @@ SUITE(parallel) {
55995720
RUN_TEST(parallel_total_edges);
56005721
RUN_TEST(parallel_spill_mode_builds_the_same_graph);
56015722
RUN_TEST(parallel_post_extract_projection_spills_before_resolve);
5723+
RUN_TEST(sequential_definitions_cache_drops_trees_and_compacts);
5724+
RUN_TEST(sequential_definitions_configured_failure_releases_work_arena);
56025725
RUN_TEST(parallel_empty_files);
56035726
RUN_TEST(parallel_args_json_no_overflow);
56045727
RUN_TEST(parallel_wide_definition_keeps_fp_sp_bt_f14);

0 commit comments

Comments
 (0)