collectRefs: record idents only for tracked objects
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 85/100
- Issue 类型
- 重构
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- go
- 领域
- devtools, performance
调研方向
从 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 内容生成。
描述
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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
相似的 Issue
-
bug performance scalability
难度 2/5 1-3 小时 新手友好度 62/100
Davidjustice28/basildb#35 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
rhysd/actionlint#761 ·
-
bug needs-triage P2 tier:1-mechanical
难度 2/5 1-3 小时 新手友好度 68/100
krakenhavoc/cmd_and_ctrl#3652 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
puppe1990/amarra-cais#345 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
limitlessprojects/Go-Hybrid#50 ·
维护者通常 1 天内回复