Skip to content

Commit

Permalink
Experimental cache change
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-abram committed Feb 10, 2018
1 parent 06ed2d4 commit 54198b3
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 159 deletions.
5 changes: 2 additions & 3 deletions src/PrefsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,9 @@ PrefsManager::PrefsManager() :
m_bPAL ( "PAL", false ),
m_bDelayedTextureDelete ( "DelayedTextureDelete", false ),
m_bDelayedModelDelete ( "DelayedModelDelete", false ),
m_ImageCache ( "ImageCache", IMGCACHE_LOW_RES_PRELOAD ),
m_ImageCache ( "ImageCache", IMGCACHE_OFF),
m_bFastLoad ( "FastLoad", true ),
m_bFastLoadAdditionalSongs ( "FastLoadAdditionalSongs", true ),
m_bHyperLoad("HyperLoad", false),
m_bShrinkSongCache("RemoveCacheEnriesForDeletedSongs", false),
m_NeverCacheList ( "NeverCacheList", ""),

m_bOnlyDedicatedMenuButtons ( "OnlyDedicatedMenuButtons", false ),
Expand Down
3 changes: 1 addition & 2 deletions src/PrefsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ class PrefsManager
Preference<bool> m_bDelayedModelDelete;
Preference<ImageCacheMode> m_ImageCache;
Preference<bool> m_bFastLoad;
Preference<bool> m_bHyperLoad;
Preference<bool> m_bFastLoadAdditionalSongs;
Preference<bool> m_bShrinkSongCache;
Preference<RString> m_NeverCacheList;

Preference<bool> m_bOnlyDedicatedMenuButtons;
Expand Down
2 changes: 1 addition & 1 deletion src/Song.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ void Song::FinalizeLoading()
{
for( std::string Image : ImageDir )
{
IMAGECACHE->LoadImage( Image, GetCacheFile( Image ) );
//IMAGECACHE->LoadImage( Image, GetCacheFile( Image ) );
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/SongCacheIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ void SongCacheIndex::LoadHyperCache(LoadingWindow * ld, map<RString, Song*>& hyp
if (ld)
{
ld->SetProgress(progress);
ld->SetText(("Loading Cache (" + lastDir + ")").c_str());
ld->SetText(("Loading Cache\n (" + lastDir + ")").c_str());
progress++;
}
}
Expand Down Expand Up @@ -647,7 +647,7 @@ void SongCacheIndex::LoadCache(LoadingWindow * ld, map<pair<RString, unsigned in
if (ld)
{
ld->SetProgress(progress);
ld->SetText(("Loading Cache (" + lastDir + ")").c_str());
ld->SetText(("Loading Cache\n(" + lastDir + ")").c_str());
progress++;
}
}
Expand Down
223 changes: 73 additions & 150 deletions src/SongManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,9 @@ int SongManager::DifferentialReload() {
FILEMAN->FlushDirCache(ADDITIONAL_SONGS_DIR);
FILEMAN->FlushDirCache(EDIT_SUBDIR);
int newsongs = 0;
SONGINDEX->delay_save_cache = true;
newsongs += DifferentialReloadDir(SpecialFiles::SONGS_DIR);

const bool bOldVal = PREFSMAN->m_bFastLoad;
PREFSMAN->m_bFastLoad.Set(PREFSMAN->m_bFastLoadAdditionalSongs);
newsongs += DifferentialReloadDir(ADDITIONAL_SONGS_DIR);
PREFSMAN->m_bFastLoad.Set(bOldVal);
LoadEnabledSongsFromPref();
SONGINDEX->SaveCacheIndex();
SONGINDEX->delay_save_cache = false;

return newsongs;
}

Expand Down Expand Up @@ -227,16 +219,14 @@ void SongManager::InitSongsFromDisk( LoadingWindow *ld )
RageTimer tm;
// Tell SONGINDEX to not write the cache index file every time a song adds
// an entry. -Kyz
SONGINDEX->delay_save_cache= true;
SONGINDEX->delay_save_cache= false;
if(PREFSMAN->m_bFastLoad)
SONGINDEX->LoadCache(ld, cache);
LoadStepManiaSongDir( SpecialFiles::SONGS_DIR, ld );

const bool bOldVal = PREFSMAN->m_bFastLoad;
PREFSMAN->m_bFastLoad.Set( PREFSMAN->m_bFastLoadAdditionalSongs );
LoadStepManiaSongDir( ADDITIONAL_SONGS_DIR, ld );
PREFSMAN->m_bFastLoad.Set( bOldVal );
LoadEnabledSongsFromPref();
SONGINDEX->SaveCacheIndex();
SONGINDEX->delay_save_cache= false;
//SONGINDEX->SaveCacheIndex();
//SONGINDEX->delay_save_cache= false;

LOG->Trace( "Found %i songs in %f seconds.", m_pSongs.size(), tm.GetDeltaTime() );
}
Expand Down Expand Up @@ -478,15 +468,15 @@ void SongManager::SanityCheckGroupDir( const RString &sDir ) const
}
}

