8000 Performance improvements ClrObject - Dispose PyObj · saaib/pythonnet@f4a047b · GitHub
[go: up one dir, main page]

Skip to content

Commit f4a047b

Browse files
Performance improvements ClrObject - Dispose PyObj
- Increasing minor version to 14 - Adding `UnsafeDispose()` for `PyObject` which does not require acquiring/releasing the lock - Adding check before calling `SetArgsAndCause` for the `ClrObject`, the call only makes sense when we are an exception and causes an overhead
1 parent 69b7f6a commit f4a047b

File tree

8 files changed

+32
-11
lines changed

8 files changed

+32
-11
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 1.0.5.13
2+
current_version = 1.0.5.14
33
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\.(?P<release>[a-z]+)(?P<dev>\d+))?
44
serialize =
55
{major}.{minor}.{patch}.{release}{dev}

conda.recipe/meta.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package:
22
name: pythonnet
3-
version: "1.0.5.13"
3+
version: "1.0.5.14"
44

55
build:
66
skip: True # [not win]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def run(self):
485485

486486
setup(
487487
name="pythonnet",
488-
version="1.0.5.13",
488+
version="1.0.5.14",
489489
description=".Net and Mono integration for Python",
490490
url='https://pythonnet.github.io/',
491491
license='MIT',

src/SharedAssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
// Version Information. Keeping it simple. May need to revisit for Nuget
2626
// See: https://codingforsmarties.wordpress.com/2016/01/21/how-to-version-assemblies-destined-for-nuget/
2727
// AssemblyVersion can only be numeric
28-
[assembly: AssemblyVersion("1.0.5.13")]
28+
[assembly: AssemblyVersion("1.0.5.14")]

src/clrmodule/ClrModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void initclr()
5353
{
5454
#if USE_PYTHON_RUNTIME_VERSION
5555
// Has no effect until SNK works. Keep updated anyways.
56-
Version = new Version("1.0.5.12"),
56+
Version = new Version("1.0.5.14"),
5757
#endif
5858
CultureInfo = CultureInfo.InvariantCulture
5959
};

src/runtime/clrobject.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Runtime.InteropServices;
33

44
namespace Python.Runtime
@@ -29,9 +29,13 @@ internal CLRObject(object ob, IntPtr tp)
2929
gcHandle = gc;
3030
inst = ob;
3131

32-
// Fix the BaseException args (and __cause__ in case of Python 3)
33-
// slot if wrapping a CLR exception
34-
Exceptions.SetArgsAndCause(py);
32+
// for performance before calling SetArgsAndCause() lets check if we are an exception
33+
if (inst is Exception)
34+
{
35+
// Fix the BaseException args (and __cause__ in case of Python 3)
36+
// slot if wrapping a CLR exception
37+
Exceptions.SetArgsAndCause(py);
38+
}
3539
}
3640

3741

src/runtime/pyobject.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public object AsManagedObject(Type t)
101101
}
102102
return result;
103103
}
104-
104+
105105
/// <summary>
106106
/// As Method
107107
/// </summary>
@@ -156,6 +156,23 @@ public void Dispose()
156156
GC.SuppressFinalize(this);
157157
}
158158

159+
/// <summary>
160+
/// Unsafe Dispose Method.
161+
/// To be used when already owning the GIL lock. <see cref="Dispose()"/>
162+
/// </summary>
163+
public void UnsafeDispose()
164+
{
165+
if (!disposed)
166+
{
167+
if (!Runtime.IsFinalizing)
168+
{
169+
Runtime.XDecref(obj);
170+
obj = IntPtr.Zero;
171+
}
172+
disposed = true;
173+
}
174+
GC.SuppressFinalize(this);
175+
}
159176

160177
/// <summary>
161178
/// GetPythonType Method

src/runtime/resources/clr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Code in this module gets loaded into the main clr module.
33
"""
44

5-
__version__ = "1.0.5.13"
5+
__version__ = "1.0.5.14"
66

77

88
class clrproperty(object):

0 commit comments

Comments
 (0)
0