QTranslator: Extract Method is_readable_file from find_translation()

The old code re-used a QFileInfo object, repeatedly
setting new file names with QFileInfo::setFile(QString).

But QFileInfo::setFile() simply assigns itself a
new QFileInfo object:
    *this = QFileInfo(...)
so it's just as efficient to re-create a new QFIleInfo
object for every file name under test.

To avoid extra {} for object lifetime scoping, factor
the repeated evaluation of isReadable() && isFile()
into a small helper function, which also creates and
destroys the QFileInfo object.

The deeper significance of this change is that it
avoids implicit sharing of 'realname', which is
permanently modified. A later patch will make
changes that make 'realname' re-use its capacity
through the lifetime of the find_translation()
function, and sharing the variable implicitly will
nip any auch attempts in the bud.

Force the compiler to not inline the new function.
There's really no point in spending ~0.5KiB in text
size on inlining the code; the miniscule speed
improvement is dwarfed by the memory allocation of
the QFileInfo ctor, anyway.

As a consequence, this change even saves 96b in text
size on optimized GCC 4.9 Linux AMD64 builds, even
though that wasn't even the goal.

Change-Id: I08c5cbb7b6f1ba59440a1597e28d962ce63a7c65
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Marc Mutz 2015-12-27 10:20:14 +01:00
parent 5f542f3cca
commit b20de8a23d
1 changed files with 14 additions and 15 deletions

View File

@ -613,6 +613,13 @@ bool QTranslatorPrivate::do_load(const QString &realname, const QString &directo
return false;
}
Q_NEVER_INLINE
static bool is_readable_file(const QString &name)
{
const QFileInfo fi(name);
return fi.isReadable() && fi.isFile();
}
static QString find_translation(const QLocale & locale,
const QString & filename,
const QString & prefix,
@ -626,7 +633,6 @@ static QString find_translation(const QLocale & locale,
path += QLatin1Char('/');
}
QFileInfo fi;
QString realname;
QStringList fuzzyLocales;
@ -647,13 +653,11 @@ static QString find_translation(const QLocale & locale,
localeName.replace(QLatin1Char('-'), QLatin1Char('_'));
realname = path + filename + prefix + localeName + (suffix.isNull() ? QLatin1String(".qm") : suffix);
fi.setFile(realname);
if (fi.isReadable() && fi.isFile())
if (is_readable_file(realname))
return realname;
realname = path + filename + prefix + localeName;
fi.setFile(realname);
if (fi.isReadable() && fi.isFile())
if (is_readable_file(realname))
return realname;
fuzzyLocales.append(localeName);
@ -669,32 +673,27 @@ static QString find_translation(const QLocale & locale,
localeName.truncate(rightmost);
realname = path + filename + prefix + localeName + (suffix.isNull() ? QLatin1String(".qm") : suffix);
fi.setFile(realname);
if (fi.isReadable() && fi.isFile())
if (is_readable_file(realname))
return realname;
realname = path + filename + prefix + localeName;
fi.setFile(realname);
if (fi.isReadable() && fi.isFile())
if (is_readable_file(realname))
return realname;
}
}
if (!suffix.isNull()) {
realname = path + filename + suffix;
fi.setFile(realname);
if (fi.isReadable() && fi.isFile())
if (is_readable_file(realname))
return realname;
}
realname = path + filename + prefix;
fi.setFile(realname);
if (fi.isReadable() && fi.isFile())
if (is_readable_file(realname))
return realname;
realname = path + filename;
fi.setFile(realname);
if (fi.isReadable() && fi.isFile())
if (is_readable_file(realname))
return realname;
return QString();