From efe11290ae3c71d2a7a3735c265ff22a0cf92921 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 9 Sep 2018 15:25:05 +0200 Subject: PathTimeTable/DB: Improve performance by removing usage of regex in hot places This cuts execution time on my workstation from about 85 seconds for an --update-cache adding one archive to roughly 62 seconds. Measured totally unscientifically by simply running the full application instead of isolating the code, but it makes sense that regexes are slower so I'll take it. Signed-off-by: Florian Pritz --- lib/App/BorgRestore/PathTimeTable/DB.pm | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/App/BorgRestore/PathTimeTable/DB.pm b/lib/App/BorgRestore/PathTimeTable/DB.pm index 1a24999..b0c0bb8 100644 --- a/lib/App/BorgRestore/PathTimeTable/DB.pm +++ b/lib/App/BorgRestore/PathTimeTable/DB.pm @@ -43,15 +43,19 @@ method set_archive_id($archive_id) { method add_path($path, $time) { $self->{stats}->{total_paths}++; my $old_cache_path = $self->{current_path_in_cache}; - while ($old_cache_path =~ m#/#) { - unless ($path =~ m#^\Q$old_cache_path\E/#) { - delete $self->{cache}->{$old_cache_path}; + while ((my $slash_index = rindex($old_cache_path, "/")) != -1) { + $self->{stats}->{cache_invalidation_loop_iterations}++; + if ($old_cache_path eq substr($path, 0, length($old_cache_path))) { + # keep the cache content for the part of the path that stays the same + last; } - $old_cache_path =~ s|/[^/]*$||; + delete $self->{cache}->{$old_cache_path}; + # strip last part of path + $old_cache_path = substr($old_cache_path, 0, $slash_index); } my $full_path = $path; - while ($path =~ m#/#) { + while ((my $slash_index = rindex($path, "/")) != -1) { $self->_add_path_to_db($self->{archive_id}, $path, $time); my $cached = $self->{cache}->{$path}; if ($path ne $full_path && (!defined $cached || $cached < $time)) { @@ -60,7 +64,7 @@ method add_path($path, $time) { #$log->tracef("Setting cache time for path '%s' to %d", $path, $time); $self->{cache}->{$path} = $time; } - $path =~ s|/[^/]*$||; + $path = substr($path, 0, $slash_index); } $self->_add_path_to_db($self->{archive_id}, $path, $time) unless $path eq "."; my $cached = $self->{cache}->{$path}; -- cgit v1.2.3-24-g4f1b