none
Interop performance problem (.NET to VB6) RRS feed

  • Question

  • Hello !
    I´m having performance problems with my Dll(C# Interop)
    I consume this Dll in a VB6 client to recieve a lot of events (about 2000 events per second). But the CPU usage is very high.

    My PC is a Intel Core2 - 1,86GHz, 2GB of RAM - Windows XP Professional
    I´m using Visual Studio 2005 Professional Edition and .NET Framework 2.0.50727 to made the Interop Dll and Visual Studio 6 (SP6) to made the client that consumes the interop Dll

    With 1000 events per second CPU go to 100% of usage. This is too high for an events like this:

        [Guid("6027F32B-9360-4615-9EFC-A6043806E64A")]
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface BovespaEvents
        {
            void UpdateNotify(string strategyname, string symbolname, string fieldname, float fieldvalue);
        }


    and the method called is

            public void UpdateEvent(DataObject sender)
            {
                try
                {
                    UpdateNotify(sender.strategy, sender.symbol, sender.field, sender.value);
                }
                catch (Exception err)
                {
                    LogError("UpdateEvent", err.Message);
                    if (err.InnerException != null)
                    {
                        LogError("UpdateEvent", err.InnerException.Message);
                    }
                }
            }


    and the class DataObject is

        [Serializable()]
        public class DataObject
        {
            public string strategy;
            public string symbol;
            public string field;
            public float value;

            ~DataObject()
            {
            }

        }

    As you see, is a simple event that is consumed in a VB6 client method without implementation, just for see the performance

    Private Sub myInteropClient_UpdateNotify(ByVal strategyname As String, ByVal symbolname As String, ByVal fieldname As String, ByVal fieldvalue As Single)
    '
    ' nothing here, just for see the performance
    '
    End Sub

    How i can improve the performance of this ?

    Thanks for all !

    Wednesday, December 2, 2009 7:01 PM

Answers

  • Unfortunately, COM interop is not particularly fast.  The best option would probably be to implement short lived functions like this in C# directly, or migrate them to VB.NET, to avoid the interop entirely.  You're going to have to do some work to try to help improve the interop performance across the COM boundary, if this is a requirement of yours.

    I recommend going through Microsoft's Interop Performance Checklist.  There are quite a few things here that could potentially help, in particular, if you can minimize the string usage, and decorate using [in] and [out] to reduce the amount of marshalling required.

    That being said, there are other things you can try.  They are documented, in detail, in Improving Interop Performance.
    Reed Copsey, Jr. - http://reedcopsey.com
    Wednesday, December 2, 2009 7:21 PM
    Moderator
  • You could use [in] on all string args of UpdateNotify, right? (Although I am not sure how much that would help)
    [in] and [out] and IntPtr are general advices, maybe not suitable for the code you pasted above.

    Try to read the articles above, implement their suggestions. If it doesn't improve your perf significantly, you will likely have to profile your code and see what takes so long.

    -Karel

    Wednesday, December 23, 2009 7:05 PM
    Moderator

All replies

  • Unfortunately, COM interop is not particularly fast.  The best option would probably be to implement short lived functions like this in C# directly, or migrate them to VB.NET, to avoid the interop entirely.  You're going to have to do some work to try to help improve the interop performance across the COM boundary, if this is a requirement of yours.

    I recommend going through Microsoft's Interop Performance Checklist.  There are quite a few things here that could potentially help, in particular, if you can minimize the string usage, and decorate using [in] and [out] to reduce the amount of marshalling required.

    That being said, there are other things you can try.  They are documented, in detail, in Improving Interop Performance.
    Reed Copsey, Jr. - http://reedcopsey.com
    Wednesday, December 2, 2009 7:21 PM
    Moderator
  • Thanks for the help Reed Copsey !

    But, how i can use [in] and [out] in my method/event ?
    Thursday, December 3, 2009 12:45 PM
  • And how I pass reference with IntPtr by an event ?

    Friday, December 4, 2009 6:46 PM
  • anyone here ?

    Monday, December 7, 2009 12:53 PM
  • You could use [in] on all string args of UpdateNotify, right? (Although I am not sure how much that would help)
    [in] and [out] and IntPtr are general advices, maybe not suitable for the code you pasted above.

    Try to read the articles above, implement their suggestions. If it doesn't improve your perf significantly, you will likely have to profile your code and see what takes so long.

    -Karel

    Wednesday, December 23, 2009 7:05 PM
    Moderator
  • Bruno, you refused the previous answers, but didn't mention why. There is no better general answer to your question than trying what was adviced or use profiler to measure the perf. Some problems are specific and don't have general answers.

    Please let us know how far did you get, if you tried all the advices from the replies and if we can mark them as answers.

    Thanks,
    -Karel
    Monday, January 25, 2010 7:25 PM
    Moderator
  • Also, please do not cross-post - I have noticed that you sent this question to about 10 forums. That is not way how forums should be used.

    Also this post gives pretty good explanation, why you may have problems with perf: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/15d1b148-e1c6-486a-adac-9a68f94afbb2
    Even though you unmarked it as answer, I bet that when you properly measure the perf, you will see that the time is no spend in COM interop, but rather somewhere else.

    -Karel

    Monday, January 25, 2010 7:32 PM
    Moderator