Skip to content

Commit

Permalink
Perf improvements.
Browse files Browse the repository at this point in the history
- Improved performance for Server.
  • Loading branch information
FirstGearGames committed Apr 4, 2024
1 parent 49f0dfc commit de11a19
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 157 deletions.
230 changes: 115 additions & 115 deletions FishNet/Plugins/Bayou/Core/BidirectionalDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,115 +1,115 @@
using System.Collections;
using System.Collections.Generic;

namespace FishNet.Transporting.Bayou
{
internal class BidirectionalDictionary<T1, T2> : IEnumerable
{
private Dictionary<T1, T2> t1ToT2Dict = new Dictionary<T1, T2>();
private Dictionary<T2, T1> t2ToT1Dict = new Dictionary<T2, T1>();

public IEnumerable<T1> FirstTypes => t1ToT2Dict.Keys;
public IEnumerable<T2> SecondTypes => t2ToT1Dict.Keys;

public IEnumerator GetEnumerator() => t1ToT2Dict.GetEnumerator();

public int Count => t1ToT2Dict.Count;

public Dictionary<T1, T2> First => t1ToT2Dict;
public Dictionary<T2, T1> Second => t2ToT1Dict;

public void Add(T1 key, T2 value)
{
if (t1ToT2Dict.ContainsKey(key))
{
Remove(key);
}

t1ToT2Dict[key] = value;
t2ToT1Dict[value] = key;
}

public void Add(T2 key, T1 value)
{
if (t2ToT1Dict.ContainsKey(key))
{
Remove(key);
}

t2ToT1Dict[key] = value;
t1ToT2Dict[value] = key;
}

public T2 Get(T1 key) => t1ToT2Dict[key];

public T1 Get(T2 key) => t2ToT1Dict[key];

public bool TryGetValue(T1 key, out T2 value) => t1ToT2Dict.TryGetValue(key, out value);

public bool TryGetValue(T2 key, out T1 value) => t2ToT1Dict.TryGetValue(key, out value);

public bool Contains(T1 key) => t1ToT2Dict.ContainsKey(key);

public bool Contains(T2 key) => t2ToT1Dict.ContainsKey(key);

public void Remove(T1 key)
{
if (First.TryGetValue(key, out T2 value))
{
t1ToT2Dict.Remove(key);
t2ToT1Dict.Remove(value);
}
}

public void Clear()
{
First.Clear();
Second.Clear();
}

public void Remove(T2 key)
{
if (Second.TryGetValue(key, out T1 value))
{
t1ToT2Dict.Remove(value);
t2ToT1Dict.Remove(key);
}
}

public void RemoveFirst(T1 key)
{
if (First.TryGetValue(key, out T2 value))
{
Second.Remove(value);
First.Remove(key);
}
}

public void RemoveSecond(T2 key)
{
if (Second.TryGetValue(key, out T1 value))
{
First.Remove(value);
Second.Remove(key);
}
}

public T1 this[T2 key]
{
get => t2ToT1Dict[key];
set
{
Add(key, value);
}
}

public T2 this[T1 key]
{
get => t1ToT2Dict[key];
set
{
Add(key, value);
}
}
}
}
//using System.Collections;
//using System.Collections.Generic;

//namespace FishNet.Transporting.Bayou
//{
// internal class BidirectionalDictionary<T1, T2> : IEnumerable
// {
// private Dictionary<T1, T2> t1ToT2Dict = new Dictionary<T1, T2>();
// private Dictionary<T2, T1> t2ToT1Dict = new Dictionary<T2, T1>();

// public IEnumerable<T1> FirstTypes => t1ToT2Dict.Keys;
// public IEnumerable<T2> SecondTypes => t2ToT1Dict.Keys;

// public IEnumerator GetEnumerator() => t1ToT2Dict.GetEnumerator();

// public int Count => t1ToT2Dict.Count;

// public Dictionary<T1, T2> First => t1ToT2Dict;
// public Dictionary<T2, T1> Second => t2ToT1Dict;

// public void Add(T1 key, T2 value)
// {
// if (t1ToT2Dict.ContainsKey(key))
// {
// Remove(key);
// }

// t1ToT2Dict[key] = value;
// t2ToT1Dict[value] = key;
// }

// public void Add(T2 key, T1 value)
// {
// if (t2ToT1Dict.ContainsKey(key))
// {
// Remove(key);
// }

// t2ToT1Dict[key] = value;
// t1ToT2Dict[value] = key;
// }

// public T2 Get(T1 key) => t1ToT2Dict[key];

// public T1 Get(T2 key) => t2ToT1Dict[key];

// public bool TryGetValue(T1 key, out T2 value) => t1ToT2Dict.TryGetValue(key, out value);

// public bool TryGetValue(T2 key, out T1 value) => t2ToT1Dict.TryGetValue(key, out value);

// public bool Contains(T1 key) => t1ToT2Dict.ContainsKey(key);

// public bool Contains(T2 key) => t2ToT1Dict.ContainsKey(key);

// public void Remove(T1 key)
// {
// if (First.TryGetValue(key, out T2 value))
// {
// t1ToT2Dict.Remove(key);
// t2ToT1Dict.Remove(value);
// }
// }

// public void Clear()
// {
// First.Clear();
// Second.Clear();
// }

// public void Remove(T2 key)
// {
// if (Second.TryGetValue(key, out T1 value))
// {
// t1ToT2Dict.Remove(value);
// t2ToT1Dict.Remove(key);
// }
// }

