Voxelmetric / Voxelmetric1

Legacy Voxelmetric framework
Other
251 stars 96 forks source link

BlockPos.Equal implementation #73

Closed bmegli closed 4 years ago

bmegli commented 8 years ago

Hi there,

Your original implementation from the tutorial was actually a better one than the new you have introduced.

HashCode requires multiplications and additions and finally comparison of codes, this is slower than just three comparisons.

Also, basing Equals on HashCode can lead to extremely difficult to debug bugs, when the hash eventually clashes.

Cheers!

bmegli commented 7 years ago

I agree -- using only GetHashCode is wrong.

It boils down to statistics and depends how much blocks you can have (and if it is probable to have a clash)

If say you could support 4 millions = 4 *10^6 = 4000000 ~= 2^22 blocks than you have 1 / 2^10 ~= 1/1000 = one to thousand chance that next random block will have collision which is non negligable.

It also depends if having a clash is really damaging or not, maybe not ;-)

I'm not sure if the calculation time or the branch time is longer though...

I don't think it will make any difference, only benchmarking could tell.

But you could:

return (object is BlockPos) && EqualsOnCoordinates( (BlockPos) object)

But I don't suppose it all has that much performance impact.

Regards!