1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/*
* 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.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class CalculateAverage_nstng {
private static final File FILE = new File("./measurements.txt");
private static class MeasurementAggregator {
private double min;
private double max;
private long sum;
private long count;
public MeasurementAggregator(double min, double max, long sum, long count) {
this.min = min;
this.max = max;
this.sum = sum;
this.count = count;
}
@Override
public String toString() {
return "%s/%s/%s".formatted(min, round(sum / 10.0 / count), max);
}
private double round(double value) {
return Math.round(value * 10.0) / 10.0;
}
}
public static void main(String[] args) throws InterruptedException {
HashMap<BytesKey, MeasurementAggregator>[] maps;
Thread[] threads;
try (RandomAccessFile raf = new RandomAccessFile(FILE, "r")) {
// calc num threads to use
int availCpus = Runtime.getRuntime().availableProcessors();
// make sure to use at least num of threads so that byte chunks fit into an array
long fileLength = raf.length();
int minThreads = (int) Math.ceil(fileLength / (1.0 * Integer.MAX_VALUE));
int numThreads = Math.max(minThreads, availCpus);
// create threads - each thread processes a chunk of the file
// the chunks have all equal size (modulo take care that a chunk does not
// start/end in the middle of a line)
threads = new Thread[numThreads];
maps = new HashMap[numThreads];
long chunkSize = fileLength / numThreads;
long lastChunkEnd = 0;
for (int i = 0; i < numThreads; i++) {
raf.seek(Math.min(lastChunkEnd + chunkSize, fileLength));
readUntilLineBreak(raf);
long chunkEnd = raf.getFilePointer();
long finalLastChunkEnd = lastChunkEnd;
// 413 possible cities
maps[i] = new HashMap<>(413);
HashMap<BytesKey, MeasurementAggregator> finalMap = maps[i];
threads[i] = new Thread(() -> {
try {
// do not work on empty chunks
if (chunkEnd - finalLastChunkEnd <= 0)
return;
// -1: left from found line break
processChunk(finalLastChunkEnd, chunkEnd - 1, finalMap);
}
catch (IOException e) {
throw new RuntimeException(e);
}
});
lastChunkEnd = chunkEnd;
}
}
catch (IOException e) {
throw new RuntimeException(e);
}
// run it ...
for (Thread thread : threads) {
thread.start();
}
// ... and wait for all threads to finish
for (Thread thread : threads) {
thread.join();
}
// merge results
TreeMap<String, MeasurementAggregator> resultMap = new TreeMap<>();
for (HashMap<BytesKey, MeasurementAggregator> map : maps) {
for (Map.Entry<BytesKey, MeasurementAggregator> entry : map.entrySet()) {
String stringKey = entry.getKey().asString();
MeasurementAggregator agg = resultMap.get(stringKey);
if (agg == null) {
resultMap.put(stringKey, entry.getValue());
}
else {
agg.min = Math.min(entry.getValue().min, agg.min);
agg.max = Math.max(entry.getValue().max, agg.max);
agg.count = entry.getValue().count + agg.count;
agg.sum = entry.getValue().sum + agg.sum;
}
}
}
System.out.println(resultMap);
}
private static void processChunk(long fromPos, long toPos, HashMap<BytesKey, MeasurementAggregator> map)
throws IOException {
byte[] byteChunk = new byte[(int) (toPos - fromPos)];
try (RandomAccessFile raf = new RandomAccessFile(FILE, "r")) {
raf.seek(fromPos);
raf.read(byteChunk);
}
IndexedByteArray chunk = new IndexedByteArray(byteChunk);
// reads one line from right to left in each iteration
while (chunk.pos > 0) {
double mes = getMeasurement(chunk);
BytesKey cityKey = getCityKey(chunk);
addOrUpdateEntry(map, cityKey, mes);
}
}
private static void addOrUpdateEntry(HashMap<BytesKey, MeasurementAggregator> map, BytesKey cityKey, double mes) {
MeasurementAggregator agg = map.get(cityKey);
if (agg == null) {
map.put(cityKey, new MeasurementAggregator(mes, mes, (long) (mes * 10), 1));
}
else {
agg.min = Math.min(agg.min, mes);
agg.max = Math.max(agg.max, mes);
agg.sum += (long) (mes * 10);
agg.count++;
}
}
// did not know how much of a bottleneck new String(byteArray) can be :)
// use the byte array representation of a city as a HashMap key
// needs to be wrapped so that hashCode/equals can be defined
private static BytesKey getCityKey(IndexedByteArray chunk) {
// city name length >=3 known
int cityLength = 3;
chunk.dec(3);
while (chunk.pos >= 0 && chunk.getCurrent() != '\n') {
chunk.dec();
cityLength++;
}
byte[] cityBytes = new byte[cityLength];
// we are on '\n' or out of bounds by -1 -> srcPos is chunk.pos + 1
System.arraycopy(chunk.array, chunk.pos + 1, cityBytes, 0, cityLength);
chunk.dec(); // move away from '\n' (or further out of bounds)
return new BytesKey(cityBytes);
}
// did not know how much of a bottleneck Double.parseDouble(s) can be :)
// parses a double value from right to left
// massively uses that a measurement value is very restricted
// (<100, >-100, exactly one decimal place)
private static double getMeasurement(IndexedByteArray chunk) {
int asciiNumbersOffset = 48;
double mes = (chunk.getCurrent() - asciiNumbersOffset) / 10.0; // 10^-1 place
chunk.dec(2); // jump over '.'
mes += chunk.getCurrent() - asciiNumbersOffset; // 10^0 place
chunk.dec();
switch (chunk.getCurrent()) {
case '-' -> {
mes *= -1; // <10 and negative
chunk.dec();
}
case ';' -> {
// do nothing - we will move to the left at the end
}
default -> {
mes += (chunk.getCurrent() - asciiNumbersOffset) * 10; // 10^1 place
chunk.dec();
}
}
if (chunk.getCurrent() == '-') {
mes *= -1; // >=10 and negative
chunk.dec();
}
chunk.dec(); // move away from ';'
return mes;
}
// just a thin wrapper around a byte array - makes handling return values and
// position updates more easy
private static class IndexedByteArray {
private final byte[] array;
private int pos;
public IndexedByteArray(byte[] array) {
this.array = array;
this.pos = array.length - 1;
}
public void dec(int by) {
pos -= by;
}
public void dec() {
pos--;
}
public int getCurrent() {
return array[pos];
}
}
private static void readUntilLineBreak(RandomAccessFile raf) throws IOException {
boolean eor = false;
while (!eor) {
int c = raf.read();
if (c == -1 || c == '\n') {
eor = true;
}
}
}
// inspired by https://www.baeldung.com/java-map-key-byte-array
private static class BytesKey {
private final byte[] array;
private int hash;
public BytesKey(byte[] array) {
this.array = array;
// pre-calculated value with perfect hash function (by trial and error :P)
// -> i.e., can be used also for equals
this.hash = 1;
for (byte b : array)
this.hash = 11 * this.hash + b;
}
@Override
public boolean equals(Object o) {
return hash == o.hashCode();
}
@Override
public int hashCode() {
return hash;
}
public String asString() {
return new String(array);
}
}
}
|