I will have to create a multi-threading project soon I have seen experiments ( delphitools.info/2011/10/13/memory-manager-investigations ) showing that the default Delphi memory manager has problems with multi-threading.
So, I have found this SynScaleMM. Anybody can give some feedback on it or on a similar memory manager?
Thanks
Our SynScaleMM is still experimental.
EDIT: Take a look at the more stable ScaleMM2 and the brand new SAPMM. But my remarks below are still worth following: the less allocation you do, the better you scale!
But it worked as expected in a multi-threaded server environment. Scaling is much better than FastMM4, for some critical tests.
But the Memory Manager is perhaps not the bigger bottleneck in Multi-Threaded applications. FastMM4 could work well, if you don't stress it.
Here are some (not dogmatic, just from experiment and knowledge of low-level Delphi RTL) advice if you want to write FAST multi-threaded application in Delphi:
const
for string or dynamic array parameters like in MyFunc(const aString: String)
to avoid allocating a temporary string per each call;s := s+'Blabla'+IntToStr(i)
) , but rely on a buffered writing such as TStringBuilder
available in latest versions of Delphi;TStringBuilder
is not perfect either: for instance, it will create a lot of temporary strings for appending some numerical data, and will use the awfully slow SysUtils.IntToStr()
function when you add some integer
value - I had to rewrite a lot of low-level functions to avoid most string allocation in our TTextWriter
class as defined in SynCommons.pas; InterlockedIncrement / InterlockedExchangeAdd
;InterlockedExchange
(from SysUtils.pas) is a good way of updating a buffer or a shared object. You create an updated version of of some content in your thread, then you exchange a shared pointer to the data (e.g. a TObject
instance) in one low-level CPU operation. It will notify the change to the other threads, with very good multi-thread scaling. You'll have to take care of the data integrity, but it works very well in practice.PosEx()
for instance;AnsiString/UnicodeString
kind of variables/functions, and check the generated asm code via Alt-F2 to track any hidden unwanted conversion (e.g. call UStrFromPCharLen
);var
parameters in a procedure
instead of function
returning a string (a function returning a string
will add an UStrAsg/LStrAsg
call which has a LOCK which will flush all CPU cores);TMemoryStream
each time you need one, but rely on a private instance in your class, already sized in enough memory, in which you will write data using Position
to retrieve the end of data and not changing its Size
(which will be the memory block allocated by the MM);record/object
pointers on already allocated memory buffers, mapping the data without copying it into temporary memory;I tried to follow those rules in our Open Source framework, and if you take a look at our code, you'll find out a lot of real-world sample code.