Mimetype optimization
Jakub Stachowski <[email protected]> Tue, 22 Apr 2008 20:06:57 +0200
| Newsgroups | gmane.comp.kde.devel.optimize |
|---|---|
| Message-ID | <[email protected]> |
--Boundary-00=_CliDIvx+XNoSGfO
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Hello,
profiling dolphin while opening /usr/bin shows that about 35% of time is spent
in QRegExp matching called by kmimefactory.cpp: matchFileName. This function
already contains optimizations: for some simple (but popular) patterns like
*.something or something* it uses direct comparision instead of QRegExp big
gun. It looks like:
if (pattern like *.something && filename long enough to match the pattern)
compare directly;.
However for short names (lots of them in /usr/bin) it often falls back to slow
path. Attached patch changes is to:
if (pattern like *.something) {
if (filename not long enough to match) return false;
else compare directly;
}
and detects patterns without wildcards (is checking for *,? and ] enough?)
Results (measured by callgrind):
Unpatched, for 179 calls to KMimeTypeFactory::findFromFileName
- 5554 calls to QRegExp::exactMatch, which accounts for 35% of CPU time
Patched, for 150 calls to KMimeTypeFactory::findFromFileName
- QRegExp matched 13 times. findFromFileName takes 0.67% of CPU time
--Boundary-00=_CliDIvx+XNoSGfO
Content-Type: text/x-diff;
charset="us-ascii";
name="mimefactory.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="mimefactory.patch"
Index: kmimetypefactory.cpp
===================================================================
--- kmimetypefactory.cpp (wersja 799070)
+++ kmimetypefactory.cpp (kopia robocza)
@@ -190,8 +190,10 @@
int len = filename.length();
// Patterns like "*~", "*.extension"
- if (pattern[0] == '*' && len + 1 >= pattern_len && pattern.indexOf('[') == -1)
+ if (pattern[0] == '*' && pattern.indexOf('[') == -1)
{
+ if ( len + 1 < pattern_len ) return false;
+
const QChar *c1 = pattern.unicode() + pattern_len - 1;
const QChar *c2 = filename.unicode() + len - 1;
int cnt = 1;
@@ -201,7 +203,8 @@
}
// Patterns like "README*" (well this is currently the only one like that...)
- if (pattern[pattern_len - 1] == '*' && len + 1 >= pattern_len) {
+ if (pattern[pattern_len - 1] == '*') {
+ if ( len + 1 < pattern_len ) return false;
if (pattern[0] == '*')
return filename.indexOf(pattern.mid(1, pattern_len - 2)) != -1;
@@ -213,7 +216,11 @@
return cnt == pattern_len;
}
- // Other patterns, like "[Mm]akefile" or "README": use slow but correct method
+ // Names without any wildcards like "README"
+ if (pattern.indexOf('[') == -1 && pattern.indexOf('*') == -1 && pattern.indexOf('?'))
+ return (pattern == filename);
+
+ // Other patterns, like "[Mm]akefile": use slow but correct method
QRegExp rx(pattern);
rx.setPatternSyntax(QRegExp::Wildcard);
return rx.exactMatch(filename);
--Boundary-00=_CliDIvx+XNoSGfO
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Kde-optimize mailing list
[email protected]
https://mail.kde.org/mailman/listinfo/kde-optimize
--Boundary-00=_CliDIvx+XNoSGfO--