Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
Closed
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
19 commits
Select commitHold shift + click to select a range
9de6f7b
Runtime/Shutdown loop stores old caches and static variables. It's pr…
Aug 31, 2017
e9e5c60
Added all tests finalizer routine.
Sep 2, 2017
64acfcb
Bug: Py_Initialize/Py_Finalize calls during alive PythonEngine. Fixed.
Sep 2, 2017
7898833
Init/Shutdown state variable (IsFinalizing) fix.
Sep 2, 2017
ed29200
TestPythonEngineProperties fixes.
Sep 5, 2017
fc549e3
Managed to python string marshaling cache added.
Sep 6, 2017
06f4faa
Net40 compatibility fix.
Sep 6, 2017
5136c85
Build warnings fix.
Sep 7, 2017
507edc9
Update TestRuntime.cs
Oct 10, 2017
741070b
Python string to CLR string marshaling cache added.
Jul 3, 2017
88c9bc8
Merge branch 'master' into string-marshaling-cache
den-run-aiFeb 10, 2018
ffceeed
Merge branch 'master' into string-marshaling-cache
filmorOct 16, 2018
f7433f6
Merge branch 'master' into string-marshaling-cache
filmorOct 17, 2018
b200bcb
Merge branch 'master' into string-marshaling-cache
filmorNov 14, 2018
0df879e
Merge branch 'master' into string-marshaling-cache
filmorNov 14, 2018
382c4d6
Merge branch 'master' into string-marshaling-cache
filmorNov 15, 2018
1865e21
Merge branch 'master' into string-marshaling-cache
filmorNov 19, 2018
46f2498
Merge branch 'master' into string-marshaling-cache
filmorMar 6, 2019
49d0d89
Merge branch 'master' into string-marshaling-cache
filmorMar 19, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletionCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
## [unreleased][]

### Added

- Improved performance. String marshaling between python and clr now cached.
Cache reduces GC pressure and saves from extensive memory copying.
- Added support for embedding python into dotnet core 2.0 (NetStandard 2.0)
- Added new build system (pythonnet.15.sln) based on dotnetcore-sdk/xplat(crossplatform msbuild).
Currently there two side-by-side build systems that produces the same output (net40) from the same sources.
Expand Down
1 change: 1 addition & 0 deletionssrc/embed_tests/Python.EmbeddingTest.csproj
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -104,6 +104,7 @@
<Compile Include="TestPyWith.cs" />
<Compile Include="TestRuntime.cs" />
<Compile Include="TestPyScope.cs" />
<Compile Include="TestsSuite.cs" />
<Compile Include="TestTypeManager.cs" />
<Compile Include="GlobalTestsSetup.cs" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletionssrc/embed_tests/TestPythonEngineProperties.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using NUnit.Framework;
using Python.Runtime;

Expand Down
2 changes: 1 addition & 1 deletionsrc/embed_tests/TestRuntime.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,7 +34,7 @@ public static void PlatformCache()

// Don't shut down the runtime: if the python engine was initialized
// but not shut down by another test, we'd end up in a bad state.
}
}

[Test]
public static void Py_IsInitializedValue()
Expand Down
18 changes: 18 additions & 0 deletionssrc/embed_tests/TestsSuite.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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();
}
}
}
}
126 changes: 105 additions & 21 deletionssrc/runtime/CustomMarshaler.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,7 @@ public object MarshalNativeToManaged(IntPtr pNativeData)

public abstract IntPtr MarshalManagedToNative(object managedObj);

public void CleanUpNativeData(IntPtr pNativeData)
publicvirtualvoid CleanUpNativeData(IntPtr pNativeData)
{
Marshal.FreeHGlobal(pNativeData);
}
Expand All@@ -44,7 +44,12 @@ internal class UcsMarshaler : MarshalerBase
private static readonly MarshalerBase Instance = new UcsMarshaler();
private static readonly Encoding PyEncoding = Runtime.PyEncoding;

public override IntPtr MarshalManagedToNative(object managedObj)
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;

Expand All@@ -53,16 +58,36 @@ public override IntPtr MarshalManagedToNative(object managedObj)
return IntPtr.Zero;
}

