Code Monkey home page Code Monkey logo

Comments (7)

Executor-Cheng avatar Executor-Cheng commented on August 18, 2024 1

@dellbeat 久等了, 我用 System.Reflection.Emit 去调用 JsonDocument.GetRawdata 方法了, 交给JIT生成asm后就不用考虑平台差异了
更新至2.1.6即可
附性能测试结果:

|            Method |     Mean |   Error |  StdDev |  Gen 0 | Allocated |
|------------------ |---------:|--------:|--------:|-------:|----------:|
|        ParseByPtr | 589.4 ns | 2.52 ns | 2.36 ns | 0.0496 |     528 B |
| ParseByReflection | 754.6 ns | 2.96 ns | 2.62 ns | 0.0610 |     648 B |
|       ParseByEmit | 591.5 ns | 3.36 ns | 2.98 ns | 0.0496 |     528 B |

from mirai-csharp.

Executor-Cheng avatar Executor-Cheng commented on August 18, 2024

和我用的奇技淫巧有关, 我会考虑加个检查的

public static T Deserialize<T>(this JsonElement element, JsonSerializerOptions? options = null)
{
JsonDocument documentRef = Unsafe.As<JsonElement, JsonDocument>(ref element);
int idxPtr = Unsafe.AddByteOffset(ref Unsafe.As<JsonElement, int>(ref element), new IntPtr(IntPtr.Size));
ReadOnlyMemory<byte> memory = GetRawdata(documentRef, idxPtr, true);
return JsonSerializer.Deserialize<T>(memory.Span, options)!;
}
private static readonly unsafe delegate*<JsonDocument, void*, int, bool, ref ReadOnlyMemory<byte>> _jsonDocumentGetRawdataWinPtr;
// ^ ^ ^ ^ ^
// | | | | \-- ret
// | | | \-- includeQuotes
// | | \-- idx
// \-- this \-- &this
// 751.1ns -> 577.1ns (-174ns, 76.83%) Alloc: -120B per invocation, this = JsonDocument
// (调用实例方法, 且不用什么MethodInfo.Invoke, 还有什么Delegate。不同方法会有不同的参数列表, 请结合实际)
private static readonly unsafe delegate*<JsonDocument, int, bool, ReadOnlyMemory<byte>> _jsonDocumentGetRawdataUnixPtr;
// ^ ^ ^ ^
// | | | \-- ret
// | | \-- includeQuotes
// | \-- idx
// \-- this
// this = JsonDocument
static unsafe Utils()
{
IntPtr methodPtr = (IntPtr)typeof(Delegate).GetField("_methodPtr", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(typeof(JsonDocument).GetMethod("GetRawValue", BindingFlags.NonPublic | BindingFlags.Instance)!.CreateDelegate(typeof(Func<int, bool, ReadOnlyMemory<byte>>), null))!;
_jsonDocumentGetRawdataWinPtr = (delegate*<JsonDocument, void*, int, bool, ref ReadOnlyMemory<byte>>)methodPtr;
_jsonDocumentGetRawdataUnixPtr = (delegate*<JsonDocument, int, bool, ReadOnlyMemory<byte>>)methodPtr;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static unsafe ReadOnlyMemory<byte> GetRawdata(JsonDocument j, int idx, bool includeQuotes)
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
return _jsonDocumentGetRawdataWinPtr(j, Unsafe.AsPointer(ref j), idx, includeQuotes);
}
return _jsonDocumentGetRawdataUnixPtr(j, idx, includeQuotes);
}

from mirai-csharp.

dellbeat avatar dellbeat commented on August 18, 2024

from mirai-csharp.

dellbeat avatar dellbeat commented on August 18, 2024

from mirai-csharp.

Executor-Cheng avatar Executor-Cheng commented on August 18, 2024

人有点晕, 找了半天没找到有什么黑科技能更加安全地调用 ReadOnlyMemory<byte> JsonDocument.GetRawValue(int, bool) 这个非公共方法的, 等我继续钻研一下
期间你可以把这一段改成下边这个凑合着用用

public static T Deserialize<T>(this JsonElement element, JsonSerializerOptions? options = null)
{
JsonDocument documentRef = Unsafe.As<JsonElement, JsonDocument>(ref element);
int idxPtr = Unsafe.AddByteOffset(ref Unsafe.As<JsonElement, int>(ref element), new IntPtr(IntPtr.Size));
ReadOnlyMemory<byte> memory = GetRawdata(documentRef, idxPtr, true);
return JsonSerializer.Deserialize<T>(memory.Span, options)!;
}

private static readonly MethodInfo _method = typeof(JsonDocument).GetMethod("GetRawValue", BindingFlags.NonPublic | BindingFlags.Instance)!;

public static T Deserialize<T>(this JsonElement element, JsonSerializerOptions? options = null)
{
    JsonDocument documentRef = Unsafe.As<JsonElement, JsonDocument>(ref element);
    int idx = Unsafe.AddByteOffset(ref Unsafe.As<JsonElement, int>(ref element), new IntPtr(IntPtr.Size));
    ReadOnlyMemory<byte> memory = (ReadOnlyMemory<byte>)_method.Invoke(documentRef, new object[] { idx, true })!;
    return JsonSerializer.Deserialize<T>(memory.Span)!;
}

from mirai-csharp.

dellbeat avatar dellbeat commented on August 18, 2024

那大佬先钻研着
我这边收消息之前用自制ws先混过去了XD

from mirai-csharp.

dellbeat avatar dellbeat commented on August 18, 2024

from mirai-csharp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.