Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db6c99a

Browse files
committed
Add CLRObject cache
1 parent 391628b commit db6c99a

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/runtime/classbase.cs

+5
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ public static void tp_dealloc(IntPtr ob)
276276
{
277277
Runtime.XDecref(dict);
278278
}
279+
var clrObj = self as CLRObject;
280+
if (clrObj != null)
281+
{
282+
CLRObject.ObjDict.Remove(clrObj.inst);
283+
}
279284
Runtime.PyObject_GC_UnTrack(self.pyHandle);
280285
Runtime.PyObject_GC_Del(self.pyHandle);
281286
Runtime.XDecref(self.tpHandle);

src/runtime/clrobject.cs

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
using System;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Runtime.InteropServices;
34

45
namespace Python.Runtime
56
{
67
internal class CLRObject : ManagedType
78
{
89
internal object inst;
9-
10+
internal static Dictionary<object, CLRObject> ObjDict { get; private set; } = new Dictionary<object, CLRObject>();
11+
1012
internal CLRObject(object ob, IntPtr tp)
1113
{
1214
IntPtr py = Runtime.PyType_GenericAlloc(tp, 0);
@@ -37,7 +39,15 @@ internal CLRObject(object ob, IntPtr tp)
3739

3840
internal static CLRObject GetInstance(object ob, IntPtr pyType)
3941
{
40-
return new CLRObject(ob, pyType);
42+
CLRObject clrObj;
43+
if (ObjDict.TryGetValue(ob, out clrObj))
44+
{
45+
Runtime.XIncref(clrObj.pyHandle);
46+
return clrObj;
47+
}
48+
clrObj = new CLRObject(ob, pyType);
49+
ObjDict.Add(ob, clrObj);
50+
return clrObj;
4151
}
4252

4353

@@ -57,6 +67,12 @@ internal static IntPtr GetInstHandle(object ob, IntPtr pyType)
5767

5868
internal static IntPtr GetInstHandle(object ob, Type type)
5969
{
70+
CLRObject clrObj;
71+
if (ObjDict.TryGetValue(ob, out clrObj))
72+
{
73+
Runtime.XIncref(clrObj.pyHandle);
74+
return clrObj.pyHandle;
75+
}
6076
ClassBase cc = ClassManager.GetClass(type);
6177
CLRObject co = GetInstance(ob, cc.tpHandle);
6278
return co.pyHandle;

src/runtime/converter.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,9 @@ static IntPtr DefaultConverter(T value)
13901390
IntPtr pyValue = PyValueConverterHelper.Convert(item);
13911391
Runtime.PyList_Append(list, pyValue);
13921392
Runtime.XDecref(pyValue);
1393+
return list;
13931394
}
1395+
return list;
13941396
}
13951397
return value.ToPythonPtr();
13961398
//return CLRObject.GetInstHandle(value);

0 commit comments

Comments
 (0)