Skip to content

Commit cc2c966

Browse files
committed
doc-links(cs): a name is looked up once per region; a second candidate decides (S5)
A name that no enclosing type or namespace has is looked up in the using directives of each scope level, and a name some namespaces declare walked the min(postings, directives) of the level for every reference. A file with r references to such a name under k directives cost r x k. The using step of a lookup -- what one region's directives, and at the global namespace the unit's, give one query -- is now remembered for the pass: the references of one file (a per-file working state the doc-link layer hands to the resolver: file_begin / file_end), and the build's passes over using directives and base lists. The key holds everything the step reads: the file, the region and unit asked, the context's visibility (product code, unit, assembly) and the query. A memo that runs out of memory stops remembering; the lookups stay exact. Inside a lookup, the alias and using walks stop at a level's second candidate: the result is ambiguous whatever the rest would bring. Which candidate came first, and whether there was one, never depends on the directives skipped; only the ambiguity tally of the log line (why) may count such a reference under another kind. There is no cap: every outcome is what it was. Tests: cs_lookup_repeated_name_work (k 60 -> 240 and r 40 -> 160: 236 -> 900 steps, 3.8 x; without the memo 3004 -> 41444, 13.8 x), cs_lookup_ambiguous_stops (one ambiguous reference under 50 -> 200 imports that declare it: 17 -> 21 steps; without the stop 65 -> 219). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 6e2a462 commit cc2c966

4 files changed

Lines changed: 288 additions & 12 deletions

File tree

‎src/pipeline/doc_links.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ void cbm_doclinks_resolve_file(cbm_doclinks_t *dl, int file_idx, const CBMFileRe
429429
* file-level doc resolves */
430430
const cbm_gbuf_node_t *file_node = NULL;
431431
bool file_node_looked_up = false;
432+
void *state = (R && index && R->file_begin) ? R->file_begin(index, file_idx) : NULL;
432433
for (int i = 0; i < n; i++) {
433434
const CBMDocLink *link = &result->doc_links.items[i];
434435
cbm_doclink_outcome_t out = {.kind = CBM_DOCLINK_UNRESOLVED,
@@ -438,7 +439,7 @@ void cbm_doclinks_resolve_file(cbm_doclinks_t *dl, int file_idx, const CBMFileRe
438439
} else if (!R || !index) {
439440
continue; /* no resolver for this language: nothing to say */
440441
} else {
441-
R->resolve(index, file_idx, link, graph, &out);
442+
R->resolve(index, state, file_idx, link, graph, &out);
442443
}
443444
if (out.kind == CBM_DOCLINK_LOCAL) {
444445
atomic_fetch_add_explicit(&dl->local_refs, 1, memory_order_relaxed);
@@ -495,6 +496,9 @@ void cbm_doclinks_resolve_file(cbm_doclinks_t *dl, int file_idx, const CBMFileRe
495496
}
496497
atomic_fetch_add_explicit(&dl->reasons[reason], 1, memory_order_relaxed);
497498
}
499+
if (state && R->file_end) {
500+
R->file_end(state);
501+
}
498502
if (nm > 0) {
499503
emit_mentions(dl, fi->rel_path, mentions, nm, edge_out);
500504
}

‎src/pipeline/doc_links.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,16 @@ typedef struct {
189189
/* The language's project-wide index; NULL on allocation failure. */
190190
void *(*build)(const cbm_doclink_build_in_t *in);
191191
void (*destroy)(void *index);
192+
/* Optional working state for the references of one file, which one thread
193+
* resolves: made before its first reference, handed to every resolve call
194+
* of the file, released after its last. A NULL state (no hook, or memory
195+
* ran out) changes the cost of resolving, never an outcome. */
196+
void *(*file_begin)(const void *index, int run_file);
197+
void (*file_end)(void *state);
192198
/* Resolve one reference of run file `run_file` (its own scope is in the
193-
* index). Thread-safe: the index is read-only after build. */
194-
void (*resolve)(const void *index, int run_file, const CBMDocLink *link,
199+
* index). Thread-safe: the index is read-only after build; `state` is the
200+
* file's own (file_begin) or NULL. */
201+
void (*resolve)(const void *index, void *state, int run_file, const CBMDocLink *link,
195202
const cbm_gbuf_t *graph, cbm_doclink_outcome_t *out);
196203
/* Incremental runs; both optional.
197204
* scope_input true for a file that is no source of the language and has

‎src/pipeline/doc_links_cs.c‎

Lines changed: 140 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
28702872
typedef 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

28902895
static 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+
44404452
static 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

Comments
 (0)