| Ticket UUID: | 3d1c409f1afbca6b74f824cffe8dbb4028a410c5 | ||
| Title: | Bad performance: tclThreadAlloc vs. CPU-cache (fragmentation/granularity/etc)... | ||
| Type: | Bug | Created on: | 2024-11-18 14:11:40 |
| Submitter: | sebres | Assigned to: | nobody |
| Subsystem: | 41. Memory Allocation | Severity: | Severe |
| Priority: | 5 Medium | Last modified: | 2024-11-20 15:50:02 |
| Status: | Open | Closed by: | nobody |
| Resolution: | None | Closed on: | |
| Version: | any | ||
| Description: | ||||
|
(Just for the record, initially posted in chat in 2019 - paste.tclers.tk/5260). Issue: the longer Tcl works - the slower it becomes... The simplest example illustrating this:
Results:
First problem - releasing of objects (and its followed holding in linked LIFO of object pool) may happen randomly, so the objects are distributed across different memory blocks (by the way, not considering cache coherency), and on further allocs becomes "fragmented" across different cache blocks, what'd cause many CPU-cache washouts and related reloads. Long fragmentation of such kind in the in object pool LIFO will only exacerbate the problem. The second problem is that besides fragmentation/granularity, the single mem-block that gets "changed" in some thread, will cause that the whole block in cache needs to be invalidated for other threads, what in turn would cause more memory accesses (which is hundred times slower than L2 and thousand times slower than L1 access). In my fork I fixed this "wrong" behaviour in my own threaded-alloc module, which prefers moving of whole free pages the single objects, and combining them to single free block instead of simplest LIFO in threaded object pool:
| ||||
| User Comments: | ||||
anonymous added on 2024-11-18 15:40:02:
> the longer Tcl works - the slower it becomes Noticed as well, but couldn't catch and understand the reason. Congratulations Sergey and many thanks for your success at it. jan.nijtmans added on 2024-11-20 13:55:11:
This sounds like a useful optimization! Wondering how this compares with just using the system malloc/free. Does tclThreadAlloc() really have better performance, compared to modern malloc/free implementations? sebres added on 2024-11-20 15:50:02:
> Does tclThreadAlloc() really have better performance, compared to modern malloc/free implementations? Single-threaded it is marginally better, however the lot of the load remains in user space, and doesn't go in kernel time (syscalls), because Tcl works not in native ways with memory how any C/C++ programs would do, but everything (every object, var, hash-element) expects at least 1 allocation (mostly 2 or more) and alloc/free becomes a mass processing. With OS allocator, I saw a tenfold degradation already on 4 threads. It is like GIL in python (not really that, but the effect looks indeed very similar, especially with growth in thread count). So yes, a (smart) memory-manager is still a must for things like Tcl, even with modern OS. Surely with a disadvantage of in tcl-core reserved memory (what can also be solved with a GC, periodically freeing memory, and/or with periodical release of working threads, reaching some idle time). | ||||
