Skip to content

Excludes: Use QRegularExpression for speedup WiP - #4999

Closed
guruz wants to merge 1 commit into
2.2from
exclude_traversal_qregexp
Closed

guruz wants to merge 1 commit into
2.2from
exclude_traversal_qregexp

Conversation

@guruz

@guruz guruz commented Jun 23, 2016

Copy link
Copy Markdown
Contributor

Unfortunately this does not speed up thins significantly yet, i only see csync_exclude_traversal going from 14.5% of time to 14.0% of time or so

But maybe I need to check if the JIT is really used and also add some of the other patterns that hardcodedly use fnmatch to the hook.

FYI @ckamm

It changes our exclude list to those two lists:

".*~|~\\$.*|\\.~lock\\..*|~.*\\.tmp|\\._.*|desktop\\.ini|\\..*\\.sw.|\\..*\\..*sw.|\\.fseventd|\\.apdisk|\\.htaccess|\\.directory|.*\\.part|.*\\.filepart|.*\\.crdownload|.*\\.kate-swp|.*\\.gnucash\\.tmp-.*|\\.synkron\\..*|\\.sync\\.ffs_db|\\.symform|\\.symform-store|\\.fuse_hidden.*|.*\\.unison|\\.nfs.*|My Saved Places\\."
".*\\.~.*|Icon\r.*|\\.DS_Store|\\.ds_store|Thumbs\\.db|\\.TemporaryItems|\\.Trashes|\\.DocumentRevisions-V100|\\.Trash-.*"

Maybe I can avoid having two regexp objects by using capture groups or something like that?

@mention-bot

Copy link
Copy Markdown

By analyzing the blame information on this pull request, we identified @ckamm, @dragotin and @danimo to be potential reviewers

@guruz

guruz commented Jun 23, 2016

Copy link
Copy Markdown
Contributor Author

No big change if I put qputenv("QT_ENABLE_REGEXP_JIT", "1");

}
}

static QString convertToRegexpSyntax(char *exclude)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QRegExp::escape(exclude).replace("_", "._").replace("?", ".")

@jvillafanez

Copy link
Copy Markdown
Member

Might be worthy to read: https://www.loggly.com/blog/five-invaluable-techniques-to-improve-regex-performance/

I think we can split the list into several groups:

  • For fixed files, such as "Thumbs.db" or ".DS_Store": I'd avoid using regular expressions for these files and compare as a fixed strings.
  • For files ending in something, such as "*.part": I'm not sure if leaving it as ".part$" would perform better, or if the engine would do some kind of optimization. It's worth to try it.
  • For files starting with samething: such as ".fuse_hidden.*": the "^" anchor should be used.

And according to the blog post, the order of the expression could help too

@danimo

danimo commented Jun 23, 2016

Copy link
Copy Markdown
Contributor

Note: This is not Qt4 compatible!

@guruz

guruz commented Jun 23, 2016

Copy link
Copy Markdown
Contributor Author

@jvillafanez thanks, will check it out.

FYI wrapping in ^ and $ did not help.

Maybe I should test with a bigger dataset (testing 3400 files/folders now) to make the constant overhead part be less dominant.

@guruz

guruz commented Jun 23, 2016

Copy link
Copy Markdown
Contributor Author

Oh well. I removed all the qDebug() I had added, now csync_exclude_traversal is at ~6.5% vs 14.5% before :)
I'll try to add the hardcoded patterns also into the regexp.

@dragotin

Copy link
Copy Markdown
Contributor

I have to say that I do not like this patch. Which problem are we really solving with this? The code does not really look straightforward, especially not with the hook thing.

Which code would we replace by this?

@guruz

guruz commented Jun 27, 2016

Copy link
Copy Markdown
Contributor Author

@dragotin I need to clean it up and have it tested, indeed.
For now it's a proof that we can lower the CPU load caused by the discovery by using libpcre because it compiles the excludes into a decision tree instead of going linearly over each call with a slow fnmatch system call.

@dragotin

Copy link
Copy Markdown
Contributor

@guruz there are unit tests for the exclude files, please enhance them...

@ckamm

ckamm commented Oct 26, 2016

Copy link
Copy Markdown
Contributor

@ckamm

ckamm commented Oct 28, 2016 •

Copy link
Copy Markdown
Contributor

Note, I've also done performance tests on my version. It re-uses the performance test from check_csync_exclude that runs the default list of excludes against a non-matching path a couple of thousand of times. (it's a good test case, because it's close to what happens a lot in practice)

In my tests I could optimize the regex version until it reached these values:

baseline fnmatch: csync_excluded_traversal: 0.001308 ms per call
regex version   : csync_excluded_traversal: 0.002224 ms per call

So it's still almost 2x slower. :/

The regexes that get used are approximately:

