@@ -6179,10 +6179,36 @@ static void py_split_field_defs(CBMArena *arena, const char *field_defs, const c
61796179 * types_out = types ;
61806180}
61816181
6182+ /* Qualified names of reg->types[0..noted), for a caller that registers into a
6183+ * registry it does not finalize until the end (cbm_py_build_cross_registry):
6184+ * that registry's own lookup scans every type. qns == NULL probes the
6185+ * registry instead. */
6186+ typedef struct {
6187+ CBMHashTable * qns ;
6188+ int noted ;
6189+ } PyKnownTypes ;
6190+
6191+ /* Note the types added since the last call. Run after every add_type, so an
6192+ * add that failed silently cannot desync set and registry. If the set cannot
6193+ * grow it is dropped and probes fall back to the registry. */
6194+ static void py_note_known_types (PyKnownTypes * known , const CBMTypeRegistry * reg ) {
6195+ for (; known && known -> qns && known -> noted < reg -> type_count ; known -> noted ++ ) {
6196+ const char * qn = reg -> types [known -> noted ].qualified_name ;
6197+ if (!qn || cbm_ht_has (known -> qns , qn ))
6198+ continue ;
6199+ cbm_ht_set (known -> qns , qn , (void * )(uintptr_t )(known -> noted + 1 ));
6200+ if (!cbm_ht_has (known -> qns , qn )) {
6201+ cbm_ht_free (known -> qns );
6202+ known -> qns = NULL ;
6203+ }
6204+ }
6205+ }
6206+
61826207/* Build a registry from CBMLSPDef[] supplied by the caller — covers both
6183- * the source file's own defs and cross-file referenced defs. */
6208+ * the source file's own defs and cross-file referenced defs. known is NULL
6209+ * except for the shared Tier-2 build. */
61846210static bool py_register_lsp_defs (CBMArena * arena , CBMArena * idx_arena , CBMTypeRegistry * reg ,
6185- CBMLSPDef * defs , int def_count ) {
6211+ CBMLSPDef * defs , int def_count , PyKnownTypes * known ) {
61866212 /* Pass 1: types only — the method pass probes the registry per Method def
61876213 * (receiver auto-registration), which is a LINEAR scan pre-finalize:
61886214 * O(methods x types) per file (same quadratic as php_register_lsp_defs;
@@ -6206,6 +6232,7 @@ static bool py_register_lsp_defs(CBMArena *arena, CBMArena *idx_arena, CBMTypeRe
62066232 }
62076233 py_split_field_defs (arena , d -> field_defs , & rt .field_names , & rt .field_types );
62086234 cbm_registry_add_type (reg , rt );
6235+ py_note_known_types (known , reg );
62096236 }
62106237 }
62116238 /* idx_arena == NULL skips the mid-build finalize: the tier-2 builder
@@ -6259,13 +6286,17 @@ static bool py_register_lsp_defs(CBMArena *arena, CBMArena *idx_arena, CBMTypeRe
62596286
62606287 if (strcmp (d -> label , "Method" ) == 0 && d -> receiver_type && d -> receiver_type [0 ]) {
62616288 rf .receiver_type = d -> receiver_type ; /* borrowed */
6262- if (!cbm_registry_lookup_type (reg , rf .receiver_type )) {
6289+ bool recv_known = known && known -> qns
6290+ ? cbm_ht_has (known -> qns , rf .receiver_type )
6291+ : cbm_registry_lookup_type (reg , rf .receiver_type ) != NULL ;
6292+ if (!recv_known ) {
62636293 CBMRegisteredType auto_t ;
62646294 memset (& auto_t , 0 , sizeof (auto_t ));
62656295 auto_t .qualified_name = rf .receiver_type ;
62666296 const char * dot = strrchr (d -> receiver_type , '.' );
62676297 auto_t .short_name = dot ? dot + 1 : rf .receiver_type ; /* borrowed substring */
62686298 cbm_registry_add_type (reg , auto_t );
6299+ py_note_known_types (known , reg );
62696300 }
62706301 }
62716302 cbm_registry_add_func (reg , rf );
@@ -6277,7 +6308,7 @@ static bool py_register_lsp_defs(CBMArena *arena, CBMArena *idx_arena, CBMTypeRe
62776308#ifdef CBM_ENABLE_TEST_SEAMS
62786309bool cbm_py_lsp_test_register_defs (CBMArena * arena , CBMTypeRegistry * reg , CBMLSPDef * defs ,
62796310 int def_count ) {
6280- return py_register_lsp_defs (arena , NULL , reg , defs , def_count );
6311+ return py_register_lsp_defs (arena , NULL , reg , defs , def_count , NULL );
62816312}
62826313#endif
62836314
@@ -6774,7 +6805,7 @@ CBMLSPStatus cbm_run_py_lsp_cross_status(CBMArena *arena, const char *source, in
67746805 py_lsp_init_for_stage (& ctx , arena , source , source_len , & reg , module_qn , out , "cross" );
67756806 if (ctx .eval_failure )
67766807 goto cross_cleanup ;
6777- if (!py_register_lsp_defs (arena , & idx_arena , & reg , defs , def_count )) {
6808+ if (!py_register_lsp_defs (arena , & idx_arena , & reg , defs , def_count , NULL )) {
67786809 ctx .eval_failure = CBM_LSP_SCOPE_FAILED ;
67796810 goto cross_cleanup ;
67806811 }
@@ -6813,15 +6844,27 @@ CBMTypeRegistry *cbm_py_build_cross_registry(CBMArena *arena, CBMLSPDef *defs, i
68136844 cbm_registry_init (reg , arena );
68146845 cbm_python_stdlib_register (reg , arena );
68156846
6847+ /* The registry is not finalized until the end, so probing it for each
6848+ * Method's receiver scanned every type: O(methods x types), single-threaded
6849+ * (~54 s of lsp_cross_prepare on a large Python tree). Probe a set of
6850+ * the registered type QNs instead; registration order is unchanged. Sized
6851+ * for the stdlib seed, not def_count (the mixed all-language count). NULL
6852+ * on allocation failure: the registry probe runs as before. */
6853+ PyKnownTypes known = {cbm_ht_create ((uint32_t )reg -> type_count ), 0 };
6854+ py_note_known_types (& known , reg );
6855+
68166856 /* Filter to Python defs only — defs[] is mixed-language all_defs. */
68176857 for (int i = 0 ; i < def_count ; i ++ ) {
68186858 CBMLSPDef * d = & defs [i ];
68196859 if (d -> lang != CBM_LANG_PYTHON )
68206860 continue ;
68216861 /* Reuse the existing register fn on a single-def slice (n=1 inline). */
6822- if (!py_register_lsp_defs (arena , NULL , reg , d , 1 ))
6862+ if (!py_register_lsp_defs (arena , NULL , reg , d , 1 , & known )) {
6863+ cbm_ht_free (known .qns );
68236864 return NULL ; /* caller owns the shared arena and other registries */
6865+ }
68246866 }
6867+ cbm_ht_free (known .qns );
68256868
68266869 py_mark_ambiguous_callable_bindings (reg , arena );
68276870 cbm_registry_build_type_qn_order (reg , arena );
0 commit comments