Skip to content

Commit

Permalink
Eliminate intermediate string conversion buffer.
Browse files Browse the repository at this point in the history
  Make use of unsafe string constructor that is able to convert native
  UTF-8 string straing into the string instance buffer.
  • Loading branch information
yuslepukhin committed Nov 28, 2023
1 parent e24733c commit e5fc5d4
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions csharp/src/Microsoft.ML.OnnxRuntime/OrtValue.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,16 @@ public ReadOnlyMemory<char> GetStringElementAsMemory(int index)
/// <returns>UTF-16 string instance</returns>
public string GetStringElement(int index)
{
var chars = GetStringTensorElementChars(index);
if (chars.Length == 0)
GetStringTensorElementBuffer((UIntPtr)index, out uint bytesLen, out IntPtr bufferPtr);
if (bytesLen == 0)
{
return string.Empty;
}
return new string(chars);

unsafe
{
return new string((sbyte*)bufferPtr.ToPointer(), 0, (int)bytesLen, Encoding.UTF8);
}
}


Expand Down

0 comments on commit e5fc5d4

Please sign in to comment.