Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

collectRefs: record idents only for tracked objects

已关闭 适合新手
#124 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
85/100
Issue 类型
重构
描述清晰度
描述清楚
活跃度
活跃
技术栈
go

调研方向

从 internal/collect.go:447 开始,并对比 collect.go:431-438 中的 collectUnkeyedFields;检查 internal/analyzer.go:59-60 中 collectTargets 的顺序,以及 rename.go:70 和 131 中的 readers。将 identifier 和相关 field records 过滤为 tracked objects,同时保留现有的 refs 行为。当 c.idents 仅包含 tracked targets 且 rename readers 仍然正确时,即完成。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Go
星标
40
派生
1
平均合并
51 分钟
30 天内合并 PR
113

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。