0

I have a large data model in which the objects need to be hashable for comparision. for that I added hashValue getters to them, like this:

var hashValue:Int
{
    let h1 = (31 &* id.hashValue)
        &+ caseID.hashValue
        &+ statusID.hashValue
        &+ assignedToID.hashValue
    let h2 = h1 &+ runID.hashValue
        &+ templateID.hashValue
        &+ typeID.hashValue
        &+ priorityID.hashValue
    let h3 = h2 &+ milestoneID.hashValue
        &+ customOS.description.hashValue
        &+ title.hashValue
        &+ estimate.hashValue
        &+ estimateForecast.hashValue
    let h4 = h3 &+ refs.hashValue
        &+ customAutomated.hashValue
        &+ customPreconds.hashValue
    let h5 = h4 &+ customSteps.hashValue
        &+ customExpected.hashValue
    let h6 = h5 &+ customMission.hashValue
        &+ customGoals.hashValue
        &+ customLabel.description.hashValue
    let h7 = h6 &+ customSteps.hashValue
        &+ customStepsSeparated.description.hashValue
    return h7

If the model object had too many properties I broke them down as above. This went well until I covered several more model classes with the same treatment. Now with approx. 10 model classes that are hashable the compile time went from one minute to 10 minutes after which the compilation fails completely with the following error(s):

Error:unable to execute command: Killed: 9
Error:compile command failed due to signal 9 (use -v to see invocation)
Error:Build failed with 0 errors and 97 warnings in 14m 34s 489ms 

What can I do to prevent this? I'm using Swift 4 and Xcode 9.2.

BadmintonCat
  • 9,416
  • 14
  • 78
  • 129
  • You don't need to use all properties. You could just return `id.hashValue` and be done. Just build a hashValue that gives reasonably different values. It doesn't have to be unique for every object. – rmaddy Apr 09 '18 at 04:50
  • Yes in the above example that would actually be sufficient but several other objects have an optional ID and several other fields that aren't necessarily unique. – BadmintonCat Apr 09 '18 at 04:55
  • You may not need to implement `hashValue`, it can be synthesised for you in Swift 4 if you just make your model class/struct conform to `Hashable` provided that your variables also conform to `Hashable` – Luke Apr 09 '18 at 05:09
  • 1
    `hashValue` doesn't need to always be unique. For things like dictionary lookups, if two `hashValue`s collide, then the equality operator is used, which must be unique. – dalton_c Apr 09 '18 at 05:09

0 Answers0