JanBoonen / TwsApiCpp

Improved TWS API POSIX C++ library for the Interactive Brokers (IB) TWS (same project as TwsApiC++ in Yahoo TWSAPI).
106 stars 56 forks source link

possible performance tweak (linux) #9

Open Patrck-C opened 6 years ago

Patrck-C commented 6 years ago

This is for linux only: In TwsApiL0.cpp, the select() call (line 463) does not need a timeout when EWrapper is multi-threaded. The timeout (1 usec) will cause unnecessary spinning in the message processing loop. I changed it in my copy and works so far with no timeout, should improve performance (measureably? I don't know). I'm not going to attempt to make any changes on github though, unless someone else can also test it. Or someone can maybe explain why the timeout is needed.

JanBoonen commented 6 years ago

Hi Patrick,

Thanks for the comment.

Take a closer look at method eDisconnect() at line 360 and at the message processing loop you are referring to. Each iteration of the loop it is checked whether the messages should be handled or not: m_EWrapperL0Impl.IsEnableCalls(),

That mechanism is in place for a disconnection to happen gracefully by waiting until the ongoing EWrapper callback is finished first (wait for 100 msec) before closing the connection, and to ensure the m_EWrapperL0Impl.connectionClosed() is called while the 'EReader' thread is executing EWrapper code.

To estimate the overhead, one might use the example Clients example program:

https://github.com/JanBoonen/TwsApiCpp/blob/master/TwsApiC%2B%2B/Test/Src/Clients.cpp

It opens 8 clients simultaneously, once all in single thread mode, and all once in multi-thread mode. In the single thread mode, it request some thick data (line 95) - easy to comment out. If I remmember wel, these 8 connections uses less than 1% of the cpu.

Hope this helps.

Kind regards, Jan

JanBoonen commented 6 years ago

Closed it wrongfully - pressed the wrong button.

FarFetchd commented 4 years ago

Yup, I can confirm this is a problem, and that removing that timeout fixes it with no ill effects for my own usage.

I noticed that my program incorporating TwsApiCpp was using a surprising amount of CPU on the Raspberry Pi that I run it on. I profiled it and found that the top entry was:

  %   cumulative   self              self     total
 time   seconds   seconds    calls   s/call   s/call  name
 68.66      3.68     3.68  1701553     0.00     0.00  EClientL0Impl::checkMessagesOnce(long)

Took a look at this issues section before diving into TwsApiCpp's guts too much, and hey what do you know! Thanks very much @Patrck-C !

FarFetchd commented 4 years ago

Sent #18 for this.