Overview
| Artifact ID: | 11c586bf64b3fe2e658c8c534b7383210d7999cf972b9c6d0777112fe8a599b5 |
|---|---|
| Ticket: | 3d1c409f1afbca6b74f824cffe8dbb4028a410c5
Bad performance: tclThreadAlloc vs. CPU-cache (fragmentation/granularity/etc)... |
| User & Date: | sebres on 2024-11-18 14:11:40 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/x-markdown"
- comment changed to:
(Just for the record, initially posted in chat in 2019 - [paste.tclers.tk/5260](http://paste.tclers.tk/5260)). Issue: the longer Tcl works - the slower it becomes... The simplest example illustrating this: ``` proc test {} {timerate -calibrate {incr i} 10000 10000000}; test; # calibrate overhead for incr proc test args { puts "set: [timerate { set a([incr i]) _ } {*}$args]"; lset args 1 $i; set i 0; puts "get: [timerate { set a([incr i]) } {*}$args]" } time { test 10000 1000000 } 50 ``` Results: ``` * 1x-threaded: set: 0.153809 µs/# 1000000 # 6501570 #/sec 153.809 net-ms get: 0.049532 µs/# 1000000 # 20188968 #/sec 49.532 net-ms ... 100 times ... set: 0.598031 µs/# 1000000 # 1672154 #/sec 598.031 net-ms get: 0.331693 µs/# 1000000 # 3014836 #/sec 331.693 net-ms * 16x-threaded: set * 16x: 0.573478 µs/# 4000000 # 291248 #/sec 2293.913 net-ms get * 16x: 0.077514 µs/# 4000000 # 14923264 #/sec 310.059 net-ms ... 20 times ... set * 16x: 3.539208 µs/# 4000000 # 304646 #/sec 14156.834 net-ms get * 16x: 0.519618 µs/# 4000000 # 2048606 #/sec 2078.474 net-ms ``` 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: ``` * 1x-threaded: set : 0.140644 µs/# 250000 # 7110150 #/sec 35.161 net-ms get : 0.018572 µs/# 250000 # 53844497 #/sec 4.643 net-ms ... 100 times ... set : 0.083600 µs/# 250000 # 11961722 #/sec 20.900 net-ms get : 0.017704 µs/# 250000 # 56484410 #/sec 4.426 net-ms * 16x-threaded: set * 16x: 0.810806 µs/# 4000000 # 129652 #/sec 3243.227 net-ms get * 16x: 0.556715 µs/# 4000000 # 246766 #/sec 2226.860 net-ms ... 20 times ... set * 16x: 0.665778 µs/# 4000000 # 1671624 #/sec 2663.113 net-ms get * 16x: 0.343585 µs/# 4000000 # 3428775 #/sec 1374.340 net-ms ``` - foundin changed to: "any"
- is_private changed to: "0"
- login: "sebres"
- priority changed to: "5 Medium"
- private_contact changed to: "50e9528765c18296566374b3fde2df942c394efd"
- resolution changed to: "None"
- severity changed to: "Severe"
- status changed to: "Open"
- submitter changed to: "sebres"
- subsystem changed to: "41. Memory Allocation"
- title changed to:
Bad performance: tclThreadAlloc vs. CPU-cache (fragmentation/granularity/etc)...
- type changed to: "Bug"