@@ -694,6 +694,33 @@ std::string makeIncludeSessionCacheKey(const system::path& requestingSourceDir,
694694 return key;
695695}
696696
697+ auto lookupIncludeSessionCache (IShaderCompiler::CIncludeFinder::SSessionCache* readCache, IShaderCompiler::CIncludeFinder::SSessionCache* writeCache, const std::string& key, IShaderCompiler::IIncludeLoader::found_t & result) -> IShaderCompiler::CIncludeFinder::SSessionCache::LookupResult
698+ {
699+ using LookupResult = IShaderCompiler::CIncludeFinder::SSessionCache::LookupResult;
700+ const auto lookupResult = readCache ? readCache->lookup (key, result) : LookupResult::Miss;
701+ if (readCache && writeCache && readCache != writeCache)
702+ {
703+ switch (lookupResult)
704+ {
705+ case LookupResult::Found:
706+ writeCache->store (key, result);
707+ break ;
708+ case LookupResult::Missing:
709+ writeCache->store (key, {});
710+ break ;
711+ case LookupResult::Miss:
712+ break ;
713+ }
714+ }
715+ return lookupResult;
716+ }
717+
718+ void writeIncludeSessionCache (IShaderCompiler::CIncludeFinder::SSessionCache* writeCache, const std::string& key, const IShaderCompiler::IIncludeLoader::found_t & result)
719+ {
720+ if (writeCache)
721+ writeCache->store (key, result);
722+ }
723+
697724}
698725
699726void IShaderCompiler::CIncludeFinder::SSessionCache::clear ()
@@ -711,11 +738,16 @@ auto IShaderCompiler::CIncludeFinder::SSessionCache::lookup(const std::string& k
711738 {
712739 if (const auto foundIt = found.find (key); foundIt != found.end ())
713740 {
741+ ++stats.lookupFound ;
714742 result = foundIt->second ;
715743 return LookupResult::Found;
716744 }
717745 if (missing.contains (key))
746+ {
747+ ++stats.lookupMissing ;
718748 return LookupResult::Missing;
749+ }
750+ ++stats.lookupMiss ;
719751 return LookupResult::Miss;
720752 });
721753}
@@ -726,9 +758,23 @@ void IShaderCompiler::CIncludeFinder::SSessionCache::store(const std::string& ke
726758 {
727759 missing.erase (key);
728760 if (result)
761+ {
762+ ++stats.storeFound ;
729763 found.insert_or_assign (key, std::move (result));
764+ }
730765 else
766+ {
767+ ++stats.storeMissing ;
731768 missing.insert (key);
769+ }
770+ });
771+ }
772+
773+ auto IShaderCompiler::CIncludeFinder::SSessionCache::snapshotStats () const -> Stats
774+ {
775+ return withSessionCacheLock (const_cast <SSessionCache*>(this ), [&]() -> Stats
776+ {
777+ return stats;
732778 });
733779}
734780
@@ -744,12 +790,12 @@ IShaderCompiler::CIncludeFinder::CIncludeFinder(core::smart_refctd_ptr<system::I
744790// @param requestingSourceDir: the directory where the incude was requested
745791// @param includeName: the string within <> of the include preprocessing directive
746792// @param
747- auto IShaderCompiler::CIncludeFinder::getIncludeStandard (const system::path& requestingSourceDir, const std::string& includeName, bool needHash, SSessionCache* sessionCache ) const -> IIncludeLoader::found_t
793+ auto IShaderCompiler::CIncludeFinder::getIncludeStandard (const system::path& requestingSourceDir, const std::string& includeName, bool needHash, SSessionCache* readSessionCache, SSessionCache* writeSessionCache ) const -> IIncludeLoader::found_t
748794{
749795 const auto lookupName = normalizeIncludeLookupName (includeName);
750796 const auto cacheKey = makeIncludeSessionCacheKey (requestingSourceDir, lookupName, needHash, ' S' );
751797 IShaderCompiler::IIncludeLoader::found_t retVal;
752- switch (sessionCache ? sessionCache-> lookup ( cacheKey, retVal) : SSessionCache::LookupResult::Miss )
798+ switch (lookupIncludeSessionCache (readSessionCache, writeSessionCache, cacheKey, retVal))
753799 {
754800 case SSessionCache::LookupResult::Found:
755801 return retVal;
@@ -774,20 +820,19 @@ auto IShaderCompiler::CIncludeFinder::getIncludeStandard(const system::path& req
774820 hasher.update (reinterpret_cast <uint8_t *>(retVal.contents .data ()), retVal.contents .size () * (sizeof (char ) / sizeof (uint8_t )));
775821 retVal.hash = static_cast <core::blake3_hash_t >(hasher);
776822 }
777- if (sessionCache)
778- sessionCache->store (cacheKey, retVal);
823+ writeIncludeSessionCache (writeSessionCache, cacheKey, retVal);
779824 return retVal;
780825}
781826
782827// ! includes within ""
783828// @param requestingSourceDir: the directory where the incude was requested
784829// @param includeName: the string within "" of the include preprocessing directive
785- auto IShaderCompiler::CIncludeFinder::getIncludeRelative (const system::path& requestingSourceDir, const std::string& includeName, bool needHash, SSessionCache* sessionCache ) const -> IIncludeLoader::found_t
830+ auto IShaderCompiler::CIncludeFinder::getIncludeRelative (const system::path& requestingSourceDir, const std::string& includeName, bool needHash, SSessionCache* readSessionCache, SSessionCache* writeSessionCache ) const -> IIncludeLoader::found_t
786831{
787832 const auto lookupName = normalizeIncludeLookupName (includeName);
788833 const auto cacheKey = makeIncludeSessionCacheKey (requestingSourceDir, lookupName, needHash, ' R' );
789834 IShaderCompiler::IIncludeLoader::found_t retVal;
790- switch (sessionCache ? sessionCache-> lookup ( cacheKey, retVal) : SSessionCache::LookupResult::Miss )
835+ switch (lookupIncludeSessionCache (readSessionCache, writeSessionCache, cacheKey, retVal))
791836 {
792837 case SSessionCache::LookupResult::Found:
793838 return retVal;
@@ -809,8 +854,7 @@ auto IShaderCompiler::CIncludeFinder::getIncludeRelative(const system::path& req
809854 hasher.update (reinterpret_cast <uint8_t *>(retVal.contents .data ()), retVal.contents .size () * (sizeof (char ) / sizeof (uint8_t )));
810855 retVal.hash = static_cast <core::blake3_hash_t >(hasher);
811856 }
812- if (sessionCache)
813- sessionCache->store (cacheKey, retVal);
857+ writeIncludeSessionCache (writeSessionCache, cacheKey, retVal);
814858 return retVal;
815859}
816860
0 commit comments