So, you're flushing the existing palette cache for every category and create it freshly, for every category, before searching this "new" cache for matches, right? I can see how that works, but it takes pretty long, especially if you have a lot of blocks this could really take very long.

There are benefits of going through the actual palette contents, such as that you don't have to worry about hidden primitives and such, and you can find other stuff, like buttons and checkmarks. It's an interesting strategy. Another strategy would be to search the data source (the blocks dictionary), and to create the block templates directly from that, if they match. I bet this would be faster by several orders of magnitude, especially if you have lots of blocks. What do you think?