@@ -2867,6 +2867,8 @@ typedef struct {
28672867 cs_why_t why ; /* of an ambiguous one */
28682868} cs_res_t ;
28692869
2870+ typedef struct cs_memo cs_memo_t ;
2871+
28702872typedef struct {
28712873 const cs_index_t * ix ;
28722874 int file ;
@@ -2885,6 +2887,9 @@ typedef struct {
28852887 /* Set when a namespace this context does not see was passed over (NULL:
28862888 * nobody asks). */
28872889 bool * passed_over ;
2890+ /* What the using directives of a scope level gave a query, remembered
2891+ * for the pass the caller runs (lookup); NULL: nothing is remembered. */
2892+ cs_memo_t * memo ;
28882893} cs_ctx_t ;
28892894
28902895static cs_res_t res_edge (const cbm_gbuf_node_t * n , bool exact ) {
@@ -4437,14 +4442,21 @@ static void add_alias_target(const cs_ctx_t *c, const cs_using_t *u, cs_found_t
44374442 }
44384443}
44394444
4445+ /* True once a level has two candidates: the lookup is ambiguous whatever the
4446+ * directives after them bring, so they are not asked (S5). Which candidate
4447+ * came first, and whether there was one, never depends on the ones skipped. */
4448+ static bool found_decided (const cs_found_t * fd ) {
4449+ return fd -> n > SKIP_ONE ;
4450+ }
4451+
44404452static void level_aliases (const cs_ctx_t * c , const cs_using_t * us , int n ,
44414453 const cs_using_index_t * index , const cs_query_t * q , cs_found_t * fd ) {
44424454 /* an alias names a type or a namespace: no candidate for `Name{T}` */
44434455 if (q -> arity > 0 ) {
44444456 return ;
44454457 }
44464458 if (!index || !index -> ready ) {
4447- for (int i = 0 ; i < n ; i ++ ) {
4459+ for (int i = 0 ; i < n && ! found_decided ( fd ) ; i ++ ) {
44484460 cs_work (SKIP_ONE );
44494461 if (us [i ].kind == 'a' && strcmp (us [i ].alias , q -> name ) == 0 ) {
44504462 add_alias_target (c , & us [i ], fd );
@@ -4463,7 +4475,7 @@ static void level_aliases(const cs_ctx_t *c, const cs_using_t *us, int n,
44634475 hi = mid ;
44644476 }
44654477 }
4466- for (size_t i = lo ; i < index -> naliases ; i ++ ) {
4478+ for (size_t i = lo ; i < index -> naliases && ! found_decided ( fd ) ; i ++ ) {
44674479 cs_work (SKIP_ONE );
44684480 if (strcmp (index -> aliases [i ].name , q -> name )) {
44694481 break ;
@@ -4599,7 +4611,7 @@ static void level_usings(const cs_ctx_t *c, const cs_using_t *us, int n,
45994611 /* A common name in a large repository must not make a small scope walk
46004612 * more postings than it has directives. The baseline scan stays cheap. */
46014613 if (!indexed || hi - lo >= (size_t )n ) {
4602- for (int i = 0 ; i < n ; i ++ ) {
4614+ for (int i = 0 ; i < n && ! found_decided ( fd ) ; i ++ ) {
46034615 level_using (c , & us [i ], q , a_type_name , fd );
46044616 }
46054617 return ;
@@ -4618,7 +4630,7 @@ static void level_usings(const cs_ctx_t *c, const cs_using_t *us, int n,
46184630 if (ids .n > 1 ) {
46194631 qsort (ids .ids , ids .n , sizeof (int ), candidate_id_cmp );
46204632 }
4621- for (size_t i = 0 ; i < ids .n ; i ++ ) {
4633+ for (size_t i = 0 ; i < ids .n && ! found_decided ( fd ) ; i ++ ) {
46224634 cs_work (SKIP_ONE );
46234635 /* Own and twin postings may select the SAME directive twice.
46244636 * Distinct directive IDs must still be resolved separately. */
@@ -4631,12 +4643,91 @@ static void level_usings(const cs_ctx_t *c, const cs_using_t *us, int n,
46314643 cbm_free (CBM_MEM_CLASS_OTHER , ids .ids );
46324644 }
46334645 if (!complete ) {
4634- for (int i = 0 ; i < n ; i ++ ) {
4646+ for (int i = 0 ; i < n && ! found_decided ( fd ) ; i ++ ) {
46354647 level_using (c , & us [i ], q , a_type_name , fd );
46364648 }
46374649 }
46384650}
46394651
4652+ /* The using step of a lookup -- what the directives of one region, and at
4653+ * the global namespace the unit's, give one query -- remembered for one pass
4654+ * over a file's references (or one pass of the build): a name looked up
4655+ * again in the same region costs one probe, not the directives (S5). The key
4656+ * holds everything the step reads: the file, the region and unit asked, the
4657+ * context's visibility (product code, unit, assembly) and the query. A memo
4658+ * that ran out of memory stops remembering; the lookups stay exact. */
4659+ struct cs_memo {
4660+ CBMHashTable * steps ; /* key -> cs_found_t in `arena` */
4661+ CBMArena arena ;
4662+ bool off ;
4663+ };
4664+
4665+ enum { CS_MEMO_KEY = CS_NAME_BUF + CBM_SZ_128 };
4666+
4667+ static void memo_init (cs_memo_t * m ) {
4668+ memset (m , 0 , sizeof (* m ));
4669+ cbm_arena_init (& m -> arena );
4670+ m -> steps = cbm_ht_create (CBM_SZ_256 );
4671+ m -> off = m -> steps == NULL ;
4672+ }
4673+
4674+ static void memo_destroy (cs_memo_t * m ) {
4675+ cbm_ht_free (m -> steps );
4676+ cbm_arena_destroy (& m -> arena );
4677+ memset (m , 0 , sizeof (* m ));
4678+ }
4679+
4680+ /* The key of one using step; false when it does not fit (not remembered). */
4681+ static bool memo_key (char * buf , size_t cap , const cs_ctx_t * c , int region , int unit ,
4682+ const cs_query_t * q ) {
4683+ int n = snprintf (buf , cap , "%d|%d|%d|%d|%d|%d|%d|%d%d%d%c|%s" , c -> file , region , unit , c -> unit ,
4684+ c -> group , (int )c -> prod , q -> arity , (int )q -> types_only , (int )q -> statics ,
4685+ (int )q -> ctors , q -> kind ? q -> kind : '-' , q -> name );
4686+ return n > 0 && (size_t )n < cap ;
4687+ }
4688+
4689+ static bool memo_get (const cs_memo_t * m , const char * key , cs_found_t * out ) {
4690+ cs_work (SKIP_ONE );
4691+ const cs_found_t * hit = (const cs_found_t * )cbm_ht_get (m -> steps , key );
4692+ if (hit ) {
4693+ * out = * hit ;
4694+ }
4695+ return hit != NULL ;
4696+ }
4697+
4698+ static void memo_put (cs_memo_t * m , const char * key , const cs_found_t * step ) {
4699+ char * k = cbm_arena_strdup (& m -> arena , key );
4700+ cs_found_t * v = (cs_found_t * )cbm_arena_alloc (& m -> arena , sizeof (* v ));
4701+ if (!k || !v ) {
4702+ m -> off = true;
4703+ return ;
4704+ }
4705+ * v = * step ;
4706+ cbm_ht_set (m -> steps , k , v );
4707+ m -> off = cbm_ht_get (m -> steps , k ) != v ; /* an insert that did not take */
4708+ }
4709+
4710+ /* What the directives of this scope level give the query: the region's own
4711+ * (`us`, when asked) and, at the global namespace, the unit's. Asked once per
4712+ * key when the context has a memo. */
4713+ static void using_step (const cs_ctx_t * c , int region , const cs_using_t * us , int nus ,
4714+ const cs_using_index_t * usi , const cs_unit_t * unit , const cs_query_t * q ,
4715+ cs_found_t * step ) {
4716+ char key [CS_MEMO_KEY ];
4717+ bool keyed = c -> memo && !c -> memo -> off &&
4718+ memo_key (key , sizeof (key ), c , region , unit ? c -> unit : CS_NONE , q );
4719+ if (keyed && memo_get (c -> memo , key , step )) {
4720+ return ;
4721+ }
4722+ level_usings (c , us , nus , usi , q , step );
4723+ if (unit && !found_decided (step )) {
4724+ level_usings (c , unit -> usings , unit -> nusings , & unit -> using_index , q , step );
4725+ }
4726+ if (keyed ) {
4727+ memo_put (c -> memo , key , step );
4728+ }
4729+ }
4730+
46404731/* True when the lookup is settled by what the last level added. A level that
46414732 * had only invisible candidates does not bind: the lookup goes on, and
46424733 * remembers. */
@@ -4714,9 +4805,16 @@ static void lookup(const cs_ctx_t *c, const cs_query_t *q, cs_found_t *fd, bool
47144805 fd -> exact = true;
47154806 return ;
47164807 }
4717- level_usings (c , us , nus , usi , q , fd );
4718- if (unit ) {
4719- level_usings (c , unit -> usings , unit -> nusings , & unit -> using_index , q , fd );
4808+ if (nus > 0 || unit ) {
4809+ /* Nothing is found yet (settled), and a step only adds: the step
4810+ * is asked on its own and its candidates are the level's. */
4811+ cs_found_t step = {0 };
4812+ using_step (c , asked ? reg : CS_NONE , us , nus , usi , unit , q , & step );
4813+ fd -> first = step .first ;
4814+ fd -> n = step .n ;
4815+ fd -> invisible = step .invisible ;
4816+ fd -> why = step .why ;
4817+ fd -> joined = fd -> joined || step .joined ;
47204818 }
47214819 if (settled (fd , & invisible )) {
47224820 * statics = fd -> first .kind == 'M' ;
@@ -5515,6 +5613,10 @@ static bool resolve_usings(cs_index_t *ix) {
55155613 return false;
55165614 }
55175615 }
5616+ /* A lookup asks only regions above the directive's own, whose directives
5617+ * are resolved already: what it remembers of them stays true. */
5618+ cs_memo_t memo ;
5619+ memo_init (& memo );
55185620 for (int fi = 0 ; fi < ix -> nfiles ; fi ++ ) {
55195621 cs_file_t * f = & ix -> files [fi ];
55205622 /* Regions are in ancestor order. Publish an outer region's index
@@ -5528,10 +5630,12 @@ static bool resolve_usings(cs_index_t *ix) {
55285630 c .prod = false; /* a directive names whatever the compiler bound */
55295631 c .glob = r == 0 ;
55305632 c .skip_region = r ;
5633+ c .memo = & memo ;
55315634 resolve_using (& f -> usings [i ], & c );
55325635 }
55335636 const cs_using_t * us = reg -> u_hi > reg -> u_lo ? f -> usings + reg -> u_lo : NULL ;
55345637 if (!build_using_index (ix , us , reg -> u_hi - reg -> u_lo , & reg -> using_index )) {
5638+ memo_destroy (& memo );
55355639 return false;
55365640 }
55375641 }
@@ -5543,6 +5647,7 @@ static bool resolve_usings(cs_index_t *ix) {
55435647 }
55445648 }
55455649 }
5650+ memo_destroy (& memo );
55465651 return true;
55475652}
55485653
@@ -5570,6 +5675,8 @@ static bool resolve_bases(cs_index_t *ix) {
55705675 int * listed =
55715676 (int * )cbm_calloc (CBM_MEM_CLASS_OTHER , ((size_t )ix -> nents + SKIP_ONE ) * sizeof (int ));
55725677 bool ok = listed != NULL ;
5678+ cs_memo_t memo ; /* every directive is resolved: what a lookup remembers stays true */
5679+ memo_init (& memo );
55735680 for (int ei = 0 ; ok && ei < ix -> nents ; ei ++ ) {
55745681 cs_entity_t * e = & ix -> ents [ei ];
55755682 int written = 0 ;
@@ -5587,6 +5694,7 @@ static bool resolve_bases(cs_index_t *ix) {
55875694 /* a base list is written outside the type it belongs to */
55885695 ctx_at (& c , ix , e -> decls [d ].file , t -> region , t -> outer );
55895696 c .prod = false; /* a declared base is whatever the compiler bound */
5697+ c .memo = & memo ;
55905698 for (const char * p = t -> bases ; p && * p ;) {
55915699 const char * bar = strchr (p , '|' );
55925700 size_t n = bar ? (size_t )(bar - p ) : strlen (p );
@@ -5601,6 +5709,7 @@ static bool resolve_bases(cs_index_t *ix) {
56015709 }
56025710 }
56035711 }
5712+ memo_destroy (& memo );
56045713 cbm_free (CBM_MEM_CLASS_OTHER , listed );
56055714 ix -> oom = ix -> oom || !ok ;
56065715 return ok ;
@@ -6054,7 +6163,26 @@ static bool names_quarantined(const cs_index_t *ix, const cs_file_t *f, const cs
60546163 return false;
60556164}
60566165
6057- static void cs_resolve (const void * index , int run_file , const CBMDocLink * link ,
6166+ /* The working state of one file's references: the memo of their lookups. */
6167+ static void * cs_file_begin (const void * index , int run_file ) {
6168+ (void )index ;
6169+ (void )run_file ;
6170+ cs_memo_t * m = (cs_memo_t * )cbm_alloc (CBM_MEM_CLASS_OTHER , sizeof (* m ));
6171+ if (m ) {
6172+ memo_init (m );
6173+ }
6174+ return m ;
6175+ }
6176+
6177+ static void cs_file_end (void * state ) {
6178+ cs_memo_t * m = (cs_memo_t * )state ;
6179+ if (m ) {
6180+ memo_destroy (m );
6181+ cbm_free (CBM_MEM_CLASS_OTHER , m );
6182+ }
6183+ }
6184+
6185+ static void cs_resolve (const void * index , void * state , int run_file , const CBMDocLink * link ,
60586186 const cbm_gbuf_t * graph , cbm_doclink_outcome_t * out ) {
60596187 const cs_index_t * ix = (const cs_index_t * )index ;
60606188 (void )graph ; /* every node was looked up when the index was built */
@@ -6091,6 +6219,7 @@ static void cs_resolve(const void *index, int run_file, const CBMDocLink *link,
60916219 cs_ctx_t c ;
60926220 ctx_init (& c , ix , file , link -> def_line , file_doc );
60936221 c .glob = r .glob ;
6222+ c .memo = (cs_memo_t * )state ;
60946223 cs_res_t res = resolve_ref (& c , & r );
60956224 if (res .st == CS_LOCAL ) {
60966225 out -> kind = CBM_DOCLINK_LOCAL ;
@@ -6260,6 +6389,8 @@ const cbm_doclink_resolver_t cbm_doclink_cs_resolver = {
62606389 .scope_tag = CBM_DOCLINK_CS_SCOPE_TAG ,
62616390 .build = cs_build ,
62626391 .destroy = cs_destroy ,
6392+ .file_begin = cs_file_begin ,
6393+ .file_end = cs_file_end ,
62636394 .resolve = cs_resolve ,
62646395 .scope_delta = cs_scope_delta ,
62656396};
0 commit comments