0

Let's say I have a massive String, so hashing it is fairly expensive.

When does the hashValue gets computed? Every time I access it as words.hashValue, or every time the String gets modified? If it's on access, is it cached?

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Vladislav
  • 1,392
  • 1
  • 12
  • 21

1 Answers1

0

As the docs on hashValue state, that requirement on Hashable is deprecated, you should be using hash(into:) instead, which should only be called when you are storing the String as a Hashable, by using it as a Dictionary key or in a Set.

For more info, see SE-0206 Hashable enhancements.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • 1
    I don't quite see how this answers the question that was actually asked. – matt Feb 22 '22 at 12:08
  • @matt OP shouldn't be using `hashValue` in the first place, so when it gets calculated is irrelevant. `hash(into:)` should be used instead. And I did answer when `hash(into:)` gets called. – Dávid Pásztor Feb 22 '22 at 12:10
  • 1
    But the question is not about what the OP should be doing. The `hashValue` does exist and it is perfectly reasonable to wonder about its cost. – matt Feb 22 '22 at 12:13