-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
I'm using a lot of ConditionalWeakTable entries to store computations and runtime data attached to some input data structure. It turned out the total number of entries can get quite large (>500k) and it turned out that they are causing disturbing GC stalls in the application. I could isolate the behavior in a small test project you can find here (using .NET Core 3.1.9): https://github.com/luithefirst/WeakTableGCStall
The procedure adds 10k weak table entries every second and continuously does some computation with a small data allocation that triggers a Gen0 collection every several hundredth iteration. The time of the computation is measured and outliers are reported. The number of outliers matches the value of the "gen-0-gc-count" EventCounter quite well. The magnitude of the outlier increases linearly with the number of weak table entries:
| Entries | Outlier (GC time) |
|---|---|
| 100k | 2.5ms |
| 200k | 3.0ms |
| 300k | 4.4ms |
| 400k | 5.9ms |
| 500k | 7.4ms |
| 600k | 8.8ms |
| 700k | 10.5ms |
The actual system does a real-time rendering using the Aardvark Platform (https://github.com/aardvark-platform) where we have an attribute grammar system that caches attributes using a weak table. We also use it to attach callbacks in FSharp.Data.Adaptive (https://github.com/fsprojects/FSharp.Data.Adaptive) to adaptive computations. In an application with a huge scene graph there are random frame time spikes caused by GC stalls due to a large number of weak entries. In a scene that was using about 500k of such entries and a lot of other stuff, the frame time is typically 10ms (i7 8700K + GTX 1070 Ti) / 4ms (i7 4700K + RTX 2080 Ti) when only moving the camera (no heavy computations or new entries) and interrupted by spikes of about 20ms / 40ms.
With the new insight, I will try to reduce the usage of the ConditionalWeakTable, but it would be great if optimization in its internal implementation could be made. I'm looking forward to your input on this issue.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status