title: Lua kills multithreading performance as thread count increases (Bar Graph Included)
body: <p>My Qt/C++ app uses worker threads (<code>QThread</code>) to improve performance for users with multicore processors. Each worker's job is to manipulate some data. Each worker minds it's own business and does not need to communicate with any other workers. They also don't perform any IO operations. Perfect use case!</p>
  <p>The use of multithreading for this workload has delightfully improved performance by many factors over.</p>
  <p>Running on a Ryzen 9 3900X (12 cores)</p>
  <hr />
  <p>However, now each worker is also tasked with passing it's data through a Lua script. So, each worker get's it's own Lua script instance (an object containing it's own <code>lua_State</code>). The data is passed between the native code and the Lua script through <code>userdata</code> in the form of pointers to these things I call &quot;SharedObjects.&quot; All I have to do is derive from this <code>SharedObject</code> class and boom, Lua can talk to it!</p>
  <p>All my Lua workload does is some basic logic and calling native functions to allocate new things that derive from <code>SharedObject</code> and return them. Basically, it creates a lot of <code>SharedObjects</code> and connects them to each other in specific ways.</p>
  <hr />
  <p>When the script has a light workload the multithreaded performance stays great.</p>
  <p>But once the script has a heavy workload the performance drops as the thread count rises above 4.</p>
  <p>Here's the results of the tests I ran:</p>
  <p><a href="https://i.stack.imgur.com/MbSAy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MbSAy.png" alt="enter image description here" /></a></p>
  <p>I don't understand why a heavy workload causes performance to get worse as thread count goes up??? I would expect it to reach a maximum and flatline....</p>
  <hr />
  <p><strong>EDIT: I created a minimal reproducible example project that perfectly simulates the problem.</strong> I compiled with MSVC2010 (as per my real application). <a href="https://github.com/MRG95/LuaThreads" rel="nofollow noreferrer">https://github.com/MRG95/LuaThreads</a></p>
  <p>Explanation of GitHub project files:</p>
  <ul>
  <li>main.cpp: Entry point. Creates the workers and simulates a workload. A timer keeps track of how long it takes to complete the work.</li>
  <li>Lua/lua_script.h: The interface between the lua script and native code. Native methods and properties are accessed through Qt's <code>QMetaObject</code> implementation. the function <code>void bindObject()</code> sets up the connection.</li>
  <li>worker.h: Defines the <code>Worker</code> class which gets moved to it's <code>QThread</code> via <code>moveToThread</code>. The script function call happens in <code>void doWork()</code>.</li>
  <li>tags.h/tags.cpp: Example data types that get processed in the script.</li>
  </ul>
  <p>In the build folder is a file <code>testScript.lua</code> that is the sample workload itself. It's just a simple loop running some of the methods found in the tags.h classes.</p>