hash_equals: Add optimizations inspirated from CPython