Some fine tuning for thomaswue (#606)
* Some fine tuning. * Process 2MB segments to make all threads finish at the same time. Process with 3 scanners in parallel in the same thread.
This commit is contained in:
parent
c8dd691a27
commit
7e525c5992
@ -20,7 +20,7 @@ sdk use java 21.0.2-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 -H:InlineAllBonus=10 -H:-ParseRuntimeOptions --initialize-at-build-time=dev.morling.onebrc.CalculateAverage_thomaswue\$Scanner"
|
NATIVE_IMAGE_OPTS="--gc=epsilon -O3 -H:-GenLoopSafepoints -march=native --enable-preview -H:InlineAllBonus=10 -H:-ParseRuntimeOptions --initialize-at-build-time=dev.morling.onebrc.CalculateAverage_thomaswue\$Scanner"
|
||||||
# Use -H:MethodFilter=CalculateAverage_thomaswue.* -H:Dump=:2 -H:PrintGraph=Network for IdealGraphVisualizer graph dumping.
|
# 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
|
||||||
|
@ -23,16 +23,17 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
import java.util.stream.IntStream;
|
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.60s on my Intel i9-13900K
|
* Runs in 0.41s on my Intel i9-13900K
|
||||||
* Perf stats:
|
* Perf stats:
|
||||||
* 34,716,719,245 cpu_core/cycles/
|
* 25,286,227,376 cpu_core/cycles/
|
||||||
* 40,776,530,892 cpu_atom/cycles/
|
* 26,833,723,225 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";
|
||||||
@ -42,10 +43,11 @@ public class CalculateAverage_thomaswue {
|
|||||||
// Holding the current result for a single city.
|
// Holding the current result for a single city.
|
||||||
private static class Result {
|
private static class Result {
|
||||||
long lastNameLong, secondLastNameLong;
|
long lastNameLong, secondLastNameLong;
|
||||||
long[] name;
|
long min, max;
|
||||||
int count;
|
|
||||||
short min, max;
|
|
||||||
long sum;
|
long sum;
|
||||||
|
int count;
|
||||||
|
long[] name;
|
||||||
|
String nameAsString;
|
||||||
|
|
||||||
private Result() {
|
private Result() {
|
||||||
this.min = MAX_TEMP;
|
this.min = MAX_TEMP;
|
||||||
@ -73,36 +75,59 @@ public class CalculateAverage_thomaswue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String calcName() {
|
public String calcName() {
|
||||||
|
if (nameAsString == null) {
|
||||||
ByteBuffer bb = ByteBuffer.allocate(name.length * Long.BYTES).order(ByteOrder.nativeOrder());
|
ByteBuffer bb = ByteBuffer.allocate(name.length * Long.BYTES).order(ByteOrder.nativeOrder());
|
||||||
bb.asLongBuffer().put(name);
|
bb.asLongBuffer().put(name);
|
||||||
byte[] array = bb.array();
|
byte[] array = bb.array();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (array[i++] != ';')
|
while (array[i++] != ';')
|
||||||
;
|
;
|
||||||
return new String(array, 0, i - 1, StandardCharsets.UTF_8);
|
nameAsString = new String(array, 0, i - 1, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
return nameAsString;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException, InterruptedException {
|
||||||
if (args.length == 0 || !("--worker".equals(args[0]))) {
|
if (args.length == 0 || !("--worker".equals(args[0]))) {
|
||||||
spawnWorker();
|
spawnWorker();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Calculate input segments.
|
// Calculate input segments.
|
||||||
int numberOfChunks = Runtime.getRuntime().availableProcessors();
|
int numberOfWorkers = Runtime.getRuntime().availableProcessors();
|
||||||
long[] chunks = getSegments(numberOfChunks);
|
final AtomicLong cursor = new AtomicLong();
|
||||||
|
final long fileEnd;
|
||||||
|
final long fileStart;
|
||||||
|
|
||||||
|
try (var fileChannel = FileChannel.open(Path.of(FILE), StandardOpenOption.READ)) {
|
||||||
|
long fileSize = fileChannel.size();
|
||||||
|
fileStart = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize, java.lang.foreign.Arena.global()).address();
|
||||||
|
cursor.set(fileStart);
|
||||||
|
fileEnd = fileStart + fileSize;
|
||||||
|
}
|
||||||
|
|
||||||
// Parallel processing of segments.
|
// Parallel processing of segments.
|
||||||
List<List<Result>> allResults = IntStream.range(0, chunks.length - 1).mapToObj(chunkIndex -> parseLoop(chunks[chunkIndex], chunks[chunkIndex + 1]))
|
Thread[] threads = new Thread[numberOfWorkers];
|
||||||
.map(resultArray -> {
|
List<Result>[] allResults = new List[numberOfWorkers];
|
||||||
List<Result> results = new ArrayList<>();
|
for (int i = 0; i < threads.length; ++i) {
|
||||||
|
final int index = i;
|
||||||
|
threads[i] = new Thread(() -> {
|
||||||
|
Result[] resultArray = parseLoop(cursor, fileEnd, fileStart);
|
||||||
|
List<Result> results = new ArrayList<>(500);
|
||||||
for (Result r : resultArray) {
|
for (Result r : resultArray) {
|
||||||
if (r != null) {
|
if (r != null) {
|
||||||
|
r.calcName();
|
||||||
results.add(r);
|
results.add(r);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return results;
|
allResults[index] = results;
|
||||||
}).parallel().toList();
|
});
|
||||||
|
threads[i].start();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Thread thread : threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
|
||||||
// Final output.
|
// Final output.
|
||||||
System.out.println(accumulateResults(allResults));
|
System.out.println(accumulateResults(allResults));
|
||||||
@ -115,17 +140,12 @@ public class CalculateAverage_thomaswue {
|
|||||||
info.command().ifPresent(workerCommand::add);
|
info.command().ifPresent(workerCommand::add);
|
||||||
info.arguments().ifPresent(args -> workerCommand.addAll(Arrays.asList(args)));
|
info.arguments().ifPresent(args -> workerCommand.addAll(Arrays.asList(args)));
|
||||||
workerCommand.add("--worker");
|
workerCommand.add("--worker");
|
||||||
new ProcessBuilder()
|
new ProcessBuilder().command(workerCommand).inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE)
|
||||||
.command(workerCommand)
|
.start().getInputStream().transferTo(System.out);
|
||||||
.inheritIO()
|
|
||||||
.redirectOutput(ProcessBuilder.Redirect.PIPE)
|
|
||||||
.start()
|
|
||||||
.getInputStream()
|
|
||||||
.transferTo(System.out);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accumulate results sequentially for simplicity.
|
// Accumulate results sequentially for simplicity.
|
||||||
private static TreeMap<String, Result> accumulateResults(List<List<Result>> allResults) {
|
private static TreeMap<String, Result> accumulateResults(List<Result>[] allResults) {
|
||||||
TreeMap<String, Result> result = new TreeMap<>();
|
TreeMap<String, Result> result = new TreeMap<>();
|
||||||
for (List<Result> resultArr : allResults) {
|
for (List<Result> resultArr : allResults) {
|
||||||
for (Result r : resultArr) {
|
for (Result r : resultArr) {
|
||||||
@ -139,15 +159,13 @@ public class CalculateAverage_thomaswue {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main parse loop.
|
private static Result findResult(long initialWord, long initialPos, Scanner scanner, Result[] results) {
|
||||||
private static Result[] parseLoop(long chunkStart, long chunkEnd) {
|
|
||||||
Result[] results = new Result[1 << 17];
|
Result existingResult;
|
||||||
Scanner scanner = new Scanner(chunkStart, chunkEnd);
|
long word = initialWord;
|
||||||
long word = scanner.getLong();
|
long pos = initialPos;
|
||||||
long pos = findDelimiter(word);
|
long hash;
|
||||||
while (scanner.hasNext()) {
|
|
||||||
long nameAddress = scanner.pos();
|
long nameAddress = scanner.pos();
|
||||||
long hash = 0;
|
|
||||||
|
|
||||||
// Search for ';', one long at a time.
|
// Search for ';', one long at a time.
|
||||||
if (pos != 0) {
|
if (pos != 0) {
|
||||||
@ -156,20 +174,16 @@ public class CalculateAverage_thomaswue {
|
|||||||
word = mask(word, pos);
|
word = mask(word, pos);
|
||||||
hash = word;
|
hash = word;
|
||||||
|
|
||||||
int number = scanNumber(scanner);
|
int index = hashToIndex(hash, results);
|
||||||
long nextWord = scanner.getLong();
|
existingResult = results[index];
|
||||||
long nextPos = findDelimiter(nextWord);
|
|
||||||
|
|
||||||
Result existingResult = results[hashToIndex(hash, results)];
|
|
||||||
if (existingResult != null && existingResult.lastNameLong == word) {
|
if (existingResult != null && existingResult.lastNameLong == word) {
|
||||||
word = nextWord;
|
return existingResult;
|
||||||
pos = nextPos;
|
|
||||||
record(existingResult, number);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
scanner.setPos(nameAddress + pos);
|
scanner.setPos(nameAddress + pos);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
scanner.add(8);
|
scanner.add(8);
|
||||||
hash = word;
|
hash = word;
|
||||||
@ -181,14 +195,14 @@ public class CalculateAverage_thomaswue {
|
|||||||
scanner.add(pos);
|
scanner.add(pos);
|
||||||
word = mask(word, pos);
|
word = mask(word, pos);
|
||||||
hash ^= word;
|
hash ^= word;
|
||||||
|
int index = hashToIndex(hash, results);
|
||||||
|
existingResult = results[index];
|
||||||
|
|
||||||
Result existingResult = results[hashToIndex(hash, results)];
|
|
||||||
if (existingResult != null && existingResult.lastNameLong == word && existingResult.secondLastNameLong == prevWord) {
|
if (existingResult != null && existingResult.lastNameLong == word && existingResult.secondLastNameLong == prevWord) {
|
||||||
int number = scanNumber(scanner);
|
return existingResult;
|
||||||
word = scanner.getLong();
|
}
|
||||||
pos = findDelimiter(word);
|
else {
|
||||||
record(existingResult, number);
|
scanner.setPos(nameAddress + pos + 8);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -214,20 +228,19 @@ public class CalculateAverage_thomaswue {
|
|||||||
|
|
||||||
// Save length of name for later.
|
// Save length of name for later.
|
||||||
int nameLength = (int) (scanner.pos() - nameAddress);
|
int nameLength = (int) (scanner.pos() - nameAddress);
|
||||||
int number = scanNumber(scanner);
|
|
||||||
|
|
||||||
// Final calculation for index into hash table.
|
// Final calculation for index into hash table.
|
||||||
int tableIndex = hashToIndex(hash, results);
|
int tableIndex = hashToIndex(hash, results);
|
||||||
outer: while (true) {
|
outer: while (true) {
|
||||||
Result existingResult = results[tableIndex];
|
existingResult = results[tableIndex];
|
||||||
if (existingResult == null) {
|
if (existingResult == null) {
|
||||||
existingResult = newEntry(results, nameAddress, tableIndex, nameLength, scanner);
|
existingResult = newEntry(results, nameAddress, tableIndex, nameLength, scanner);
|
||||||
}
|
}
|
||||||
// Check for collision.
|
// Check for collision.
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int namePos = 0;
|
long[] name = existingResult.name;
|
||||||
for (; i < nameLength + 1 - 8; i += 8) {
|
for (; i < nameLength + 1 - 8; i += 8) {
|
||||||
if (namePos >= existingResult.name.length || existingResult.name[namePos++] != scanner.getLongAt(nameAddress + i)) {
|
if (scanner.getLongAt(i, name) != scanner.getLongAt(nameAddress + i)) {
|
||||||
tableIndex = (tableIndex + 31) & (results.length - 1);
|
tableIndex = (tableIndex + 31) & (results.length - 1);
|
||||||
continue outer;
|
continue outer;
|
||||||
}
|
}
|
||||||
@ -235,7 +248,6 @@ public class CalculateAverage_thomaswue {
|
|||||||
|
|
||||||
int remainingShift = (64 - (nameLength + 1 - i) << 3);
|
int remainingShift = (64 - (nameLength + 1 - i) << 3);
|
||||||
if (((existingResult.lastNameLong ^ (scanner.getLongAt(nameAddress + i) << remainingShift)) == 0)) {
|
if (((existingResult.lastNameLong ^ (scanner.getLongAt(nameAddress + i) << remainingShift)) == 0)) {
|
||||||
record(existingResult, number);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -243,37 +255,124 @@ public class CalculateAverage_thomaswue {
|
|||||||
tableIndex = (tableIndex + 31) & (results.length - 1);
|
tableIndex = (tableIndex + 31) & (results.length - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return existingResult;
|
||||||
word = scanner.getLong();
|
|
||||||
pos = findDelimiter(word);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static long nextNL(long prev) {
|
||||||
|
while (true) {
|
||||||
|
long currentWord = Scanner.UNSAFE.getLong(prev);
|
||||||
|
long pos = findNewLine(currentWord);
|
||||||
|
if (pos != 0) {
|
||||||
|
prev += Long.numberOfTrailingZeros(pos) >>> 3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
prev += 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final int SEGMENT_SIZE = 1024 * 1024 * 2;
|
||||||
|
|
||||||
|
// Main parse loop.
|
||||||
|
private static Result[] parseLoop(AtomicLong counter, long fileEnd, long fileStart) {
|
||||||
|
Result[] results = new Result[1 << 17];
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
long current = counter.addAndGet(SEGMENT_SIZE) - SEGMENT_SIZE;
|
||||||
|
|
||||||
|
if (current >= fileEnd) {
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int scanNumber(Scanner scanPtr) {
|
long segmentEnd = nextNL(Math.min(fileEnd - 1, current + SEGMENT_SIZE));
|
||||||
|
long segmentStart;
|
||||||
|
if (current == fileStart) {
|
||||||
|
segmentStart = current;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
segmentStart = nextNL(current) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
long dist = (segmentEnd - segmentStart) / 3;
|
||||||
|
long midPoint1 = nextNL(segmentStart + dist);
|
||||||
|
long midPoint2 = nextNL(segmentStart + dist + dist);
|
||||||
|
|
||||||
|
Scanner scanner1 = new Scanner(segmentStart, midPoint1);
|
||||||
|
Scanner scanner2 = new Scanner(midPoint1 + 1, midPoint2);
|
||||||
|
Scanner scanner3 = new Scanner(midPoint2 + 1, segmentEnd);
|
||||||
|
while (true) {
|
||||||
|
if (!scanner1.hasNext()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!scanner2.hasNext()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!scanner3.hasNext()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
long word1 = scanner1.getLong();
|
||||||
|
long word2 = scanner2.getLong();
|
||||||
|
long word3 = scanner3.getLong();
|
||||||
|
long pos1 = findDelimiter(word1);
|
||||||
|
long pos2 = findDelimiter(word2);
|
||||||
|
long pos3 = findDelimiter(word3);
|
||||||
|
Result existingResult1 = findResult(word1, pos1, scanner1, results);
|
||||||
|
Result existingResult2 = findResult(word2, pos2, scanner2, results);
|
||||||
|
Result existingResult3 = findResult(word3, pos3, scanner3, results);
|
||||||
|
long number1 = scanNumber(scanner1);
|
||||||
|
long number2 = scanNumber(scanner2);
|
||||||
|
long number3 = scanNumber(scanner3);
|
||||||
|
record(existingResult1, number1);
|
||||||
|
record(existingResult2, number2);
|
||||||
|
record(existingResult3, number3);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (scanner1.hasNext()) {
|
||||||
|
long word = scanner1.getLong();
|
||||||
|
long pos = findDelimiter(word);
|
||||||
|
record(findResult(word, pos, scanner1, results), scanNumber(scanner1));
|
||||||
|
}
|
||||||
|
|
||||||
|
while (scanner2.hasNext()) {
|
||||||
|
long word = scanner2.getLong();
|
||||||
|
long pos = findDelimiter(word);
|
||||||
|
record(findResult(word, pos, scanner2, results), scanNumber(scanner2));
|
||||||
|
}
|
||||||
|
|
||||||
|
while (scanner3.hasNext()) {
|
||||||
|
long word = scanner3.getLong();
|
||||||
|
long pos = findDelimiter(word);
|
||||||
|
record(findResult(word, pos, scanner3, results), scanNumber(scanner3));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long scanNumber(Scanner scanPtr) {
|
||||||
scanPtr.add(1);
|
scanPtr.add(1);
|
||||||
long numberWord = scanPtr.getLong();
|
long numberWord = scanPtr.getLong();
|
||||||
int decimalSepPos = Long.numberOfTrailingZeros(~numberWord & 0x10101000);
|
int decimalSepPos = Long.numberOfTrailingZeros(~numberWord & 0x10101000);
|
||||||
int number = convertIntoNumber(decimalSepPos, numberWord);
|
long number = convertIntoNumber(decimalSepPos, numberWord);
|
||||||
scanPtr.add((decimalSepPos >>> 3) + 3);
|
scanPtr.add((decimalSepPos >>> 3) + 3);
|
||||||
return number;
|
return number;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void record(Result existingResult, int number) {
|
private static void record(Result existingResult, long number) {
|
||||||
if (number < existingResult.min) {
|
if (number < existingResult.min) {
|
||||||
existingResult.min = (short) number;
|
existingResult.min = number;
|
||||||
}
|
}
|
||||||
if (number > existingResult.max) {
|
if (number > existingResult.max) {
|
||||||
existingResult.max = (short) number;
|
existingResult.max = number;
|
||||||
}
|
}
|
||||||
existingResult.sum += number;
|
existingResult.sum += number;
|
||||||
existingResult.count++;
|
existingResult.count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int hashToIndex(long hash, Result[] results) {
|
private static int hashToIndex(long hash, Result[] results) {
|
||||||
int hashAsInt = (int) (hash ^ (hash >>> 28));
|
long hashAsInt = hash ^ (hash >>> 37) ^ (hash >>> 17);
|
||||||
int finalHash = (hashAsInt ^ (hashAsInt >>> 17));
|
return (int) (hashAsInt & (results.length - 1));
|
||||||
return (finalHash & (results.length - 1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long mask(long word, long pos) {
|
private static long mask(long word, long pos) {
|
||||||
@ -281,7 +380,7 @@ public class CalculateAverage_thomaswue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Special method to convert a number in the ascii number into an int without branches created by Quan Anh Mai.
|
// Special method to convert a number in the ascii number into an int without branches created by Quan Anh Mai.
|
||||||
private static int convertIntoNumber(int decimalSepPos, long numberWord) {
|
private static long convertIntoNumber(int decimalSepPos, long numberWord) {
|
||||||
int shift = 28 - decimalSepPos;
|
int shift = 28 - decimalSepPos;
|
||||||
// signed is -1 if negative, 0 otherwise
|
// signed is -1 if negative, 0 otherwise
|
||||||
long signed = (~numberWord << 59) >> 63;
|
long signed = (~numberWord << 59) >> 63;
|
||||||
@ -292,8 +391,7 @@ public class CalculateAverage_thomaswue {
|
|||||||
// 0xUU00TTHH00 * (100 * 0x1000000 + 10 * 0x10000 + 1) =
|
// 0xUU00TTHH00 * (100 * 0x1000000 + 10 * 0x10000 + 1) =
|
||||||
// 0x000000UU00TTHH00 + 0x00UU00TTHH000000 * 10 + 0xUU00TTHH00000000 * 100
|
// 0x000000UU00TTHH00 + 0x00UU00TTHH000000 * 10 + 0xUU00TTHH00000000 * 100
|
||||||
long absValue = ((digits * 0x640a0001) >>> 32) & 0x3FF;
|
long absValue = ((digits * 0x640a0001) >>> 32) & 0x3FF;
|
||||||
long value = (absValue ^ signed) - signed;
|
return (absValue ^ signed) - signed;
|
||||||
return (int) value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long findDelimiter(long word) {
|
private static long findDelimiter(long word) {
|
||||||
@ -302,6 +400,12 @@ public class CalculateAverage_thomaswue {
|
|||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static long findNewLine(long word) {
|
||||||
|
long input = word ^ 0x0A0A0A0A0A0A0A0AL;
|
||||||
|
long tmp = (input - 0x0101010101010101L) & ~input & 0x8080808080808080L;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
private static Result newEntry(Result[] results, long nameAddress, int hash, int nameLength, Scanner scanner) {
|
private static Result newEntry(Result[] results, long nameAddress, int hash, int nameLength, Scanner scanner) {
|
||||||
Result r = new Result();
|
Result r = new Result();
|
||||||
results[hash] = r;
|
results[hash] = r;
|
||||||
@ -324,27 +428,6 @@ public class CalculateAverage_thomaswue {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long[] getSegments(int numberOfChunks) throws IOException {
|
|
||||||
try (var fileChannel = FileChannel.open(Path.of(FILE), StandardOpenOption.READ)) {
|
|
||||||
long fileSize = fileChannel.size();
|
|
||||||
long segmentSize = (fileSize + numberOfChunks - 1) / numberOfChunks;
|
|
||||||
long[] chunks = new long[numberOfChunks + 1];
|
|
||||||
long mappedAddress = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize, java.lang.foreign.Arena.global()).address();
|
|
||||||
chunks[0] = mappedAddress;
|
|
||||||
long endAddress = mappedAddress + fileSize;
|
|
||||||
Scanner s = new Scanner(mappedAddress, mappedAddress + fileSize);
|
|
||||||
for (int i = 1; i < numberOfChunks; ++i) {
|
|
||||||
long chunkAddress = mappedAddress + i * segmentSize;
|
|
||||||
// Align to first row start.
|
|
||||||
while (chunkAddress < endAddress && (s.getLongAt(chunkAddress++) & 0xFF) != '\n')
|
|
||||||
;
|
|
||||||
chunks[i] = Math.min(chunkAddress, endAddress);
|
|
||||||
}
|
|
||||||
chunks[numberOfChunks] = endAddress;
|
|
||||||
return chunks;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class Scanner {
|
private static class Scanner {
|
||||||
|
|
||||||
private static final sun.misc.Unsafe UNSAFE = initUnsafe();
|
private static final sun.misc.Unsafe UNSAFE = initUnsafe();
|
||||||
@ -387,6 +470,10 @@ public class CalculateAverage_thomaswue {
|
|||||||
return UNSAFE.getLong(pos);
|
return UNSAFE.getLong(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long getLongAt(long pos, long[] array) {
|
||||||
|
return UNSAFE.getLong(array, pos + sun.misc.Unsafe.ARRAY_LONG_BASE_OFFSET);
|
||||||
|
}
|
||||||
|
|
||||||
void setPos(long l) {
|
void setPos(long l) {
|
||||||
this.pos = l;
|
this.pos = l;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user