against the full path: (?!x)x|/[^/]*/[^/]*\.out$|/latex[^/]*/[^/]*\.run\.xml$|/latex/[^/]*/[^/]*\.tex\.tmp$
against the basename only: (?!x)x|^.*~$|^~\$.*$|^\.~lock\..*$|^~.*\.tmp$|^\._.*$|^desktop\.ini$|^\..*\.sw\.$|^\..*\..*sw\.$|^\.fseventd$|^\.apdisk$|^\.htaccess$|^\.directory$|^.*\.part$|^.*\.filepart$|^.*\.crdownload$|^.*\.kate-swp$|^.*\.gnucash\.tmp-.*$|^\.synkron\..*$|^\.sync\.ffs_db$|^\.symform$|^\.symform-store$|^\.fuse_hidden.*$|^.*\.unison$|^\.nfs.*$|^My Saved Places\.$|^.*\.💩$|^пятницы\..*$

There's some cost from the utf8 conversions that need to be done on the path, but being incorrect and removing them doesn't bring the runtime down much. The bname and full regexes seem to account for about 50% of the time spent, each - and most of the time is the overhead of running QRegularExpression::match -- if all regexes are replaced with trivially non-matching ones, I get 0.001571 per call.

EDIT: I had another optimization idea and reduced the number of regex match attempts from four to two (once on the basename, once on the full path) in the non-excluded case, speeding up to:

regex version   : csync_excluded_traversal: 0.001662 ms per call
regex execution overhead is 0.001044 of that

But it's still 30% slower than the fnmatch version (which stays at 0.0013)

@guruz

guruz commented Dec 6, 2016 •

Copy link
Copy Markdown
Contributor Author
regexp:
csync_excluded: 0.005359 ms per call
csync_excluded_traversal: 0.004021 ms per call
csync_excluded: 0.005415 ms per call
csync_excluded_traversal: 0.003116 ms per call
csync_excluded: 0.004884 ms per call
csync_excluded_traversal: 0.003004 ms per call
csync_excluded: 0.005025 ms per call
csync_excluded_traversal: 0.003057 ms per call


no regexp
csync_excluded: 0.486233 ms per call
csync_excluded_traversal: 0.005953 ms per call
csync_excluded: 0.504583 ms per call
csync_excluded_traversal: 0.005940 ms per call
csync_excluded: 0.498435 ms per call
csync_excluded_traversal: 0.006490 ms per call
csync_excluded: 0.495738 ms per call
csync_excluded_traversal: 0.005811 ms per call

So it improves!
For me on OS X with Qt 5.6.2 with the libpcre shipped with Qt and ckamm's branch on ckamm@34df897

We're finding out right now why @ckamm has different behaviour (worse) on Linux.

@ckamm

ckamm commented Dec 13, 2016 •

Copy link
Copy Markdown
Contributor

@guruz Here are new timings for Win7, Qt 5.3.2 in a VM:

"fnmatch"
excluded:  0.45
traversal: 0.023

regex
excluded:  0.22 (50% speedup)
traversal: 0.007 (70% speedup)

My hacked branch to make things compile on windows: https://github.com/ckamm/owncloud-client/tree/exclude_traversal_qregexp_win

@ckamm

ckamm commented Dec 14, 2016 •

Copy link
Copy Markdown
Contributor

Collected timings

table linux (ckamm) osx (guruz) win7 (ckamm) linux (ogoffart) linux2 (ckamm)
full (fnmatch) 0.038 0.5 0.45 0.031 0.039
full (regex) 0.106 0.005 0.22 0.0039 0.11
traversal (fnmatch) 0.0014 0.0060 0.023 0.0011 0.0015
traversal (regex) 0.0017 0.003 0.007 0.0017 0.0019

@ckamm

ckamm commented Jan 4, 2017

Copy link
Copy Markdown
Contributor

Note on the state of my branch at https://github.com/ckamm/owncloud-client/tree/exclude_traversal_qregexp: It does some very hacky things and has outstanding issues. It was only developed far enough for performance testing.

  • The way the csync_exclude test is #included into testexcludedfiles with preprocessor tricks isn't pretty and should be reworked.
  • I think there is an outstanding issue with / at the end of a pattern matching directories only (maybe related to * at the end of a pattern? I don't remember, but there was some issue there...)
  • More test patterns and general cleanup everywhere.

@guruz guruz added this to the 2.4.0 milestone Mar 15, 2017
@guruz

guruz commented May 24, 2017

Copy link
Copy Markdown
Contributor Author

BTW Qt 5.9 mentions

QRegularExpression now requires the PCRE2 library, at least version 10.20. Support for the PCRE1 library has been dropped. A copy of PCRE2 is shipped with Qt and will automatically be used on those platforms which lack it.

@guruz

guruz commented Sep 23, 2017

Copy link
Copy Markdown
Contributor Author

I'm trying to work on this again..

@guruz guruz removed this from the 2.4.0 milestone Sep 27, 2017
@guruz

guruz commented Sep 27, 2017

Copy link
Copy Markdown
Contributor Author

Mergeable version in #6063
There is a lot of extra things done by @ckamm in his branch in this PR, so keeping this open so we know how to improve upon this
(e.g. using regexp also for _no_ctx)

@ckamm

ckamm commented Oct 25, 2017

Copy link
Copy Markdown
Contributor

@guruz I don't think we need to keep this open. I doubt we can salvage much of the experimental code in my branch.

@ckamm ckamm closed this Nov 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants