aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/morling/onebrc/CalculateAverage_padreati.java
blob: 1a327322412e97a9321c12807a8cb53dafa31427 (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
/*
 *  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.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.StructuredTaskScope;

import jdk.incubator.vector.ByteVector;
import jdk.incubator.vector.VectorOperators;
import jdk.incubator.vector.VectorSpecies;

public class CalculateAverage_padreati {

    private static final VectorSpecies<Byte> species = ByteVector.SPECIES_PREFERRED;
    private static final String FILE = "./measurements.txt";
    private static final int CHUNK_SIZE = 1024 * 1024;

    private record ResultRow(double min, double mean, double max) {
        public String toString() {
            return round(min) + "/" + round(mean) + "/" + round(max);
        }

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

    private record MeasurementAggregator(double min, double max, double sum, long count) {

        public MeasurementAggregator(double seed) {
            this(seed, seed, seed, 1);
        }

        public MeasurementAggregator merge(MeasurementAggregator b) {
            return new MeasurementAggregator(
                    Math.min(min, b.min),
                    Math.max(max, b.max),
                    sum + b.sum,
                    count + b.count
            );
        }

        public ResultRow toResultRow() {
            return new ResultRow(min, sum / count, max);
        }
    }

    public static void main(String[] args) throws IOException {
        new CalculateAverage_padreati().run();
    }

    private void run() throws IOException {
        File file = new File(FILE);
        var splits = findFileSplits();
        List<StructuredTaskScope.Subtask<Map<String, MeasurementAggregator>>> subtasks = new ArrayList<>();
        try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
            for (int i = 0; i < splits.size(); i++) {
                long splitStart = splits.get(i);
                long splitEnd = i < splits.size() - 1 ? splits.get(i + 1) : file.length() + 1;
                subtasks.add(scope.fork(() -> chunkProcessor(file, splitStart, splitEnd)));
            }
            scope.join();
            scope.throwIfFailed();

            var resultList = subtasks.stream().map(StructuredTaskScope.Subtask::get).toList();
            TreeMap<String, ResultRow> measurements = collapseResults(resultList);
            System.out.println(measurements);

        }
        catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    private List<Long> findFileSplits() throws IOException {
        var splits = new ArrayList<Long>();
        splits.add(0L);

        File file = new File(FILE);
        long next = CHUNK_SIZE;
        while (true) {
            if (next >= file.length()) {
                break;
            }
            try (FileInputStream fis = new FileInputStream(file)) {
                long skip = fis.skip(next);
                if (skip != next) {
                    throw new RuntimeException();
                }
                // find first new line
                while (true) {
                    int ch = fis.read();
                    if (ch != '\n') {
                        next++;
                        continue;
                    }
                    break;
                }
                // skip eventual \\r
                if (fis.read() == '\r') {
                    next++;
                }
                splits.add(next + 1);
                next += CHUNK_SIZE;
            }
        }
        return splits;
    }

    public Map<String, MeasurementAggregator> chunkProcessor(File source, long start, long end) throws IOException {
        var map = new HashMap<String, MeasurementAggregator>();
        byte[] buffer = new byte[(int) (end - start)];
        int len;
        try (FileInputStream bis = new FileInputStream(source)) {
            bis.skip(start);
            len = bis.read(buffer, 0, buffer.length);
        }

        List<Integer> nlIndexes = new ArrayList<>();
        List<Integer> commaIndexes = new ArrayList<>();

        int loopBound = species.loopBound(len);
        int i = 0;

        for (; i < loopBound; i += species.length()) {
            ByteVector v = ByteVector.fromArray(species, buffer, i);
            var mask = v.compare(VectorOperators.EQ, '\n');
            for (int j = 0; j < species.length(); j++) {
                if (mask.laneIsSet(j)) {
                    nlIndexes.add(i + j);
                }
            }
            mask = v.compare(VectorOperators.EQ, ';');
            for (int j = 0; j < species.length(); j++) {
                if (mask.laneIsSet(j)) {
                    commaIndexes.add(i + j);
                }
            }
        }
        for (; i < len; i++) {
            if (buffer[i] == '\n') {
                nlIndexes.add(i);
            }
            if (buffer[i] == ';') {
                commaIndexes.add(i);
            }
        }

        int startLine = 0;
        for (int j = 0; j < nlIndexes.size(); j++) {
            int endLine = nlIndexes.get(j);
            int commaIndex = commaIndexes.get(j);
            String key = new String(buffer, startLine, commaIndex - startLine);
            double value = Double.parseDouble(new String(buffer, commaIndex + 1, endLine - commaIndex - 1));
            map.merge(key, new MeasurementAggregator(value), MeasurementAggregator::merge);
            startLine = endLine + 1;
        }
        return map;
    }

    private TreeMap<String, ResultRow> collapseResults(List<Map<String, MeasurementAggregator>> resultList) {
        HashMap<String, MeasurementAggregator> aggregate = new HashMap<>();
        for (var map : resultList) {
            for (var entry : map.entrySet()) {
                aggregate.merge(entry.getKey(), entry.getValue(), MeasurementAggregator::merge);
            }
        }
        TreeMap<String, ResultRow> measurements = new TreeMap<>();
        for (var entry : aggregate.entrySet()) {
            measurements.put(entry.getKey(), entry.getValue().toResultRow());
        }
        return measurements;
    }

}