Get the latest tech news
Requiem for a Hash Function, or: How I learned to love package maphash
I am coming off from surgery right now and have some time on my hands, so I wanted to share a story about a learning experience. This post is a bit of a dual purpose one on a teachable moment as a software engineer — a chance to offer a retrospective from bad decisions and earn penance therefor. There is one episode that sticks out in my memory with a fair bit of shame: implementing the original metric hashing for the metric family data type in Prometheus’s client metrics. This hashing was used in the internals of the client metrics, so users wouldn’t be exposed to it directly — except in terms of CPU cost!
Important: maphash.WriteComparable internally performs TLV-like delimiting on the data hashing, meaning capturing information on string length, item count, and similar are not needed when you directly use this API. Now, I can imagine some readers taking umbrage that I’ve saved a crucial point for the end: in the vast majority of situations, creating a manual hashing mechanism is unnecessary. I haven’t dug into its guts too deeply, but it appears to recursively generate instructions that get added to an intermediate representation(IR) store that are returned eventually as a closure.
Or read this on Hacker News