Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

collectRefs: record idents only for tracked objects

Chiusa Adatta ai principianti
#124 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
85/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go

Direzione di ricerca

Inizia da internal/collect.go:447 e confronta collectUnkeyedFields in collect.go:431-438; esamina l’ordinamento di collectTargets in internal/analyzer.go:59-60 e i reader in rename.go:70 e 131. Filtra i record degli identificatori e dei campi correlati sugli tracked objects, mantenendo il comportamento esistente di refs. Il lavoro è completato quando c.idents contiene solo tracked targets e i rename reader continuano a funzionare correttamente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement

Summary

collectRefs (internal/collect.go:447) appends to c.idents for every Def and every Use in the package. Locals, parameters and objects from the standard library are all included. Only tracked targets are ever read, so the map can hold only those.

Current shape

ast.Inspect(fi.file, func(n ast.Node) bool {
	...
	if obj := pass.TypesInfo.Defs[ident]; obj != nil {
		c.idents[obj] = append(c.idents[obj], ident) // every definition
	}
	obj := origin(pass.TypesInfo.Uses[ident])
	...
	c.idents[obj] = append(c.idents[obj], ident) // every use: err, ctx, fmt.Sprintf...
	if tn := embeddedTypeName(obj); tn != nil {
		c.idents[tn] = append(c.idents[tn], ident)
	}
	if _, tracked := c.byObj[obj]; tracked { // refs already filters
		c.refs[obj] = append(c.refs[obj], ref{node: ident, file: fi})
	}
	return true
})

The only readers are rename.go:70 and rename.go:131. Both read c.idents[t.obj], where t is a tracked target.

Why the change helps

c.idents grows with every identifier in the package. Each new key allocates a map bucket and a slice. For a package of 50,000 identifiers, that is tens of thousands of allocations whose results nobody reads. This walk runs on every pass, so every user pays for it.

// tracked reports whether obj is a key of c.byObj.
if obj := pass.TypesInfo.Defs[ident]; obj != nil && c.tracked(obj) {
	c.idents[obj] = append(c.idents[obj], ident)
}
...
if _, tracked := c.byObj[obj]; tracked {
	c.idents[obj] = append(c.idents[obj], ident)
	c.refs[obj] = append(c.refs[obj], ref{node: ident, file: fi})
}
if tn := embeddedTypeName(obj); tn != nil && c.tracked(tn) {
	c.idents[tn] = append(c.idents[tn], ident)
}

With the filter, the map holds entries only for the declarations declscope checks.

Related

collectUnkeyedFields (collect.go:431-438) also records fields that are not targets. Its comment says so: "refs is only ever read for a target, so the entry is never asked for." The same filter applies there.

Risk

Very low. collectTargets runs before collectRefs (internal/analyzer.go:59-60), so c.byObj is complete when the walk starts. refs already uses the same filter.

Not measured yet.

Lingua principale
Go
Stelle
40
Fork
1
Merge medio
51m
PR unite (30g)
113

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.