Skip to content

Commit

Permalink
cut the tail
Browse files Browse the repository at this point in the history
  • Loading branch information
abeobk committed Jan 31, 2024
1 parent 4d05044 commit 9078da5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
2 changes: 1 addition & 1 deletion prepare_abeobk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ sdk use java 21.0.2-graal 1>&2

# ./mvnw clean verify removes target/ and will re-trigger native image creation.
if [ ! -f target/CalculateAverage_abeobk_image ]; then
NATIVE_IMAGE_OPTS="--gc=epsilon -O3 -dsa -march=native -H:InlineAllBonus=10 -H:-GenLoopSafepoints -H:-ParseRuntimeOptions --enable-preview --initialize-at-build-time=dev.morling.onebrc.CalculateAverage_abeobk"
NATIVE_IMAGE_OPTS="--gc=epsilon -O3 -march=native -H:InlineAllBonus=10 -H:-GenLoopSafepoints --enable-preview --initialize-at-build-time=dev.morling.onebrc.CalculateAverage_abeobk"
native-image $NATIVE_IMAGE_OPTS -cp target/average-1.0.0-SNAPSHOT.jar -o target/CalculateAverage_abeobk_image dev.morling.onebrc.CalculateAverage_abeobk
fi
44 changes: 18 additions & 26 deletions src/main/java/dev/morling/onebrc/CalculateAverage_abeobk.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class CalculateAverage_abeobk {
private static final long BUCKET_MASK = BUCKET_SIZE - 1;
private static final int MAX_STR_LEN = 100;
private static final int MAX_STATIONS = 10000;
private static final long CHUNK_SZ = 1 << 22; // 4MB chunk
private static final long CHUNK_SZ = 1 << 22;
private static final Unsafe UNSAFE = initUnsafe();
private static final long[] HASH_MASKS = new long[]{
0x0L,
Expand All @@ -59,10 +59,6 @@ public class CalculateAverage_abeobk {
private static int chunk_cnt;
private static long start_addr, end_addr;

private static final void debug(String s, Object... args) {
System.out.println(String.format(s, args));
}

private static Unsafe initUnsafe() {
try {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
Expand Down Expand Up @@ -185,7 +181,6 @@ final static class Node {
long addr;
long hash;
long word0;
long tail;
long sum;
long min, max;
int keylen;
Expand All @@ -203,40 +198,36 @@ final String key() {
return new String(sbuf, 0, (int) keylen, StandardCharsets.UTF_8);
}

Node(long a, long t, int kl, long h, long v) {
Node(long a, long h, int kl, long v) {
addr = a;
tail = t;
min = max = v;
keylen = kl;
hash = h;
}

Node(long a, long t, int kl, long h) {
Node(long a, long h, int kl) {
addr = a;
tail = t;
hash = h;
min = 999;
max = -999;
keylen = kl;
hash = h;
}

Node(long a, long w0, long t, int kl, long h, long v) {
Node(long a, long w0, long h, int kl, long v) {
addr = a;
word0 = w0;
hash = h;
min = max = v;
tail = t;
keylen = kl;
hash = h;
}

Node(long a, long w0, long t, int kl, long h) {
Node(long a, long w0, long h, int kl) {
addr = a;
word0 = w0;
hash = h;
min = 999;
max = -999;
tail = t;
keylen = kl;
hash = h;
}

final void add(long val) {
Expand All @@ -261,8 +252,8 @@ final void merge(Node other) {
}
}

final boolean contentEquals(long other_addr, long other_word0, long other_tail, long kl) {
if (word0 != other_word0 || tail != other_tail)
final boolean contentEquals(long other_addr, long other_word0, long other_hash, long kl) {
if (word0 != other_word0 || hash != other_hash)
return false;
// this is faster than comparision if key is short
long xsum = 0;
Expand All @@ -274,7 +265,7 @@ final boolean contentEquals(long other_addr, long other_word0, long other_tail,
}

final boolean contentEquals(Node other) {
if (tail != other.tail)
if (hash != other.hash)
return false;
long n = keylen & 0xF8;
for (long i = 0; i < n; i += 8) {
Expand All @@ -291,6 +282,7 @@ static final class Worker extends Thread {

Worker(int i) {
thread_id = i;
this.setPriority(Thread.MAX_PRIORITY);
this.start();
}

Expand Down Expand Up @@ -443,9 +435,9 @@ final Node key(long word0, long semipos_code) {
while (true) {
Node node = map[bucket];
if (node == null) {
return (map[bucket] = new Node(row_addr, tail, semi_pos, hash, val0()));
return (map[bucket] = new Node(row_addr, hash, semi_pos));
}
if (node.tail == tail) {
if (node.hash == hash) {
return node;
}
bucket++;
Expand All @@ -465,9 +457,9 @@ final Node key(long word0, long semipos_code) {
while (true) {
Node node = map[bucket];
if (node == null) {
return (map[bucket] = new Node(row_addr, word0, tail, semi_pos + 8, hash, val0()));
return (map[bucket] = new Node(row_addr, word0, hash, semi_pos + 8));
}
if (node.word0 == word0 && node.tail == tail) {
if (node.word0 == word0 && node.hash == hash) {
return node;
}
bucket++;
Expand All @@ -494,9 +486,9 @@ final Node key(long word0, long semipos_code) {
while (true) {
Node node = map[bucket];
if (node == null) {
return (map[bucket] = new Node(row_addr, word0, tail, (int) keylen, hash, val0()));
return (map[bucket] = new Node(row_addr, word0, hash, (int) keylen));
}
if (node.contentEquals(row_addr, word0, tail, keylen)) {
if (node.contentEquals(row_addr, word0, hash, keylen)) {
return node;
}
bucket++;
Expand Down

0 comments on commit 9078da5

Please sign in to comment.