Skip to content

Commit e8cf561

Browse files
author
LLT21
committed
Testing faster conversions
1 parent 434b2bc commit e8cf561

5 files changed

Lines changed: 93 additions & 5 deletions

File tree

frameworks/CSharp/appmpower/src/appMpower.Orm/DotnetMethods.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public static class DotnetMethods
2121

2222
public static byte[] Db()
2323
{
24+
DbProviderFactory.SetConnectionString();
25+
var byteArray = RawDb.LoadSingleQueryBytes().GetAwaiter().GetResult();
26+
27+
return byteArray;
28+
29+
/*
2430
var world = RawDb.LoadSingleQueryRow().GetAwaiter().GetResult();
2531
2632
var memoryStream = new MemoryStream();
@@ -29,6 +35,7 @@ public static byte[] Db()
2935
_worldSerializer.Serialize(utf8JsonWriter, world);
3036
3137
return memoryStream.ToArray();
38+
*/
3239
}
3340

3441
public static byte[] Query(int queries)

frameworks/CSharp/appmpower/src/appMpower.Orm/NativeMethods.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public static void FreeHandlePointer(IntPtr handlePointer)
4545
[UnmanagedCallersOnly(EntryPoint = "Db")]
4646
public static unsafe IntPtr Db(int* length, IntPtr* handlePointer)
4747
{
48+
//DbProviderFactory.SetConnectionString();
49+
/*
4850
var world = RawDb.LoadSingleQueryRow().GetAwaiter().GetResult();
4951
5052
var memoryStream = new MemoryStream();
@@ -54,6 +56,10 @@ public static unsafe IntPtr Db(int* length, IntPtr* handlePointer)
5456
5557
*length = (int)utf8JsonWriter.BytesCommitted;
5658
byte[] byteArray = memoryStream.ToArray();
59+
*/
60+
61+
var byteArray = RawDb.LoadSingleQueryBytes().GetAwaiter().GetResult();
62+
*length = byteArray.Length;
5763

5864
GCHandle handle = GCHandle.Alloc(byteArray, GCHandleType.Pinned);
5965
// return the managed and byteArrayPointer pointer

frameworks/CSharp/appmpower/src/appMpower.Orm/RawDb.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ public static async Task<World> LoadSingleQueryRow()
2828
}
2929
}
3030

31+
public static async Task<byte[]> LoadSingleQueryBytes()
32+
{
33+
using var pooledConnection = new DbConnection(DbProviderFactory.ConnectionString);
34+
await pooledConnection.OpenAsync();
35+
36+
var (dbCommand, _) = CreateReadCommand(pooledConnection);
37+
38+
using (dbCommand)
39+
{
40+
var dataReader = await dbCommand.ExecuteReaderAsync(CommandBehavior.SingleRow & CommandBehavior.SequentialAccess);
41+
42+
dataReader.Read();
43+
44+
byte[] buffer = new byte[4];
45+
46+
BitConverter.TryWriteBytes(buffer.AsSpan(0, 2), dataReader.GetInt16(0));
47+
BitConverter.TryWriteBytes(buffer.AsSpan(2, 2), dataReader.GetInt16(1));
48+
49+
dataReader.Close();
50+
51+
return buffer;
52+
}
53+
}
54+
3155
public static async Task<World> LoadSingleQueryRowById(int id)
3256
{
3357
using var pooledConnection = new DbConnection(DbProviderFactory.ConnectionString);

frameworks/CSharp/appmpower/src/appMpower/Middleware/SingleQueryMiddleware.cs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
using System;
2+
using System.Buffers.Binary;
23
using System.Collections.Generic;
34
using System.Runtime.InteropServices;
5+
using System.Text.Json;
46
using System.Threading.Tasks;
57
using Microsoft.AspNetCore.Builder;
68
using Microsoft.AspNetCore.Http;
79
using Microsoft.Extensions.Primitives;
10+
using appMpower.Serializers;
11+
using System.Runtime.CompilerServices;
812

913
namespace appMpower;
1014

@@ -15,6 +19,9 @@ public class SingleQueryMiddleware
1519
private readonly static KeyValuePair<string, StringValues> _headerContentType =
1620
new KeyValuePair<string, StringValues>("Content-Type", new StringValues("application/json"));
1721

22+
[ThreadStatic]
23+
private static Utf8JsonWriter _utf8JsonWriter;
24+
1825
private readonly RequestDelegate _nextStage;
1926

2027
public SingleQueryMiddleware(RequestDelegate nextStage)
@@ -34,14 +41,42 @@ public unsafe Task Invoke(HttpContext httpContext)
3441
IntPtr handlePointer;
3542

3643
IntPtr bytePointer = NativeMethods.Db(out payloadLength, out handlePointer);
37-
byte[] json = new byte[payloadLength];
38-
Marshal.Copy(bytePointer, json, 0, payloadLength);
39-
NativeMethods.FreeHandlePointer(handlePointer);
4044

45+
//byte[] byteArray = new byte[payloadLength];
46+
47+
/*
48+
fixed (byte* destination = byteArray)
49+
{
50+
// Use Unsafe.CopyBlock to copy the memory from source to destination
51+
Unsafe.CopyBlock(destination, (void*)bytePointer, (uint)payloadLength);
52+
}
53+
*/
54+
55+
ReadOnlySpan<byte> sourceSpan = new ReadOnlySpan<byte>((void*)bytePointer, payloadLength);
56+
57+
// Create a managed array and copy the data
58+
//sourceSpan.CopyTo(byteArray);
59+
60+
//Marshal.Copy(bytePointer, byteArray, 0, payloadLength);
61+
//NativeMethods.FreeHandlePointer(handlePointer);
62+
63+
//byte[] byteArray = DotnetMethods.Db();
64+
65+
var pipeWriter = httpContext.Response.BodyWriter;
66+
Utf8JsonWriter utf8JsonWriter = _utf8JsonWriter ??= new Utf8JsonWriter(pipeWriter, new JsonWriterOptions { SkipValidation = true });
67+
utf8JsonWriter.Reset(pipeWriter);
68+
69+
WorldSerializer.Serialize(utf8JsonWriter,
70+
BinaryPrimitives.ReadInt16LittleEndian(sourceSpan.Slice(0, 2)),
71+
BinaryPrimitives.ReadInt16LittleEndian(sourceSpan.Slice(2, 2)));
72+
utf8JsonWriter.Flush();
73+
NativeMethods.FreeHandlePointer(handlePointer);
74+
4175
response.Headers.Add(
42-
new KeyValuePair<string, StringValues>("Content-Length", payloadLength.ToString()));
76+
new KeyValuePair<string, StringValues>("Content-Length", utf8JsonWriter.BytesCommitted.ToString()));
4377

44-
return response.Body.WriteAsync(json, 0, payloadLength);
78+
//return response.Body.WriteAsync(Encoding.UTF8.GetBytes(json), 0, Encoding.UTF8.GetBytes(json).Length);
79+
return Task.CompletedTask;
4580
}
4681

4782
return _nextStage(httpContext);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Text.Json;
3+
4+
namespace appMpower.Serializers
5+
{
6+
public static class WorldSerializer
7+
{
8+
public static void Serialize(Utf8JsonWriter utf8JsonWriter, Int16 id, Int16 randomNumber)
9+
{
10+
utf8JsonWriter.WriteStartObject();
11+
utf8JsonWriter.WriteNumber("id", id);
12+
utf8JsonWriter.WriteNumber("randomNumber", randomNumber);
13+
utf8JsonWriter.WriteEndObject();
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)