New submission from mxmlnkn <maxinator...@googlemail.com>:

Consider this example replicating a real use case where I was downloading the 
1.191TiB ImageNet in sequential order for ~1GiB in order to preview it:

echo "foo" > bar
tar cf sparse.tar bar


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import tarfile
import time

t0 = time.time()
for tarInfo in tarfile.open( 'sparse.tar', 'r:', ignore_zeros = True ):
    pass
t1 = time.time()
print( f"Small TAR took {t1 - t0}s to iterate over" )

f = open( 'sparse.tar', 'wb' )
f.truncate( 2*1024*1024*1024 )
f.close()

t0 = time.time()
for tarInfo in tarfile.open( 'sparse.tar', 'r:', ignore_zeros = True ):
    pass
t1 = time.time()
print( f"Small TAR with sparse tail took {t1 - t0}s to iterate over" )


Output:

Small TAR took 0.00020813941955566406s to iterate over
Small TAR with sparse tail took 6.999570846557617s to iterate over


So, iterating over sparse holes takes tarfile ~300MiB/s. Which sounds fast but 
is really slow for 1.2TiB and when thinking about it as tarfile doing basically 
>nothing<.

There should be better options like using os.lseek with os.SEEK_DATA if 
available to skip those empty holes.

An alternative would be an option to tell tarfile how many zeros it should at 
maximum skip.
Personally, I only use the ignore_zeros option to be able to work with 
concatenated TARs, which in my case only have up to 19*512 byte empty tar 
blocks to be skipped. Anything longer would indicate an invalid file. I'm aware 
that these maximum runs of zeros vary depending on the tar blocking factor, so 
it should be adjustable.

----------
messages: 370611
nosy: mxmlnkn
priority: normal
severity: normal
status: open
title: tarfile: ignore_zeros = True exceedingly slow on a sparse tar file
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40843>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to