Dictionary in Swift with Mutable Array as value is performing very slow? How to optimize or construct properly?

Copy on write is a tricky thing, and you need to think carefully about how many things are sharing a structure that you’re trying to modify. The culprit is here. countToColorMap[colorCount]?.append(CountedColor(color: color as! UIColor, colorCount: colorCount)) This is generating a temporary value that is modified and put back into the dictionary. Since two “things” are … Read more