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
|
/*
* 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.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
/**
* Samuel Yvon's entry.
* <p>
* Explanation behind my reasoning:
* - I want to make it as fast as possible without it being an unreadable mess; I want to avoid bit fiddling UTF-8
* and use the provided facilities
* - I use the fact that we know the number of stations to optimize HashMap creation (75% rule)
* - I stole branch-less compare from royvanrijn
* - I assume valid ASCII encoding for the number part, which allows me to parse it manually
* (should hold for valid UTF-8)
* - I have not done Java in forever. Especially what the heck it's become. I've looked at the other submissions and
* the given sample to get inspiration. I did not even know about this Stream API thing.
* </p>
*
* <p>
* Future ideas:
* - Probably can Vector-Apirize the number parsing (but it's three to four numbers, is it worth?)
* </p>
*
* <p>
* Observations:
* - [2024-01-09] The branch-less code from royvarijn does not have a huge impact
* </p>
*
* <p>
* Changelogs:
* 2024-01-09: Naive multi-threaded, no floats, manual line parsing
* </p>
*/
public class CalculateAverage_samuelyvon {
private static final String FILE = "./measurements.txt";
private static final int MAX_STATIONS = 10000;
private static final byte SEMICOL = 0x3B;
private static final byte MINUS = '-';
private static final byte ZERO = '0';
private static final byte NEWLINE = '\n';
// The minimum line length in bytes (over-egg.)
private static final int MIN_LINE_LENGTH_BYTES = 200;
/**
* Branchless min (unprecise for large numbers, but good enough)
*
* @author royvanrijn
*/
private static int branchlessMax(final int a, final int b) {
final int diff = a - b;
final int dsgn = diff >> 31;
return a - (diff & dsgn);
}
/**
* Branchless min (unprecise for large numbers, but good enough)
*
* @author royvanrijn
*/
private static int branchlessMin(final int a, final int b) {
final int diff = a - b;
final int dsgn = diff >> 31;
return b + (diff & dsgn);
}
private static class StationMeasureAgg {
private int min;
private int max;
private long sum;
private long count;
private final String city;
public StationMeasureAgg(String city) {
// Actual numbers are between -99.9 and 99.9, but we *10 to avoid float
this.city = city;
min = 1000;
max = -1000;
sum = 0;
count = 0;
}
public String city() {
return this.city;
}
public StationMeasureAgg mergeWith(StationMeasureAgg other) {
min = branchlessMin(min, other.min);
max = branchlessMax(max, other.max);
sum += other.sum;
count += other.count;
return this;
}
public void accumulate(int number) {
min = branchlessMin(min, number);
max = branchlessMax(max, number);
sum += number;
count++;
}
@Override
public String toString() {
double min = Math.round((double) this.min) / 10.0;
double max = Math.round((double) this.max) / 10.0;
double mean = Math.round((((double) this.sum / this.count))) / 10.0;
return min + "/" + mean + "/" + max;
}
}
private static HashMap<String, StationMeasureAgg> parseChunk(MappedByteBuffer chunk) {
HashMap<String, StationMeasureAgg> m = HashMap.newHashMap(MAX_STATIONS);
int i = 0;
while (i < chunk.limit()) {
int j = i;
for (; j < chunk.limit(); ++j) {
// TODO: Could compute a hash here, store a byte array, and decode UTF-8 at the
// latest moment.
if (chunk.get(j) == SEMICOL) {
break;
}
}
byte[] backingNameArray = new byte[j - i];
chunk.get(i, backingNameArray);
String name = new String(backingNameArray, StandardCharsets.UTF_8);
// Skip the `;`
j++;
// Parse the int ourselves, avoids a 'String::replace' to remove the
// digit.
int temp = 0;
boolean neg = chunk.get(j) == MINUS;
for (j = j + (neg ? 1 : 0); j < chunk.limit(); ++j) {
temp *= 10;
byte c = chunk.get(j);
if (c != '.') {
temp += (char) (c - ZERO);
}
else {
j++;
break;
}
}
// The decimal point
temp += (char) (chunk.get(j) - ZERO);
i = j + 1;
while (chunk.get(i++) != '\n')
;
if (neg)
temp = -temp;
m.computeIfAbsent(name, StationMeasureAgg::new).accumulate(temp);
}
return m;
}
private static int approximateChunks() {
// https://stackoverflow.com/a/4759606
// I don't remember Java :D
return Runtime.getRuntime().availableProcessors();
}
private static List<MappedByteBuffer> getFileChunks() throws IOException {
int approxChunkCount = approximateChunks();
List<MappedByteBuffer> fileChunks = new ArrayList<>(approxChunkCount * 2);
// Compute chunks offsets and lengths
try (RandomAccessFile file = new RandomAccessFile(FILE, "r")) {
long totalOffset = 0;
long fLength = file.length();
long approximateLength = Long.max(fLength / approxChunkCount, MIN_LINE_LENGTH_BYTES);
while (totalOffset < fLength) {
long offset = totalOffset;
int length = (int) approximateLength;
boolean eof = offset + length >= fLength;
if (eof) {
length = (int) (fLength - offset);
}
MappedByteBuffer out = file.getChannel().map(FileChannel.MapMode.READ_ONLY, totalOffset, length);
while (out.get(length - 1) != NEWLINE) {
length--;
}
out.position(0);
out.limit(length);
fileChunks.add(out);
totalOffset += length;
}
}
return fileChunks;
}
public static void main(String[] args) throws IOException {
var fileChunks = getFileChunks();
// Map per core, giving the non-overlapping memory slices
final Map<String, StationMeasureAgg> sortedMeasures = fileChunks.parallelStream().map(CalculateAverage_samuelyvon::parseChunk)
.flatMap(x -> x.values().stream()).collect(Collectors.toMap(StationMeasureAgg::city, x -> x, StationMeasureAgg::mergeWith, TreeMap::new));
System.out.println(sortedMeasures);
}
}
|