Add fast-path in QLibraryStore::findOrCreate() for !instance()

If there's no QLibraryStore::instance(), then there's no LibraryMap,
so no need to construct a mapName we know we'll just throw away.

We must keep locking the qt_library_mutex to check instance(), but we
can drop it immediately if we find there isn't, and construct the
QLibraryPrivate object outside the critical section (the hope is that
the compiler sees this as an opportunity to tail-call).

The final goal of this exercise is to get rid of the double-lookup in
LibraryMap.

Pick-to: 6.6 6.5
Change-Id: I181dd2657e831a37e2d6f1c5d4df7e2a25ac48cd
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit c9b967437df344b5fc95121a3560a34bf352c696)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-02-07 21:52:01 +01:00 committed by Qt Cherry-pick Bot
parent 219fbe7b21
commit 0fe424a9ae

View File

@ -407,23 +407,24 @@ inline QLibraryPrivate *QLibraryStore::findOrCreate(const QString &fileName, con
QMutexLocker locker(&qt_library_mutex);
QLibraryStore *data = instance();
if (Q_UNLIKELY(!data)) {
locker.unlock();
return lazyNewLib();
}
QString mapName = version.isEmpty() ? fileName : fileName + u'\0' + version;
// check if this library is already loaded
QLibraryPrivate *lib = nullptr;
if (Q_LIKELY(data)) {
lib = data->libraryMap.value(mapName);
if (lib) {
lib->libraryRefCount.ref();
lib->mergeLoadHints(loadHints);
}
}
if (!lib)
QLibraryPrivate *lib = data->libraryMap.value(mapName);
if (lib) {
lib->libraryRefCount.ref();
lib->mergeLoadHints(loadHints);
} else {
lib = lazyNewLib();
}
// track this library
if (Q_LIKELY(data))
data->libraryMap.insert(mapName, lib);
data->libraryMap.insert(mapName, lib);
return lib;
}