[fpc-pascal] GetMem slow in threads

Hairy Pixels genericptr at gmail.com
Sat Nov 9 14:59:36 CET 2024


I diagnosed a threading performance problem down to merely calling GetMem.
I did a test and it was 4x slower to call GetMem on 8 threads (my computer
core count) than one thread. Due the large amount of allocations in the
program it was actually slower to multithread the task.

I assume the problem is a mutex blocking the threads right?

The solution would be to be to use a different allocator but because FPC
only allows overriding NewInstance per class it would be mean you would
need a mutex anyway to control which allocator was used depending on the
thread.

Maybe a threadvar could be used to get the allocator for the class type? I
haven’t tried but I know there’s a penalty to use that too and it may wipe
out of the gains again. I’m not sure what a threadvar is though or how it’s
implemented just that the docs say it could be slow.

Any ideas would be appreciated.

Regards,
    Ryan Joseph
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20241109/b2e3a3e8/attachment.htm>


More information about the fpc-pascal mailing list