1818using System . Diagnostics ;
1919using System . Numerics ;
2020using System . Runtime . CompilerServices ;
21+ using System . Runtime . InteropServices ;
2122
2223namespace Game . Server ;
2324
@@ -426,6 +427,7 @@ public void SetPhysicsFlag(PlayerPhysFlag flag, bool set) {
426427 public TimeUnit_t LerpTime ;
427428 public bool LagCompensation ;
428429 public bool PredictWeapons ;
430+ readonly List < UserCmd > AvailableCommandsBuf = [ ] ;
429431
430432 public static void SendProxy_CropFlagsToPlayerFlagBitsLength ( SendProp prop , object instance , IFieldAccessor field , ref DVariant outData , int element , int objectID ) {
431433 int mask = ( 1 << Constants . PLAYER_FLAG_BITS ) - 1 ;
@@ -943,7 +945,7 @@ public virtual void ImpulseCommands() {
943945 }
944946
945947 if ( tr . Fraction != 1.0 ) { // line hit something, so paint a decal
946- // todo
948+ // todo
947949 }
948950
949951 break ;
@@ -1219,7 +1221,7 @@ CommandContext RemoveAllCommandContextsExceptNewest() {
12191221
12201222 void RemoveAllCommandContexts ( ) => CommandContext . Clear ( ) ;
12211223
1222- void ReplaceContextCommands ( CommandContext ctx , UserCmd [ ] cmds , int commands ) {
1224+ void ReplaceContextCommands ( CommandContext ctx , ReadOnlySpan < UserCmd > cmds , int commands ) {
12231225 ctx . Cmds . Clear ( ) ;
12241226
12251227 ctx . NumCmds = commands ;
@@ -1386,7 +1388,7 @@ public override void PhysicsSimulate() {
13861388
13871389 int command_context_count = GetCommandContextCount ( ) ;
13881390
1389- List < UserCmd > vecAvailCommands = [ ] ;
1391+ AvailableCommandsBuf . Clear ( ) ;
13901392
13911393 for ( int context_number = 0 ; context_number < command_context_count ; context_number ++ ) {
13921394 CommandContext ctx = GetCommandContext ( context_number ) ;
@@ -1404,32 +1406,32 @@ public override void PhysicsSimulate() {
14041406 while ( droppedcmds > numbackup ) {
14051407 UserCmd lastCmd = GetLastUserCommand ( ) ;
14061408 lastCmd . CommandNumber ++ ;
1407- vecAvailCommands . Add ( lastCmd ) ;
1409+ AvailableCommandsBuf . Add ( lastCmd ) ;
14081410 droppedcmds -- ;
14091411 }
14101412
14111413 while ( droppedcmds > 0 ) {
14121414 int cmdnum = ctx . NumCmds + droppedcmds - 1 ;
1413- vecAvailCommands . Add ( ctx . Cmds [ cmdnum ] ) ;
1415+ AvailableCommandsBuf . Add ( ctx . Cmds [ cmdnum ] ) ;
14141416 droppedcmds -- ;
14151417 }
14161418 }
14171419
14181420 for ( int i = ctx . NumCmds - 1 ; i >= 0 ; i -- )
1419- vecAvailCommands . Add ( ctx . Cmds [ i ] ) ;
1421+ AvailableCommandsBuf . Add ( ctx . Cmds [ i ] ) ;
14201422
14211423 LastCmd = ctx . Cmds [ ctx . Cmds . Count - 1 ] ;
14221424 }
14231425
14241426 int commandLimit = IsSimulatingOnAlternateTicks ( ) ? 2 : 1 ;
1425- int commandsToRun = vecAvailCommands . Count ;
1426- if ( gpGlobals . SimTicksThisFrame >= commandLimit && vecAvailCommands . Count > commandLimit ) {
1427- int commandsToRollOver = Math . Min ( vecAvailCommands . Count , ( gpGlobals . SimTicksThisFrame - 1 ) ) ;
1428- commandsToRun = vecAvailCommands . Count - commandsToRollOver ;
1427+ int commandsToRun = AvailableCommandsBuf . Count ;
1428+ if ( gpGlobals . SimTicksThisFrame >= commandLimit && AvailableCommandsBuf . Count > commandLimit ) {
1429+ int commandsToRollOver = Math . Min ( AvailableCommandsBuf . Count , ( gpGlobals . SimTicksThisFrame - 1 ) ) ;
1430+ commandsToRun = AvailableCommandsBuf . Count - commandsToRollOver ;
14291431 Assert ( commandsToRun >= 0 ) ;
14301432 if ( commandsToRollOver > 0 ) {
14311433 CommandContext ctx = RemoveAllCommandContextsExceptNewest ( ) ;
1432- ReplaceContextCommands ( ctx , [ .. vecAvailCommands . GetRange ( vecAvailCommands . Count - commandsToRollOver , commandsToRollOver ) ] , commandsToRollOver ) ;
1434+ ReplaceContextCommands ( ctx , CollectionsMarshal . AsSpan ( AvailableCommandsBuf ) . Slice ( AvailableCommandsBuf . Count - commandsToRollOver , commandsToRollOver ) , commandsToRollOver ) ;
14331435 }
14341436 else
14351437 RemoveAllCommandContexts ( ) ;
@@ -1458,7 +1460,7 @@ public override void PhysicsSimulate() {
14581460 IPredictionSystem . SuppressHostEvents ( this ) ;
14591461
14601462 for ( int i = 0 ; i < commandsToRun ; ++ i ) {
1461- PlayerRunCommand ( vecAvailCommands [ i ] , MoveHelperServer . s_MoveHelperServer ) ;
1463+ PlayerRunCommand ( AvailableCommandsBuf [ i ] , MoveHelperServer . s_MoveHelperServer ) ;
14621464
14631465 if ( PhysicsController != null ) {
14641466 UpdateVPhysicsPosition ( NewVPhysicsPosition , NewVPhysicsVelocity , ( float ) vphysicsArrivalTime ) ;
0 commit comments