From 2e7f153d95fa4daf991f59cea8cc2efcc18c1ba0 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Fri, 12 Dec 2003 04:40:35 +0000 Subject: [PATCH] optimization experiment --- stepmania/src/Song.cpp | 51 +++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/stepmania/src/Song.cpp b/stepmania/src/Song.cpp index 9fd67d8dcb..bb4cfb81bc 100644 --- a/stepmania/src/Song.cpp +++ b/stepmania/src/Song.cpp @@ -1387,40 +1387,45 @@ void SortSongPointerArrayByBPM( vector &arraySongPointers ) sort( arraySongPointers.begin(), arraySongPointers.end(), CompareSongPointersByBPM ); } -struct GradeCnt { int n[NUM_GRADES]; }; -static map NumNotesWithGrade; - -bool CompareSongPointersByGrade(const Song *pSong1, const Song *pSong2) +void AppendOctal( int n, int digits, CString &out ) { - const GradeCnt &cnt1=NumNotesWithGrade[(Song *) pSong1]; - const GradeCnt &cnt2=NumNotesWithGrade[(Song *) pSong2]; - for( int i=NUM_GRADES-1; i>GRADE_NO_DATA; i-- ) + for( int p = digits; p >= 0; --p ) { - const int iCount1 = cnt1.n[i]; - const int iCount2 = cnt2.n[i]; - - if( iCount1 > iCount2 ) - return true; - if( iCount1 < iCount2 ) - return false; + const int shift = p*3; + int n2 = (n >> shift) & 0x7; + out.push_back( (char) n2 ); } - - return CompareSongPointersByTitle( pSong1, pSong2 ); } +bool CompDescending( const pair &a, const pair &b ) +{ return a.second > b.second; } + void SortSongPointerArrayByGrade( vector &arraySongPointers ) { - for(unsigned i = 0; i < arraySongPointers.size(); ++i) - { - GradeCnt cnt; - memset( cnt.n, 0, sizeof(cnt.n) ); + /* Optimize by pre-writing a string to compare, since doing GetNumNotesWithGrade + * inside the sort is too slow. */ + typedef pair< Song *, CString > val; + vector vals; + vals.reserve( arraySongPointers.size() ); + for( unsigned i = 0; i < arraySongPointers.size(); ++i ) + { Song *pSong = arraySongPointers[i]; + + CString foo; + foo.reserve(256); for( int g=NUM_GRADES-1; g>GRADE_NO_DATA; g-- ) - cnt.n[g] = pSong->GetNumNotesWithGrade( (Grade)g ); - NumNotesWithGrade[pSong] = cnt; + { + int n = pSong->GetNumNotesWithGrade( (Grade)g ); + AppendOctal( n, 3, foo ); + } + vals.push_back( val(pSong, foo) ); } - sort( arraySongPointers.begin(), arraySongPointers.end(), CompareSongPointersByGrade ); + + sort( vals.begin(), vals.end(), CompDescending ); + + for( unsigned i = 0; i < arraySongPointers.size(); ++i ) + arraySongPointers[i] = vals[i].first; }