The Perfect Follow-Up to Dictionary Performance - mapKeyedValues (SE-0510)

Before WWDC26, I published an article called “Advanced Dictionaries: Performance and Hidden Traps.” In that article, I explained why mapValues(_:) is very fast because it reuses the same hash structure and memory slots. However, we had a big problem: mapValues does not give us the keys inside the closure. To use keys, we had to choose slow methods like reduce(into:) which cause a lot of rehashing.

At WWDC26, Apple solved this problem with Dictionary.mapKeyedValues (SE-0510).

I wrote a new follow-up article that connects my previous dictionary performance notes with this new API. In this short post, I talk about:

  • A quick recap of older performance traps like uniqueKeysWithValues.
  • How mapKeyedValues lets us use keys as context with less performance tax.

You can read the article here

I would love to hear your thoughts on this new API and my article

2 Likes

SE-0510 is still using the name mapValuesWithKeys, despite this:

mapKeyedValues or mapValuesWithKeys?

@scanon, @taylorswift: which one?

1 Like

It's supposed to be mapKeyedValues, but it doesn't look like it has shipped with Xcode 27b2 yet, so I can't confirm.

2 Likes

mapKeyedValues.

The implementation PR is still open; Nate and Alex requested changes two weeks ago, which I think @taylorswift has made, so we should be pretty close to landing it.

1 Like