Skip to content

Commit

Permalink
Added improvments on string copying, string comparation & calculation…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
JaimePolidura authored Jan 29, 2024
1 parent 036f9a0 commit 2a44f8d
Showing 1 changed file with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,14 @@ public SimpleMap(int size) {
}

public void put(long hashToPut, byte[] nameToPut, int nameLength, int valueToPut) {
int index = hashToIndex(hashToPut);
int index = toIndex(hashToPut);

for (;;) {
Result actualEntry = entries[index];

if (actualEntry == null) {
byte[] nameToPutCopy = new byte[nameLength];
for (int i = 0; i < nameLength; i++) {
nameToPutCopy[i] = nameToPut[i];
}
UNSAFE.copyMemory(nameToPut, Unsafe.ARRAY_BYTE_BASE_OFFSET, nameToPutCopy, Unsafe.ARRAY_BYTE_BASE_OFFSET, nameLength);

entries[index] = new Result(hashToPut, nameToPutCopy, nameLength, valueToPut,
valueToPut, valueToPut, 1);
Expand All @@ -331,14 +329,12 @@ public void put(long hashToPut, byte[] nameToPut, int nameLength, int valueToPut
actualEntry.sum = actualEntry.sum + valueToPut;
return;
}
// If the name is not the same, we try to go to the next slot
if (++index >= this.size) {
index = 0;
}

index = toIndex(index + 31);
}
}

private int hashToIndex(long hash) {
private int toIndex(long hash) {
return (int) (((hash >> 32) ^ ((int) hash)) & (this.size - 1));
}
}
Expand Down Expand Up @@ -367,8 +363,15 @@ public boolean isSameName(byte[] otherNameBytes, int otherNameLength) {
}

private boolean isSameNameBytes(byte[] otherNameBytes) {
for (int i = 0; i < this.nameLength; i++) {
if (this.name[i] != otherNameBytes[i]) {
for (int i = 0; i < this.nameLength; i += 8) {
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;
}
}
Expand Down

0 comments on commit 2a44f8d

Please sign in to comment.