- Notifications
You must be signed in to change notification settings - Fork752
Python to CLR string marshaling LRU cache.#538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
9de6f7b
e9e5c60
64acfcb
7898833
ed29200
fc549e3
06f4faa
5136c85
507edc9
741070b
88c9bc8
ffceeed
f7433f6
b200bcb
0df879e
382c4d6
1865e21
46f2498
49d0d89
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using NUnit.Framework; | ||
using Python.Runtime; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using NUnit.Framework; | ||
using Python.Runtime; | ||
namespace Python.EmbeddingTest | ||
{ | ||
[SetUpFixture] | ||
public class TestsSuite | ||
{ | ||
[OneTimeTearDown] | ||
public void FinalCleanup() | ||
{ | ||
if (PythonEngine.IsInitialized) | ||
{ | ||
PythonEngine.Shutdown(); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -18,7 +18,7 @@ public object MarshalNativeToManaged(IntPtr pNativeData) | ||
public abstract IntPtr MarshalManagedToNative(object managedObj); | ||
publicvirtualvoid CleanUpNativeData(IntPtr pNativeData) | ||
{ | ||
Marshal.FreeHGlobal(pNativeData); | ||
} | ||
@@ -44,7 +44,12 @@ internal class UcsMarshaler : MarshalerBase | ||
private static readonly MarshalerBase Instance = new UcsMarshaler(); | ||
private static readonly Encoding PyEncoding = Runtime.PyEncoding; | ||
private const int MaxStringLength = 100; | ||
private const int MaxItemSize = 4 * (MaxStringLength + 1); | ||
private static readonly EncodedStringsFifoDictionary EncodedStringsDictionary = | ||
new EncodedStringsFifoDictionary(10000, MaxItemSize); | ||
public override unsafe IntPtr MarshalManagedToNative(object managedObj) | ||
{ | ||
var s = managedObj as string; | ||
@@ -53,16 +58,36 @@ public override IntPtr MarshalManagedToNative(object managedObj) | ||
return IntPtr.Zero; | ||
} | ||
IntPtr mem; | ||
int stringBytesCount; | ||
if (s.Length <= MaxStringLength) | ||
{ | ||
if (EncodedStringsDictionary.TryGetValue(s, out mem)) | ||
{ | ||
return mem; | ||
} | ||
stringBytesCount = PyEncoding.GetByteCount(s); | ||
mem = EncodedStringsDictionary.AddUnsafe(s); | ||
} | ||
else | ||
{ | ||
stringBytesCount = PyEncoding.GetByteCount(s); | ||
mem = Marshal.AllocHGlobal(stringBytesCount + 4); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Why | ||
} | ||
fixed (char* str = s) | ||
{ | ||
try | ||
{ | ||
PyEncoding.GetBytes(str, s.Length, (byte*)mem, stringBytesCount); | ||
} | ||
catch | ||
{ | ||
// Do nothing with this. Very strange problem. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. When does this happen? | ||
} | ||
*(int*)(mem + stringBytesCount) = 0; | ||
} | ||
return mem; | ||
@@ -106,6 +131,14 @@ public static int GetUnicodeByteLength(IntPtr p) | ||
} | ||
} | ||
public override void CleanUpNativeData(IntPtr pNativeData) | ||
{ | ||
if (!EncodedStringsDictionary.IsKnownPtr(pNativeData)) | ||
{ | ||
base.CleanUpNativeData(pNativeData); | ||
} | ||
} | ||
/// <summary> | ||
/// Utility function for Marshaling Unicode on PY3 and AnsiStr on PY2. | ||
/// Use on functions whose Input signatures changed between PY2/PY3. | ||
@@ -118,11 +151,29 @@ public static int GetUnicodeByteLength(IntPtr p) | ||
/// <remarks> | ||
/// You MUST deallocate the IntPtr of the Return when done with it. | ||
/// </remarks> | ||
publicunsafestatic IntPtr Py3UnicodePy2StringtoPtr(string s) | ||
{ | ||
if (Runtime.IsPython3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This means that your optimisation will only work at all for Python 3, is that necessary? | ||
{ | ||
int stringBytesCount = PyEncoding.GetByteCount(s); | ||
IntPtr mem = Marshal.AllocHGlobal(stringBytesCount + 4); | ||
fixed (char* str = s) | ||
{ | ||
try | ||
{ | ||
PyEncoding.GetBytes(str, s.Length, (byte*)mem, stringBytesCount); | ||
} | ||
catch | ||
{ | ||
// Do nothing with this. Very strange problem. | ||
} | ||
*(int*)(mem + stringBytesCount) = 0; | ||
} | ||
return mem; | ||
} | ||
return Marshal.StringToHGlobalAnsi(s); | ||
} | ||
/// <summary> | ||
@@ -208,7 +259,12 @@ internal class Utf8Marshaler : MarshalerBase | ||
private static readonly MarshalerBase Instance = new Utf8Marshaler(); | ||
private static readonly Encoding PyEncoding = Encoding.UTF8; | ||
private const int MaxStringLength = 100; | ||
private static readonly EncodedStringsFifoDictionary EncodedStringsDictionary = | ||
new EncodedStringsFifoDictionary(10000, 4 * (MaxStringLength + 1)); | ||
public override unsafe IntPtr MarshalManagedToNative(object managedObj) | ||
{ | ||
var s = managedObj as string; | ||
@@ -217,21 +273,49 @@ public override IntPtr MarshalManagedToNative(object managedObj) | ||
return IntPtr.Zero; | ||
} | ||
IntPtr mem; | ||
int stringBytesCount; | ||
if (s.Length <= MaxStringLength) | ||
{ | ||
if (EncodedStringsDictionary.TryGetValue(s, out mem)) | ||
{ | ||
return mem; | ||
} | ||
stringBytesCount = PyEncoding.GetByteCount(s); | ||
mem = EncodedStringsDictionary.AddUnsafe(s); | ||
} | ||
else | ||
{ | ||
stringBytesCount = PyEncoding.GetByteCount(s); | ||
mem = Marshal.AllocHGlobal(stringBytesCount + 1); | ||
} | ||
fixed (char* str = s) | ||
{ | ||
try | ||
{ | ||
PyEncoding.GetBytes(str, s.Length, (byte*)mem, stringBytesCount); | ||
} | ||
catch | ||
{ | ||
// Do nothing with this. Very strange problem. | ||
} | ||
((byte*)mem)[stringBytesCount] = 0; | ||
} | ||
return mem; | ||
} | ||
public override void CleanUpNativeData(IntPtr pNativeData) | ||
{ | ||
if (!EncodedStringsDictionary.IsKnownPtr(pNativeData)) | ||
{ | ||
base.CleanUpNativeData(pNativeData); | ||
} | ||
} | ||
public static ICustomMarshaler GetInstance(string cookie) | ||
{ | ||
return Instance; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
namespace Python.Runtime | ||
{ | ||
using System.Runtime.InteropServices; | ||
public class EncodedStringsFifoDictionary: IDisposable | ||
{ | ||
private readonly FifoDictionary<string, IntPtr> _innerDictionary; | ||
private readonly IntPtr _rawMemory; | ||
private readonly int _allocatedSize; | ||
public EncodedStringsFifoDictionary(int capacity, int maxItemSize) | ||
{ | ||
if (maxItemSize < 1) | ||
{ | ||
throw new ArgumentOutOfRangeException( | ||
nameof(maxItemSize), | ||
"Maximum item size should be non-zero positive."); | ||
} | ||
_innerDictionary = new FifoDictionary<string, IntPtr>(capacity); | ||
_allocatedSize = maxItemSize * capacity; | ||
_rawMemory = Marshal.AllocHGlobal(_allocatedSize); | ||
MaxItemSize = maxItemSize; | ||
} | ||
public int MaxItemSize { get; } | ||
public bool TryGetValue(string key, out IntPtr value) | ||
{ | ||
return _innerDictionary.TryGetValue(key, out value); | ||
} | ||
public IntPtr AddUnsafe(string key) | ||
{ | ||
int nextSlot = _innerDictionary.NextSlotToAdd; | ||
IntPtr ptr = _rawMemory + (MaxItemSize * nextSlot); | ||
_innerDictionary.AddUnsafe(key, ptr); | ||
return ptr; | ||
} | ||
public bool IsKnownPtr(IntPtr ptr) | ||
{ | ||
var uptr = (ulong)ptr; | ||
var umem = (ulong)_rawMemory; | ||
return uptr >= umem && uptr < umem + (ulong)_allocatedSize; | ||
} | ||
private void ReleaseUnmanagedResources() | ||
{ | ||
if (_rawMemory != IntPtr.Zero) | ||
{ | ||
Marshal.FreeHGlobal(_rawMemory); | ||
} | ||
} | ||
public void Dispose() | ||
{ | ||
ReleaseUnmanagedResources(); | ||
GC.SuppressFinalize(this); | ||
} | ||
~EncodedStringsFifoDictionary() | ||
{ | ||
ReleaseUnmanagedResources(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
namespace Python.Runtime | ||
{ | ||
#if !NETSTANDARD | ||
/// <summary> | ||
/// This polyfill is thread unsafe. | ||
/// </summary> | ||
[CLSCompliant(false)] | ||
public static class EncodingGetStringPolyfill | ||
{ | ||
private static readonly MethodInfo PlatformGetStringMethodInfo = | ||
typeof(Encoding).GetMethod( | ||
"GetString", | ||
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, | ||
new[] | ||
{ | ||
typeof(byte*), typeof(int) | ||
}, null); | ||
private static readonly byte[] StdDecodeBuffer = PlatformGetStringMethodInfo == null ? new byte[1024 * 1024] : null; | ||
private static Dictionary<Encoding, EncodingGetStringUnsafeDelegate> PlatformGetStringMethodsDelegatesCache = new Dictionary<Encoding, EncodingGetStringUnsafeDelegate>(); | ||
private unsafe delegate string EncodingGetStringUnsafeDelegate(byte* pstr, int size); | ||
public unsafe static string GetString(this Encoding encoding, byte* pstr, int size) | ||
{ | ||
if (PlatformGetStringMethodInfo != null) | ||
{ | ||
EncodingGetStringUnsafeDelegate getStringDelegate; | ||
if (!PlatformGetStringMethodsDelegatesCache.TryGetValue(encoding, out getStringDelegate)) | ||
{ | ||
getStringDelegate = | ||
(EncodingGetStringUnsafeDelegate)Delegate.CreateDelegate( | ||
typeof(EncodingGetStringUnsafeDelegate), encoding, PlatformGetStringMethodInfo); | ||
PlatformGetStringMethodsDelegatesCache.Add(encoding, getStringDelegate); | ||
} | ||
return getStringDelegate(pstr, size); | ||
} | ||
byte[] buffer = size <= StdDecodeBuffer.Length ? StdDecodeBuffer : new byte[size]; | ||
Marshal.Copy((IntPtr)pstr, buffer, 0, size); | ||
return encoding.GetString(buffer, 0, size); | ||
} | ||
} | ||
#endif | ||
} |
Uh oh!
There was an error while loading.Please reload this page.