byte[] bStr = PyEncoding.GetBytes(s + "\0");
IntPtr mem = Marshal.AllocHGlobal(bStr.Length);
try
IntPtr mem;
int stringBytesCount;
if (s.Length <= MaxStringLength)
{
Marshal.Copy(bStr, 0, mem, bStr.Length);
if (EncodedStringsDictionary.TryGetValue(s, out mem))
{
return mem;
}

stringBytesCount = PyEncoding.GetByteCount(s);
mem = EncodedStringsDictionary.AddUnsafe(s);
}
catch (Exception)
else
{
Marshal.FreeHGlobal(mem);
throw;
stringBytesCount = PyEncoding.GetByteCount(s);
mem = Marshal.AllocHGlobal(stringBytesCount + 4);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why+4, wouldn't one additional byte be enough?

}

fixed (char* str = s)
{
try
{
PyEncoding.GetBytes(str, s.Length, (byte*)mem, stringBytesCount);
}
catch
{
// Do nothing with this. Very strange problem.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

When does this happen?

}

*(int*)(mem + stringBytesCount) = 0;
}

return mem;
Expand DownExpand Up@@ -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.
Expand All@@ -118,11 +151,29 @@ public static int GetUnicodeByteLength(IntPtr p)
/// <remarks>
/// You MUST deallocate the IntPtr of the Return when done with it.
/// </remarks>
public static IntPtr Py3UnicodePy2StringtoPtr(string s)
publicunsafestatic IntPtr Py3UnicodePy2StringtoPtr(string s)
{
return Runtime.IsPython3
? Instance.MarshalManagedToNative(s)
: Marshal.StringToHGlobalAnsi(s);
if (Runtime.IsPython3)
Copy link
Member

Choose a reason for hiding this comment

The 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>
Expand DownExpand Up@@ -208,7 +259,12 @@ internal class Utf8Marshaler : MarshalerBase
private static readonly MarshalerBase Instance = new Utf8Marshaler();
private static readonly Encoding PyEncoding = Encoding.UTF8;

public override IntPtr MarshalManagedToNative(object managedObj)
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;

Expand All@@ -217,21 +273,49 @@ public override IntPtr MarshalManagedToNative(object managedObj)
return IntPtr.Zero;
}

byte[] bStr = PyEncoding.GetBytes(s + "\0");
IntPtr mem = Marshal.AllocHGlobal(bStr.Length);
try
IntPtr mem;
int stringBytesCount;
if (s.Length <= MaxStringLength)
{
Marshal.Copy(bStr, 0, mem, bStr.Length);
if (EncodedStringsDictionary.TryGetValue(s, out mem))
{
return mem;
}

stringBytesCount = PyEncoding.GetByteCount(s);
mem = EncodedStringsDictionary.AddUnsafe(s);
}
catch (Exception)
else
{
Marshal.FreeHGlobal(mem);
throw;
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;
Expand Down
6 changes: 6 additions & 0 deletionssrc/runtime/Python.Runtime.csproj
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -76,6 +76,12 @@
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="perf_utils\EncodedStringsFifoDictionary.cs" />
<Compile Include="perf_utils\EncodingGetStringPolyfill.cs" />
<Compile Include="perf_utils\FifoDictionary.cs" />
<Compile Include="perf_utils\RawImmutableMemBlock.cs" />
<Compile Include="perf_utils\RawMemoryFifoDictionary.cs" />
<Compile Include="perf_utils\RawMemUtils.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="..\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
Expand Down
1 change: 1 addition & 0 deletionssrc/runtime/assemblymanager.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,6 +27,7 @@ internal class AssemblyManager
// So for multidomain support it is better to have the dict. recreated for each app-domain initialization
private static ConcurrentDictionary<string, ConcurrentDictionary<Assembly, string>> namespaces =
new ConcurrentDictionary<string, ConcurrentDictionary<Assembly, string>>();

//private static Dictionary<string, Dictionary<string, string>> generics;
private static AssemblyLoadEventHandler lhandler;
private static ResolveEventHandler rhandler;
Expand Down
73 changes: 73 additions & 0 deletionssrc/runtime/perf_utils/EncodedStringsFifoDictionary.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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();
}
}
}
53 changes: 53 additions & 0 deletionssrc/runtime/perf_utils/EncodingGetStringPolyfill.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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

}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp