It is my first time with COM interoperability and I am stuck at the following problem:
Consider the following .NET component to store JScript values in a static ConcurrentDictionary to be shared between threads/sessions:
namespace Test
{
[ComVisible(true)]
[Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ITestClass
{
[DispId(1)]
bool TryAdd(string key, object value);
[DispId(2)]
object TryGetValue(string key);
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY")]
[ProgId("Test.Test")]
public class TestClass : ITestClass
{
static ConcurrentDictionary<string, object> dataDic = new ConcurrentDictionary<string, object>(Environment.ProcessorCount * 4, 256);
public bool TryAdd(string key, object value)
{
return dataDic.TryAdd(key, value);
}
public object TryGetValue(string key)
{
object o;
dataDic.TryGetValue(key, out o)
return o;
}
}
}
And consider the folllowing ASP Classic page (test.asp) that stores an array of objects in the shared/static ConcurrentDictionary using COM:
<script language="jscript" runat="server">
var dic=Server.CreateObject('Test.Test');
var objArray=[];
for(var n=0; n<5000; n++)
{
objArray.push({num:n,str:'test'+n});
}
dic.TryAdd('objectArray',objArray);
var result=dic.TryGetValue('objectArray'); // is it a safearray?
var iterable=result.toArray(); // is it a vbArray?
var nativeA=new Array(iterable.length); // native JScript array
var t1=(new Date()).getTime();
for(var i=0;i<iterable.length;i++)
{
nativeA[i]=iterable[i];
}
Response.write('Conversion time:'+(new Date()).getTime()-t1+' miliseconds');
</script>
When the script runs for the first time (first thread) and adds the rawData into the dictionary, the value returned by TryGetValue method is perfect (interation runs in 200ms).
The problem is that when the script is executed a second time (by another thread) to get the data added in the dictionary by the first thread, the iteration over the value returned by TryGetValue method takes 1000 times more time and rises the memory usage to the roof.
Why does it happen? How can I fix it?
Thank you