Skip to content

Commit aba5ee9

Browse files
committed
refactor
1 parent b0d8ae7 commit aba5ee9

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

src/Backdash/Data/IObjectPool.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
namespace Backdash.Data;
22

3+
/// <summary>
4+
/// Defines a object pooling contract
5+
/// </summary>
6+
/// <typeparam name="T"></typeparam>
7+
public interface IObjectPool<T>
8+
{
9+
/// <summary>
10+
/// Rent an instance on <typeparamref name="T"/> from the pool
11+
/// </summary>
12+
T Rent();
13+
14+
/// <summary>
15+
/// Return <paramref name="value"/> to the pool
16+
/// </summary>
17+
void Return(T value);
18+
}
19+
320
sealed class DefaultObjectPool<T> : IObjectPool<T> where T : class, new()
421
{
522
public static readonly IObjectPool<T> Instance = new DefaultObjectPool<T>();

src/Backdash/Synchronizing/Input/InputQueue.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ sealed class InputQueue<TInput> where TInput : unmanaged
1616
GameInput<TInput> prediction;
1717
readonly int id;
1818
public int LocalFrameDelay { get; set; }
19+
1920
public InputQueue(int id, int queueSize, Logger logger)
2021
{
2122
this.id = id;
@@ -31,11 +32,13 @@ public InputQueue(int id, int queueSize, Logger logger)
3132
inputs = new GameInput<TInput>[queueSize];
3233
Array.Fill(inputs, new(Frame.Zero));
3334
}
35+
3436
int BackIndex => head == 0 ? inputs.Length - 1 : head - 1;
3537
ref GameInput<TInput> Back => ref inputs[BackIndex];
3638
ref GameInput<TInput> Front => ref inputs[tail];
3739
ref GameInput<TInput> AtFrame(Frame frame) => ref inputs[frame.Number % inputs.Length];
3840
ref GameInput<TInput> NextAfter(int offset) => ref inputs[(offset + tail) % inputs.Length];
41+
3942
void Push(GameInput<TInput> input)
4043
{
4144
// Add the frame to the back of the queue
@@ -44,15 +47,18 @@ void Push(GameInput<TInput> input)
4447
length++;
4548
Trace.Assert(length <= inputs.Length);
4649
}
50+
4751
void Skip(int offset)
4852
{
4953
Trace.Assert(offset >= 0);
5054
tail = (tail + offset) % inputs.Length;
5155
length -= offset;
5256
Trace.Assert(length >= 0);
5357
}
58+
5459
void Reset() => tail = head;
5560
public Frame FirstIncorrectFrame => firstIncorrectFrame;
61+
5662
public void DiscardConfirmedFrames(Frame frame)
5763
{
5864
Trace.Assert(frame >= Frame.Zero);
@@ -68,10 +74,12 @@ public void DiscardConfirmedFrames(Frame frame)
6874
logger.Write(LogLevel.Trace, $"Queue {id} => difference of {offset} frames.");
6975
Skip(offset);
7076
}
77+
7178
logger.Write(LogLevel.Trace,
7279
$"Queue {id} => after discarding, new back is {Back.Frame} (front:{Front.Frame})."
7380
);
7481
}
82+
7583
public void ResetPrediction(in Frame frame)
7684
{
7785
Trace.Assert(firstIncorrectFrame.IsNull || frame <= firstIncorrectFrame);
@@ -82,6 +90,7 @@ public void ResetPrediction(in Frame frame)
8290
firstIncorrectFrame = Frame.Null;
8391
lastFrameRequested = Frame.Null;
8492
}
93+
8594
public bool GetConfirmedInput(in Frame requestedFrame, ref GameInput<TInput> input)
8695
{
8796
Trace.Assert(firstIncorrectFrame.IsNull || requestedFrame < firstIncorrectFrame);
@@ -91,6 +100,7 @@ public bool GetConfirmedInput(in Frame requestedFrame, ref GameInput<TInput> inp
91100
input = requested;
92101
return true;
93102
}
103+
94104
public bool GetInput(Frame requestedFrame, out GameInput<TInput> input)
95105
{
96106
logger.Write(LogLevel.Trace, $"Queue {id} => requesting input frame {requestedFrame}.");
@@ -115,6 +125,7 @@ public bool GetInput(Frame requestedFrame, out GameInput<TInput> input)
115125
logger.Write(LogLevel.Trace, $"Queue {id} => returning confirmed frame number {input.Frame}.");
116126
return true;
117127
}
128+
118129
// The requested frame isn't in the queue. Bummer. This means we need
119130
// to return a prediction frame. Predict that the user will do the
120131
// same thing they did last time.
@@ -137,8 +148,10 @@ public bool GetInput(Frame requestedFrame, out GameInput<TInput> input)
137148
);
138149
prediction = Back;
139150
}
151+
140152
prediction.IncrementFrame();
141153
}
154+
142155
Trace.Assert(prediction.Frame >= 0);
143156
// If we've made it this far, we must be predicting. Go ahead and
144157
// forward the prediction frame contents. Be sure to return the
@@ -149,6 +162,7 @@ public bool GetInput(Frame requestedFrame, out GameInput<TInput> input)
149162
$"Queue {id} => returning prediction frame number {input.Frame} ({prediction.Frame}).");
150163
return false;
151164
}
165+
152166
public void AddInput(ref GameInput<TInput> input)
153167
{
154168
logger.Write(LogLevel.Trace, $"Queue {id} => adding input frame number {input.Frame} to queue.");
@@ -166,6 +180,7 @@ public void AddInput(ref GameInput<TInput> input)
166180
// design).
167181
input.Frame = newFrame;
168182
}
183+
169184
void AddDelayedInputToQueue(GameInput<TInput> input, in Frame frameNumber)
170185
{
171186
logger.Write(LogLevel.Trace, $"Queue {id} => adding delayed input frame number {frameNumber} to queue.");
@@ -188,9 +203,10 @@ void AddDelayedInputToQueue(GameInput<TInput> input, in Frame frameNumber)
188203
$"Queue {id} => frame {frameNumber} does not match prediction. marking error.");
189204
firstIncorrectFrame = frameNumber;
190205
}
206+
191207
// If this input is the same frame as the last one requested, and we
192208
// still haven't found any mis-predicted inputs, we can dump out
193-
// of predition mode entirely! Otherwise, advance the prediction frame
209+
// of prediction mode entirely! Otherwise, advance the prediction frame
194210
// count up.
195211
if (prediction.Frame == lastFrameRequested &&
196212
firstIncorrectFrame.IsNull)
@@ -203,6 +219,7 @@ void AddDelayedInputToQueue(GameInput<TInput> input, in Frame frameNumber)
203219
prediction.IncrementFrame();
204220
}
205221
}
222+
206223
Frame AdvanceQueueHead(Frame frame)
207224
{
208225
logger.Write(LogLevel.Trace, $"advancing queue head to frame {frame}.");
@@ -217,6 +234,7 @@ Frame AdvanceQueueHead(Frame frame)
217234
$"Queue {id} => Dropping input frame {frame} (expected next frame to be {expectedFrame})");
218235
return Frame.Null;
219236
}
237+
220238
while (expectedFrame < frame)
221239
{
222240
// This can occur when the frame delay has been increased since the last
@@ -229,6 +247,7 @@ Frame AdvanceQueueHead(Frame frame)
229247
AddDelayedInputToQueue(lastFrame, in expectedFrame);
230248
expectedFrame++;
231249
}
250+
232251
Trace.Assert(frame == 0 || frame == Back.Frame.Next());
233252
return frame;
234253
}

0 commit comments

Comments
 (0)