@@ -35,7 +35,7 @@ use crate::{
3535 skin:: PlayerSkin ,
3636 } ,
3737 storage:: { Events , PlayerJoinServer , SkinHandler } ,
38- util:: { SendableRef , TracingExt , mojang:: MojangClient } ,
38+ util:: { TracingExt , mojang:: MojangClient } ,
3939} ;
4040
4141#[ derive( Component , Debug ) ]
@@ -311,9 +311,9 @@ impl Module for IngressModule {
311311
312312 let world = it. world ( ) ;
313313
314- let mut recv = receive. 0 . lock ( ) ;
314+ let recv = & receive. 0 ;
315315
316- for connect in recv. player_connect . drain ( ..) {
316+ for connect in recv. player_connect . lock ( ) . drain ( ..) {
317317 info ! ( "player_connect" ) ;
318318 let view = world
319319 . entity ( )
@@ -328,7 +328,7 @@ impl Module for IngressModule {
328328 lookup. insert ( connect, view. id ( ) ) ;
329329 }
330330
331- for disconnect in recv. player_disconnect . drain ( ..) {
331+ for disconnect in recv. player_disconnect . lock ( ) . drain ( ..) {
332332 // will initiate the removal of entity
333333 info ! ( "queue pending remove" ) ;
334334 let Some ( id) = lookup. get ( & disconnect) . copied ( ) else {
@@ -341,64 +341,28 @@ impl Module for IngressModule {
341341 }
342342 } ) ;
343343
344- #[ expect(
345- clippy:: unwrap_used,
346- reason = "this is only called once on startup; it should be fine. we mostly care \
347- about crashing during server execution"
348- ) ]
349- let num_threads = i32:: try_from ( rayon:: current_num_threads ( ) ) . unwrap ( ) ;
350-
351- let worlds = ( 0 ..num_threads)
352- // SAFETY: promoting world to static lifetime, system won't outlive world
353- . map ( |i| unsafe { std:: mem:: transmute ( world. stage ( i) ) } )
354- . map ( SendableRef )
355- . collect :: < Vec < _ > > ( ) ;
356-
357- system ! (
358- "ingress_to_ecs" ,
359- world,
360- & StreamLookup ( $) ,
361- & ReceiveState ( $) ,
362- )
363- . immediate ( true )
364- . kind :: < flecs:: pipeline:: PostLoad > ( )
365- . each ( move |( lookup, receive) | {
366- use rayon:: prelude:: * ;
367-
368- // 134µs with par_iter
369- // 150-208µs with regular drain
370- let span = info_span ! ( "ingress_to_ecs" ) ;
371- let _enter = span. enter ( ) ;
372-
373- let mut recv = receive. 0 . lock ( ) ;
374-
375- recv. packets . par_drain ( ) . for_each ( |( entity_id, bytes) | {
376- #[ expect(
377- clippy:: indexing_slicing,
378- reason = "it should be impossible to get a thread index that is out of bounds \
379- unless the rayon thread pool changes size which does not occur"
380- ) ]
381- let world = & worlds[ rayon:: current_thread_index ( ) . unwrap_or_default ( ) ] ;
382- let world = & world. 0 ;
383-
384- let Some ( entity_id) = lookup. get ( & entity_id) else {
385- // this is not necessarily a bug; race conditions occur
386- warn ! ( "player_packets: entity for {entity_id:?}" ) ;
387- return ;
388- } ;
389-
390- if !world. is_alive ( * entity_id) {
344+ world
345+ . system_named :: < ( & ReceiveState , & ConnectionId , & mut PacketDecoder ) > ( "ingress_to_ecs" )
346+ . term_at ( 0u32 )
347+ . singleton ( ) // StreamLookup
348+ // .multi_threaded()
349+ . immediate ( true )
350+ . kind :: < flecs:: pipeline:: PostLoad > ( )
351+ . each ( move |( receive, connection_id, decoder) | {
352+ // 134µs with par_iter
353+ // 150-208µs with regular drain
354+ let span = info_span ! ( "ingress_to_ecs" ) ;
355+ let _enter = span. enter ( ) ;
356+
357+ let mut bytes = receive. 0 . packets . get_mut ( & connection_id. inner ( ) ) . unwrap ( ) ;
358+ if bytes. is_empty ( ) {
391359 return ;
392360 }
393361
394- let entity = world. entity_from_id ( * entity_id) ;
395-
396- entity. get :: < & mut PacketDecoder > ( |decoder| {
397- decoder. shift_excess ( ) ;
398- decoder. queue_slice ( bytes. as_ref ( ) ) ;
399- } ) ;
362+ decoder. shift_excess ( ) ;
363+ decoder. queue_slice ( bytes. as_ref ( ) ) ;
364+ bytes. clear ( ) ;
400365 } ) ;
401- } ) ;
402366
403367 system ! (
404368 "remove_player_from_visibility" ,
@@ -591,9 +555,10 @@ impl Module for IngressModule {
591555 // todo: better way?
592556 if let Some ( ( position, pose) ) = position. as_mut ( ) . zip ( pose. as_mut ( ) ) {
593557 let world = & world;
558+ let id = entity. id ( ) ;
594559
595560 let mut query = PacketSwitchQuery {
596- id : entity . id ( ) ,
561+ id,
597562 view : entity,
598563 compose,
599564 io_ref,
0 commit comments