void SongManager::AddGroup( const RString &sDir, const RString &sGroupDirName )
bool SongManager::AddGroup( const RString &sDir, const RString &sGroupDirName )
{
unsigned j;
for(j = 0; j < m_sSongGroupNames.size(); ++j)
if( sGroupDirName == m_sSongGroupNames[j] )
break;

if( j != m_sSongGroupNames.size() )
return; // the group is already added
return false; // the group is already added

// Look for a group banner in this group folder
vector<RString> arrayGroupBanners;
Expand Down Expand Up @@ -541,168 +531,101 @@ void SongManager::AddGroup( const RString &sDir, const RString &sGroupDirName )
m_sSongGroupNames.emplace_back( sGroupDirName );
m_sSongGroupBannerPaths.emplace_back( sBannerPath );
//m_sSongGroupBackgroundPaths.emplace_back( sBackgroundPath );
return true;
}

static LocalizedString LOADING_SONGS ( "SongManager", "Loading songs..." );
void SongManager::LoadStepManiaSongDir( RString sDir, LoadingWindow *ld )
{
// Compositors and other stuff can impose some overhead on updating the
// loading window, which slows down startup time for some people.
// loading_window_last_update_time provides a timer so the loading window
// isn't updated after every song and course. -Kyz
RageTimer loading_window_last_update_time;
loading_window_last_update_time.Touch();
// Make sure sDir has a trailing slash.
if( sDir.Right(1) != "/" )
sDir += "/";

// Find all group directories in "Songs" folder

if( ld ) {
ld->SetIndeterminate( false );
ld->SetTotalWork( cache.size() );
}
int cacheIndex = 0;
for (auto& pair : cache) {
cacheIndex++;
ld->SetProgress(cacheIndex);
auto& pNewSong = pair.second;
if (!FILEMAN->IsADirectory(pNewSong->GetSongDir().substr(0, pNewSong->GetSongDir().size() - 1))) {
if (PREFSMAN->m_bShrinkSongCache)
SONGINDEX->DeleteSongFromDB(pair.second);
delete pair.second;
continue;
}
pNewSong->FinalizeLoading();
AddSongToList(pNewSong);
AddKeyedPointers(pNewSong);
m_mapSongGroupIndex[pNewSong->m_sGroupName].emplace_back(pNewSong);
if (AddGroup(sDir, pNewSong->m_sGroupName))
IMAGECACHE->CacheImage("Banner", GetSongGroupBannerPath(pNewSong->m_sGroupName));
}

vector<RString> arrayGroupDirs;
GetDirListing( sDir+"*", arrayGroupDirs, true );
StripCvsAndSvn( arrayGroupDirs );
StripMacResourceForks( arrayGroupDirs );
GetDirListing(sDir + "*", arrayGroupDirs, true);
StripCvsAndSvn(arrayGroupDirs);
StripMacResourceForks(arrayGroupDirs);
SortRStringArray(arrayGroupDirs);

vector< vector<RString> > arrayGroupSongDirs;
int groupIndex, songCount, songIndex;

groupIndex = 0;
songCount = 0;
if(ld)
int songCount = 0;
if (ld)
{
ld->SetIndeterminate(false);
ld->SetTotalWork(arrayGroupDirs.size());
ld->SetText("Sanity checking groups");
}
int sanity_index= 0;
FOREACH_CONST( RString, arrayGroupDirs, s ) // foreach dir in /Songs/
{
int groupsChecked = 0;
FOREACH_CONST(RString, arrayGroupDirs, s) {
RString sGroupDirName = *s;
if(ld && loading_window_last_update_time.Ago() > next_loading_window_update)
{
loading_window_last_update_time.Touch();
ld->SetProgress(sanity_index);
ld->SetText(SANITY_CHECKING_GROUPS.GetValue() + ssprintf("\n%s",
Basename(sGroupDirName).c_str()));
}
// TODO: If this check fails, log a warning instead of crashing.
SanityCheckGroupDir(sDir+sGroupDirName);

// Find all Song folders in this group directory
SanityCheckGroupDir(sDir + sGroupDirName);
vector<RString> arraySongDirs;
GetDirListing( sDir+sGroupDirName + "/*", arraySongDirs, true, true );
StripCvsAndSvn( arraySongDirs );
StripMacResourceForks( arraySongDirs );
SortRStringArray( arraySongDirs );

GetDirListing(sDir + sGroupDirName + "/*", arraySongDirs, true, true);
StripCvsAndSvn(arraySongDirs);
StripMacResourceForks(arraySongDirs);
SortRStringArray(arraySongDirs);
arrayGroupSongDirs.emplace_back(arraySongDirs);
songCount += arraySongDirs.size();

ld->SetProgress(++groupsChecked);
}

if( songCount==0 ) return;

map<pair<RString, unsigned int>, Song*> cache;
map<RString, Song*> hyperCache;
if (PREFSMAN->m_bHyperLoad) {
SONGINDEX->LoadHyperCache(ld, hyperCache);
}
else {
SONGINDEX->LoadCache(ld, cache);
}

if( ld ) {
ld->SetIndeterminate( false );
ld->SetTotalWork( songCount );
}

groupIndex = 0;
songIndex = 0;
FOREACH_CONST( RString, arrayGroupDirs, s ) // foreach dir in /Songs/
if (ld)
{
RString sGroupDirName = *s;
ld->SetIndeterminate(false);
ld->SetTotalWork(songCount);
}
int groupIndex = 0;
int songIndex = 0;
FOREACH_CONST(RString, arrayGroupDirs, s) {
RString sGroupDirName = *s;
vector<RString> &arraySongDirs = arrayGroupSongDirs[groupIndex++];

LOG->Trace("Attempting to load %i songs from \"%s\"", static_cast<int>(arraySongDirs.size()),
(sDir+sGroupDirName).c_str() );
int loaded = 0;

SongPointerVector& index_entry = m_mapSongGroupIndex[sGroupDirName];
RString group_base_name= Basename(sGroupDirName);
for (unsigned j = 0; j < arraySongDirs.size(); ++j) // for each song dir
{
RString group_base_name = Basename(sGroupDirName);
for (size_t j = 0; j < arraySongDirs.size(); ++j) {
songIndex++;
RString sSongDirName = arraySongDirs[j];

// this is a song directory. Load a new song.
if (ld && loading_window_last_update_time.Ago() > next_loading_window_update)
{
loading_window_last_update_time.Touch();
ld->SetProgress(songIndex);
ld->SetText(LOADING_SONGS.GetValue() +
ssprintf("\n%s\n%s",
group_base_name.c_str(),
Basename(sSongDirName).c_str()
)
);
}
Song* pNewSong = nullptr;
if (sSongDirName.Right(1) != "/")
sSongDirName += "/";
if (PREFSMAN->m_bHyperLoad) {
if (hyperCache.count(sSongDirName) > 0) {
pNewSong = hyperCache[sSongDirName];
pNewSong->FinalizeLoading();
hyperCache.erase(sSongDirName); //Only erases pointer
}
else {
pNewSong = new Song;
if (!pNewSong->LoadFromSongDir(sSongDirName))
{
// The song failed to load.
delete pNewSong;
continue;
}
}
}
else {
pair<RString, unsigned int> songID = { sSongDirName, GetHashForDirectory(sSongDirName) };
if (cache.count(songID) > 0) {
pNewSong = cache[songID];
pNewSong->FinalizeLoading();
cache.erase(songID); //Only erases pointer
}
else {
pNewSong = new Song;
if (!pNewSong->LoadFromSongDir(sSongDirName))
{
// The song failed to load.
delete pNewSong;
continue;
}
}
RString hur = sSongDirName + "/";
hur.MakeLower();
if (m_SongsByDir.count(hur))
continue;
Song* pNewSong = new Song;
if (!pNewSong->LoadFromSongDir(sSongDirName)) {
delete pNewSong;
continue;
}
AddSongToList(pNewSong);
AddKeyedPointers(pNewSong);

index_entry.emplace_back( pNewSong );

index_entry.emplace_back(pNewSong);
loaded++;
songIndex++;
}

LOG->Trace("Loaded %i songs from \"%s\"", loaded, (sDir+sGroupDirName).c_str() );

// Don't add the group name if we didn't load any songs in this group.
if(!loaded) continue;

// Add this group to the group array.
if (ld) {
ld->SetProgress(songIndex);
ld->SetText("Loading Songs From Disk\n (" + sGroupDirName + "()");
}
if (!loaded) continue;
LOG->Trace("Loaded %i songs from \"%s\"", loaded, (sDir + sGroupDirName).c_str());
AddGroup(sDir, sGroupDirName);

// Cache and load the group banner. (and background if it has one -aj)
IMAGECACHE->CacheImage( "Banner", GetSongGroupBannerPath(sGroupDirName) );
}
for (auto& pair : cache) {
//SONGINDEX->DeleteSongFromDB(pair.second);
delete pair.second;
IMAGECACHE->CacheImage("Banner", GetSongGroupBannerPath(sGroupDirName));
}
if( ld ) {
ld->SetIndeterminate( true );
Expand Down
4 changes: 3 additions & 1 deletion src/SongManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,15 @@ class SongManager
void LoadStepManiaSongDir( RString sDir, LoadingWindow *ld );
void LoadDWISongDir( const RString &sDir );
void SanityCheckGroupDir( const RString &sDir ) const;
void AddGroup( const RString &sDir, const RString &sGroupDirName );
bool AddGroup( const RString &sDir, const RString &sGroupDirName );

void AddSongToList(Song* new_song);
/** @brief All of the songs that can be played. */
vector<Song*> m_pSongs;
map<RString, Song*> m_SongsByDir;

map<pair<RString, unsigned int>, Song*> cache;

// Indexed by chartkeys
void AddKeyedPointers(Song* new_song);
unordered_map<string, Song*> SongsByKey;
Expand Down

0 comments on commit 54198b3

Please sign in to comment.