aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliot Barlas <elliotbarlas@gmail.com>2024-01-13 19:17:16 -0800
committerGunnar Morling <gunnar.morling@googlemail.com>2024-01-14 10:24:52 +0100
commitd608f14886d740cad8ae58513b5e20b8cba20d85 (patch)
tree17e7eec9223c7d0cb1c76a6544790a83be7f29f9
parenta0f826c2e4441c93b4c6cf54df77793c03f2cc67 (diff)
Consider file size when calculating partition count. Add simple fast-path optimization to equals method.
-rw-r--r--src/main/java/dev/morling/onebrc/CalculateAverage_ebarlas.java16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/main/java/dev/morling/onebrc/CalculateAverage_ebarlas.java b/src/main/java/dev/morling/onebrc/CalculateAverage_ebarlas.java
index b2a89d0..7c24afd 100644
--- a/src/main/java/dev/morling/onebrc/CalculateAverage_ebarlas.java
+++ b/src/main/java/dev/morling/onebrc/CalculateAverage_ebarlas.java
@@ -49,8 +49,8 @@ public class CalculateAverage_ebarlas {
public static void main(String[] args) throws IOException, InterruptedException {
var path = Paths.get("measurements.txt");
- var numPartitions = Math.max(8, Runtime.getRuntime().availableProcessors());
var channel = FileChannel.open(path, StandardOpenOption.READ);
+ var numPartitions = (int) Math.max((channel.size() / Integer.MAX_VALUE) + 1, Runtime.getRuntime().availableProcessors());
var partitionSize = channel.size() / numPartitions;
var partitions = new Partition[numPartitions];
var threads = new Thread[numPartitions];
@@ -257,7 +257,19 @@ public class CalculateAverage_ebarlas {
if (len1 != len2) {
return false;
}
- for (long i = 0; i < len1; i++) {
+ if (len1 == 2) {
+ return UNSAFE.getLong(key1) == UNSAFE.getLong(key2);
+ }
+ if (len1 == 3) {
+ return UNSAFE.getInt(key1) == UNSAFE.getInt(key2) && UNSAFE.getInt(key1 + 4) == UNSAFE.getInt(key2 + 4);
+ }
+ if (len1 == 1) {
+ return UNSAFE.getInt(key1) == UNSAFE.getInt(key2);
+ }
+ if (len1 == 4) {
+ return UNSAFE.getLong(key1) == UNSAFE.getLong(key2) && UNSAFE.getLong(key1 + 8) == UNSAFE.getLong(key2 + 8);
+ }
+ for (int i = 0; i < len1; i++) {
var offset = i << 2;
if (UNSAFE.getInt(key1 + offset) != UNSAFE.getInt(key2 + offset)) {
return false;