This isn’t quite so much about dictionaries with optional values as transformations of a dictionary’s values that yield an optional. For example:
let d = ["a": "1", "b": "2", "c": "three"]
// This is .compactMapValues(Int.init):
let e = d.mapValues(Int.init).filter({ $0.value != nil }).mapValues({ $0! })
There’s no real savings here from a hashing perspective—the filtering means we’ll need to rehash all the elements—but it would save on allocating more space than we need for a bunch of nil
values.