// public void RemoveFirst(T1 key)
// {
// if (First.TryGetValue(key, out T2 value))
// {
// Second.Remove(value);
// First.Remove(key);
// }
// }

// public void RemoveSecond(T2 key)
// {
// if (Second.TryGetValue(key, out T1 value))
// {
// First.Remove(value);
// Second.Remove(key);
// }
// }

// public T1 this[T2 key]
// {
// get => t2ToT1Dict[key];
// set
// {
// Add(key, value);
// }
// }

// public T2 this[T1 key]
// {
// get => t1ToT2Dict[key];
// set
// {
// Add(key, value);
// }
// }
// }
//}
2 changes: 1 addition & 1 deletion FishNet/Plugins/Bayou/Core/ServerSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal RemoteConnectionState GetConnectionState(int connectionId)
/// <summary>
/// Currently connected clients.
/// </summary>
private List<int> _clients = new List<int>();
private HashSet<int> _clients = new HashSet<int>();
/// <summary>
/// Server socket manager.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions FishNet/Plugins/Bayou/SimpleWebTransport/Common/BufferPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public ArrayBuffer Take()
}
else
{
Log.Verbose($"BufferBucket({arraySize}) create new");
//Log.Verbose($"BufferBucket({arraySize}) create new");
return new ArrayBuffer(this, arraySize);
}
}
Expand All @@ -145,13 +145,13 @@ public void Return(ArrayBuffer buffer)
void IncrementCreated()
{
int next = Interlocked.Increment(ref _current);
Log.Verbose($"BufferBucket({arraySize}) count:{next}");
//Log.Verbose($"BufferBucket({arraySize}) count:{next}");
}
[Conditional("DEBUG")]
void DecrementCreated()
{
int next = Interlocked.Decrement(ref _current);
Log.Verbose($"BufferBucket({arraySize}) count:{next}");
//Log.Verbose($"BufferBucket({arraySize}) count:{next}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void Loop(Config config)
{
(Connection conn, int maxMessageSize, bool expectMask, ConcurrentQueue<Message> queue, BufferPool _) = config;

Profiler.BeginThreadProfiling("SimpleWeb", $"ReceiveLoop {conn.connId}");
//Profiler.BeginThreadProfiling("SimpleWeb", $"ReceiveLoop {conn.connId}");

byte[] readBuffer = new byte[Constants.HeaderSize + (expectMask ? Constants.MaskSize : 0) + maxMessageSize];
try
Expand Down Expand Up @@ -95,7 +95,7 @@ public static void Loop(Config config)
}
finally
{
Profiler.EndThreadProfiling();
//Profiler.EndThreadProfiling();

conn.Dispose();
}
Expand Down
32 changes: 23 additions & 9 deletions FishNet/Plugins/Bayou/SimpleWebTransport/Common/SendLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void Loop(Config config)
{
(Connection conn, int bufferSize, bool setMask) = config;

Profiler.BeginThreadProfiling("SimpleWeb", $"SendLoop {conn.connId}");
//Profiler.BeginThreadProfiling("SimpleWeb", $"SendLoop {conn.connId}");

// create write buffer for this thread
byte[] writeBuffer = new byte[bufferSize];
Expand Down Expand Up @@ -70,7 +70,11 @@ public static void Loop(Config config)
while (conn.sendQueue.TryDequeue(out ArrayBuffer msg))
{
// check if connected before sending message
if (!client.Connected) { Log.Info($"SendLoop {conn} not connected"); return; }
if (!client.Connected)
{
//Log.Info($"SendLoop {conn} not connected");
return;
}

int maxLength = msg.count + Constants.HeaderSize + Constants.MaskSize;

Expand All @@ -95,19 +99,29 @@ public static void Loop(Config config)
while (conn.sendQueue.TryDequeue(out ArrayBuffer msg))
{
// check if connected before sending message
if (!client.Connected) { Log.Info($"SendLoop {conn} not connected"); return; }
if (!client.Connected)
{
//Log.Info($"SendLoop {conn} not connected");
return;
}

int length = SendMessage(writeBuffer, 0, msg, setMask, maskHelper);
stream.Write(writeBuffer, 0, length);
msg.Release();
}
}
}

Log.Info($"{conn} Not Connected");
}

Log.Info($"{conn} Not Connected");
}
catch (ThreadInterruptedException e)
{
Log.InfoException(e);
}
catch (ThreadAbortException e)
{
Log.InfoException(e);
}
catch (ThreadInterruptedException e) { Log.InfoException(e); }
catch (ThreadAbortException e) { Log.InfoException(e); }
catch (Exception e)
{
Log.Exception(e);
Expand Down Expand Up @@ -135,7 +149,7 @@ static int SendMessage(byte[] buffer, int startOffset, ArrayBuffer msg, bool set
offset += msgLength;

// dump before mask on
Log.DumpBuffer("Send", buffer, startOffset, offset);
//Log.DumpBuffer("Send", buffer, startOffset, offset);

if (setMask)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public bool TryHandshake(Connection conn)

if (!IsGet(getHeader.array))
{
Log.Warn($"First bytes from client was not 'GET' for handshake, instead was {Log.BufferToString(getHeader.array, 0, GetSize)}");
//Log.Warn($"First bytes from client was not 'GET' for handshake, instead was {Log.BufferToString(getHeader.array, 0, GetSize)}");
return false;
}
}
Expand Down
Loading

0 comments on commit de11a19

Please sign in to comment.