Compute each position once before sorting by comparePos
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 72/100
- Tipo de issue
- Refactorización
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- go
- Área
- performance, tooling
Línea de trabajo
Start with comparePos in internal/subject.go:313, then inspect its ten sorting call sites in internal/ignore.go, report.go, scopesite.go, surplus.go, and survey.go. Check the two internal/surplus.go sites around lines 579 and 659 for later sorted-order use, and run the existing test suite; done means each sort preserves its current ordering while avoiding repeated position lookups.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Summary
comparePos (internal/subject.go:313) computes two token.Position values on every comparison. It is the comparator of 10 sorts, in ignore.go, report.go, scopesite.go, surplus.go and survey.go. Computing each position once before the sort cuts the work from about 2·n·log n position lookups to n.
Current shape
func comparePos(fset *token.FileSet, a, b token.Pos) int {
pa, pb := fset.Position(a), fset.Position(b) // two lookups per comparison
if c := strings.Compare(pa.Filename, pb.Filename); c != 0 {
return c
}
return cmp.Compare(pa.Offset, pb.Offset)
}
// internal/ignore.go:202
slices.SortFunc(sites, func(a, b *ignoreSite) int { return comparePos(pass.Fset, a.ig.Pos, b.ig.Pos) })
fset.Position finds the file with a binary search, then finds the line with another binary search. It also applies //line adjustment.
Why the change helps
A sort of n elements makes about n·log n comparisons. Each comparison looks up two positions, and the same element's position is looked up again every time it is compared.
type keyed[T any] struct {
pos token.Position
v T
}
// One lookup per element.
ks := make([]keyed[finding], len(findings))
for i, f := range findings {
ks[i] = keyed[finding]{fset.Position(f.pos), f}
}
slices.SortFunc(ks, func(a, b keyed[finding]) int {
if c := strings.Compare(a.pos.Filename, b.pos.Filename); c != 0 {
return c
}
return cmp.Compare(a.pos.Offset, b.pos.Offset)
})
For 1,000 findings, that is about 20,000 lookups today and 1,000 after the change.
A sort that only needs the first element
Two call sites sort only to take the smallest element:
// internal/surplus.go:579 and :659
slices.SortFunc(names, func(a, b *target) int { return comparePos(pass.Fset, a.ident.Pos(), b.ident.Pos()) })
pos := names[0].boundBy.ScopePos
slices.MinFunc finds the same element in one pass, with n-1 comparisons instead of about n·log n. Check first that nothing reads names in sorted order after this line.
Risk
None, provided the key is the same fset.Position the comparator reads today.
Low impact. The sorted lists are findings and directives, which are small in a clean package. It matters most in a baseline or survey run over a large module with many findings.
- Lenguaje dominante
- Go
- Estrellas
- 42
- Forks
- 1
- Merge medio
- 1 h 18 min
- PR fusionados (30 d)
- 94
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de mpyw/declscope
-
bug P3
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
mpyw/declscope#262 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
documentation P3
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
Los mantenedores suelen responder en 1 día
-
documentation P3
Dificultad 1/5 Menos de una hora Aptitud para principiantes 92/100
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 80/100
Los mantenedores suelen responder en 1 día
-
bug P2
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
Los mantenedores suelen responder en 1 día
Todos los issues de mpyw/declscope
Issues similares
-
Mend: dependency security vulnerability
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
kube-tarian/sigrun#202 ·
-
question raised-by:verifier
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
medici-finance/assay#4013 ·
Los mantenedores suelen responder en 1 día
-
TestRegistry_ConcurrentMixedOperations fetches from the network and can hang the short suiteAbiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 83/100
tsukumogami/tsuku#2775 ·
Los mantenedores suelen responder en 1 día
-
automation models
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 69/100
Los mantenedores suelen responder en 1 día