dominion-dev / dominion-ecs-java

Insanely fast ECS (Entity Component System) for Java
https://dominion-dev.github.io
MIT License
298 stars 18 forks source link

`ClassIndex` does not handle duplicate identity hash codes #26

Closed TheMode closed 2 years ago

TheMode commented 2 years ago

I may be wrong, but ClassIndex does not seem to handle System.identityHashCode returning the same for two distinct classes, as collision detection is only implemented based on the even smaller hashBits field. Which is an unlikely scenario, but not impossible.

enricostara commented 2 years ago

Good question.

SA: Yes, ClassIndex detects the unlikely collision and will exit with a clear error without starting to mess up with the user application data.

LA: you went straight on to the core of the library where (one of) the magic happens. Java doesn't provide a safe unique id for a Class type - as far as I know, and I'm open to discussion and very happy if someone will come up with a different approach. That means you should always rely on the equals() check to prevent collisions. Here is a trade-off, implement lightning-fast (around 1ns) checking to detect a class type with a 99.999999999% success rate or have the worst performance just to avoid a very unlikely collision that could occur much less frequently than a common OutOfMemory error.

I will not close this issue just yet to leave the discussion open for a while.

enricostara commented 2 years ago

Updated implementation: