2024-01-03 10:14:19 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2023 The original authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package dev.morling.onebrc;
|
|
|
|
|
2024-01-03 14:53:38 +01:00
|
|
|
import java.io.*;
|
2024-01-03 20:22:39 +01:00
|
|
|
import java.lang.foreign.Arena;
|
|
|
|
import java.lang.foreign.MemorySegment;
|
|
|
|
import java.lang.foreign.ValueLayout;
|
2024-01-03 19:14:15 +01:00
|
|
|
import java.nio.channels.FileChannel;
|
2024-01-03 14:53:38 +01:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2024-01-03 10:14:19 +01:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
2024-01-03 19:14:15 +01:00
|
|
|
import java.nio.file.StandardOpenOption;
|
2024-01-03 10:14:19 +01:00
|
|
|
import java.time.Duration;
|
|
|
|
import java.time.Instant;
|
2024-01-06 19:24:48 +01:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.atomic.LongAdder;
|
2024-01-03 10:14:19 +01:00
|
|
|
|
|
|
|
public class CalculateAverage_ddimtirov {
|
2024-01-03 19:14:15 +01:00
|
|
|
private static final String FILE = "./measurements.txt";
|
2024-01-06 19:24:48 +01:00
|
|
|
private static final int MAX_STATIONS = 100_000;
|
|
|
|
private static final int MAX_STATION_NAME_LENGTH = 100;
|
|
|
|
private static final int MAX_UTF8_CODEPOINT_SIZE = 3;
|
2024-01-03 10:14:19 +01:00
|
|
|
|
2024-01-03 19:14:15 +01:00
|
|
|
private static final int OFFSET_MIN = 0;
|
|
|
|
private static final int OFFSET_MAX = 1;
|
|
|
|
private static final int OFFSET_COUNT = 2;
|
2024-01-03 10:14:19 +01:00
|
|
|
|
2024-01-06 19:24:48 +01:00
|
|
|
private static final boolean assertions = CalculateAverage_ddimtirov.class.desiredAssertionStatus();
|
|
|
|
private static final Map<String, LongAdder> hashCollisionOccurrences = new ConcurrentHashMap<>();
|
|
|
|
|
2024-01-03 14:53:38 +01:00
|
|
|
@SuppressWarnings("RedundantSuppression")
|
2024-01-06 19:24:48 +01:00
|
|
|
public static void main(String[] args) throws IOException, InterruptedException {
|
|
|
|
var path = Path.of(args.length>0 ? args[0] : FILE);
|
|
|
|
Instant start = null;// Instant.now();
|
2024-01-03 19:14:15 +01:00
|
|
|
|
2024-01-06 19:24:48 +01:00
|
|
|
var desiredSegmentsCount = Runtime.getRuntime().availableProcessors();
|
2024-01-03 20:22:39 +01:00
|
|
|
var fileSegments = FileSegment.forFile(path, desiredSegmentsCount);
|
2024-01-03 19:14:15 +01:00
|
|
|
|
2024-01-06 19:24:48 +01:00
|
|
|
var loaders = new ThreadGroup("Loaders");
|
|
|
|
var trackers = Collections.synchronizedList(new ArrayList<Tracker>());
|
|
|
|
var threads = fileSegments.stream().map(fileSegment -> Thread // manually start thread per segment
|
|
|
|
.ofPlatform()
|
|
|
|
.group(loaders)
|
|
|
|
.name(STR."Segment \{fileSegment}")
|
|
|
|
.start(() -> {
|
|
|
|
try (var fileChannel = (FileChannel) Files.newByteChannel(path, StandardOpenOption.READ)) {
|
|
|
|
var tracker = new Tracker();
|
|
|
|
var memorySegment = fileChannel.map(FileChannel.MapMode.READ_ONLY, fileSegment.start(), fileSegment.size(), Arena.ofConfined());
|
|
|
|
tracker.processSegment(memorySegment);
|
|
|
|
trackers.add(tracker);
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
).toList();
|
|
|
|
|
|
|
|
for (Thread thread : threads) thread.join();
|
|
|
|
assert trackers.size() == threads.size();
|
|
|
|
assert trackers.size() <= desiredSegmentsCount;
|
2024-01-03 14:53:38 +01:00
|
|
|
|
2024-01-06 19:24:48 +01:00
|
|
|
var result = summarizeTrackers(trackers.toArray(Tracker[]::new));
|
2024-01-03 19:14:15 +01:00
|
|
|
System.out.println(result);
|
2024-01-03 14:53:38 +01:00
|
|
|
|
2024-01-03 20:22:39 +01:00
|
|
|
// noinspection ConstantValue
|
2024-01-06 19:24:48 +01:00
|
|
|
if (start != null) {
|
2024-01-03 20:22:39 +01:00
|
|
|
System.err.println(Duration.between(start, Instant.now()));
|
2024-01-06 19:24:48 +01:00
|
|
|
if (assertions) System.err.printf("hash clashes: %s%n", hashCollisionOccurrences);
|
|
|
|
}
|
|
|
|
assert Files.readAllLines(Path.of("measurements.out")).getFirst().equals(result);
|
2024-01-03 14:53:38 +01:00
|
|
|
}
|
|
|
|
|
2024-01-06 19:24:48 +01:00
|
|
|
record FileSegment(int index, long start, long size) {
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return STR."#\{index} [\{start}..\{start + size}] \{size} bytes";
|
|
|
|
}
|
|
|
|
|
2024-01-03 19:14:15 +01:00
|
|
|
public static List<FileSegment> forFile(Path file, int desiredSegmentsCount) throws IOException {
|
|
|
|
try (var raf = new RandomAccessFile(file.toFile(), "r")) {
|
2024-01-03 20:22:39 +01:00
|
|
|
var segments = new ArrayList<FileSegment>();
|
|
|
|
var fileSize = raf.length();
|
2024-01-06 19:24:48 +01:00
|
|
|
var segmentSize = Math.max(1024 * 1024, fileSize / desiredSegmentsCount);
|
2024-01-03 10:14:19 +01:00
|
|
|
|
2024-01-06 19:24:48 +01:00
|
|
|
var i = 1;
|
|
|
|
var prevEnd = 0L;
|
|
|
|
while (prevEnd < fileSize-1) {
|
|
|
|
var start = prevEnd;
|
|
|
|
var end = findNewLineAfter(raf, prevEnd + segmentSize, fileSize);
|
|
|
|
segments.add(new FileSegment(i, start, end - start));
|
|
|
|
prevEnd = end;
|
2024-01-03 19:14:15 +01:00
|
|
|
}
|
|
|
|
return segments;
|
|
|
|
}
|
2024-01-03 10:14:19 +01:00
|
|
|
}
|
|
|
|
|
2024-01-06 19:24:48 +01:00
|
|
|
private static long findNewLineAfter(RandomAccessFile raf, long location, long fileSize) throws IOException {
|
2024-01-03 19:14:15 +01:00
|
|
|
raf.seek(location);
|
|
|
|
while (location < fileSize) {
|
|
|
|
location++;
|
2024-01-06 19:24:48 +01:00
|
|
|
int c = raf.read();
|
|
|
|
if (c == '\r' || c == '\n') break;
|
2024-01-03 14:53:38 +01:00
|
|
|
}
|
2024-01-06 19:24:48 +01:00
|
|
|
return Math.min(location, fileSize - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class Accumulator {
|
|
|
|
public final String name;
|
|
|
|
public int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, count;
|
|
|
|
public long sum;
|
|
|
|
|
|
|
|
public Accumulator(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void accumulate(int min, int max, int count, long sum) {
|
|
|
|
if (this.min > min)
|
|
|
|
this.min = min;
|
|
|
|
if (this.max < max)
|
|
|
|
this.max = max;
|
|
|
|
this.count += count;
|
|
|
|
this.sum += sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
var mean = Math.round((double) sum / count) / 10.0;
|
|
|
|
return (min / 10.0) + "/" + mean + "/" + (max / 10.0);
|
2024-01-03 19:14:15 +01:00
|
|
|
}
|
|
|
|
}
|
2024-01-03 14:53:38 +01:00
|
|
|
|
2024-01-06 19:24:48 +01:00
|
|
|
private static String summarizeTrackers(Tracker[] trackers) {
|
|
|
|
var result = new TreeMap<String, Accumulator>();
|
|
|
|
|
|
|
|
for (var i = 0; i < Tracker.SIZE; i++) {
|
|
|
|
Accumulator acc = null;
|
2024-01-03 19:14:15 +01:00
|
|
|
|
|
|
|
for (Tracker tracker : trackers) {
|
2024-01-06 19:24:48 +01:00
|
|
|
var name = tracker.names[i];
|
|
|
|
if (name == null) {
|
2024-01-03 20:22:39 +01:00
|
|
|
continue;
|
2024-01-06 19:24:48 +01:00
|
|
|
}
|
|
|
|
else if (acc == null || !name.equals(acc.name)) {
|
|
|
|
acc = result.computeIfAbsent(name, Accumulator::new);
|
|
|
|
}
|
|
|
|
acc.accumulate(
|
|
|
|
tracker.minMaxCount[i * 3 + OFFSET_MIN],
|
|
|
|
tracker.minMaxCount[i * 3 + OFFSET_MAX],
|
|
|
|
tracker.minMaxCount[i * 3 + OFFSET_COUNT],
|
|
|
|
tracker.sums[i]);
|
2024-01-03 14:53:38 +01:00
|
|
|
}
|
2024-01-03 19:14:15 +01:00
|
|
|
}
|
|
|
|
return result.toString();
|
|
|
|
}
|
2024-01-03 14:53:38 +01:00
|
|
|
|
2024-01-03 19:14:15 +01:00
|
|
|
static class Tracker {
|
2024-01-06 19:24:48 +01:00
|
|
|
public static final int SIZE = MAX_STATIONS * 10;
|
|
|
|
|
|
|
|
private static final int CORRECTION_0_TO_9 = '0' * 10 + '0';
|
|
|
|
private static final int CORRECTION_10_TO_99 = '0' * 100 + '0' * 10 + '0';
|
|
|
|
|
|
|
|
private final byte[] tempNameBytes = new byte[MAX_STATION_NAME_LENGTH * MAX_UTF8_CODEPOINT_SIZE];
|
|
|
|
private final int[] minMaxCount = new int[SIZE * 3];
|
|
|
|
private final long[] sums = new long[SIZE];
|
|
|
|
private final String[] names = new String[SIZE];
|
|
|
|
private final byte[][] nameBytes = new byte[SIZE][];
|
2024-01-03 19:14:15 +01:00
|
|
|
|
2024-01-03 20:22:39 +01:00
|
|
|
private void processSegment(MemorySegment memory) {
|
|
|
|
long limit = memory.byteSize();
|
2024-01-06 19:24:48 +01:00
|
|
|
|
|
|
|
var pos = 0;
|
|
|
|
|
|
|
|
// skip newlines so the chunk limit check can work correctly
|
|
|
|
while (pos < limit) {
|
|
|
|
byte c = memory.get(ValueLayout.JAVA_BYTE, pos);
|
|
|
|
if (c != '\r' && c != '\n')
|
|
|
|
break;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (pos < limit) {
|
2024-01-03 19:14:15 +01:00
|
|
|
byte b;
|
|
|
|
|
|
|
|
int nameLength = 0, nameHash = 0;
|
2024-01-03 20:22:39 +01:00
|
|
|
while ((b = memory.get(ValueLayout.JAVA_BYTE, pos++)) != ';') {
|
2024-01-06 19:24:48 +01:00
|
|
|
tempNameBytes[nameLength++] = b;
|
2024-01-03 20:22:39 +01:00
|
|
|
nameHash = nameHash * 31 + b;
|
2024-01-03 14:53:38 +01:00
|
|
|
}
|
2024-01-03 19:14:15 +01:00
|
|
|
|
2024-01-06 19:24:48 +01:00
|
|
|
int sign;
|
|
|
|
if (memory.get(ValueLayout.JAVA_BYTE, pos) == '-') {
|
|
|
|
sign = -1;
|
|
|
|
pos++;
|
2024-01-03 14:53:38 +01:00
|
|
|
}
|
2024-01-06 19:24:48 +01:00
|
|
|
else {
|
|
|
|
sign = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int temperature; // between [-99.9 and 99.9], mapped to fixed point int (scaled by 10)
|
|
|
|
if (memory.get(ValueLayout.JAVA_BYTE, pos + 1) == '.') { // between -9.99 and 9.99
|
|
|
|
assert memory.get(ValueLayout.JAVA_BYTE, pos + 1) == '.';
|
|
|
|
temperature = memory.get(ValueLayout.JAVA_BYTE, pos) * 10 +
|
|
|
|
memory.get(ValueLayout.JAVA_BYTE, pos + 2) - CORRECTION_0_TO_9;
|
|
|
|
pos += 3; // #.# - 3 chars
|
|
|
|
}
|
|
|
|
else { // between [-99.9 and -9.99] OR [9.99 and 99.9]
|
|
|
|
assert memory.get(ValueLayout.JAVA_BYTE, pos + 2) == '.';
|
|
|
|
temperature = memory.get(ValueLayout.JAVA_BYTE, pos) * 100 +
|
|
|
|
memory.get(ValueLayout.JAVA_BYTE, pos + 1) * 10 +
|
|
|
|
memory.get(ValueLayout.JAVA_BYTE, pos + 3) - CORRECTION_10_TO_99;
|
|
|
|
pos += 4; // ##.# - 4 chars
|
|
|
|
}
|
|
|
|
|
|
|
|
processLine(nameHash, tempNameBytes, nameLength, temperature * sign);
|
2024-01-03 14:53:38 +01:00
|
|
|
|
2024-01-06 19:24:48 +01:00
|
|
|
// skip newlines so the chunk limit check can work correctly
|
|
|
|
while (pos < limit) {
|
|
|
|
byte c = memory.get(ValueLayout.JAVA_BYTE, pos);
|
|
|
|
if (c != '\r' && c != '\n')
|
|
|
|
break;
|
|
|
|
pos++;
|
|
|
|
}
|
2024-01-03 19:14:15 +01:00
|
|
|
}
|
2024-01-03 10:14:19 +01:00
|
|
|
}
|
2024-01-03 14:53:38 +01:00
|
|
|
|
2024-01-06 19:24:48 +01:00
|
|
|
public void processLine(int nameHash, byte[] nameBytesBuffer, int nameLength, int temperature) {
|
|
|
|
var i = Math.abs(nameHash) % SIZE;
|
2024-01-03 14:53:38 +01:00
|
|
|
|
2024-01-06 19:24:48 +01:00
|
|
|
while (true) {
|
|
|
|
if (names[i] == null) {
|
|
|
|
byte[] trimmedBytes = Arrays.copyOf(nameBytesBuffer, nameLength);
|
|
|
|
names[i] = new String(trimmedBytes, StandardCharsets.UTF_8);
|
|
|
|
nameBytes[i] = trimmedBytes;
|
|
|
|
minMaxCount[i*3 + OFFSET_MIN] = Integer.MAX_VALUE;
|
|
|
|
minMaxCount[i*3 + OFFSET_MAX] = Integer.MIN_VALUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (nameBytes[i].length==nameLength && Arrays.equals(nameBytes[i], 0, nameLength, nameBytesBuffer, 0, nameLength)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (assertions) {
|
|
|
|
var key = new String(nameBytesBuffer, 0, nameLength, StandardCharsets.UTF_8);
|
|
|
|
hashCollisionOccurrences.computeIfAbsent(key, _ -> new LongAdder()).increment();
|
|
|
|
}
|
|
|
|
i = (i + 1) % SIZE;
|
2024-01-03 20:22:39 +01:00
|
|
|
}
|
2024-01-06 19:24:48 +01:00
|
|
|
if (assertions) {
|
|
|
|
var key = new String(nameBytesBuffer, 0, nameLength, StandardCharsets.UTF_8);
|
|
|
|
if (hashCollisionOccurrences.containsKey(key)) {
|
|
|
|
hashCollisionOccurrences.computeIfAbsent(STR."\{key}[\{i}]", _ -> new LongAdder()).increment();
|
|
|
|
}
|
2024-01-03 19:14:15 +01:00
|
|
|
}
|
2024-01-03 14:53:38 +01:00
|
|
|
|
2024-01-03 19:14:15 +01:00
|
|
|
sums[i] += temperature;
|
2024-01-03 14:53:38 +01:00
|
|
|
|
|
|
|
int mmcIndex = i * 3;
|
|
|
|
var min = minMaxCount[mmcIndex + OFFSET_MIN];
|
|
|
|
var max = minMaxCount[mmcIndex + OFFSET_MAX];
|
2024-01-03 20:22:39 +01:00
|
|
|
if (temperature < min)
|
|
|
|
minMaxCount[mmcIndex + OFFSET_MIN] = temperature;
|
|
|
|
if (temperature > max)
|
|
|
|
minMaxCount[mmcIndex + OFFSET_MAX] = temperature;
|
2024-01-03 14:53:38 +01:00
|
|
|
|
|
|
|
minMaxCount[mmcIndex + OFFSET_COUNT]++;
|
2024-01-03 10:14:19 +01:00
|
|
|
}
|
|
|
|
|
2024-01-03 14:53:38 +01:00
|
|
|
}
|
2024-01-03 10:14:19 +01:00
|
|
|
}
|