aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/morling/onebrc/CalculateAverage_yavuztas.java
blob: e33fe7e9293ee128ab1f81ee9840d4dae712fde4 (plain)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 *  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 sun.misc.Unsafe;

import java.io.IOException;
import java.lang.foreign.Arena;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.TreeMap;
import java.util.function.Consumer;

public class CalculateAverage_yavuztas {

    private static final Path FILE = Path.of("./measurements.txt");

    private static final Unsafe UNSAFE = unsafe();

    // Tried all there: MappedByteBuffer, MemorySegment and Unsafe
    // Accessing the memory using Unsafe is still the fastest in my experience
    private static Unsafe unsafe() {
        try {
            final Field f = Unsafe.class.getDeclaredField("theUnsafe");
            f.setAccessible(true);
            return (Unsafe) f.get(null);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // Only one object, both for measurements and keys, less object creation in hotpots is always faster
    static class Record {

        // keep memory starting address for each segment
        // since we use Unsafe, this is enough to align and fetch the data
        long segment;
        int start;
        int length;
        int hash;

        private int min = 1000; // calculations over int is faster than double, we convert to double in the end only once
        private int max = -1000;
        private long sum;
        private long count;

        public Record(long segment, int start, int length, int hash) {
            this.segment = segment;
            this.start = start;
            this.length = length;
            this.hash = hash;
        }

        @Override
        public boolean equals(Object o) {
            final Record record = (Record) o;
            return equals(record.segment, record.start, record.length, record.hash);
        }

        /**
         * Stateless equals, no Record object needed
         */
        public boolean equals(long segment, int start, int length, int hash) {
            if (this.length != length || this.hash != hash)
                return false;

            int i = 0; // bytes mismatch check
            while (i < this.length
                    && UNSAFE.getByte(this.segment + this.start + i) == UNSAFE.getByte(segment + start + i)) {
                i++;
            }
            return i == this.length;
        }

        @Override
        public int hashCode() {
            return this.hash;
        }

        @Override
        public String toString() {
            final byte[] bytes = new byte[this.length];
            int i = 0;
            while (i < this.length) {
                bytes[i] = UNSAFE.getByte(this.segment + this.start + i++);
            }

            return new String(bytes, StandardCharsets.UTF_8);
        }

        public Record collect(int temp) {
            this.min = Math.min(this.min, temp);
            this.max = Math.max(this.max, temp);
            this.sum += temp;
            this.count++;
            return this;
        }

        public void merge(Record other) {
            this.min = Math.min(this.min, other.min);
            this.max = Math.max(this.max, other.max);
            this.sum += other.sum;
            this.count += other.count;
        }

        public String measurements() {
            // here is only executed once for each unique key, so StringBuilder creation doesn't harm
            final StringBuilder sb = new StringBuilder(14);
            sb.append(this.min / 10.0);
            sb.append("/");
            sb.append(round((this.sum / 10.0) / this.count));
            sb.append("/");
            sb.append(this.max / 10.0);
            return sb.toString();
        }
    }

    // Inspired by @spullara - customized hashmap on purpose
    // The main difference is we hold only one array instead of two
    static class RecordMap {

        static final int SIZE = 1 << 15; // 32k - bigger bucket size less collisions
        static final int BITMASK = SIZE - 1;
        Record[] keys = new Record[SIZE];

        static int hashBucket(int hash) {
            hash = hash ^ (hash >>> 16); // naive bit spreading but surprisingly decreases collision :)
            return hash & BITMASK; // fast modulo, to find bucket
        }

        void putAndCollect(long segment, int start, int length, int hash, int temp) {
            int bucket = hashBucket(hash);
            Record existing = this.keys[bucket];
            if (existing == null) {
                this.keys[bucket] = new Record(segment, start, length, hash)
                        .collect(temp);
                return;
            }

            if (!existing.equals(segment, start, length, hash)) {
                // collision, linear probing to find a slot
                while ((existing = this.keys[++bucket & BITMASK]) != null && !existing.equals(segment, start, length, hash)) {
                    // can be stuck here if all the buckets are full :(
                    // However, since the data set is max 10K (unique) this shouldn't happen
                    // So, I'm happily leave here branchless :)
                }
                if (existing == null) {
                    this.keys[bucket & BITMASK] = new Record(segment, start, length, hash)
                            .collect(temp);
                    return;
                }
                existing.collect(temp);
            }
            else {
                existing.collect(temp);
            }
        }

        void putOrMerge(Record key) {
            int bucket = hashBucket(key.hash);
            Record existing = this.keys[bucket];
            if (existing == null) {
                this.keys[bucket] = key;
                return;
            }

            if (!existing.equals(key)) {
                // collision, linear probing to find a slot
                while ((existing = this.keys[++bucket & BITMASK]) != null && !existing.equals(key)) {
                    // can be stuck here if all the buckets are full :(
                    // However, since the data set is max 10K (unique keys) this shouldn't happen
                    // So, I'm happily leave here branchless :)
                }
                if (existing == null) {
                    this.keys[bucket & BITMASK] = key;
                    return;
                }
                existing.merge(key);
            }
            else {
                existing.merge(key);
            }
        }

        void forEach(Consumer<Record> consumer) {
            int pos = 0;
            Record key;
            while (pos < this.keys.length) {
                if ((key = this.keys[pos++]) == null) {
                    continue;
                }
                consumer.accept(key);
            }
        }

        void merge(RecordMap other) {
            other.forEach(this::putOrMerge);
        }

    }

    // One actor for one thread, no synchronization
    static class RegionActor {

        final FileChannel channel;
        final long startPos;
        final int size;
        final RecordMap map = new RecordMap();
        long segmentAddress;
        int position;
        Thread runner; // each actor has its own thread

        public RegionActor(FileChannel channel, long startPos, int size) {
            this.channel = channel;
            this.startPos = startPos;
            this.size = size;
        }

        void accumulate() {
            this.runner = new Thread(() -> {
                try {
                    // get the segment memory address, this is the only thing we need for Unsafe
                    this.segmentAddress = this.channel.map(FileChannel.MapMode.READ_ONLY, this.startPos, this.size, Arena.global()).address();
                }
                catch (IOException e) {
                    // no-op - skip intentionally, no handling for the purpose of this challenge
                }

                int start;
                int keyHash;
                int length;
                while (this.position < this.size) {
                    byte b;
                    start = this.position; // save line start position
                    keyHash = UNSAFE.getByte(this.segmentAddress + this.position++); // first byte is guaranteed not to be ';'
                    length = 1; // min key length
                    while ((b = UNSAFE.getByte(this.segmentAddress + this.position++)) != ';') { // read until semicolon
                        keyHash = calculateHash(keyHash, b); // calculate key hash ahead, eleminates one more loop later
                        length++;
                    }

                    final int temp = readTemperature();
                    this.map.putAndCollect(this.segmentAddress, start, length, keyHash, temp);

                    this.position++; // skip linebreak
                }
            });
            this.runner.start();
        }

        static int calculateHash(int hash, int b) {
            return 31 * hash + b;
        }

        // 1. Inspired by @yemreinci - Reading temparature value without Double.parse
        // 2. Inspired by @obourgain - Fetching first 4 bytes ahead, then masking
        int readTemperature() {
            int temp = 0;
            // read 4 bytes ahead
            final int first4 = UNSAFE.getInt(this.segmentAddress + this.position);
            this.position += 3;

            final byte b1 = (byte) first4; // first byte
            final byte b2 = (byte) ((first4 >> 8) & 0xFF); // second byte
            final byte b3 = (byte) ((first4 >> 16) & 0xFF); // third byte
            if (b1 == '-') {
                if (b3 == '.') {
                    temp -= 10 * (b2 - '0') + (byte) ((first4 >> 24) & 0xFF) - '0'; // fourth byte
                    this.position++;
                }
                else {
                    this.position++; // skip dot
                    temp -= 100 * (b2 - '0') + 10 * (b3 - '0') + UNSAFE.getByte(this.segmentAddress + this.position++) - '0'; // fifth byte
                }
            }
            else {
                if (b2 == '.') {
                    temp = 10 * (b1 - '0') + b3 - '0';
                }
                else {
                    temp = 100 * (b1 - '0') + 10 * (b2 - '0') + (byte) ((first4 >> 24) & 0xFF) - '0'; // fourth byte
                    this.position++;
                }
            }

            return temp;
        }

        /**
         * blocks until the map is fully collected
         */
        RecordMap get() throws InterruptedException {
            this.runner.join();
            return this.map;
        }
    }

    private static double round(double value) {
        return Math.round(value * 10.0) / 10.0;
    }

    /**
     * Scans the given buffer to the left
     */
    private static long findClosestLineEnd(long start, int size, FileChannel channel) throws IOException {
        final long position = start + size;
        final long left = Math.max(position - 101, 0);
        final ByteBuffer buffer = ByteBuffer.allocate(101); // enough size to find at least one '\n'
        if (channel.read(buffer.clear(), left) != -1) {
            int bufferPos = buffer.position() - 1;
            while (buffer.get(bufferPos) != '\n') {
                bufferPos--;
                size--;
            }
        }
        return size;
    }

    public static void main(String[] args) throws IOException, InterruptedException {

        var concurrency = Runtime.getRuntime().availableProcessors();
        final long fileSize = Files.size(FILE);
        long regionSize = fileSize / concurrency;

        // handling extreme cases
        while (regionSize > Integer.MAX_VALUE) {
            concurrency *= 2;
            regionSize /= 2;
        }
        if (fileSize <= 1 << 20) { // small file (1mb), no need concurrency
            concurrency = 1;
            regionSize = fileSize;
        }

        long startPos = 0;
        final FileChannel channel = (FileChannel) Files.newByteChannel(FILE, StandardOpenOption.READ);
        final RegionActor[] actors = new RegionActor[concurrency];
        for (int i = 0; i < concurrency; i++) {
            // calculate boundaries
            long maxSize = (startPos + regionSize > fileSize) ? fileSize - startPos : regionSize;
            // shift position to back until we find a linebreak
            maxSize = findClosestLineEnd(startPos, (int) maxSize, channel);

            final RegionActor region = (actors[i] = new RegionActor(channel, startPos, (int) maxSize));
            region.accumulate();

            startPos += maxSize;
        }

        final RecordMap output = new RecordMap(); // output to merge all regions
        for (RegionActor actor : actors) {
            final RecordMap partial = actor.get(); // blocks until get the result
            output.merge(partial);
        }

        // sort and print the result
        final TreeMap<String, String> sorted = new TreeMap<>();
        output.forEach(key -> sorted.put(key.toString(), key.measurements()));
        System.out.println(sorted);

    }

}