···
+
* 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;
+
import java.nio.channels.*;
+
import java.util.stream.*;
+
/* A simple implementation that memory maps the file, reads chunks in parallel and minimises allocation without any unsafe.
+
* Timings on 4 core i7-7500U CPU @ 2.70GHz:
+
* average_baseline: 4m48s
+
public class CalculateAverage_ianopolous {
+
public static final int MAX_LINE_LENGTH = 107;
+
public static final int MAX_STATIONS = 10000;
+
public static void main(String[] args) {
+
File input = new File("./measurements.txt");
+
long filesize = input.length();
+
long chunkSize = 256 * 1024 * 1024;
+
int nChunks = (int) ((filesize + chunkSize - 1) / chunkSize);
+
List<HashMap<String, Stat>> allResults = IntStream.range(0, nChunks).mapToObj(i -> {
+
HashMap<String, Stat> results = new HashMap(512);
+
parseStats(i * chunkSize, Math.min((i + 1) * chunkSize, filesize), results);
+
}).parallel().toList();
+
HashMap<String, Stat> result = allResults.getFirst();
+
for (int i = 1; i < allResults.size(); ++i) {
+
for (Map.Entry<String, Stat> entry : allResults.get(i).entrySet()) {
+
Stat current = result.putIfAbsent(entry.getKey(), entry.getValue());
+
current.merge(entry.getValue());
+
System.out.println(new TreeMap<>(result));
+
public record Station(String name, ByteBuffer buf) {
+
public static boolean matchingStationBytes(int start, int end, MappedByteBuffer buffer, Station existing) {
+
buffer.position(start);
+
for (int i = start; i < end; i++) {
+
if (existing.buf.get(i - start) != buffer.get(i))
+
public static Station parseStation(int start, int end, int hash, MappedByteBuffer buffer, List<List<Station>> stations) {
+
int index = Math.floorMod(hash, MAX_STATIONS);
+
List<Station> matches = stations.get(index);
+
List<Station> value = new ArrayList<>();
+
byte[] stationBuffer = new byte[end - start];
+
buffer.position(start);
+
buffer.get(stationBuffer);
+
String name = new String(stationBuffer);
+
Station res = new Station(name, ByteBuffer.wrap(stationBuffer));
+
stations.set(index, value);
+
for (int i = 0; i < matches.size(); i++) {
+
Station s = matches.get(i);
+
if (matchingStationBytes(start, end, buffer, s))
+
byte[] stationBuffer = new byte[end - start];
+
buffer.position(start);
+
buffer.get(stationBuffer);
+
Station res = new Station(new String(stationBuffer), ByteBuffer.wrap(stationBuffer));
+
public static void parseStats(long startByte, long endByte, Map<String, Stat> results) {
+
RandomAccessFile file = new RandomAccessFile("./measurements.txt", "r");
+
long maxEnd = Math.min(file.length(), endByte + MAX_LINE_LENGTH);
+
long len = maxEnd - startByte;
+
if (len > Integer.MAX_VALUE)
+
throw new RuntimeException("Segment size must fit into an int");
+
int maxDone = (int) (endByte - startByte);
+
MappedByteBuffer buffer = file.getChannel().map(FileChannel.MapMode.READ_ONLY, startByte, len);
+
// read first partial line
+
for (int i = 0; i < MAX_LINE_LENGTH; i++) {
+
byte b = buffer.get(i);
+
List<List<Station>> stations = new ArrayList<>(MAX_STATIONS);
+
for (int i = 0; i < MAX_STATIONS; i++)
+
boolean negative = false;
+
while (done < maxDone) {
+
Station station = null;
+
for (int i = done; i < done + MAX_LINE_LENGTH && i < maxEnd; i++) {
+
byte b = buffer.get(i);
+
Stat res = results.get(station.name);
+
temperature = negative ? -temperature : temperature;
+
results.put(station.name, res);
+
station = parseStation(lineStart, lineSplit, hash, buffer, stations);
+
else if (b == '-' && station != null) {
+
else if (b != '.' && station != null) {
+
temperature = temperature * 10 + (b - 0x30);
+
catch (IOException e) {
+
throw new RuntimeException(e);
+
public static class Stat {
+
long min = Long.MAX_VALUE, max = Long.MIN_VALUE, total = 0, count = 0;
+
public void add(long value) {
+
public void merge(Stat value) {
+
private static double round(double value) {
+
return Math.round(value) / 10.0;
+
public String toString() {
+
return round((double) min) + "/" + round(((double) total) / count) + "/" + round((double) max);