pin_upload memory use grows with file size

Aberta Para iniciantes
#367 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
78/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
python
Domínio
data

Direção de pesquisa

Comece lendo Version.from_files e Version.hash_file, que a issue identifica como a provável origem do pico de memória. Verifique como block_size é usado e por que seu valor padrão é -1, depois execute os testes pertinentes de hash e upload. O trabalho está concluído quando o upload de um arquivo grande usa uma quantidade de memória aproximadamente constante, preservando o hash de versão esperado do pin.

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

Descrição

Scenario

I use Polars to process data frames that are larger than available memory. Polars streams the work and writes the result straight to a parquet file on disk, so the data is never fully in memory. The file is published with pin_upload to an S3 board.

We expected the upload to stream the file as well. Instead, memory use during pin_upload grows with the file size. Our largest file is larger than available memory and the process is killed during the upload.

From a quick look, the spike seems to come from computing the pin version hash. Version.from_files calls Version.hash_file without a block_size, and the default of -1 reads the whole file in one call. hash_file already loops over chunks, so passing a fixed block_size, or changing the default, might be enough. I may be missing context on why the default is -1.

Reproducible example

import os
import resource
import tempfile

import pins

with tempfile.TemporaryDirectory() as d:
    path = os.path.join(d, "big.bin")
    with open(path, "wb") as f:
        for _ in range(1024):  # 1 GiB, written in 1 MiB chunks
            f.write(os.urandom(1024**2))

    before = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    board = pins.board_temp()
    board.pin_upload(paths=[path], name="big")
    after = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    print(f"peak RSS before: {before}, after: {after}")  # grows by about 1 GiB

On macOS (ru_maxrss is in bytes), peak RSS grows from about 35 MiB to about 1,059 MiB. When hash_file is patched to use an 8 MiB block_size, the peak only reaches about 50 MiB.

Expected behavior

Uploading a file should use roughly constant memory regardless of file size.

Environment

  • pins 0.9.2
  • Python 3.12
  • S3 board through s3fs
Linguagem predominante
Python
Estrelas
59
Forks
11
Métricas de merge de PRs
Nenhum PR com merge em 30d

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 rstudio/pins-python

Todas as issues de rstudio/pins-python

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

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