Watch
2
0
Fork
You've already forked raylib-cs
0

Testing another string approach using ArrayPool

- ArrayPool to reduce memory allocation.
- Allocate max number of bytes for performance.
This commit is contained in:
Rgebee 2021-12-21 12:41:53 +00:00
commit 64bbe88518
2 changed files with 92 additions and 49 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Text;
using System.Runtime.InteropServices;
@ -27,6 +28,28 @@ namespace Raylib_cs
#endregion
/// <summary>
/// Converts text to a UTF8 buffer for passing to native code.<br/>
/// Uses ArrayPool to reduce memory allocation.
/// </summary>
public ref struct UTF8Buffer
{
public byte[] buffer;
public UTF8Buffer(string text)
{
int length = (text.Length * 4) + 1;
buffer = ArrayPool<byte>.Shared.Rent(length);
int count = Encoding.UTF8.GetBytes(text.AsSpan(), buffer.AsSpan());
buffer[count] = 0;
}
public void Dispose()
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
/// <summary>
/// A raw string representation suitable for passing into many core SQLite apis. These will normally be pointers to
/// utf8 encoded bytes, with a trailing <c>\0</c> terminator. <see langword="null"/> strings can be represented as
@ -149,7 +172,7 @@ namespace Raylib_cs
/// <summary>
/// UTF16 String representation
/// Returns "NUL" on Null native type
/// Returns "NUL" on Null native type
/// </summary>
/// <returns></returns>
public override string ToString()