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

Improve performance of unwrapping .NET objects passed from Python#930

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

Merged
filmor merged 13 commits intopythonnet:masterfromfilmor:losttech-perf-interop
May 16, 2020
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
39add65
Improve performance of unwrapping .NET objects passed from Python
lostmsuJun 14, 2019
83e0539
Merge branch 'master' into losttech-perf-interop
filmorSep 11, 2019
bc8a992
Merge branch 'master' into losttech-perf-interop
filmorSep 12, 2019
d0d7545
Merge branch 'master' into losttech-perf-interop
filmorOct 10, 2019
d14105d
Merge branch 'master' into losttech-perf-interop
lostmsuNov 21, 2019
b480a0c
added Runtime.PyType_IsSameAsOrSubtype
lostmsuNov 21, 2019
3ecccf3
Merge branch 'master' into losttech-perf-interop
lostmsuFeb 21, 2020
34c2744
Merge branch 'master' into losttech-perf-interop
filmorFeb 26, 2020
d41c6bb
Merge branch 'master' into losttech-perf-interop
lostmsuMar 6, 2020
0564832
Update performance goals according to test results
lostmsuMar 7, 2020
59b2f29
Merge branch 'master' into losttech-perf-interop
lostmsuMay 14, 2020
2f16997
Merge branch 'master' into losttech-perf-interop
filmorMay 16, 2020
d3d6e50
Merge branch 'master' into losttech-perf-interop
filmorMay 16, 2020
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
1 change: 1 addition & 0 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,6 +24,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Added argument types information to "No method matches given arguments" message
- Moved wheel import in setup.py inside of a try/except to prevent pip collection failures
- Removes PyLong_GetMax and PyClass_New when targetting Python3
- Improved performance of calls from Python to C#
- Added support for converting python iterators to C# arrays
- Changed usage of obselete function GetDelegateForFunctionPointer(IntPtr, Type) to GetDelegateForFunctionPointer<TDelegate>(IntPtr)
- When calling C# from Python, enable passing argument of any type to a parameter of C# type `object` by wrapping it into `PyObject` instance. ([#881][i881])
Expand Down
4 changes: 2 additions & 2 deletionssrc/perf_tests/BenchmarkTests.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,14 +30,14 @@ public void SetUp()
public void ReadInt64Property()
{
double optimisticPerfRatio = GetOptimisticPerfRatio(this.summary.Reports);
AssertPerformanceIsBetterOrSame(optimisticPerfRatio, target: 0.66);
AssertPerformanceIsBetterOrSame(optimisticPerfRatio, target: 0.57);
}

[Test]
public void WriteInt64Property()
{
double optimisticPerfRatio = GetOptimisticPerfRatio(this.summary.Reports);
AssertPerformanceIsBetterOrSame(optimisticPerfRatio, target: 0.64);
AssertPerformanceIsBetterOrSame(optimisticPerfRatio, target: 0.57);
}

