fine tuning performance further (#526)

* final comit

changing using mappedbytebuffer

changes before using unsafe address

using unsafe

* using graalvm,correct unsafe mem implementation

---------

Co-authored-by: Karthikeyans <karthikeyan.sn@zohocorp.com>
This commit is contained in:
karthikeyan97 2024-01-24 00:51:52 +05:30 committed by GitHub
parent 6c0949969a
commit 292edc629f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 59 deletions

View File

@ -15,15 +15,15 @@
# limitations under the License.
#
JAVA_OPTS="-Xms20480m -Xmx40960m "
JAVA_OPTS="-Xms10240m -Xmx40960m "
if [ -f target/CalculateAverage_karthikeyan97_image ]; then
#echo "Picking up existing native image 'target/CalculateAverage_karthikeyan97_image', delete the file to select JVM mode." 1>&2
target/CalculateAverage_karthikeyan97_image -Xms20480m -Xmx32768m
target/CalculateAverage_karthikeyan97_image -Xms10240m -Xmx40960m
else
JAVA_OPTS="--enable-preview"
#echo "Chosing to run the app in JVM mode as no native image was found, use prepare_karthikeyan97.sh to generate." 1>&2
java -Xms20480m -Xmx32768m --enable-preview --class-path target/average-1.0.0-SNAPSHOT.jar dev.morling.onebrc.CalculateAverage_karthikeyan97
java -Xms10240m -Xmx40960m --enable-preview --class-path target/average-1.0.0-SNAPSHOT.jar dev.morling.onebrc.CalculateAverage_karthikeyan97
fi

View File

@ -125,7 +125,7 @@ public class CalculateAverage_karthikeyan97 {
final long mappedAddress = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, raf.length(), Arena.global()).address();
long length = raf.length();
final long endAddress = mappedAddress + length - 1;
int cores = length > 1000 ? Runtime.getRuntime().availableProcessors() * 2 : 1;
int cores = length > 1000 ? Runtime.getRuntime().availableProcessors() : 1;
long boundary[][] = new long[cores][2];
long segments = length / (cores);
long before = -1;
@ -145,25 +145,21 @@ public class CalculateAverage_karthikeyan97 {
boundary[cores - 1][0] = before + 1;
boundary[cores - 1][1] = length - 1;
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Unsafe unsafe = (Unsafe) f.get(null);
int l3Size = (13 * 1024 * 1024);// unsafe.l3Size();
int l3Size = (12 * 1024 * 1024);// unsafe.l3Size();
System.out.println(new TreeMap((Arrays.stream(boundary).parallel().map(i -> {
FileInputStream fileInputStream = null;
try {
int seglen = (int) (i[1] - i[0] + 1);
HashMap<modifiedbytearray, MeasurementAggregator> resultmap = new HashMap<>(1000);
HashMap<modifiedbytearray, MeasurementAggregator> resultmap = new HashMap<>(4000);
long segstart = mappedAddress + i[0];
int bytesRemaining = seglen;
long num = 0;
int sign = 1;
boolean isNumber = false;
byte bi;
int sign = 1;
modifiedbytearray stationName = null;
int hascode = 5381;
// System.out.println("start:" + System.nanoTime() / 1000000);
while (bytesRemaining > 0) {
int bytesptr = 0;
// int bytesread = buffer.remaining() > l3Size ? l3Size : buffer.remaining();
@ -178,64 +174,59 @@ public class CalculateAverage_karthikeyan97 {
while (bytesptr < actualReadSize) {
bi = readArr[bytesptr++];// UNSAFE.getByte(segstart + bytesReading++);
if (!isNumber) {
if (bi >= 192) {
hascode = (hascode << 5) + hascode ^ bi;
}
else if (bi == 59) {
isNumber = true;
stationName = new modifiedbytearray(readArr, bbstart, bytesptr - 2, hascode & 0xFFFFFFFF);
bbstart = 0;
hascode = 5381;
if (bytesptr >= readSize) {
break;
}
}
else {
while (bi != 59) {
hascode = (hascode << 5) + hascode ^ bi;
bi = readArr[bytesptr++];
}
isNumber = true;
stationName = new modifiedbytearray(readArr, bbstart, bytesptr - 2, hascode & 0xFFFFFFFF);
bbstart = 0;
hascode = 5381;
}
else {
switch (bi) {
case 0x2E:
break;
case 0x2D:
while (bi != 10) {
if (bi == 0x2D) {
sign = -1;
break;
case 10:
hascode = 5381;
isNumber = false;
bbstart = bytesptr;
MeasurementAggregator agg = resultmap.get(stationName);
num *= sign;
if (agg == null) {
agg = new MeasurementAggregator();
agg.min = num;
agg.max = num;
agg.sum = (long) (num);
agg.count = 1;
resultmap.put(stationName, agg);
}
else {
if (agg.min >= num) {
agg.min = num;
}
if (agg.max <= num) {
agg.max = num;
}
agg.sum += (long) (num);
agg.count++;
}
num = 0;
sign = 1;
break;
default:
}
else if (bi != 0x2E) {
num = num * 10 + (bi - 0x30);
}
bi = readArr[bytesptr++];
}
hascode = 5381;
isNumber = false;
bbstart = bytesptr;
num *= sign;
MeasurementAggregator agg = resultmap.get(stationName);
if (agg == null) {
agg = new MeasurementAggregator();
agg.min = num;
agg.max = num;
agg.sum = (long) (num);
agg.count = 1;
resultmap.put(stationName, agg);
}
else {
if (agg.min >= num) {
agg.min = num;
}
if (agg.max <= num) {
agg.max = num;
}
agg.sum += (long) (num);
agg.count++;
}
num = 0;
sign = 1;
if (bytesptr >= readSize) {
break;
}
}
}
bytesRemaining -= bytesptr;
segstart += bytesptr;
}
// System.out.println("end:" + System.nanoTime() / 1000000);
/*
* while (bytesReading < (i[1] - i[0] + 1) && buffer.position() < buffer.limit()) {
* buffer.clear();