FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Python to CLR string marshaling LRU cache. by dmitriyse · Pull Request #538 · pythonnet/pythonnet · GitHub

Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension .cs  (20) .csproj  (2) .md  (1) All 3 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 4 additions & 0 deletions CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +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 All @@ -25,6 +27,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].

### Fixed

- Fixed secondary PythonEngine.Initialize call, all sensitive static variables now reseted.
This is a hidden bug. Once python cleaning up enough memory, objects from previous engine run becomes corrupted.
- Fixed Visual Studio 2017 compat ([#434][i434]) for setup.py
- Fixed crash on exit of the Python interpreter if a python class
derived from a .NET class has a `__namespace__` or `__assembly__`
Expand Down
1 change: 1 addition & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<Compile Include="TestPyWith.cs" />
<Compile Include="TestRuntime.cs" />
<Compile Include="TestPyScope.cs" />
<Compile Include="TestsSuite.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\runtime\Python.Runtime.csproj">
Expand Down
40 changes: 37 additions & 3 deletions src/embed_tests/TestPythonEngineProperties.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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;

Expand Down Expand Up @@ -109,18 +110,41 @@ public static void GetPythonHomeDefault()
[Test]
public void SetPythonHome()
{
// We needs to ensure that engine was started and shutdown at least once before setting dummy home.
// Otherwise engine will not run with dummy path with random problem.
if (!PythonEngine.IsInitialized)
{
PythonEngine.Initialize();
}

PythonEngine.Shutdown();

var pythonHomeBackup = PythonEngine.PythonHome;

var pythonHome = "/dummypath/";

PythonEngine.PythonHome = pythonHome;
PythonEngine.Initialize();

Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
PythonEngine.Shutdown();

// Restoring valid pythonhome.
PythonEngine.PythonHome = pythonHomeBackup;
}

[Test]
public void SetPythonHomeTwice()
{
// We needs to ensure that engine was started and shutdown at least once before setting dummy home.
// Otherwise engine will not run with dummy path with random problem.
if (!PythonEngine.IsInitialized)
{
PythonEngine.Initialize();
}
PythonEngine.Shutdown();

var pythonHomeBackup = PythonEngine.PythonHome;

var pythonHome = "/dummypath/";

PythonEngine.PythonHome = "/dummypath2/";
Expand All @@ -129,18 +153,29 @@ public void SetPythonHomeTwice()

Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
PythonEngine.Shutdown();

PythonEngine.PythonHome = pythonHomeBackup;
}

[Test]
public void SetProgramName()
{
if (PythonEngine.IsInitialized)
{
PythonEngine.Shutdown();
}

var programNameBackup = PythonEngine.ProgramName;

var programName = "FooBar";

PythonEngine.ProgramName = programName;
PythonEngine.Initialize();

Assert.AreEqual(programName, PythonEngine.ProgramName);
PythonEngine.Shutdown();

PythonEngine.ProgramName = programNameBackup;
}

[Test]
Expand All @@ -156,7 +191,7 @@ public void SetPythonPath()
string path = PythonEngine.PythonPath;
PythonEngine.Shutdown();

PythonEngine.ProgramName = path;
PythonEngine.PythonPath = path;
PythonEngine.Initialize();

Assert.AreEqual(path, PythonEngine.PythonPath);
Expand All @@ -171,7 +206,6 @@ public void SetPythonPathExceptionOn27()
Assert.Pass();
}

// Get previous path to avoid crashing Python
PythonEngine.Initialize();
string path = PythonEngine.PythonPath;
PythonEngine.Shutdown();
Expand Down
15 changes: 13 additions & 2 deletions src/embed_tests/TestRuntime.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
using System;
using System;
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest
{
public class TestRuntime
{
[OneTimeSetUp]
public void SetUp()
{
// We needs to ensure that no any engines are running.
if (PythonEngine.IsInitialized)
{
PythonEngine.Shutdown();
}
}

[Test]
public static void Py_IsInitializedValue()
{
Runtime.Runtime.Py_Finalize(); // In case another test left it on.
// We know for sure that all engines are shut down.
// Runtime.Runtime.Py_Finalize();
Assert.AreEqual(0, Runtime.Runtime.Py_IsInitialized());
Runtime.Runtime.Py_Initialize();
Assert.AreEqual(1, Runtime.Runtime.Py_IsInitialized());
Expand Down
18 changes: 18 additions & 0 deletions src/embed_tests/TestsSuite.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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();
}
}
}
}
126 changes: 105 additions & 21 deletions src/runtime/CustomMarshaler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public object MarshalNativeToManaged(IntPtr pNativeData)

public abstract IntPtr MarshalManagedToNative(object managedObj);

public void CleanUpNativeData(IntPtr pNativeData)
public virtual void 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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

When does this happen?

}

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

return mem;
Expand Down Expand 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)
public unsafe static IntPtr Py3UnicodePy2StringtoPtr(string s)
{
return Runtime.IsPython3
? Instance.MarshalManagedToNative(s)
: Marshal.StringToHGlobalAnsi(s);
if (Runtime.IsPython3)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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 Down Expand 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 deletions src/runtime/Python.Runtime.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff 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
10 changes: 4 additions & 6 deletions src/runtime/assemblymanager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ internal class AssemblyManager
// than it can end up referring to assemblies that are already unloaded (default behavior after unload appDomain -
// unless LoaderOptimization.MultiDomain is used);
// 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;
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;

// updated only under GIL?
private static Dictionary<string, int> probed;
private static Dictionary<string, int> probed = new Dictionary<string, int>(32);

// modified from event handlers below, potentially triggered from different .NET threads
private static ConcurrentQueue<Assembly> assemblies;
Expand All @@ -48,10 +49,7 @@ private AssemblyManager()
/// </summary>
internal static void Initialize()
{
namespaces = new ConcurrentDictionary<string, ConcurrentDictionary<Assembly, string>>();
probed = new Dictionary<string, int>(32);
//generics = new Dictionary<string, Dictionary<string, string>>();
assemblies = new ConcurrentQueue<Assembly>();
assemblies = new AssemblyList(16);
pypath = new List<string>(16);

AppDomain domain = AppDomain.CurrentDomain;
Expand Down
Loading

Back | FazBrowse Home | New Git URL