Question: Take the following two approaches of storing a directory with a lot (several thousands) of rather small files inside a tarball:

1. Store all the files in the `root` of the tarball
2. Store the directory itself in the tarball, with the files inside the directory.

Would this have any performance effects on how fast the tar is unpacked (`tar -xf`)?

I can imagine that the second approach could be faster (possibly much faster), but I'm not aware of how tar works exactly; hence my question.

Remarks:

* Same question can be asked for packaging (`tar -cf`) but this is of less importance to me.
* Off course I can run some tests myself, but I would like some theoretical explanation if it's actually faster.

Answer: 
This largely depends on the filesystem you are using. A flat directory will be slow on ext2 and other older filesystems that need O(n) lookups to check whether a directory entry of a particular name exists. ext3/4 and other modern filesystems use tree based indexes for larger directories, so they only need O(log n) lookup time

> 
> Same question can be asked for packaging (tar -cf) but this is of less importance to me.
> 
> 
> 

Tar creation on the other hand largely depends on disk IO and whether the implementation does readaheads. Small files incur a lot of random reads and single-file readahead does not work effectively on small files. I have written [fastar](https://github.com/the8472/fastar) as a specialized implementation for this use-case that optimizes the order in which the files are read and performs readaheads across multiple files.
The difference in extraction time is not significant compared to the total time the extraction takes, at least ta scale (thousands of files). The tar format is surprisingly simple: it s basically a concatenation of header and file, header and file. So when you extract, tar just unspools the data. In particular it does not care if it is overwriting an existing file, so it doesn't burn time checking. (tarballs with absolute paths are treated slightly differently, but thats bad tar practice anyways).