Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Compute each position once before sorting by comparePos

Cerrado
#129 0 comentarios 0 reacciones 0 asignados Ver en GitHub

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

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

enhancement

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

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de mpyw/declscope

Todos los issues de mpyw/declscope

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.