corywalker / expreduce

An experimental computer algebra system written in Go
MIT License
388 stars 26 forks source link

Significance of the 8 byte prefix of the fnv hash? #195

Closed rljacobson closed 2 years ago

rljacobson commented 2 years ago

Hi Cory,

I notice you are feeding in a different 8 byte prefix to the fnv hash function before hashing the data for each atom type. This method for String is typical:

func (str *String) Hash() uint64 {
    h := fnv.New64a()
    h.Write([]byte{102, 206, 57, 172, 207, 100, 198, 133})
    h.Write([]byte(str.Val))
    return h.Sum64()
}

What's the significance of this prefix? Are you just avoiding collisions in case of coincidental equality of data being hashed for different types? Are your choices of prefix chosen randomly?

corywalker commented 2 years ago

Hi Robert,

The prefixes are indeed chosen randomly. I do believe the original intent was, as you suspect, to avoid collisions of data being hashed for different types. For example, if a String happened to have the same text as a Symbol, this allows it to hash to a different value. I don't remember why exactly I chose 8 bytes to do this instead of 1. Eight seems like overkill right now. That's what I remember.

rljacobson commented 2 years ago

Thanks, Cory! I hope you are doing well.