@@ -2,6 +2,7 @@ use std::{borrow::Cow, future::Future, mem::replace, panic, pin::Pin};
2
2
3
3
use anyhow:: { anyhow, Result } ;
4
4
use auto_hash_map:: AutoSet ;
5
+ use futures:: { StreamExt , TryStreamExt } ;
5
6
use parking_lot:: Mutex ;
6
7
use rustc_hash:: FxHashSet ;
7
8
use tracing:: { Instrument , Span } ;
@@ -17,6 +18,8 @@ use crate::{
17
18
CollectiblesSource , NonLocalValue , ReadRef , ResolvedVc , TryJoinIterExt ,
18
19
} ;
19
20
21
+ const APPLY_EFFECTS_CONCURRENCY_LIMIT : usize = 1024 ;
22
+
20
23
/// A trait to emit a task effect as collectible. This trait only has one
21
24
/// implementation, `EffectInstance` and no other implementation is allowed.
22
25
/// The trait is private to this module so that no other implementation can be
@@ -168,17 +171,16 @@ pub async fn apply_effects(source: impl CollectiblesSource) -> Result<()> {
168
171
}
169
172
let span = tracing:: info_span!( "apply effects" , count = effects. len( ) ) ;
170
173
async move {
171
- effects
172
- . into_iter ( )
173
- . map ( async |effect| {
174
+ // Limit the concurrency of effects
175
+ futures:: stream:: iter ( effects)
176
+ . map ( Ok )
177
+ . try_for_each_concurrent ( APPLY_EFFECTS_CONCURRENCY_LIMIT , async |effect| {
174
178
let Some ( effect) = ResolvedVc :: try_downcast_type :: < EffectInstance > ( effect) else {
175
179
panic ! ( "Effect must only be implemented by EffectInstance" ) ;
176
180
} ;
177
181
effect. await ?. apply ( ) . await
178
182
} )
179
- . try_join ( )
180
- . await ?;
181
- Ok ( ( ) )
183
+ . await
182
184
}
183
185
. instrument ( span)
184
186
. await
@@ -251,12 +253,13 @@ impl Effects {
251
253
pub async fn apply ( & self ) -> Result < ( ) > {
252
254
let span = tracing:: info_span!( "apply effects" , count = self . effects. len( ) ) ;
253
255
async move {
254
- self . effects
255
- . iter ( )
256
- . map ( async |effect| effect. apply ( ) . await )
257
- . try_join ( )
258
- . await ?;
259
- Ok ( ( ) )
256
+ // Limit the concurrency of effects
257
+ futures:: stream:: iter ( self . effects . iter ( ) )
258
+ . map ( Ok )
259
+ . try_for_each_concurrent ( APPLY_EFFECTS_CONCURRENCY_LIMIT , async |effect| {
260
+ effect. apply ( ) . await
261
+ } )
262
+ . await
260
263
}
261
264
. instrument ( span)
262
265
. await
0 commit comments