Fix O(n^2) time bug in --delay-directory-restore

delayed_set_stat avoids inserting duplicate entries into
delayed_set_stat_head. It was doing this by scanning the entire
list.

Normally this list is small, but if --delay-directory-restore is
used (including automatically for incremental archives), this list
grows with the total number of directories in the archive.

The entire scan takes O(n) time. Extracting an archive with n
directories could therefore take O(n^2) time.

The included test uses AT_SKIP_LARGE_FILES, allowing it to optionally be
skipped. It may execute slowly on certain filesystems or disks, as it
creates thousands of directories.

There are still potentially problematic O(n) scans in
find_direct_ancestor and remove_delayed_set_stat, which this patch does
not attempt to fix.

* NEWS: Update.
* src/extract.c (delayed_set_stat_table): Create a table for O(1)
lookups of entries in the delayed_set_stat_head list. The list
remains, as tracking insertion order is important.
(dl_hash, dl_compare): New hash table helper functions.
(delay_set_stat): Create the hash table, replace the O(n) list scan
with a hash_lookup, insert new entries into the hash table.
(remove_delayed_set_stat): Also remove entry from hash table.
(apply_nonancestor_delayed_set_stat): Also remove entry from hash
table.
(extract_finish): Free the (empty) hash table.
* tests/extrac26.at: New file.
* tests/Makefile.am (TESTSUITE_AT): Include extrac26.at.
* tests/testsuite.at: Include extrac26.at.
This commit is contained in:
Benjamin Woodruff
2023-08-21 13:42:14 -07:00
committed by Paul Eggert
parent bfee1d44a3
commit a5afb36765
5 changed files with 84 additions and 5 deletions
+1
View File
@@ -133,6 +133,7 @@ TESTSUITE_AT = \
extrac23.at\
extrac24.at\
extrac25.at\
extrac26.at\
filerem01.at\
filerem02.at\
grow.at\
+43
View File
@@ -0,0 +1,43 @@
# Test suite for GNU tar. -*- autotest -*-
# Copyright 2022-2023 Free Software Foundation, Inc.
#
# This file is part of GNU tar.
#
# GNU tar is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# GNU tar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
AT_SETUP([extract a large directory tree with --delay-directory-restore])
AT_KEYWORDS([extract extrac26])
AT_TAR_CHECK([
AT_SKIP_LARGE_FILES
AT_TIMEOUT_PREREQ
echo Creating dirtree
awk 'BEGIN { for (j = 0; j < 300; j++) for (k = 0; k < 300; k++) print "dirtree/" j "/" k }' | \
xargs mkdir -p
echo Creating archive
tar -cf archive.tar dirtree
echo Extracting archive
mkdir output
timeout 15 tar -xf archive.tar --delay-directory-restore -C output
],
[0],
[Creating dirtree
Creating archive
Extracting archive
],
[],[],[],[gnu])
AT_CLEANUP
+1
View File
@@ -349,6 +349,7 @@ m4_include([extrac22.at])
m4_include([extrac23.at])
m4_include([extrac24.at])
m4_include([extrac25.at])
m4_include([extrac26.at])
m4_include([backup01.at])