Skip to content

Commit

Permalink
Back to previous implementation of vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Dekeyser authored and Justin Dekeyser committed Jan 30, 2024
1 parent 3183781 commit 8387269
Showing 1 changed file with 30 additions and 41 deletions.
71 changes: 30 additions & 41 deletions src/main/java/dev/morling/onebrc/CalculateAverage_Judekeyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.UncheckedIOException;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
Expand All @@ -35,14 +34,19 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static java.lang.foreign.ValueLayout.OfByte.JAVA_BYTE;

public class CalculateAverage_Judekeyser {
private static final String FILE = "./measurements.txt";
private static final int chunkSize = (1 << 7) << 13; // This can't go beyond 2^21, because otherwise we might exceed int capacity

private static final int numberOfIOWorkers = 1 << 11; // We are going to need (numberOfIOWorkers-1) * chunkSize capacity
private static final int numberOfParallelWorkers = 2 * Runtime.getRuntime().availableProcessors() - 1;

private static final VectorSpecies<Byte> SPECIES = ByteVector.SPECIES_PREFERRED;

public static void main(String[] args) throws Exception {
var tic = System.currentTimeMillis();
class SimpleStatistics {
int min, max, sum, count;
SimpleStatistics() {
Expand Down Expand Up @@ -160,7 +164,7 @@ public MemorySegment next() {
b:
{
for (int N = 0; N < 128; N++) {
var b = memorySegment.get(ValueLayout.OfByte.JAVA_BYTE, size);
var b = memorySegment.get(JAVA_BYTE, size);
size += 1;
if (b == '\n') {
break b;
Expand All @@ -187,17 +191,14 @@ class It implements Iterator<Line> {
int offset;
final int length;
final MemorySegment memorySegment;

final VectorSpecies<Byte> preferred;
final ByteOrder endian;

It(MemorySegment memorySegment) {
offset = 0;
preferred = ByteVector.SPECIES_PREFERRED;
endian = ByteOrder.nativeOrder();
this.memorySegment = memorySegment;
length = (int) memorySegment.byteSize();
assert '\n' == memorySegment.get(ValueLayout.OfByte.JAVA_BYTE, length - 1);
assert '\n' == memorySegment.get(JAVA_BYTE, length - 1);
}

@Override
Expand All @@ -208,78 +209,66 @@ public boolean hasNext() {
@Override
public Line next() {
int size;
var chunk = new byte[128];
b: {
/*
* Vectorization does not seem to bring anything interesting.
* This is a bit disappointing. What am I doing wrong?
*/
size = 0;

while (offset + preferred.length() <= length) {
while (offset+SPECIES.length() <= length) {
var vector = ByteVector.fromMemorySegment(
preferred, memorySegment,
offset + size, endian
SPECIES, memorySegment,
offset+size, endian
);
vector.intoArray(chunk, size);
var j = vector.eq((byte) '\n').firstTrue();
if (j < preferred.length()) {
if (j < SPECIES.length()) {
assert j >= 0;
size += j;
assert memorySegment.get(ValueLayout.OfByte.JAVA_BYTE, offset + size) == '\n';
assert memorySegment.get(JAVA_BYTE, offset+size) == '\n';
break b;
} else {
assert j == preferred.length();
size += preferred.length();
assert j == SPECIES.length();
size += SPECIES.length();
}
}
{
byte b;
for (; size < 128; size++) {
b = memorySegment.get(ValueLayout.OfByte.JAVA_BYTE, offset + size);
chunk[size] = b;
b = memorySegment.get(JAVA_BYTE, offset+size);
if (b == '\n') break b;
}
assert false : "Lines are smaller than 128 bytes";
}
assert memorySegment.get(ValueLayout.OfByte.JAVA_BYTE, offset + size) == '\n';
assert memorySegment.get(JAVA_BYTE, offset+size) == '\n';
assert size < 128;
}

Name name;
int value;
{
int cursor = size - 1;
long cursor = offset+size - 1L;
{
value = chunk[cursor] - '0';
value += (chunk[cursor - 2] - '0') * 10;
cursor -= 3;
if (chunk[cursor] == '-') {
value = memorySegment.get(JAVA_BYTE, cursor) - '0';
value += (memorySegment.get(JAVA_BYTE, cursor-2L) - '0') * 10;
cursor -= 3L;
if (memorySegment.get(JAVA_BYTE, cursor) == '-') {
value *= -1;
cursor -= 1;
} else if (chunk[cursor] != ';') {
value += (chunk[cursor] - '0') * 100;
cursor -= 1;
if (chunk[cursor] == '-') {
cursor -= 1L;
} else if (memorySegment.get(JAVA_BYTE, cursor) != ';') {
value += (memorySegment.get(JAVA_BYTE, cursor) - '0') * 100;
cursor -= 1L;
if (memorySegment.get(JAVA_BYTE, cursor) == '-') {
value *= -1;
cursor -= 1;
cursor -= 1L;
}
}
}
var data = new byte[cursor];
System.arraycopy(chunk, 0, data, 0, data.length);
var data = memorySegment.asSlice(offset, cursor-offset).toArray(JAVA_BYTE);
//System.arraycopy(chunk, 0, data, 0, data.length);
assert ';' != data[data.length - 1];
name = new Name(data);
}
/*
var stack = new byte[size];
//memorySegment.asSlice(offset, size).asByteBuffer().get(stack);
System.arraycopy(chunk, 0, stack, 0, stack.length);
assert '\n' != stack[size - 1];
offset += size + 1;
return stack;
*/
offset += size + 1;
return new Line(name, value);
}
Expand Down Expand Up @@ -317,7 +306,6 @@ private static Map<Name, SimpleStatistics> call(MemorySegment memorySegment) thr
}
}


var ls = new Ls();

try(
Expand Down Expand Up @@ -388,6 +376,7 @@ private static Map<Name, SimpleStatistics> call(MemorySegment memorySegment) thr
joiner.add(STR. "\{ entry.getKey() }=\{ entry.getValue() }" );
}
System.out.println(joiner);
System.out.println(System.currentTimeMillis() - tic);
}
}
}

0 comments on commit 8387269

Please sign in to comment.