473,388 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Fastest way to pass large array to and from COM

apm
Any and all:

Is there an efficient way to pass a large array from .NET to COM? Can
references (or pointer) be passed from COM to NET and NET to COM without the
object it refers to being copied?

Thanks in advance.

David
Nov 17 '05 #1
5 2692
David,

It depends on the interface. If you are using an Automation interface
and passing a SafeArray, then the runtime is going to have to create the
safearray and marshal the values into it.

However, if you are using COM interfaces that are not Automation based,
you can pass C-style arrays to the methods. Normally, the marshaller would
marshal the values over and cause a copy in most cases (where you are not
dealing with reference types). But, if you have an unsafe declaration that
takes a pointer to the type that contains the array, you can get the address
of the array and pass that.

You can ONLY do this with non-reference types though. Doing it on a
reference type would be pointless, since the marshaller has to marshal the
..NET reference to an unmanaged thunk that unmanaged code can work with.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:LlEWe.11756$nq.8691@lakeread05...
Any and all:

Is there an efficient way to pass a large array from .NET to COM? Can
references (or pointer) be passed from COM to NET and NET to COM without
the object it refers to being copied?

Thanks in advance.

David

Nov 17 '05 #2
apm
You can ONLY do this with non-reference types though. Doing it on a
reference type would be pointless, since the marshaller has to marshal the
.NET reference to an unmanaged thunk that unmanaged code can work with.

Hope this helps.

Great help. Thank you. Will you offer further enlightenment on the subject
of what happens with reference types? This seems like it may be a bigger
problem.

If .NET would copy only once and use the copy in subsequent calls there
would be no problem. The problem I have is that a number of COM objects
were developed that support methods that may be called many thousands of
times before a run is complete. At one time the plan was to do future
development using .NET. Unfortunately the code took significantly (very,
very) longer to run when COM and .NET components were mixed.

David

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:LlEWe.11756$nq.8691@lakeread05...
Any and all:

Is there an efficient way to pass a large array from .NET to COM? Can
references (or pointer) be passed from COM to NET and NET to COM without
the object it refers to being copied?

Thanks in advance.

David


Nov 17 '05 #3
apm,

With reference types, the types have to be marshaled to the unmanaged
world. With an array of reference types, an unmanaged interface thunk is
created which can marshal the calls back to the managed realm.

You can optmize this though, if you have a good deal of processing to
do. What you can do is create an array of IntPtrs, one for each element in
your array of reference types. Then, you can cycle through your array,
passing the object to the static GetIUnknownForObject method on the Marshal
class, and placing the IntPtr in the array. Then, you can pass this array
to a method that accepts an array of interface pointers (not a SafeArray),
through unsafe code, and it should reduce the overhead of making multiple
calls.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:b2FWe.11759$nq.4936@lakeread05...
You can ONLY do this with non-reference types though. Doing it on a
reference type would be pointless, since the marshaller has to marshal
the .NET reference to an unmanaged thunk that unmanaged code can work
with.

Hope this helps.


Great help. Thank you. Will you offer further enlightenment on the subject
of what happens with reference types? This seems like it may be a bigger
problem.

If .NET would copy only once and use the copy in subsequent calls there
would be no problem. The problem I have is that a number of COM objects
were developed that support methods that may be called many thousands of
times before a run is complete. At one time the plan was to do future
development using .NET. Unfortunately the code took significantly (very,
very) longer to run when COM and .NET components were mixed.

David

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:LlEWe.11756$nq.8691@lakeread05...
Any and all:

Is there an efficient way to pass a large array from .NET to COM? Can
references (or pointer) be passed from COM to NET and NET to COM without
the object it refers to being copied?

Thanks in advance.

David



Nov 17 '05 #4
You have to make a distinction between calling into COM and calling into
..NET.
When calling into COM from managed code, no marshaling is done when passing
an array, that is a reference is passed to the callee, no data is copied.
However, when calling into managed code from COM, the (Safe) array is copied
from unmanaged memory to managed memory.
The reason is simple, to be accesible by managed code, the array must
somehow be stored in the GC heap as an array of Type xxx, there is NO other
way to achieve this than by copying from unmanaged code to the managed heap.
When passed from managed code to COM, the managed array is wrapped in a
Safearray who's pvData field (a pointer) points to the adress of the first
array element, and this SafeArray is passed by reference.

