From dad928df02b23da86f956906af286cb4bd7023fe Mon Sep 17 00:00:00 2001 From: timyhac Date: Sun, 17 May 2026 09:03:55 +1000 Subject: [PATCH] Add `Span` overloads for `get/set_raw_bytes` This is anticipated to be part 1 of a series of performance-oriented changes around the native interop. --- src/libplctag.NativeImport/NativeMethods.cs | 4 +-- .../libplctag.NativeImport.csproj | 2 ++ src/libplctag.NativeImport/plctag.cs | 26 +++++++++++++++++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/libplctag.NativeImport/NativeMethods.cs b/src/libplctag.NativeImport/NativeMethods.cs index 7076d38..37d2ee2 100644 --- a/src/libplctag.NativeImport/NativeMethods.cs +++ b/src/libplctag.NativeImport/NativeMethods.cs @@ -257,9 +257,9 @@ static NativeMethods() [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_raw_bytes), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, [Out] byte[] buffer, int buffer_length); + public unsafe static extern int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, byte* buffer, int buffer_length); [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_raw_bytes), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, [In] byte[] buffer, int buffer_length); + public unsafe static extern int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, byte* buffer, int buffer_length); } } diff --git a/src/libplctag.NativeImport/libplctag.NativeImport.csproj b/src/libplctag.NativeImport/libplctag.NativeImport.csproj index 6dd8ce8..9141956 100644 --- a/src/libplctag.NativeImport/libplctag.NativeImport.csproj +++ b/src/libplctag.NativeImport/libplctag.NativeImport.csproj @@ -33,10 +33,12 @@ true true embedded + true + diff --git a/src/libplctag.NativeImport/plctag.cs b/src/libplctag.NativeImport/plctag.cs index 0da0f80..07eb31d 100644 --- a/src/libplctag.NativeImport/plctag.cs +++ b/src/libplctag.NativeImport/plctag.cs @@ -295,12 +295,34 @@ public static int plc_tag_get_string_total_length(Int32 tag_id, int string_start public static int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, byte[] buffer, int buffer_length) { - return NativeMethods.plc_tag_get_raw_bytes(tag_id, start_offset, buffer, buffer_length); + return plc_tag_get_raw_bytes(tag_id, start_offset, buffer.AsSpan(0, buffer_length)); + } + + public static int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, Span buffer) + { + unsafe + { + fixed (byte* ptr = buffer) + { + return NativeMethods.plc_tag_get_raw_bytes(tag_id, start_offset, ptr, buffer.Length); + } + } } public static int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, byte[] buffer, int buffer_length) { - return NativeMethods.plc_tag_set_raw_bytes(tag_id, start_offset, buffer, buffer_length); + return plc_tag_set_raw_bytes(tag_id, start_offset, new ReadOnlySpan(buffer, 0, buffer_length)); + } + + public static int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, ReadOnlySpan buffer) + { + unsafe + { + fixed (byte* ptr = buffer) + { + return NativeMethods.plc_tag_set_raw_bytes(tag_id, start_offset, ptr, buffer.Length); + } + } }