@@ -360,9 +360,11 @@ await Task.WhenAll(
360360 RuntimeHelpers . CopyAspNetSharedFrameworkToCoreRootAsync ( this , "artifacts-main" ) ,
361361 RuntimeHelpers . CopyAspNetSharedFrameworkToCoreRootAsync ( this , "artifacts-pr" ) ) ;
362362
363+ int memoryAvailableGB = GetRemainingSystemMemoryGB ( ) ;
364+
363365 await Task . WhenAll (
364- DiffExtraProjectsAsync ( "artifacts-main" , "clr-checked-main" , DiffsMainDirectory ) ,
365- DiffExtraProjectsAsync ( "artifacts-pr" , "clr-checked-pr" , DiffsPrDirectory ) ) ;
366+ DiffExtraProjectsAsync ( "artifacts-main" , "clr-checked-main" , DiffsMainDirectory , memoryAvailableGB ) ,
367+ DiffExtraProjectsAsync ( "artifacts-pr" , "clr-checked-pr" , DiffsPrDirectory , memoryAvailableGB ) ) ;
366368 }
367369 finally
368370 {
@@ -379,7 +381,7 @@ await Task.WhenAll(
379381
380382 return diffAnalyzeSummary ;
381383
382- async Task DiffExtraProjectsAsync ( string coreRootFolder , string checkedClrFolder , string outputFolder )
384+ async Task DiffExtraProjectsAsync ( string coreRootFolder , string checkedClrFolder , string outputFolder , int memoryAvailableGB )
383385 {
384386 string projectsRoot = ExtraProjectsDirectory ;
385387 string branch = coreRootFolder . Contains ( "main" , StringComparison . Ordinal ) ? "main" : "pr" ;
@@ -411,16 +413,27 @@ async Task DiffExtraProjectsAsync(string coreRootFolder, string checkedClrFolder
411413 return ;
412414 }
413415
414- int coreRootCopies = Math . Min ( Math . Min ( Environment . ProcessorCount / 2 , GetRemainingSystemMemoryGB ( ) / 3 ) , projectDirs . Count / 20 ) ;
415- coreRootCopies = Math . Max ( coreRootCopies , 1 ) ;
416+ int memoryParallelism = OnRamDisk ? ( int ) ( memoryAvailableGB / 2.5 ) : memoryAvailableGB * 2 ;
417+ int coreRootCopies = Math . Min ( Math . Min ( Environment . ProcessorCount , memoryParallelism ) , projectDirs . Count ) ;
418+ coreRootCopies = Math . Max ( coreRootCopies / 2 , 1 ) ; // / 2 because we run for main and pr in parallel
419+ var countdown = new CountdownEvent ( coreRootCopies ) ;
416420
417421 await Parallel . ForAsync ( 0 , coreRootCopies , async ( index , _ ) =>
418422 {
419- string newCoreRootFolder = $ "{ coreRootFolder } _{ index } ";
420- string newCheckedClrFolder = $ "{ checkedClrFolder } _{ index } ";
423+ string newCoreRootFolder = coreRootFolder ;
424+ string newCheckedClrFolder = checkedClrFolder ;
425+
426+ if ( index > 0 )
427+ {
428+ newCoreRootFolder = $ "{ newCoreRootFolder } _{ index } ";
429+ newCheckedClrFolder = $ "{ newCheckedClrFolder } _{ index } ";
421430
422- CopyDirectory ( coreRootFolder , newCoreRootFolder ) ;
423- CopyDirectory ( checkedClrFolder , newCheckedClrFolder ) ;
431+ CopyDirectory ( coreRootFolder , newCoreRootFolder ) ;
432+ CopyDirectory ( checkedClrFolder , newCheckedClrFolder ) ;
433+ }
434+
435+ countdown . Signal ( ) ;
436+ countdown . Wait ( JobTimeout ) ;
424437
425438 while ( true )
426439 {
@@ -466,8 +479,11 @@ await Parallel.ForAsync(0, coreRootCopies, async (index, _) =>
466479 }
467480 }
468481
469- try { Directory . Delete ( newCoreRootFolder , recursive : true ) ; } catch { }
470- try { Directory . Delete ( newCheckedClrFolder , recursive : true ) ; } catch { }
482+ if ( index > 0 )
483+ {
484+ try { Directory . Delete ( newCoreRootFolder , recursive : true ) ; } catch { }
485+ try { Directory . Delete ( newCheckedClrFolder , recursive : true ) ; } catch { }
486+ }
471487 } ) ;
472488 }
473489
0 commit comments