0

I have an Event that is triggered by a COM Server up to many thousands times per seconds, it provides realtime price information from exchanges. In my code I can access this Event (C#):

    private static void COMDataStream_Price(int SymbolNr, float Price, float Volume, DateTime Time)
    { // my code goes here }

I want to process the incoming data as fast as possible, because I realized that when it is very busy at exchanges, the frequency of the Events are even higher and sometime the Stream seems to be stuck and no Events are then triggered anymore.

So what was my Idea? I wanted to use a ConcurrentQueue to queue the data and in parallel have several Workerthreads running which dequeue the items to process them.

But now I'm somehow stuck because I can not queue the data (which consists of the most important values int SymbolNr, float Price and DateTime Time) at once into the ConcurrentQueue without wrapping them into a Instance of a (generic) Class. Because this approach seems to me very time consuming, we are talking about instantiating maybe up to 100.000 objects per second.

Am I right? What is the best performant approach to deal with this situation?

flo
  • 559
  • 1
  • 8
  • 26
  • 100k/sec doesn't seem crazily high. Try wrapping them in a `struct` so that you don't create any pressure on the garbage collector. You can then enqueue/dequeue that struct from the queue. – Drew Noakes Oct 17 '16 at 11:31
  • Thanks but one rule of thumb when choosing between class and struct is: It logically represents a single value, similar to primitive types (integer, double, and so on), taken from https://msdn.microsoft.com/en-us/library/ms229017.aspx so I am not so sure about that? – flo Oct 17 '16 at 18:24
  • That is a rule of thumb. You have a specific requirement, and using a struct satisfies it. But with everything, if you're concerned about performance, measure it. From what you've described, I'd use a struct. – Drew Noakes Oct 17 '16 at 18:27
  • Ok many thanks anyway, I will try to measure that like you said. – flo Oct 17 '16 at 18:32
  • 1
    If you use a class, I'd expect to see a raised "% time in GC" metric, as well as lots of churn through Gen0 and Gen1. You wouldn't have any of that with structs. The only way the struct option might come out worse off is if the struct is so large that the cost of copying the value (rather than the reference) outweight the benefits. But I doubt it. Most high perf trading systems avoid any kind of allocation, as GC introduces random pauses in your system, which is usually unacceptable. – Drew Noakes Oct 17 '16 at 18:45
  • This sounds really convincing. The struct in my case will contain an int, a float (precision 3) and a DataTime type. I guess this will not be considered as "large" I hope. Thanks for the additional explanation! – flo Oct 17 '16 at 20:05
  • Use Tuple? public ConcurrentQueue<(int SymbolNr, float Price, float Volume, DateTime Time)> ObjectsToSync { get; set; } – Mark Dec 15 '21 at 13:24

0 Answers0