Adding Scanner object and also tuning for better branch prediction for about +6%. (#341)

This commit is contained in:
Thomas Wuerthinger 2024-01-12 20:51:22 +01:00 committed by GitHub
parent dac38bc97f
commit bd4cff945d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 183 additions and 101 deletions

View File

@ -21,5 +21,6 @@ sdk use java 21.0.1-graal 1>&2
# ./mvnw clean verify removes target/ and will re-trigger native image creation. # ./mvnw clean verify removes target/ and will re-trigger native image creation.
if [ ! -f target/CalculateAverage_thomaswue_image ]; then if [ ! -f target/CalculateAverage_thomaswue_image ]; then
NATIVE_IMAGE_OPTS="--gc=epsilon -O3 -march=native --enable-preview" NATIVE_IMAGE_OPTS="--gc=epsilon -O3 -march=native --enable-preview"
# Use -H:MethodFilter=CalculateAverage_thomaswue.* -H:Dump=:2 -H:PrintGraph=Network for IdealGraphVisualizer graph dumping.
native-image $NATIVE_IMAGE_OPTS -cp target/average-1.0.0-SNAPSHOT.jar -o target/CalculateAverage_thomaswue_image dev.morling.onebrc.CalculateAverage_thomaswue native-image $NATIVE_IMAGE_OPTS -cp target/average-1.0.0-SNAPSHOT.jar -o target/CalculateAverage_thomaswue_image dev.morling.onebrc.CalculateAverage_thomaswue
fi fi

View File

@ -32,30 +32,25 @@ import java.util.stream.IntStream;
* Simple solution that memory maps the input file, then splits it into one segment per available core and uses * Simple solution that memory maps the input file, then splits it into one segment per available core and uses
* sun.misc.Unsafe to directly access the mapped memory. Uses a long at a time when checking for collision. * sun.misc.Unsafe to directly access the mapped memory. Uses a long at a time when checking for collision.
* <p> * <p>
* Runs in 0.70s on my Intel i9-13900K * Runs in 0.66s on my Intel i9-13900K
* Perf stats: * Perf stats:
* 40,622,862,783 cpu_core/cycles/ * 35,935,262,091 cpu_core/cycles/
* 48,241,929,925 cpu_atom/cycles/ * 47,305,591,173 cpu_atom/cycles/
*/ */
public class CalculateAverage_thomaswue { public class CalculateAverage_thomaswue {
private static final String FILE = "./measurements.txt"; private static final String FILE = "./measurements.txt";
// Holding the current result for a single city. // Holding the current result for a single city.
private static class Result { private static class Result {
final long nameAddress; long lastNameLong, secondLastNameLong, nameAddress;
long lastNameLong; int nameLength, remainingShift;
int remainingShift; int min, max, count;
int min;
int max;
long sum; long sum;
int count;
private Result(long nameAddress, int value) { private Result(long nameAddress) {
this.nameAddress = nameAddress; this.nameAddress = nameAddress;
this.min = value; this.min = Integer.MAX_VALUE;
this.max = value; this.max = Integer.MIN_VALUE;
this.sum = value;
this.count = 1;
} }
public String toString() { public String toString() {
@ -73,6 +68,10 @@ public class CalculateAverage_thomaswue {
sum += other.sum; sum += other.sum;
count += other.count; count += other.count;
} }
public String calcName() {
return new Scanner(nameAddress, nameAddress + nameLength).getString(nameLength);
}
} }
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
@ -81,122 +80,155 @@ public class CalculateAverage_thomaswue {
long[] chunks = getSegments(numberOfChunks); long[] chunks = getSegments(numberOfChunks);
// Parallel processing of segments. // Parallel processing of segments.
List<HashMap<String, Result>> allResults = IntStream.range(0, chunks.length - 1).mapToObj(chunkIndex -> { List<List<Result>> allResults = IntStream.range(0, chunks.length - 1).mapToObj(chunkIndex -> parseLoop(chunks[chunkIndex], chunks[chunkIndex + 1]))
HashMap<String, Result> cities = HashMap.newHashMap(1 << 10); .map(resultArray -> {
parseLoop(chunks[chunkIndex], chunks[chunkIndex + 1], cities); List<Result> results = new ArrayList<>();
return cities; for (Result r : resultArray) {
}).parallel().toList(); if (r != null) {
results.add(r);
}
}
return results;
}).parallel().toList();
// Accumulate results sequentially. // Final output.
HashMap<String, Result> result = allResults.getFirst(); System.out.println(accumulateResults(allResults));
for (int i = 1; i < allResults.size(); ++i) { }
for (Map.Entry<String, Result> entry : allResults.get(i).entrySet()) {
Result current = result.putIfAbsent(entry.getKey(), entry.getValue()); // Accumulate results sequentially for simplicity.
private static TreeMap<String, Result> accumulateResults(List<List<Result>> allResults) {
TreeMap<String, Result> result = new TreeMap<>();
for (List<Result> resultArr : allResults) {
for (Result r : resultArr) {
String name = r.calcName();
Result current = result.putIfAbsent(name, r);
if (current != null) { if (current != null) {
current.add(entry.getValue()); current.add(r);
} }
} }
} }
return result;
// Final output.
System.out.println(new TreeMap<>(result));
} }
private static final Unsafe UNSAFE = initUnsafe(); // Main parse loop.
private static Result[] parseLoop(long chunkStart, long chunkEnd) {
private static Unsafe initUnsafe() {
try {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(Unsafe.class);
}
catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private static void parseLoop(long chunkStart, long chunkEnd, HashMap<String, Result> cities) {
Result[] results = new Result[1 << 18]; Result[] results = new Result[1 << 18];
long scanPtr = chunkStart; Scanner scanner = new Scanner(chunkStart, chunkEnd);
while (scanPtr < chunkEnd) { while (scanner.hasNext()) {
long nameAddress = scanPtr; long nameAddress = scanner.pos();
long hash = 0; long hash = 0;
// Search for ';', one long at a time. // Search for ';', one long at a time.
long word = UNSAFE.getLong(scanPtr); long word = scanner.getLong();
int pos = findDelimiter(word); int pos = findDelimiter(word);
if (pos != 8) { if (pos != 8) {
scanPtr += pos; scanner.add(pos);
word = word & (-1L >>> ((8 - pos - 1) << 3)); word = mask(word, pos);
hash ^= word; hash ^= word;
Result existingResult = results[hashToIndex(hash, results)];
if (existingResult != null && existingResult.lastNameLong == word) {
scanAndRecord(scanner, existingResult);
continue;
}
} }
else { else {
scanPtr += 8; scanner.add(8);
hash ^= word; hash ^= word;
while (true) { long prevWord = word;
word = UNSAFE.getLong(scanPtr); word = scanner.getLong();
pos = findDelimiter(word); pos = findDelimiter(word);
if (pos != 8) { if (pos != 8) {
scanPtr += pos; scanner.add(pos);
word = word & (-1L >>> ((8 - pos - 1) << 3)); word = mask(word, pos);
hash ^= word; hash ^= word;
break; Result existingResult = results[hashToIndex(hash, results)];
if (existingResult != null && existingResult.lastNameLong == word && existingResult.secondLastNameLong == prevWord) {
scanAndRecord(scanner, existingResult);
continue;
} }
else { }
scanPtr += 8; else {
hash ^= word; scanner.add(8);
hash ^= word;
while (true) {
word = scanner.getLong();
pos = findDelimiter(word);
if (pos != 8) {
scanner.add(pos);
word = mask(word, pos);
hash ^= word;
break;
}
else {
scanner.add(8);
hash ^= word;
}
} }
} }
} }
// Save length of name for later. // Save length of name for later.
int nameLength = (int) (scanPtr - nameAddress); int nameLength = (int) (scanner.pos() - nameAddress);
scanPtr++; scanner.add(1);
long numberWord = UNSAFE.getLong(scanPtr); long numberWord = scanner.getLong();
// The 4th binary digit of the ascii of a digit is 1 while
// that of the '.' is 0. This finds the decimal separator
// The value can be 12, 20, 28
int decimalSepPos = Long.numberOfTrailingZeros(~numberWord & 0x10101000); int decimalSepPos = Long.numberOfTrailingZeros(~numberWord & 0x10101000);
int number = convertIntoNumber(decimalSepPos, numberWord); int number = convertIntoNumber(decimalSepPos, numberWord);
scanner.add((decimalSepPos >>> 3) + 3);
// Skip past new line.
// scanPtr++;
scanPtr += (decimalSepPos >>> 3) + 3;
// Final calculation for index into hash table. // Final calculation for index into hash table.
int hashAsInt = (int) (hash ^ (hash >>> 32)); int tableIndex = hashToIndex(hash, results);
int finalHash = (hashAsInt ^ (hashAsInt >>> 18));
int tableIndex = (finalHash & (results.length - 1));
outer: while (true) { outer: while (true) {
Result existingResult = results[tableIndex]; Result existingResult = results[tableIndex];
if (existingResult == null) { if (existingResult == null) {
newEntry(results, cities, nameAddress, number, tableIndex, nameLength); existingResult = newEntry(results, nameAddress, tableIndex, nameLength, scanner);
}
// Check for collision.
int i = 0;
for (; i < nameLength + 1 - 8; i += 8) {
if (scanner.getLongAt(existingResult.nameAddress + i) != scanner.getLongAt(nameAddress + i)) {
tableIndex = (tableIndex + 1) & (results.length - 1);
continue outer;
}
}
if (((existingResult.lastNameLong ^ scanner.getLongAt(nameAddress + i)) << existingResult.remainingShift) == 0) {
record(existingResult, number);
break; break;
} }
else { else {
// Check for collision. // Collision error, try next.
int i = 0; tableIndex = (tableIndex + 1) & (results.length - 1);
for (; i < nameLength + 1 - 8; i += 8) {
if (UNSAFE.getLong(existingResult.nameAddress + i) != UNSAFE.getLong(nameAddress + i)) {
tableIndex = (tableIndex + 1) & (results.length - 1);
continue outer;
}
}
if (((existingResult.lastNameLong ^ UNSAFE.getLong(nameAddress + i)) << existingResult.remainingShift) == 0) {
existingResult.min = Math.min(existingResult.min, number);
existingResult.max = Math.max(existingResult.max, number);
existingResult.sum += number;
existingResult.count++;
break;
}
else {
// Collision error, try next.
tableIndex = (tableIndex + 1) & (results.length - 1);
}
} }
} }
} }
return results;
}
private static void scanAndRecord(Scanner scanPtr, Result existingResult) {
scanPtr.add(1);
long numberWord = scanPtr.getLong();
int decimalSepPos = Long.numberOfTrailingZeros(~numberWord & 0x10101000);
int number = convertIntoNumber(decimalSepPos, numberWord);
scanPtr.add((decimalSepPos >>> 3) + 3);
record(existingResult, number);
}
private static void record(Result existingResult, int number) {
existingResult.min = Math.min(existingResult.min, number);
existingResult.max = Math.max(existingResult.max, number);
existingResult.sum += number;
existingResult.count++;
}
private static int hashToIndex(long hash, Result[] results) {
int hashAsInt = (int) (hash ^ (hash >>> 32));
int finalHash = (hashAsInt ^ (hashAsInt >>> 18));
return (finalHash & (results.length - 1));
}
private static long mask(long word, int pos) {
return word & (-1L >>> ((8 - pos - 1) << 3));
} }
// Special method to convert a number in the specific format into an int value without branches created by // Special method to convert a number in the specific format into an int value without branches created by
@ -229,19 +261,18 @@ public class CalculateAverage_thomaswue {
return Long.numberOfTrailingZeros(tmp) >>> 3; return Long.numberOfTrailingZeros(tmp) >>> 3;
} }
private static void newEntry(Result[] results, HashMap<String, Result> cities, long nameAddress, int number, int hash, int nameLength) { private static Result newEntry(Result[] results, long nameAddress, int hash, int nameLength, Scanner scanner) {
Result r = new Result(nameAddress, number); Result r = new Result(nameAddress);
results[hash] = r; results[hash] = r;
byte[] bytes = new byte[nameLength];
int i = 0; int i = 0;
for (; i < nameLength + 1 - 8; i += 8) { for (; i < nameLength + 1 - 8; i += 8) {
r.secondLastNameLong = (scanner.getLongAt(nameAddress + i));
} }
r.lastNameLong = UNSAFE.getLong(nameAddress + i);
r.remainingShift = (64 - (nameLength + 1 - i) << 3); r.remainingShift = (64 - (nameLength + 1 - i) << 3);
UNSAFE.copyMemory(null, nameAddress, bytes, Unsafe.ARRAY_BYTE_BASE_OFFSET, nameLength); r.lastNameLong = (scanner.getLongAt(nameAddress + i) & (-1L >>> r.remainingShift));
String nameAsString = new String(bytes, StandardCharsets.UTF_8); r.nameLength = nameLength;
cities.put(nameAsString, r); return r;
} }
private static long[] getSegments(int numberOfChunks) throws IOException { private static long[] getSegments(int numberOfChunks) throws IOException {
@ -252,10 +283,11 @@ public class CalculateAverage_thomaswue {
long mappedAddress = fileChannel.map(MapMode.READ_ONLY, 0, fileSize, Arena.global()).address(); long mappedAddress = fileChannel.map(MapMode.READ_ONLY, 0, fileSize, Arena.global()).address();
chunks[0] = mappedAddress; chunks[0] = mappedAddress;
long endAddress = mappedAddress + fileSize; long endAddress = mappedAddress + fileSize;
Scanner s = new Scanner(mappedAddress, mappedAddress + fileSize);
for (int i = 1; i < numberOfChunks; ++i) { for (int i = 1; i < numberOfChunks; ++i) {
long chunkAddress = mappedAddress + i * segmentSize; long chunkAddress = mappedAddress + i * segmentSize;
// Align to first row start. // Align to first row start.
while (chunkAddress < endAddress && UNSAFE.getByte(chunkAddress++) != '\n') { while (chunkAddress < endAddress && (s.getLongAt(chunkAddress++) & 0xFF) != '\n') {
// nop // nop
} }
chunks[i] = Math.min(chunkAddress, endAddress); chunks[i] = Math.min(chunkAddress, endAddress);
@ -264,4 +296,53 @@ public class CalculateAverage_thomaswue {
return chunks; return chunks;
} }
} }
private static class Scanner {
private static final Unsafe UNSAFE = initUnsafe();
private static Unsafe initUnsafe() {
try {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(Unsafe.class);
}
catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
long pos, end;
public Scanner(long start, long end) {
this.pos = start;
this.end = end;
}
boolean hasNext() {
return pos < end;
}
long pos() {
return pos;
}
void add(int delta) {
pos += delta;
}
long getLong() {
return UNSAFE.getLong(pos);
}
long getLongAt(long pos) {
return UNSAFE.getLong(pos);
}
public String getString(int nameLength) {
byte[] bytes = new byte[nameLength];
UNSAFE.copyMemory(null, pos, bytes, Unsafe.ARRAY_BYTE_BASE_OFFSET, nameLength);
return new String(bytes, StandardCharsets.UTF_8);
}
}
} }