@@ -745,10 +745,15 @@ export function initializeGlobalTrackOrder(
745745export function initializeSelectedThreadIndex (
746746 selectedThreadIndexes : Set < ThreadIndex > | null ,
747747 visibleThreadIndexes : ThreadIndex [ ] ,
748- profile : Profile
748+ profile : Profile ,
749+ threadActivityScores : Array < ThreadActivityScore >
749750) : Set < ThreadIndex > {
750751 if ( selectedThreadIndexes === null ) {
751- return getDefaultSelectedThreadIndexes ( visibleThreadIndexes , profile ) ;
752+ return getDefaultSelectedThreadIndexes (
753+ visibleThreadIndexes ,
754+ profile ,
755+ threadActivityScores
756+ ) ;
752757 }
753758
754759 // Filter out hidden threads from the set of selected threads.
@@ -758,7 +763,11 @@ export function initializeSelectedThreadIndex(
758763 ) ;
759764 if ( visibleSelectedThreadIndexes . size === 0 ) {
760765 // No selected threads were visible. Fall back to default selection.
761- return getDefaultSelectedThreadIndexes ( visibleThreadIndexes , profile ) ;
766+ return getDefaultSelectedThreadIndexes (
767+ visibleThreadIndexes ,
768+ profile ,
769+ threadActivityScores
770+ ) ;
762771 }
763772 return visibleSelectedThreadIndexes ;
764773}
@@ -767,7 +776,8 @@ export function initializeSelectedThreadIndex(
767776// order.
768777function getDefaultSelectedThreadIndexes (
769778 visibleThreadIndexes : ThreadIndex [ ] ,
770- profile : Profile
779+ profile : Profile ,
780+ threadActivityScores : Array < ThreadActivityScore >
771781) : Set < ThreadIndex > {
772782 if ( profile . meta . initialSelectedThreads !== undefined ) {
773783 return new Set (
@@ -785,10 +795,11 @@ function getDefaultSelectedThreadIndexes(
785795 } )
786796 ) ;
787797 }
788- const visibleThreads = visibleThreadIndexes . map (
789- ( threadIndex ) => profile . threads [ threadIndex ]
798+ const defaultThread = _findDefaultThread (
799+ visibleThreadIndexes ,
800+ profile. threads ,
801+ threadActivityScores
790802 ) ;
791- const defaultThread = _findDefaultThread ( visibleThreads ) ;
792803 const defaultThreadIndex = profile . threads . indexOf ( defaultThread ) ;
793804 if ( defaultThreadIndex === - 1 ) {
794805 throw new Error ( 'Expected to find a thread index to select.' ) ;
@@ -1309,16 +1320,30 @@ function _computeThreadSampleScore(
13091320 return nonIdleSampleCount * referenceCPUDeltaPerInterval ;
13101321}
13111322
1312- function _findDefaultThread ( threads : RawThread [ ] ) : RawThread | null {
1323+ function _findDefaultThread (
1324+ visibleThreadIndexes : ThreadIndex [ ] ,
1325+ threads : RawThread [ ] ,
1326+ threadActivityScores : Array < ThreadActivityScore >
1327+ ) : RawThread | null {
13131328 if ( threads . length === 0 ) {
13141329 // Tests may have no threads.
13151330 return null ;
13161331 }
1317- const contentThreadId = threads . findIndex (
1318- ( thread ) => thread . name === 'GeckoMain' && thread . processType === 'tab'
1332+
1333+ const threadOrder = defaultThreadOrder (
1334+ visibleThreadIndexes ,
1335+ threads ,
1336+ threadActivityScores
13191337 ) ;
1338+
1339+ // Try to find a tab process with the highest activity score. If it can't
1340+ // find one, select the first thread with the highest one.
13201341 const defaultThreadIndex =
1321- contentThreadId !== - 1 ? contentThreadId : defaultThreadOrder ( threads ) [ 0 ] ;
1342+ threadOrder . find (
1343+ ( threadIndex ) =>
1344+ threads [ threadIndex ] . name === 'GeckoMain' &&
1345+ threads [ threadIndex ] . processType === 'tab'
1346+ ) ?? threadOrder [ 0 ] ;
13221347
13231348 return threads [ defaultThreadIndex ] ;
13241349}
0 commit comments