static double GetOptimisticPerfRatio(
Expand Down
2 changes: 1 addition & 1 deletionsrc/runtime/classbase.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -291,7 +291,7 @@ public static IntPtr tp_repr(IntPtr ob)
public static void tp_dealloc(IntPtr ob)
{
ManagedType self = GetManagedObject(ob);
IntPtr dict = Marshal.ReadIntPtr(ob, ObjectOffset.DictOffset(ob));
IntPtr dict = Marshal.ReadIntPtr(ob, ObjectOffset.TypeDictOffset(self.tpHandle));
if (dict != IntPtr.Zero)
{
Runtime.XDecref(dict);
Expand Down
2 changes: 1 addition & 1 deletionsrc/runtime/classderived.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -877,7 +877,7 @@ public static void Finalize(IPythonDerivedType obj)
// the C# object is being destroyed which must mean there are no more
// references to the Python object as well so now we can dealloc the
// python object.
IntPtr dict = Marshal.ReadIntPtr(self.pyHandle, ObjectOffset.DictOffset(self.pyHandle));
IntPtr dict = Marshal.ReadIntPtr(self.pyHandle, ObjectOffset.TypeDictOffset(self.tpHandle));
if (dict != IntPtr.Zero)
{
Runtime.XDecref(dict);
Expand Down
4 changes: 2 additions & 2 deletionssrc/runtime/clrobject.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,11 +14,11 @@ internal CLRObject(object ob, IntPtr tp)
long flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
if ((flags & TypeFlags.Subclass) != 0)
{
IntPtr dict = Marshal.ReadIntPtr(py, ObjectOffset.DictOffset(tp));
IntPtr dict = Marshal.ReadIntPtr(py, ObjectOffset.TypeDictOffset(tp));
if (dict == IntPtr.Zero)
{
dict = Runtime.PyDict_New();
Marshal.WriteIntPtr(py, ObjectOffset.DictOffset(tp), dict);
Marshal.WriteIntPtr(py, ObjectOffset.TypeDictOffset(tp), dict);
}
}

Expand Down
136 changes: 96 additions & 40 deletionssrc/runtime/interop.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Text;
Expand DownExpand Up@@ -68,11 +69,47 @@ public ModulePropertyAttribute()
}
}

internal static class ManagedDataOffsets
{
static ManagedDataOffsets()
{
FieldInfo[] fi = typeof(ManagedDataOffsets).GetFields(BindingFlags.Static | BindingFlags.Public);
for (int i = 0; i < fi.Length; i++)
{
fi[i].SetValue(null, -(i * IntPtr.Size) - IntPtr.Size);
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal class ObjectOffset
size = fi.Length * IntPtr.Size;
}

public static readonly int ob_data;
public static readonly int ob_dict;

private static int BaseOffset(IntPtr type)
{
Debug.Assert(type != IntPtr.Zero);
int typeSize = Marshal.ReadInt32(type, TypeOffset.tp_basicsize);
Debug.Assert(typeSize > 0 && typeSize <= ExceptionOffset.Size());
return typeSize;
}
public static int DataOffset(IntPtr type)
{
return BaseOffset(type) + ob_data;
}

public static int DictOffset(IntPtr type)
{
return BaseOffset(type) + ob_dict;
}

public static int Size { get { return size; } }

private static readonly int size;
}

internal static class OriginalObjectOffsets
{
staticObjectOffset()
staticOriginalObjectOffsets()
{
int size = IntPtr.Size;
var n = 0; // Py_TRACE_REFS add two pointers to PyObject_HEAD
Expand All@@ -83,42 +120,58 @@ static ObjectOffset()
#endif
ob_refcnt = (n + 0) * size;
ob_type = (n + 1) * size;
ob_dict = (n + 2) * size;
ob_data = (n + 3) * size;
}

public static int magic(IntPtr ob)
public static int Size { get { return size; } }

private static readonly int size =
#if PYTHON_WITH_PYDEBUG
4 * IntPtr.Size;
#else
2 * IntPtr.Size;
#endif

#if PYTHON_WITH_PYDEBUG
public static int _ob_next;
public static int _ob_prev;
#endif
public static int ob_refcnt;
public static int ob_type;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal class ObjectOffset
{
static ObjectOffset()
{
#if PYTHON_WITH_PYDEBUG
_ob_next = OriginalObjectOffsets._ob_next;
_ob_prev = OriginalObjectOffsets._ob_prev;
#endif
ob_refcnt = OriginalObjectOffsets.ob_refcnt;
ob_type = OriginalObjectOffsets.ob_type;

size = OriginalObjectOffsets.Size + ManagedDataOffsets.Size;
}

public static int magic(IntPtr type)
{
if ((Runtime.PyObject_TypeCheck(ob, Exceptions.BaseException) ||
(Runtime.PyType_Check(ob) && Runtime.PyType_IsSubtype(ob, Exceptions.BaseException))))
{
return ExceptionOffset.ob_data;
}
return ob_data;
return ManagedDataOffsets.DataOffset(type);
}

public static intDictOffset(IntPtrob)
public static intTypeDictOffset(IntPtrtype)
{
if ((Runtime.PyObject_TypeCheck(ob, Exceptions.BaseException) ||
(Runtime.PyType_Check(ob) && Runtime.PyType_IsSubtype(ob, Exceptions.BaseException))))
{
return ExceptionOffset.ob_dict;
}
return ob_dict;
return ManagedDataOffsets.DictOffset(type);
}

public static int Size(IntPtrob)
public static int Size(IntPtrpyType)
{
if ((Runtime.PyObject_TypeCheck(ob, Exceptions.BaseException) ||
(Runtime.PyType_Check(ob) && Runtime.PyType_IsSubtype(ob, Exceptions.BaseException))))
if (IsException(pyType))
{
return ExceptionOffset.Size();
}
#if PYTHON_WITH_PYDEBUG
return 6 * IntPtr.Size;
#else
return 4 * IntPtr.Size;
#endif

return size;
}

#if PYTHON_WITH_PYDEBUG
Expand All@@ -127,8 +180,15 @@ public static int Size(IntPtr ob)
#endif
public static int ob_refcnt;
public static int ob_type;
private static int ob_dict;
private static int ob_data;
private static readonly int size;

private static bool IsException(IntPtr pyObject)
{
var type = Runtime.PyObject_TYPE(pyObject);
return Runtime.PyType_IsSameAsOrSubtype(type, ofType: Exceptions.BaseException)
|| Runtime.PyType_IsSameAsOrSubtype(type, ofType: Runtime.PyTypeType)
&& Runtime.PyType_IsSubtype(pyObject, Exceptions.BaseException);
}
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
Expand All@@ -137,19 +197,17 @@ internal class ExceptionOffset
static ExceptionOffset()
{
Type type = typeof(ExceptionOffset);
FieldInfo[] fi = type.GetFields();
int size = IntPtr.Size;
FieldInfo[] fi = type.GetFields(BindingFlags.Static | BindingFlags.Public);
for (int i = 0; i < fi.Length; i++)
{
fi[i].SetValue(null, (i *size) +ObjectOffset.ob_type + size);
fi[i].SetValue(null, (i *IntPtr.Size) +OriginalObjectOffsets.Size);
}
}

public static int Size()
{
return ob_data + IntPtr.Size;
size = fi.Length * IntPtr.Size + OriginalObjectOffsets.Size + ManagedDataOffsets.Size;
}

public static int Size() { return size; }

// PyException_HEAD
// (start after PyObject_HEAD)
public static int dict = 0;
Expand All@@ -163,9 +221,7 @@ public static int Size()
public static int suppress_context = 0;
#endif

// extra c# data
public static int ob_dict;
public static int ob_data;
private static readonly int size;
}


Expand Down
2 changes: 1 addition & 1 deletionsrc/runtime/managedtype.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,7 +33,7 @@ internal static ManagedType GetManagedObject(IntPtr ob)
{
IntPtr op = tp == ob
? Marshal.ReadIntPtr(tp, TypeOffset.magic())
: Marshal.ReadIntPtr(ob, ObjectOffset.magic(ob));
: Marshal.ReadIntPtr(ob, ObjectOffset.magic(tp));
if (op == IntPtr.Zero)
{
return null;
Expand Down
2 changes: 1 addition & 1 deletionsrc/runtime/moduleobject.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -54,7 +54,7 @@ public ModuleObject(string name)
Runtime.XDecref(pyfilename);
Runtime.XDecref(pydocstring);

Marshal.WriteIntPtr(pyHandle, ObjectOffset.DictOffset(pyHandle), dict);
Marshal.WriteIntPtr(pyHandle, ObjectOffset.TypeDictOffset(tpHandle), dict);

InitializeModuleMembers();
}
Expand Down
5 changes: 5 additions & 0 deletionssrc/runtime/runtime.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1889,6 +1889,11 @@ internal static bool PyObject_TypeCheck(IntPtr ob, IntPtr tp)
return (t == tp) || PyType_IsSubtype(t, tp);
}

internal static bool PyType_IsSameAsOrSubtype(IntPtr type, IntPtr ofType)
{
return (type == ofType) || PyType_IsSubtype(type, ofType);
}

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyType_GenericNew(IntPtr type, IntPtr args, IntPtr kw);

Expand Down
6 changes: 3 additions & 3 deletionssrc/runtime/typemanager.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -87,7 +87,7 @@ internal static IntPtr CreateType(Type impl)
// Set tp_basicsize to the size of our managed instance objects.
Marshal.WriteIntPtr(type, TypeOffset.tp_basicsize, (IntPtr)ob_size);

var offset = (IntPtr)ObjectOffset.DictOffset(type);
var offset = (IntPtr)ObjectOffset.TypeDictOffset(type);
Marshal.WriteIntPtr(type, TypeOffset.tp_dictoffset, offset);

InitializeSlots(type, impl);
Expand DownExpand Up@@ -125,17 +125,17 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)

IntPtr base_ = IntPtr.Zero;
int ob_size = ObjectOffset.Size(Runtime.PyTypeType);
int tp_dictoffset = ObjectOffset.DictOffset(Runtime.PyTypeType);

// XXX Hack, use a different base class for System.Exception
// Python 2.5+ allows new style class exceptions but they *must*
// subclass BaseException (or better Exception).
if (typeof(Exception).IsAssignableFrom(clrType))
{
ob_size = ObjectOffset.Size(Exceptions.Exception);
tp_dictoffset = ObjectOffset.DictOffset(Exceptions.Exception);
}

int tp_dictoffset = ob_size + ManagedDataOffsets.ob_dict;

if (clrType == typeof(Exception))
{
base_ = Exceptions.Exception;
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp