packfile.Parser (not low-memory, with a storage) keeps every delta's buffer after writing it to storage — intended?

Aberta
#2,575 1 comentário 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 3 dias

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
55/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
go
Domínio
backend, performance

Direção de pesquisa

Start with plumbing/format/packfile/parser.go, especially storeOrCache, parentReader, and the storage capability checks; then read parser_options.go for WithHighMemoryMode. Reproduce the reported memory growth with a file-backed storer and a delta chain, and inspect existing packfile parser tests. Done means the chosen buffer behavior is verified for both high- and low-memory modes without breaking delta resolution.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

go-git version: v6.0.0-beta.1. Unchanged on main at 9decb13c (2026-11-06).

This may be expected behaviour. We're asking because what the parser keeps does not seem to serve as a cache.

What happens

When a Parser has a storage and is not in low-memory mode, it still holds about the raw size of every resolved delta and base until Parse returns. This holds even though it has written every delta to the storage. That is the case for packfile.UpdateObjectStorage with a storer that is not a PackfileWriter (e.g. memory.Storage), or for any storer that is not LowMemoryCapable.

Why

storeOrCache (parser.go#L130-L166; main L132-L168) works like this:

  1. It copies the resolved delta to storage with ioutil.CopyBufferPool(w, oh.content). oh.content is a bytes.Buffer, so io.CopyBuffer uses its WriteTo, which drains it: length 0, capacity kept.
  2. Outside low-memory mode it does not release the buffer, so p.cache.Add(oh) keeps that capacity.
  3. When a child of oh is resolved, parentReader finds parent.content.Len() == 0 (parser.go#L473). It reads the object back from storage into the same buffer (L481-L496), and the buffer then stays full until the parse ends.

So the parser holds a cache-sized amount of memory, but every child's base is still fetched from storage.

WithHighMemoryMode documents that delta contents are cached (parser_options.go#L50-L59), so keeping them may be intended. The drain seems not to be.

Possibly related: WithHighMemoryMode sets p.lowMemoryMode = false in the options loop, and NewParser then overwrites the field from the storage's LowMemoryMode() (parser.go#L99-L119). The option therefore looks like a no-op. We may be misreading it.

Reproduction (public API only)
  1. Build a ~2 MiB pack of 32 versions of a 2 MiB blob, as a delta chain (memory.Storage + packfile.NewEncoder(...).Encode(hashes, 10)).
  2. Parse it with packfile.WithStorage(s). s is an EncodedObjectStorer that appends RawObjectWriter output to a temp file and serves EncodedObject from there, so the storer itself holds nothing in memory.
  3. Add an Observer whose OnInflatedObjectContent runs runtime.GC() and reads MemStats.HeapAlloc at the last object.
s Live heap at the last object
no LowMemoryMode method 64 MiB (126 MiB if its readers are not io.WriterTo; that part is a separate issue)
LowMemoryMode() == true 0 MiB
What we observed in our application

We index packs through a file-backed storage. When it declines low-memory mode, indexing a chain of 32 × 2 MiB versions grows the process by +64 MiB, against +6 to +9 MiB in low-memory mode.

Question / suggested fix

If the intent is a cache, one option is to copy to storage without draining, e.g. CopyBufferPool(w, bytes.NewReader(oh.content.Bytes())), so the cached bytes actually serve the children.

If the storage is meant to be the source of truth, the other option is to release oh.content after the copy, as low-memory mode does.

With the second option, memory.Storage users would also stop holding each delta twice. We're happy to send a PR for whichever you prefer.

Found while working on peircelabs/commens#1852.

Linguagem predominante
Go
Estrelas
7.9k
Forks
1.1k
Merge médio
4d 19h
PRs com merge (30d)
34

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de go-git/go-git

Todas as issues de go-git/go-git

Issues semelhantes

Mais issues de Go

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.