Block: Use xxhash'd XOR mask to improve state data lower bits distribution

xxhash is generally well known for its hash key properties, so this is a suitable use case.
We XOR the state data with a partial hash of xxh3(typeID), which provides sufficient hash distribution regardless of the size of state data.
The previous method started to break down as the number of bits exceeded the number of significant bits of type ID (about 10 currently).

As well as being better for hash distribution regardless of state data size, this also reduces the load factor of RuntimeBlockRegistry to 1.08 (previously around 1.24), which is a nice bonus.
This commit is contained in:
Dylan K. Taylor
2023-10-17 15:20:31 +01:00
parent d0d16cdeb7
commit 63fcf9879a
3 changed files with 36 additions and 9 deletions

View File

@ -28,6 +28,7 @@ use function asort;
use function file_get_contents;
use function is_array;
use function json_decode;
use function log;
use function print_r;
use const SORT_STRING;
@ -125,6 +126,14 @@ class BlockTest extends TestCase{
self::assertInstanceOf(Air::class, $block);
}
public function testStateDataSizeNotTooLarge() : void{
$typeIdBitsMin = ((int) log(BlockTypeIds::FIRST_UNUSED_BLOCK_ID, 2)) + 1;
$typeIdBitsMin++; //for custom blocks
self::assertLessThanOrEqual(32, Block::INTERNAL_STATE_DATA_BITS + $typeIdBitsMin, "State data size cannot be larger than " . (32 - $typeIdBitsMin) . " bits (need at least $typeIdBitsMin bits for block type ID)");
}
public function testAsItemFromItem() : void{
$block = VanillaBlocks::FLOWER_POT();
$item = $block->asItem();