@@ -751,6 +751,91 @@ TEST(pylsp_fused_self_attr_chain_via_overlay) {
751751 PASS ();
752752}
753753
754+ /* Characterization of the shared Tier-2 build order. Defs register one at a
755+ * time in defs[] order, so a Method seen before its Class auto-registers a
756+ * placeholder receiver type that stays ahead of the Class entry, and a Method
757+ * whose receiver is already known adds no type. Any change to how the build
758+ * probes for known receivers must keep this exact sequence. */
759+ TEST (pylsp_cross_registry_interleaved_order ) {
760+ CBMArena arena ;
761+ cbm_arena_init (& arena );
762+ CBMTypeRegistry * base = cbm_py_build_cross_registry (& arena , NULL , 0 );
763+ ASSERT_NOT_NULL (base );
764+ int tb = base -> type_count , fb = base -> func_count ;
765+
766+ CBMLSPDef defs [] = {
767+ {.qualified_name = "t.m.A.m" ,
768+ .short_name = "m" ,
769+ .label = "Method" ,
770+ .receiver_type = "t.m.A" ,
771+ .def_module_qn = "t.m" ,
772+ .lang = CBM_LANG_PYTHON },
773+ {.qualified_name = "t.m.A" ,
774+ .short_name = "A" ,
775+ .label = "Class" ,
776+ .def_module_qn = "t.m" ,
777+ .lang = CBM_LANG_PYTHON },
778+ {.qualified_name = "t.m.B" ,
779+ .short_name = "B" ,
780+ .label = "Class" ,
781+ .def_module_qn = "t.m" ,
782+ .lang = CBM_LANG_PYTHON },
783+ {.qualified_name = "t.m.B.f" ,
784+ .short_name = "f" ,
785+ .label = "Method" ,
786+ .receiver_type = "t.m.B" ,
787+ .def_module_qn = "t.m" ,
788+ .lang = CBM_LANG_PYTHON },
789+ {.qualified_name = "t.m.C.g" ,
790+ .short_name = "g" ,
791+ .label = "Method" ,
792+ .receiver_type = "t.m.C" ,
793+ .def_module_qn = "t.m" ,
794+ .lang = CBM_LANG_PYTHON },
795+ /* Receiver auto-registered by C.g just above: no second placeholder. */
796+ {.qualified_name = "t.m.C.h" ,
797+ .short_name = "h" ,
798+ .label = "Method" ,
799+ .receiver_type = "t.m.C" ,
800+ .def_module_qn = "t.m" ,
801+ .lang = CBM_LANG_PYTHON },
802+ /* Non-Python defs are skipped by the builder. */
803+ {.qualified_name = "t.m.D" ,
804+ .short_name = "D" ,
805+ .label = "Class" ,
806+ .def_module_qn = "t.m" ,
807+ .lang = CBM_LANG_GO },
808+ };
809+ CBMTypeRegistry * reg =
810+ cbm_py_build_cross_registry (& arena , defs , (int )(sizeof (defs ) / sizeof (defs [0 ])));
811+ ASSERT_NOT_NULL (reg );
812+
813+ /* Types: placeholder A (from A.m), Class A, Class B, placeholder C. */
814+ ASSERT_EQ (reg -> type_count , tb + 4 );
815+ ASSERT_TRUE (reg -> types [tb ].qualified_name == defs [0 ].receiver_type );
816+ ASSERT_STR_EQ (reg -> types [tb ].short_name , "A" );
817+ ASSERT_TRUE (reg -> types [tb + 1 ].qualified_name == defs [1 ].qualified_name );
818+ ASSERT_TRUE (reg -> types [tb + 2 ].qualified_name == defs [2 ].qualified_name );
819+ ASSERT_TRUE (reg -> types [tb + 3 ].qualified_name == defs [4 ].receiver_type );
820+ ASSERT_STR_EQ (reg -> types [tb + 3 ].short_name , "C" );
821+
822+ /* Funcs keep defs[] order. */
823+ ASSERT_EQ (reg -> func_count , fb + 4 );
824+ ASSERT_STR_EQ (reg -> funcs [fb ].qualified_name , "t.m.A.m" );
825+ ASSERT_STR_EQ (reg -> funcs [fb + 1 ].qualified_name , "t.m.B.f" );
826+ ASSERT_STR_EQ (reg -> funcs [fb + 2 ].qualified_name , "t.m.C.g" );
827+ ASSERT_STR_EQ (reg -> funcs [fb + 2 ].receiver_type , "t.m.C" );
828+ ASSERT_STR_EQ (reg -> funcs [fb + 3 ].qualified_name , "t.m.C.h" );
829+
830+ const CBMRegisteredType * a = cbm_registry_lookup_type (reg , "t.m.A" );
831+ ASSERT_NOT_NULL (a );
832+ ASSERT_STR_EQ (a -> qualified_name , "t.m.A" );
833+ ASSERT_NULL (cbm_registry_lookup_type (reg , "t.m.D" ));
834+
835+ cbm_arena_destroy (& arena );
836+ PASS ();
837+ }
838+
754839/* Issue #228: a class/static method invoked directly on a CROSS-FILE imported
755840 * class name — ActionRecordX.build_from_text(...) — produced no CALLS edge, so
756841 * the method showed in/out degree 0 and was flagged as dead code. Distinct from
@@ -2448,6 +2533,59 @@ TEST(pylsp_scale_work_is_linear) {
24482533 PASS ();
24492534}
24502535
2536+ /* #2413 (O9: no clock): the shared Tier-2 build registers one def at a time
2537+ * into a registry it finalizes only at the end, so a receiver probe that scans
2538+ * the types registered so far makes it O(methods x types). The registry
2539+ * primitives count every candidate they examine (CBM_LSP_WORK): 4x the classes
2540+ * must cost at most ~4x the work; the scanning probe lands at ~16x. */
2541+ static uint64_t cross_registry_work_for (int n_classes , int * out_types ) {
2542+ CBMArena arena ;
2543+ cbm_arena_init (& arena );
2544+ int n_defs = n_classes * 3 ;
2545+ CBMLSPDef * defs = (CBMLSPDef * )cbm_arena_calloc (& arena , (size_t )n_defs * sizeof (* defs ));
2546+ if (!defs ) {
2547+ cbm_arena_destroy (& arena );
2548+ * out_types = -1 ;
2549+ return 0 ;
2550+ }
2551+ /* Extraction order: each class followed by its two methods. */
2552+ for (int i = 0 ; i < n_classes ; i ++ ) {
2553+ CBMLSPDef * cls = & defs [i * 3 ];
2554+ cls -> qualified_name = cbm_arena_sprintf (& arena , "scale.mod.Cls%d" , i );
2555+ cls -> short_name = cls -> qualified_name + strlen ("scale.mod." );
2556+ cls -> label = "Class" ;
2557+ cls -> def_module_qn = "scale.mod" ;
2558+ cls -> lang = CBM_LANG_PYTHON ;
2559+ for (int j = 1 ; j <= 2 ; j ++ ) {
2560+ CBMLSPDef * m = & defs [i * 3 + j ];
2561+ m -> qualified_name = cbm_arena_sprintf (& arena , "%s.m%d" , cls -> qualified_name , j );
2562+ m -> short_name = j == 1 ? "m1" : "m2" ;
2563+ m -> label = "Method" ;
2564+ m -> receiver_type = cls -> qualified_name ;
2565+ m -> def_module_qn = "scale.mod" ;
2566+ m -> lang = CBM_LANG_PYTHON ;
2567+ }
2568+ }
2569+ (void )cbm_lsp_work_take ();
2570+ CBMTypeRegistry * reg = cbm_py_build_cross_registry (& arena , defs , n_defs );
2571+ uint64_t work = cbm_lsp_work_take ();
2572+ * out_types = reg ? reg -> type_count : -1 ;
2573+ cbm_arena_destroy (& arena );
2574+ return work ;
2575+ }
2576+
2577+ TEST (pylsp_cross_registry_build_work_is_linear ) {
2578+ int t1 = 0 , t4 = 0 ;
2579+ uint64_t w1 = cross_registry_work_for (1000 , & t1 );
2580+ uint64_t w4 = cross_registry_work_for (4000 , & t4 );
2581+ printf (" cross registry work: 1000=%llu (types=%d) 4000=%llu (types=%d)\n" ,
2582+ (unsigned long long )w1 , t1 , (unsigned long long )w4 , t4 );
2583+ /* Every class registered exactly once (no duplicate placeholder). */
2584+ ASSERT_EQ (t4 - t1 , 3000 );
2585+ ASSERT (w4 <= 5 * w1 + 1000 );
2586+ PASS ();
2587+ }
2588+
24512589extern const TSLanguage * tree_sitter_python (void );
24522590
24532591TEST (pylsp_issue1527_memo_allocation_failure_is_reported ) {
@@ -4850,6 +4988,7 @@ SUITE(py_lsp) {
48504988 RUN_TEST (pylsp_issue1527_long_binary_chain_is_not_nesting );
48514989 RUN_TEST (pylsp_issue1527_long_receiver_chain_is_not_nesting );
48524990 RUN_TEST (pylsp_scale_work_is_linear );
4991+ RUN_TEST (pylsp_cross_registry_build_work_is_linear );
48534992 /* Phase 2 — smoke */
48544993 RUN_TEST (pylsp_smoke_empty );
48554994 RUN_TEST (pylsp_smoke_one_function );
@@ -4886,6 +5025,7 @@ SUITE(py_lsp) {
48865025 RUN_TEST (pylsp_crossfile_receiver_through_typed_field );
48875026 RUN_TEST (pylsp_crossfile_typed_field_shared_registry );
48885027 RUN_TEST (pylsp_fused_self_attr_chain_via_overlay );
5028+ RUN_TEST (pylsp_cross_registry_interleaved_order );
48895029 RUN_TEST (pylsp_crossfile_classmethod_on_class_issue228 );
48905030 RUN_TEST (pylsp_crossfile_inheritance );
48915031 RUN_TEST (pylsp_batch_two_files );
0 commit comments