Not sure what your problem is, copying a 2MB array for instance takes 1/10th
of the time needed to fill the array itself at the COM side (calling
SafeArrayPutElement).
Willy.

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:LlEWe.11756$nq.8691@lakeread05...
Any and all:

Is there an efficient way to pass a large array from .NET to COM? Can
references (or pointer) be passed from COM to NET and NET to COM without
the object it refers to being copied?

Thanks in advance.

David

Nov 17 '05 #5
Hi David,

I see your problem. Of course there is the marshaling overhead when calling
into .NET and passing array's (SafeArray) from COM to the CLR. The major
overhead is due to the copying (memcpy) of the array to a managed array
reference type, the cost of marshaling back into COM is less important.

To give you an idea, marshaling a 20000 elem. Array of double's takes
~250µsec[*]. When using copy-in semantics (SomeMethod([In] arg...))

And ~300µsec when using copy in/out semantics ( SomeMethod([In, Out]
arg...))

You can reduce this call overhead to something like 20-30 µsec. when passing
a pointer to a C style array from COM to .NET in an unsafe context. Of
course this restrict you to use C++ as your COM components implementation
language, and because C style array's are not self describing, you have to
pass the size of the array as argument too.

For instance ...

unsafe public void SomeMethod([In, Out]double* dar, int al)

{ // use array indexed syntax to access elements

dar[n] = ...;

Now all depends on the service time (the time spent in the called method),
is this one is large compared to the marshaling time, you won't take much
advantage of pointer marshaling, however, if the call time is small and the
number of calls is high you can effectively reduce the call overhead by
using pointer marshaling.
Willy.
[*] PS the performance figures are those measured on a 3 Ghz P4 system
running XP SP2, you mileage may vary.

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:b2FWe.11759$nq.4936@lakeread05...
You can ONLY do this with non-reference types though. Doing it on a
reference type would be pointless, since the marshaller has to marshal
the .NET reference to an unmanaged thunk that unmanaged code can work
with.

Hope this helps.


Great help. Thank you. Will you offer further enlightenment on the subject
of what happens with reference types? This seems like it may be a bigger
problem.

If .NET would copy only once and use the copy in subsequent calls there
would be no problem. The problem I have is that a number of COM objects
were developed that support methods that may be called many thousands of
times before a run is complete. At one time the plan was to do future
development using .NET. Unfortunately the code took significantly (very,
very) longer to run when COM and .NET components were mixed.

David

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"apm" <Co*********@AdsorptionProcessModeling.com> wrote in message
news:LlEWe.11756$nq.8691@lakeread05...
Any and all:

Is there an efficient way to pass a large array from .NET to COM? Can
references (or pointer) be passed from COM to NET and NET to COM without
the object it refers to being copied?

Thanks in advance.

David



Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jonathan | last post by:
I am hoping that someone more experienced than myself can point me towards what might be the fastest data lookup method to use for storing ip addresses. My situation is that I will need to maintain...
4
by: Danny Smith | last post by:
Can anyone help? I want to find the fastest way of inserting a large number of records (50,000+) into a SQL Server database (using C#). The records are held in memory and the options I can...
5
by: Loui Mercieca | last post by:
Hi, In my design i have a data structure used to store large amount of numbers ( in the range of lots of thousands ). Each element contains 3 items and the no of elements are dynamic.. of the...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
4
by: steve | last post by:
I want to set all elements in a numeric array to a value other than 0. The default numeric initialisers set the elements to 0. Is looped assignment the fastest way, or is there an equivalent to the...
14
by: romayankin | last post by:
Hello All, I'm writing cross-platform code so i'm bound to standards. Here is the code I have: ~~~~~~~~~~~~~~~~~~ double **mx = new double*; for(int i = 0; i < col - 1; i++) { mx = new...
24
by: ThunderMusic | last post by:
Hi, The subject says it all... I want to use a byte and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic
9
by: JRough | last post by:
I tried to pass the $result from a mysql_query in a url like this line Header("Location:clm_historyXL.php?_result=".$result); but on the redirect location clm_history.php page I get an error on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.