File tree Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change 403403 // From: https://en.wikipedia.org/wiki/Merge_sort
404404 function topDownSplitMerge ( B , iBegin , iEnd , A ) {
405405 if ( iEnd - iBegin <= 6 ) return ;
406+
407+ // Optimization: Don't do merge sort if it's already sorted
408+ let isAlreadySorted = true ;
409+ for ( let i = iBegin + 3 , j = i + 6 ; j < iEnd ; i = j , j += 6 ) {
410+ // Compare mappings first by original line (index 3) and then by original column (index 4)
411+ if ( A [ i ] < A [ j ] || ( A [ i ] === A [ j ] && A [ i + 1 ] <= A [ j + 1 ] ) ) continue ;
412+ isAlreadySorted = false ;
413+ break ;
414+ }
415+ if ( isAlreadySorted ) {
416+ return ;
417+ }
418+
406419 const iMiddle = ( ( iEnd / 6 + iBegin / 6 ) >> 1 ) * 6 ;
407420 topDownSplitMerge ( A , iBegin , iMiddle , B ) ;
408421 topDownSplitMerge ( A , iMiddle , iEnd , B ) ;
You can’t perform that action at this time.
0 commit comments