Added improvments on string copying, string comparation & calculation of next index in case of collision in custom map (#650)

* added code

* Fixed pointers bugs

* removed my own benchmark

* added comment on how I handle hash collisions

* executed mwvn clean verify

* made scripts executable & fixed rounding issues

* Fixed way of dealing with hash collisions

* changed method name sameNameBytes to isSameNameBytes

* changes script from sh to bash

* fixed chunking bug

* Fixed bug in chunking when file size is too small

* added Runtime.getRuntime().availableProcessors

* added improvemnts on string copying, calculation of next index of Map in case on collision & improved string comparing
This commit is contained in:
Jaime Polidura 2024-01-29 22:22:22 +01:00 committed by GitHub
parent 036f9a01b1
commit 2a44f8d390
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -309,16 +309,14 @@ public final class CalculateAverage_JaimePolidura {
} }
public void put(long hashToPut, byte[] nameToPut, int nameLength, int valueToPut) { public void put(long hashToPut, byte[] nameToPut, int nameLength, int valueToPut) {
int index = hashToIndex(hashToPut); int index = toIndex(hashToPut);
for (;;) { for (;;) {
Result actualEntry = entries[index]; Result actualEntry = entries[index];
if (actualEntry == null) { if (actualEntry == null) {
byte[] nameToPutCopy = new byte[nameLength]; byte[] nameToPutCopy = new byte[nameLength];
for (int i = 0; i < nameLength; i++) { UNSAFE.copyMemory(nameToPut, Unsafe.ARRAY_BYTE_BASE_OFFSET, nameToPutCopy, Unsafe.ARRAY_BYTE_BASE_OFFSET, nameLength);
nameToPutCopy[i] = nameToPut[i];
}
entries[index] = new Result(hashToPut, nameToPutCopy, nameLength, valueToPut, entries[index] = new Result(hashToPut, nameToPutCopy, nameLength, valueToPut,
valueToPut, valueToPut, 1); valueToPut, valueToPut, 1);
@ -331,14 +329,12 @@ public final class CalculateAverage_JaimePolidura {
actualEntry.sum = actualEntry.sum + valueToPut; actualEntry.sum = actualEntry.sum + valueToPut;
return; return;
} }
// If the name is not the same, we try to go to the next slot
if (++index >= this.size) { index = toIndex(index + 31);
index = 0;
}
} }
} }
private int hashToIndex(long hash) { private int toIndex(long hash) {
return (int) (((hash >> 32) ^ ((int) hash)) & (this.size - 1)); return (int) (((hash >> 32) ^ ((int) hash)) & (this.size - 1));
} }
} }
@ -367,8 +363,15 @@ public final class CalculateAverage_JaimePolidura {
} }
private boolean isSameNameBytes(byte[] otherNameBytes) { private boolean isSameNameBytes(byte[] otherNameBytes) {
for (int i = 0; i < this.nameLength; i++) { for (int i = 0; i < this.nameLength; i += 8) {
if (this.name[i] != otherNameBytes[i]) { long thisNameBytesAsLong = UNSAFE.getLong(this.name, Unsafe.ARRAY_BYTE_BASE_OFFSET + i);
long otherNameBytesAsLong = UNSAFE.getLong(otherNameBytes, Unsafe.ARRAY_BYTE_BASE_OFFSET + i);
int isPositiveAsInt = (((8 - nameLength + i) >> 31) & 1) ^ 0x01;
int shift = ((8 - nameLength + i) * isPositiveAsInt) * 8;
otherNameBytesAsLong = (otherNameBytesAsLong << shift) >>> shift;
if (thisNameBytesAsLong != otherNameBytesAsLong) {
return false; return false;
} }
} }