packfile.DeltaSelector has no memory bound for large objects (no pack.windowMemory / core.bigFileThreshold equivalent)

Abierto
#2,573 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
39/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
git, go

Línea de trabajo

Start with plumbing/format/packfile/delta_selector.go and diff_delta.go, then trace how Encoder.Encode uses DeltaSelector and how objects are read and compared. The issue proposes byte limits for delta search and lazy undeltifying; review those approaches against the current selector and encoder options. Done means large-object encoding respects bounded memory while existing delta selection still works.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

go-git version: v6.0.0-beta.1. delta_selector.go and diff_delta.go are identical on main at 9decb13c (2026-11-06).

What happens

When Encoder.Encode(hashes, 10) runs over large blobs (for example, the versions of a big file), memory grows with the total size of the objects, not with any bound. Git bounds the same work with pack.windowMemory, and skips delta search entirely above core.bigFileThreshold. go-git has neither.

Why
  • Every object is read up front and kept. objectsToPack reads every input up front (delta_selector.go#L95-L129). From storage/filesystem, a loose object below LargeObjectThreshold comes back as a whole MemoryObject, and it stays as Object/Original for the rest of the encode.
  • Undeltifying keeps a whole copy. undeltify → restoreOriginal (L215-L244) handles a reused delta whose base is not in the set, or one that is on a cycle. It replaces the delta with EncodedObject(h), which is a whole MemoryObject for a packed delta, and keeps that copy until the encode ends.
  • The window is a count, not bytes.
    • walk (L250-L299) keeps a deltaIndex in indexMap for each of up to packWindow bases. Each index is proportional to its base: one int per 16-byte block, plus the table (delta_index.go#L10-L22).
    • Only delta targets drop Original when they leave the window (L263-L266).
  • Each comparison reads both objects whole. getDelta (diff_delta.go#L33-L75) reads base and target whole into pooled bytes.Buffers through ReadFrom. That grows each buffer to about twice its object, and the buffer goes back to the pool at that size.
Reproduction (public API only)
st := filesystem.NewStorage(osfs.New(t.TempDir()), cache.NewObjectLRUDefault())
rnd := rand.New(rand.NewSource(1))
// 32 append-only versions, 64 KiB .. 2 MiB (about 33 MiB raw), stored loose.
var v []byte
var hs []plumbing.Hash
for i := 0; i < 32; i++ {
	chunk := make([]byte, 64<<10); rnd.Read(chunk)
	v = append(v, chunk...)
	o := st.NewEncodedObject(); o.SetType(plumbing.BlobObject)
	w, _ := o.Writer(); w.Write(v); w.Close()
	h, _ := st.SetEncodedObject(o); hs = append(hs, h)
}
otp, _ := packfile.NewDeltaSelector(st).ObjectsToPack(hs, 10)
// Live heap after GC, with otp still referenced: about 37 MiB (every version, whole).
// Peak during the call (sampling MemStats every ms): about 100 MiB, 3x the raw size.
What we observed in our application

Consolidating 32 packs, each holding one version of a document (0.5 to 16 MiB, 264 MiB raw):

  • ObjectsToPack(hashes, 10) alone grew the process by +476 MiB;
  • the whole encode grew it by +472 to +483 MiB.
Suggested fix

Bound the selector by bytes, as git does:

  • A window-memory limit. Stop comparing a target once the bases compared sum to W bytes, but always try at least one base.
  • A size above which an object is never a delta base or target. Like bigFileThreshold, this sets the worst pair.
  • Undeltify lazily. Keep a handle that rebuilds the object on each Reader() call, instead of holding a MemoryObject for the whole encode.
  • Compare in exact-size buffers. Size the pair's buffers from the objects, instead of ReadFrom-grown pooled buffers.

These could be exposed as DeltaSelector / EncoderOption settings.

Our workaround is a custom ObjectSelector through WithObjectSelector:

  • objects up to 256 KiB go to DeltaSelector unchanged;
  • larger ones are read as stored, never held whole;
  • their stored deltas are reused, with a visiting guard;
  • new deltas are searched one pair at a time with the exported DiffDelta, in reused exact-size buffers, with Dmax = W = 16 MiB.

The same consolidation then grew by +115 MiB. The pack was 15.15 MiB instead of 8.45 MiB, because the versions above Dmax stay whole.

Happy to send a PR if the approach is welcome.

Found while working on peircelabs/commens#1852.

Lenguaje dominante
Go
Estrellas
7.9k
Forks
1.1k
Merge medio
4 d 19 h
PR fusionados (30 d)
34

Preparar el entorno

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 go-git/go-git

Todos los issues de go-git/go-git

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.