From 8159590ecf86ef1cb124df9e03c9859dff0dd570 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 29 May 2025 10:53:23 +1000 Subject: [PATCH] Avoid some other lengthy lambdas in call_once --- src/core/callouts/qgscallout.cpp | 5 +- src/core/network/qgsnetworkdiskcache.cpp | 125 +++++++++--------- src/core/providers/gdal/qgsgdalprovider.cpp | 9 +- src/core/qgsapplication.cpp | 134 ++++++++++---------- src/core/qgsmaplayerelevationproperties.cpp | 5 +- src/core/raster/qgsrasterpipe.cpp | 5 +- 6 files changed, 136 insertions(+), 147 deletions(-) diff --git a/src/core/callouts/qgscallout.cpp b/src/core/callouts/qgscallout.cpp index 7e674cfbdfd..9dbc6161993 100644 --- a/src/core/callouts/qgscallout.cpp +++ b/src/core/callouts/qgscallout.cpp @@ -194,10 +194,7 @@ void QgsCallout::setEnabled( bool enabled ) QgsPropertiesDefinition QgsCallout::propertyDefinitions() { static std::once_flag initialized; - std::call_once( initialized, [ = ]( ) - { - initPropertyDefinitions(); - } ); + std::call_once( initialized, initPropertyDefinitions ); return sPropertyDefinitions; } diff --git a/src/core/network/qgsnetworkdiskcache.cpp b/src/core/network/qgsnetworkdiskcache.cpp index d54d64664ec..e2680dc1453 100644 --- a/src/core/network/qgsnetworkdiskcache.cpp +++ b/src/core/network/qgsnetworkdiskcache.cpp @@ -123,75 +123,76 @@ void QgsNetworkDiskCache::clear() return sDiskCache.clear(); } -qint64 QgsNetworkDiskCache::smartCacheSize( const QString &cacheDir ) +void determineSmartCacheSize( const QString &cacheDir, qint64 &cacheSize ) { - static qint64 cacheSize = 0; - static std::once_flag initialized; - std::call_once( initialized, [ = ] + std::function dirSize; + dirSize = [&dirSize]( const QString & dirPath ) -> qint64 { - std::function dirSize; - dirSize = [&dirSize]( const QString & dirPath ) -> qint64 + qint64 size = 0; + QDir dir( dirPath ); + + const QStringList filePaths = dir.entryList( QDir::Files | QDir::System | QDir::Hidden ); + for ( const QString &filePath : filePaths ) { - qint64 size = 0; - QDir dir( dirPath ); - - const QStringList filePaths = dir.entryList( QDir::Files | QDir::System | QDir::Hidden ); - for ( const QString &filePath : filePaths ) - { - QFileInfo fi( dir, filePath ); - size += fi.size(); - } - - const QStringList childDirPaths = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::NoSymLinks ); - for ( const QString &childDirPath : childDirPaths ) - { - size += dirSize( dirPath + QDir::separator() + childDirPath ); - } - - return size; - }; - - qint64 bytesFree; - QStorageInfo storageInfo( cacheDir ); - bytesFree = storageInfo.bytesFree() + dirSize( cacheDir ); - - // NOLINTBEGIN(bugprone-narrowing-conversions) - // Logic taken from Firefox's smart cache size handling - qint64 available10MB = bytesFree / 1024 / ( 1024LL * 10 ); - qint64 cacheSize10MB = 0; - if ( available10MB > 2500 ) - { - // Cap the cache size to 1GB - cacheSize10MB = 100; + QFileInfo fi( dir, filePath ); + size += fi.size(); } - else + + const QStringList childDirPaths = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::NoSymLinks ); + for ( const QString &childDirPath : childDirPaths ) { - if ( available10MB > 700 ) - { - // Add 2.5% of the free space above 7GB - cacheSize10MB += ( available10MB - 700 ) * 0.025; - available10MB = 700; - } - if ( available10MB > 50 ) - { - // Add 7.5% of free space between 500MB to 7GB - cacheSize10MB += ( available10MB - 50 ) * 0.075; - available10MB = 50; - } + size += dirSize( dirPath + QDir::separator() + childDirPath ); + } + + return size; + }; + + qint64 bytesFree; + QStorageInfo storageInfo( cacheDir ); + bytesFree = storageInfo.bytesFree() + dirSize( cacheDir ); + + // NOLINTBEGIN(bugprone-narrowing-conversions) + // Logic taken from Firefox's smart cache size handling + qint64 available10MB = bytesFree / 1024 / ( 1024LL * 10 ); + qint64 cacheSize10MB = 0; + if ( available10MB > 2500 ) + { + // Cap the cache size to 1GB + cacheSize10MB = 100; + } + else + { + if ( available10MB > 700 ) + { + // Add 2.5% of the free space above 7GB + cacheSize10MB += ( available10MB - 700 ) * 0.025; + available10MB = 700; + } + if ( available10MB > 50 ) + { + // Add 7.5% of free space between 500MB to 7GB + cacheSize10MB += ( available10MB - 50 ) * 0.075; + available10MB = 50; + } #if defined( Q_OS_ANDROID ) - // On Android, smaller/older devices may have very little storage + // On Android, smaller/older devices may have very little storage - // Add 16% of free space up to 500 MB - cacheSize10MB += std::max( 2LL, static_cast( available10MB * 0.16 ) ); -#else - // Add 30% of free space up to 500 MB - cacheSize10MB += std::max( 5LL, static_cast( available10MB * 0.30 ) ); + // Add 16% of free space up to 500 MB + cacheSize10MB += std::max( 2LL, static_cast( available10MB * 0.16 ) ); +#else \ + // Add 30% of free space up to 500 MB + cacheSize10MB += std::max( 5LL, static_cast( available10MB * 0.30 ) ); #endif - } - cacheSize = cacheSize10MB * 10 * 1024 * 1024; - // NOLINTEND(bugprone-narrowing-conversions) - } ); - - return cacheSize; + } + cacheSize = cacheSize10MB * 10 * 1024 * 1024; + // NOLINTEND(bugprone-narrowing-conversions) +} + +qint64 QgsNetworkDiskCache::smartCacheSize( const QString &cacheDir ) +{ + static qint64 sCacheSize = 0; + static std::once_flag initialized; + std::call_once( initialized, determineSmartCacheSize, cacheDir, sCacheSize ); + return sCacheSize; } diff --git a/src/core/providers/gdal/qgsgdalprovider.cpp b/src/core/providers/gdal/qgsgdalprovider.cpp index 57863f0edb8..bb9db99bf22 100644 --- a/src/core/providers/gdal/qgsgdalprovider.cpp +++ b/src/core/providers/gdal/qgsgdalprovider.cpp @@ -2961,7 +2961,7 @@ void buildSupportedRasterFileFilterAndExtensions( QString &fileFiltersString, QS QgsDebugMsgLevel( "Raster filter list built: " + fileFiltersString, 2 ); QgsDebugMsgLevel( "Raster extension list built: " + extensions.join( ' ' ), 2 ); -} // buildSupportedRasterFileFilter_() +} bool QgsGdalProvider::isValidRasterFileName( QString const &fileNameQString, QString &retErrMsg ) { @@ -4429,12 +4429,7 @@ QList QgsGdalProviderMetadata::querySublayers( const // get supported extensions static std::once_flag initialized; - std::call_once( initialized, [ = ] - { - buildSupportedRasterFileFilterAndExtensions( sFilterString, sExtensions, sWildcards ); - QgsDebugMsgLevel( QStringLiteral( "extensions: " ) + sExtensions.join( ' ' ), 2 ); - QgsDebugMsgLevel( QStringLiteral( "wildcards: " ) + sWildcards.join( ' ' ), 2 ); - } ); + std::call_once( initialized, buildSupportedRasterFileFilterAndExtensions, sFilterString, sExtensions, sWildcards ); const QString suffix = uriParts.value( QStringLiteral( "vsiSuffix" ) ).toString().isEmpty() ? pathInfo.suffix().toLower() diff --git a/src/core/qgsapplication.cpp b/src/core/qgsapplication.cpp index dcb59cb4cbf..3890982c194 100644 --- a/src/core/qgsapplication.cpp +++ b/src/core/qgsapplication.cpp @@ -226,6 +226,73 @@ QgsApplication::QgsApplication( int &argc, char **argv, bool GUIenabled, const Q } +void registerMetaTypes() +{ + qRegisterMetaType( "QgsGeometry::Error" ); + qRegisterMetaType( "QgsDatabaseQueryLogEntry" ); + qRegisterMetaType( "QgsProcessingFeatureSourceDefinition" ); + qRegisterMetaType( "QgsProcessingOutputLayerDefinition" ); + qRegisterMetaType( "Qgis::LayoutUnit" ); + qRegisterMetaType( "QgsUnsetAttributeValue" ); + qRegisterMetaType( "QgsFeatureId" ); + qRegisterMetaType( "QgsFields" ); + qRegisterMetaType( "QgsFeatureIds" ); + qRegisterMetaType( "QgsProperty" ); + qRegisterMetaType( "QgsFeatureStoreList" ); + qRegisterMetaType( "Qgis::MessageLevel" ); + qRegisterMetaType( "Qgis::BrowserItemState" ); + qRegisterMetaType( "Qgis::GpsFixStatus" ); + qRegisterMetaType( "QgsReferencedRectangle" ); + qRegisterMetaType( "QgsReferencedPointXY" ); + qRegisterMetaType( "QgsReferencedGeometry" ); + qRegisterMetaType( "QgsLayoutRenderContext::Flags" ); + qRegisterMetaType( "QgsStyle::StyleEntity" ); + qRegisterMetaType( "QgsCoordinateReferenceSystem" ); + qRegisterMetaType( "QgsAuthManager::MessageLevel" ); + qRegisterMetaType( "QgsNetworkRequestParameters" ); + qRegisterMetaType( "QgsNetworkReplyContent" ); + qRegisterMetaType( "QgsFeature" ); + qRegisterMetaType( "QgsGeometry" ); + qRegisterMetaType( "QgsInterval" ); + qRegisterMetaType( "QgsRectangle" ); + qRegisterMetaType( "QgsPointXY" ); + qRegisterMetaType( "QgsPoint" ); + qRegisterMetaType( "QgsDatumTransform::GridDetails" ); + qRegisterMetaType( "QgsDatumTransform::TransformDetails" ); + qRegisterMetaType( "QgsNewsFeedParser::Entry" ); + qRegisterMetaType( "QgsRectangle" ); + qRegisterMetaType( "QgsLocatorResult" ); + qRegisterMetaType( "QgsGradientColorRamp" ); + qRegisterMetaType( "QgsProcessingModelChildParameterSource" ); +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + // Qt6 documentation says these are not needed anymore (https://www.qt.io/blog/whats-new-in-qmetatype-qvariant) #spellok + // TODO: when tests can run against Qt6 builds, check for any regressions + qRegisterMetaTypeStreamOperators( "QgsProcessingModelChildParameterSource" ); +#endif + qRegisterMetaType( "QgsRemappingSinkDefinition" ); + qRegisterMetaType( "QgsProcessingModelChildDependency" ); + qRegisterMetaType( "QgsTextFormat" ); +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + QMetaType::registerComparators(); + QMetaType::registerEqualsComparator(); + QMetaType::registerEqualsComparator(); + QMetaType::registerEqualsComparator(); + QMetaType::registerEqualsComparator(); + QMetaType::registerEqualsComparator(); +#endif + qRegisterMetaType( "QPainter::CompositionMode" ); + qRegisterMetaType( "QgsDateTimeRange" ); + qRegisterMetaType( "QgsDoubleRange" ); + qRegisterMetaType( "QgsIntRange" ); + qRegisterMetaType>( "QList" ); + qRegisterMetaType>( "QMap" ); + qRegisterMetaType>( "QMap" ); + qRegisterMetaType>( "QList" ); + qRegisterMetaType< QAuthenticator * >( "QAuthenticator*" ); + qRegisterMetaType< QgsGpsInformation >( "QgsGpsInformation" ); + qRegisterMetaType< QgsSensorThingsExpansionDefinition >( "QgsSensorThingsExpansionDefinition" ); +}; + void QgsApplication::init( QString profileFolder ) { // Initialize application members in desktop app (at this point, profile folder is known) @@ -257,72 +324,7 @@ void QgsApplication::init( QString profileFolder ) *sProfilePath() = profileFolder; static std::once_flag sMetaTypesRegistered; - std::call_once( sMetaTypesRegistered, [] - { - qRegisterMetaType( "QgsGeometry::Error" ); - qRegisterMetaType( "QgsDatabaseQueryLogEntry" ); - qRegisterMetaType( "QgsProcessingFeatureSourceDefinition" ); - qRegisterMetaType( "QgsProcessingOutputLayerDefinition" ); - qRegisterMetaType( "Qgis::LayoutUnit" ); - qRegisterMetaType( "QgsUnsetAttributeValue" ); - qRegisterMetaType( "QgsFeatureId" ); - qRegisterMetaType( "QgsFields" ); - qRegisterMetaType( "QgsFeatureIds" ); - qRegisterMetaType( "QgsProperty" ); - qRegisterMetaType( "QgsFeatureStoreList" ); - qRegisterMetaType( "Qgis::MessageLevel" ); - qRegisterMetaType( "Qgis::BrowserItemState" ); - qRegisterMetaType( "Qgis::GpsFixStatus" ); - qRegisterMetaType( "QgsReferencedRectangle" ); - qRegisterMetaType( "QgsReferencedPointXY" ); - qRegisterMetaType( "QgsReferencedGeometry" ); - qRegisterMetaType( "QgsLayoutRenderContext::Flags" ); - qRegisterMetaType( "QgsStyle::StyleEntity" ); - qRegisterMetaType( "QgsCoordinateReferenceSystem" ); - qRegisterMetaType( "QgsAuthManager::MessageLevel" ); - qRegisterMetaType( "QgsNetworkRequestParameters" ); - qRegisterMetaType( "QgsNetworkReplyContent" ); - qRegisterMetaType( "QgsFeature" ); - qRegisterMetaType( "QgsGeometry" ); - qRegisterMetaType( "QgsInterval" ); - qRegisterMetaType( "QgsRectangle" ); - qRegisterMetaType( "QgsPointXY" ); - qRegisterMetaType( "QgsPoint" ); - qRegisterMetaType( "QgsDatumTransform::GridDetails" ); - qRegisterMetaType( "QgsDatumTransform::TransformDetails" ); - qRegisterMetaType( "QgsNewsFeedParser::Entry" ); - qRegisterMetaType( "QgsRectangle" ); - qRegisterMetaType( "QgsLocatorResult" ); - qRegisterMetaType( "QgsGradientColorRamp" ); - qRegisterMetaType( "QgsProcessingModelChildParameterSource" ); -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - // Qt6 documentation says these are not needed anymore (https://www.qt.io/blog/whats-new-in-qmetatype-qvariant) #spellok - // TODO: when tests can run against Qt6 builds, check for any regressions - qRegisterMetaTypeStreamOperators( "QgsProcessingModelChildParameterSource" ); -#endif - qRegisterMetaType( "QgsRemappingSinkDefinition" ); - qRegisterMetaType( "QgsProcessingModelChildDependency" ); - qRegisterMetaType( "QgsTextFormat" ); -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - QMetaType::registerComparators(); - QMetaType::registerEqualsComparator(); - QMetaType::registerEqualsComparator(); - QMetaType::registerEqualsComparator(); - QMetaType::registerEqualsComparator(); - QMetaType::registerEqualsComparator(); -#endif - qRegisterMetaType( "QPainter::CompositionMode" ); - qRegisterMetaType( "QgsDateTimeRange" ); - qRegisterMetaType( "QgsDoubleRange" ); - qRegisterMetaType( "QgsIntRange" ); - qRegisterMetaType>( "QList" ); - qRegisterMetaType>( "QMap" ); - qRegisterMetaType>( "QMap" ); - qRegisterMetaType>( "QList" ); - qRegisterMetaType< QAuthenticator * >( "QAuthenticator*" ); - qRegisterMetaType< QgsGpsInformation >( "QgsGpsInformation" ); - qRegisterMetaType< QgsSensorThingsExpansionDefinition >( "QgsSensorThingsExpansionDefinition" ); - } ); + std::call_once( sMetaTypesRegistered, registerMetaTypes ); ( void ) resolvePkgPath(); diff --git a/src/core/qgsmaplayerelevationproperties.cpp b/src/core/qgsmaplayerelevationproperties.cpp index 59eeb08ad54..9bb560fda30 100644 --- a/src/core/qgsmaplayerelevationproperties.cpp +++ b/src/core/qgsmaplayerelevationproperties.cpp @@ -124,10 +124,7 @@ void QgsMapLayerElevationProperties::setDataDefinedProperties( const QgsProperty QgsPropertiesDefinition QgsMapLayerElevationProperties::propertyDefinitions() { static std::once_flag initialized; - std::call_once( initialized, [ = ]( ) - { - initPropertyDefinitions(); - } ); + std::call_once( initialized, initPropertyDefinitions ); return sPropertyDefinitions; } diff --git a/src/core/raster/qgsrasterpipe.cpp b/src/core/raster/qgsrasterpipe.cpp index c835bb84a95..97c927fe0b6 100644 --- a/src/core/raster/qgsrasterpipe.cpp +++ b/src/core/raster/qgsrasterpipe.cpp @@ -454,9 +454,6 @@ void QgsRasterPipe::initPropertyDefinitions() QgsPropertiesDefinition QgsRasterPipe::propertyDefinitions() { static std::once_flag initialized; - std::call_once( initialized, [ = ]( ) - { - initPropertyDefinitions(); - } ); + std::call_once( initialized, initPropertyDefinitions ); return sPropertyDefinitions; }