Skip to content

fixed several of bugs + updated nino.shared + optimized performance #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions UnityProject/Assets/Dependencies/JEngine/Templates/MonoAdapter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@

async void OnEnable()
{
try
{
while (Application.isPlaying && !awaked)
{
await TimeMgr.Delay(1);
}
}
catch (MissingReferenceException) //如果gameObject被删了,就会触发这个,这个时候就直接return了
{
return;
}

if (instance != null)
{
if (!_mOnEnableMethodGot)
Expand All @@ -98,18 +110,6 @@

if (_mOnEnableMethod != null)
{
try
{
while (Application.isPlaying && !awaked)
{
await TimeMgr.Delay(1);
}
}
catch (MissingReferenceException) //如果gameObject被删了,就会触发这个,这个时候就直接return了
{
return;
}

if (_destoryed || !Application.isPlaying)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ExtensibleBuffer() : this(DefaultBufferSize)
/// Init extensible buffer with a capacity
/// </summary>
/// <param name="size"></param>
public ExtensibleBuffer(int size = DefaultBufferSize)
public ExtensibleBuffer([In] int size = DefaultBufferSize)
{
sizeOfT = (byte)sizeof(T);
ExpandSize = size;
Expand All @@ -60,31 +60,32 @@ public ExtensibleBuffer(int size = DefaultBufferSize)
/// Get element at index
/// </summary>
/// <param name="index"></param>
public T this[int index]
public T this[in int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Data[index];
get => *(Data + index);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
EnsureCapacity(ref index);
Data[index] = value;
EnsureCapacity(in index);
*(Data + index) = value;
}
}

/// <summary>
/// Ensure index exists
/// </summary>
/// <param name="index"></param>
private void EnsureCapacity(ref int index)
private void EnsureCapacity(in int index)
{
if (index < TotalLength) return;
GC.RemoveMemoryPressure(TotalLength * sizeOfT);
while (index >= TotalLength)
{
TotalLength += ExpandSize;
GC.AddMemoryPressure(sizeOfT * ExpandSize);
}
Extend();
GC.AddMemoryPressure(TotalLength * sizeOfT);
}

/// <summary>
Expand All @@ -102,7 +103,7 @@ private void Extend()
/// <param name="startIndex"></param>
/// <param name="length"></param>
/// <returns></returns>
public T[] ToArray(int startIndex, int length)
public T[] ToArray([In] int startIndex, [In] int length)
{
T[] ret = new T[length];
CopyTo(ref ret, startIndex, length);
Expand All @@ -115,11 +116,11 @@ public T[] ToArray(int startIndex, int length)
/// <param name="startIndex"></param>
/// <param name="length"></param>
/// <returns></returns>
public Span<T> AsSpan(int startIndex, int length)
public Span<T> AsSpan([In] int startIndex, [In] int length)
{
var l = startIndex + length;
//size check
EnsureCapacity(ref l);
EnsureCapacity(in l);
return new Span<T>(Data + startIndex, length);
}

Expand All @@ -138,7 +139,7 @@ public Span<T> AsSpan(int startIndex, int length)
/// <param name="dstIndex"></param>
/// <param name="length"></param>
/// <exception cref="InvalidOperationException"></exception>
public void CopyFrom(T[] src, int srcIndex, int dstIndex, int length)
public void CopyFrom(T[] src, [In] int srcIndex, [In] int dstIndex, [In] int length)
{
fixed (T* ptr = src)
{
Expand All @@ -155,11 +156,11 @@ public void CopyFrom(T[] src, int srcIndex, int dstIndex, int length)
/// <param name="dstIndex"></param>
/// <param name="length"></param>
/// <exception cref="InvalidOperationException"></exception>
public void CopyFrom(T* src, int srcIndex, int dstIndex, int length)
public void CopyFrom([In] T* src, [In] int srcIndex, [In] int dstIndex, [In] int length)
{
var l = dstIndex + length;
//size check
EnsureCapacity(ref l);
EnsureCapacity(in l);
//copy
Unsafe.CopyBlockUnaligned(Data + dstIndex, src + srcIndex, (uint)length);
}
Expand All @@ -171,7 +172,7 @@ public void CopyFrom(T* src, int srcIndex, int dstIndex, int length)
/// <param name="srcIndex"></param>
/// <param name="length"></param>
/// <exception cref="OverflowException"></exception>
public void CopyTo(ref T[] dst, int srcIndex, int length)
public void CopyTo(ref T[] dst, [In] int srcIndex, [In] int length)
{
fixed (T* ptr = dst)
{
Expand All @@ -186,11 +187,11 @@ public void CopyTo(ref T[] dst, int srcIndex, int length)
/// <param name="srcIndex"></param>
/// <param name="length"></param>
/// <exception cref="OverflowException"></exception>
public void CopyTo(T* dst, int srcIndex, int length)
public void CopyTo([In] T* dst, [In] int srcIndex, [In] int length)
{
var l = srcIndex + length;
//size check
EnsureCapacity(ref l);
EnsureCapacity(in l);
//copy
Unsafe.CopyBlockUnaligned(dst, Data + srcIndex, (uint)length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@ public sealed class FlexibleStream : Stream
private readonly uint maxLength = 2147483648;
private const int MemStreamMaxLength = Int32.MaxValue;

public void ChangeBuffer(byte[] data)
public void ChangeBuffer(byte[] data, int offset, int count)
{
Reset();
internalBuffer = data;
origin = offset;
position = offset;
length = data.Length;
capacity = length;
}


public void ChangeBuffer(ArraySegment<byte> data)
{
ChangeBuffer(data.Array, data.Offset, data.Count);
}

public void Reset()
{
position = 0;
Expand All @@ -57,6 +64,17 @@ public FlexibleStream(byte[] internalBuffer)
isOpen = true;
origin = 0;
}

public FlexibleStream(ArraySegment<byte> internalBuffer)
{
this.internalBuffer = internalBuffer.Array ?? throw new ArgumentNullException(nameof(internalBuffer), "buffer == null");
length = capacity = internalBuffer.Array.Length;
exposable = true;
expandable = true;
isOpen = true;
origin = internalBuffer.Offset;
position = internalBuffer.Offset;
}

public override bool CanRead => isOpen;

Expand Down
84 changes: 70 additions & 14 deletions UnityProject/Assets/Dependencies/Nino/Shared/Mgr/CompressMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static CompressMgr()
var compressedStream = new FlexibleStream(BufferPool.RequestBuffer(10240));
var zipStream = new DeflateStream(compressedStream, CompressionMode.Compress, true);
CompressStreams.Push(zipStream);
var empty = Array.Empty<byte>();
var empty = new ArraySegment<byte>(Array.Empty<byte>());
GetDecompressInformation(out _, ref empty);
compressedStream = new FlexibleStream(BufferPool.RequestBuffer(10240));
zipStream = new DeflateStream(compressedStream, CompressionMode.Decompress, true);
Expand Down Expand Up @@ -57,26 +57,27 @@ static CompressMgr()
/// <returns></returns>
public static byte[] Compress(byte[] data)
{
return Compress(data, data.Length);
return Compress(new ArraySegment<byte>(data));
}

/// <summary>
/// Compress the given bytes
/// </summary>
/// <param name="data"></param>
/// <param name="length"></param>
/// <returns></returns>
public static byte[] Compress(byte[] data, int length)
public static byte[] Compress(ArraySegment<byte> data)
{
lock (CompressedLock)
{
if (!ConstMgr.EnableNativeDeflate)
{
return CompressOnNative(data, length);
return CompressOnNative(data);
}

GetCompressInformation(out var zipStream, out var compressedStream);
zipStream.Write(data, 0, length);
// ReSharper disable AssignNullToNotNullAttribute
zipStream.Write(data.Array, data.Offset, data.Count);
// ReSharper restore AssignNullToNotNullAttribute
return GetCompressBytes(zipStream, compressedStream);
}
}
Expand Down Expand Up @@ -149,6 +150,30 @@ private static void GetCompressInformation(out DeflateStream zipStream, out Flex
/// <param name="outputLength"></param>
/// <returns></returns>
public static IntPtr Decompress(byte[] data, out int outputLength)
{
lock (DecompressedLock)
{
var seg = new ArraySegment<byte>(data);
if (!ConstMgr.EnableNativeDeflate)
{
return DecompressOnNative(seg, out outputLength);
}

GetDecompressInformation(out var zipStream, ref seg);
var ret = zipStream.GetDecompressedBytes(out outputLength, data.Length);
//push
DecompressStreams.Push(zipStream);
return ret;
}
}

/// <summary>
/// Decompress thr given bytes (NEED TO BE AWARE OF UNMANAGED INTPTR, REMEMBER TO FREE IT)
/// </summary>
/// <param name="data"></param>
/// <param name="outputLength"></param>
/// <returns></returns>
public static IntPtr Decompress(ArraySegment<byte> data, out int outputLength)
{
lock (DecompressedLock)
{
Expand All @@ -158,7 +183,7 @@ public static IntPtr Decompress(byte[] data, out int outputLength)
}

GetDecompressInformation(out var zipStream, ref data);
var ret = zipStream.GetDecompressedBytes(out outputLength, data.Length);
var ret = zipStream.GetDecompressedBytes(out outputLength, data.Count);
//push
DecompressStreams.Push(zipStream);
return ret;
Expand All @@ -171,6 +196,36 @@ public static IntPtr Decompress(byte[] data, out int outputLength)
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Decompress(byte[] data)
{
lock (DecompressedLock)
{
var seg = new ArraySegment<byte>(data);
if (!ConstMgr.EnableNativeDeflate)
{
var ptr = DecompressOnNative(seg, out var length);
byte[] buf = new byte[length];
Marshal.Copy(ptr, buf, 0, length);
Marshal.FreeHGlobal(ptr);
return buf;
}

GetDecompressInformation(out var zipStream, ref seg);
var ret = zipStream.GetDecompressedBytes(out var len, data.Length);
//push
DecompressStreams.Push(zipStream);
var buffer = new byte[len];
Marshal.Copy(ret, buffer, 0, len);
Marshal.FreeHGlobal(ret);
return buffer;
}
}

/// <summary>
/// Decompress thr given bytes
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Decompress(ArraySegment<byte> data)
{
lock (DecompressedLock)
{
Expand All @@ -184,7 +239,7 @@ public static byte[] Decompress(byte[] data)
}

GetDecompressInformation(out var zipStream, ref data);
var ret = zipStream.GetDecompressedBytes(out var len, data.Length);
var ret = zipStream.GetDecompressedBytes(out var len, data.Count);
//push
DecompressStreams.Push(zipStream);
var buffer = new byte[len];
Expand All @@ -200,7 +255,7 @@ public static byte[] Decompress(byte[] data)
/// </summary>
/// <param name="zipStream"></param>
/// <param name="data"></param>
private static void GetDecompressInformation(out DeflateStream zipStream, ref byte[] data)
private static void GetDecompressInformation(out DeflateStream zipStream, ref ArraySegment<byte> data)
{
lock (DecompressedLock)
{
Expand All @@ -227,14 +282,15 @@ private static void GetDecompressInformation(out DeflateStream zipStream, ref by
/// Compress the given bytes
/// </summary>
/// <param name="data"></param>
/// <param name="length"></param>
/// <returns></returns>
private static byte[] CompressOnNative(byte[] data, int length)
private static byte[] CompressOnNative(ArraySegment<byte> data)
{
using (var compressedStream = new MemoryStream())
using (var zipStream = new System.IO.Compression.DeflateStream(compressedStream, CompressionMode.Compress))
{
zipStream.Write(data, 0, length);
// ReSharper disable AssignNullToNotNullAttribute
zipStream.Write(data.Array, data.Offset, data.Count);
// ReSharper restore AssignNullToNotNullAttribute
zipStream.Close();
return compressedStream.ToArray();
}
Expand Down Expand Up @@ -263,15 +319,15 @@ private static byte[] CompressOnNative(ExtensibleBuffer<byte> data, int length)
/// <param name="data"></param>
/// <param name="len"></param>
/// <returns></returns>
private static IntPtr DecompressOnNative(byte[] data, out int len)
private static IntPtr DecompressOnNative(ArraySegment<byte> data, out int len)
{
FlexibleStream result = ObjectPool<FlexibleStream>.Request();
result.Reset();
FlexibleStream compressedStream;
if (ObjectPool<FlexibleStream>.Peak() != null)
{
compressedStream = ObjectPool<FlexibleStream>.Request();
compressedStream.ChangeBuffer(data);
compressedStream.ChangeBuffer(data.Array, data.Offset, data.Count);
}
else
{
Expand Down
Loading