-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Entry for abhinav-upadhyay #696
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a110004
Add entry for GH user abhinav-upadhyay.
abhinav-upadhyay 3a1d351
Fix output
abhinav-upadhyay baca219
Remove extra calls to min and max
abhinav-upadhyay 10c68a1
Rename the script
abhinav-upadhyay 833ff01
Hanle hash collisions
abhinav-upadhyay 3429050
Fix the handling of hash collisions
abhinav-upadhyay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/sh | ||
# | ||
# 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. | ||
# | ||
|
||
# Uncomment below to use sdk | ||
# source "$HOME/.sdkman/bin/sdkman-init.sh" | ||
# sdk use java 21.0.1-graal 1>&2 | ||
|
||
JAVA_OPTS="--enable-preview" | ||
java $JAVA_OPTS --class-path target/average-1.0.0-SNAPSHOT.jar dev.morling.onebrc.CalculateAverage_abhinavupadhyay |
256 changes: 256 additions & 0 deletions
256
src/main/java/dev/morling/onebrc/CalculateAverage_abhinavupadhyay.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
/* | ||
* 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 sun.misc.Unsafe; | ||
|
||
import java.io.IOException; | ||
import java.lang.foreign.Arena; | ||
import java.lang.reflect.Field; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
public class CalculateAverage_abhinavupadhyay { | ||
|
||
private static final String FILE_NAME = "./measurements.txt"; | ||
|
||
private static Unsafe initUnsafe() { | ||
try { | ||
final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); | ||
theUnsafe.setAccessible(true); | ||
return (Unsafe) theUnsafe.get(Unsafe.class); | ||
} | ||
catch (NoSuchFieldException | IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private final static Unsafe UNSAFE = initUnsafe(); | ||
|
||
public static final long HASSEMI = 0x3B3B3B3B3B3B3B3BL; | ||
|
||
static long has0(long x) { | ||
return (x - 0x0101010101010101L) & (~x) & 0x8080808080808080L; | ||
} | ||
|
||
private static class Table { | ||
private static final int TABLE_SIZE = 1 << 21; // 0x8000; // collisions with table smaller than this. Need a better hash function | ||
private static final int TABLE_MASK = TABLE_SIZE - 1; | ||
Row[] table = new Row[TABLE_SIZE]; | ||
byte[] array = new byte[256]; | ||
|
||
public void put(long cityStartOffset, long cityLength, long nameHash, int temperature) { | ||
final long uhash = nameHash ^ (nameHash >> 29); | ||
// final long uhash = nameHash; | ||
int index = (int) uhash & TABLE_MASK; | ||
Row row = table[index]; | ||
if (row == null) { | ||
int i = 0; | ||
for (; i < cityLength - 1;) { | ||
array[i++] = UNSAFE.getByte(cityStartOffset++); | ||
array[i++] = UNSAFE.getByte(cityStartOffset++); | ||
} | ||
for (; i < cityLength; i++) { | ||
array[i] = UNSAFE.getByte(cityStartOffset++); | ||
} | ||
table[index] = new Row(new String(array, 0, i), temperature, temperature, 1, temperature, (int) uhash); | ||
return; | ||
} | ||
|
||
row.update(temperature); | ||
|
||
} | ||
|
||
} | ||
|
||
private static final class Row { | ||
private final String name; | ||
private int minTemp; | ||
private int maxTemp; | ||
private int count; | ||
private int sum; | ||
private final long hash; | ||
|
||
public Row(String name, int minTemp, int maxTemp, int count, int sum, long hash) { | ||
this.name = name; | ||
this.minTemp = minTemp; | ||
this.maxTemp = maxTemp; | ||
this.count = count; | ||
this.sum = sum; | ||
this.hash = hash; | ||
} | ||
|
||
void update(int temperature) { | ||
this.count++; | ||
this.sum += temperature; | ||
if (temperature < minTemp) { | ||
this.minTemp = temperature; | ||
return; | ||
} | ||
if (temperature > maxTemp) { | ||
this.maxTemp = temperature; | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%.1f/%.1f/%.1f", this.minTemp / 10.0, this.sum / (count * 10.0), maxTemp / 10.0); | ||
} | ||
|
||
public Row update(Row value) { | ||
this.minTemp = Integer.min(this.minTemp, value.minTemp); | ||
this.maxTemp = Integer.max(this.maxTemp, value.maxTemp); | ||
this.count += value.count; | ||
this.sum += value.sum; | ||
return this; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == this) | ||
return true; | ||
if (obj == null || obj.getClass() != this.getClass()) | ||
return false; | ||
return this.hash == ((Row) obj).hash; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return (int) this.hash; | ||
} | ||
|
||
private static int max(int a, int b) { | ||
a -= b; | ||
a &= (~a) >> 31; | ||
return a + b; | ||
} | ||
|
||
private static int min(int a, int b) { | ||
a -= b; | ||
a &= a >> 31; | ||
return a + b; | ||
} | ||
|
||
} | ||
|
||
private static Table readFile(long startAddress, long endAddress) { | ||
Table table = new Table(); | ||
long currentOffset = startAddress; | ||
long nameHash = 1; | ||
while (currentOffset < endAddress) { | ||
long cityStart = currentOffset; | ||
long word = UNSAFE.getLong(currentOffset); | ||
long hasSemi = has0(word ^ HASSEMI); | ||
while (hasSemi == 0) { | ||
currentOffset += 8; | ||
for (int i = 0; i < 8; i++) { | ||
byte b = (byte) ((word >> (i * 8)) & 0xff); | ||
nameHash = nameHash * 31 + b; | ||
nameHash += (nameHash << 10); | ||
nameHash ^= (nameHash >> 6); | ||
} | ||
word = UNSAFE.getLong(currentOffset); | ||
hasSemi = has0(word ^ HASSEMI); | ||
} | ||
int trailingZeros = Long.numberOfTrailingZeros(hasSemi); | ||
int semiColonIndex = trailingZeros >> 3; | ||
if (trailingZeros >= 8) { | ||
int nonZeroBits = 64 - trailingZeros; | ||
nameHash ^= ((word << nonZeroBits) >> nonZeroBits); | ||
nameHash += (nameHash << 10); | ||
nameHash ^= (nameHash >> 6); | ||
currentOffset += semiColonIndex; | ||
} | ||
long cityLength = currentOffset - cityStart; | ||
currentOffset++; // skip ; | ||
int temperature = 0; | ||
int scale = 1; | ||
int isNegative = 1; | ||
byte b = UNSAFE.getByte(currentOffset++); | ||
if (b == '-') { | ||
isNegative = -1; | ||
} | ||
else { | ||
temperature = b - '0'; | ||
scale = 10; | ||
} | ||
while ((b = UNSAFE.getByte(currentOffset++)) != '\n') { | ||
if (b == '.') { | ||
continue; | ||
} | ||
temperature = temperature * scale + b - '0'; | ||
scale = 10; | ||
} | ||
temperature *= isNegative; | ||
table.put(cityStart, cityLength, nameHash, temperature); | ||
nameHash = 1; | ||
} | ||
return table; | ||
|
||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
String filename = args.length > 0 ? args[0] : FILE_NAME; | ||
FileChannel fc = FileChannel.open(Paths.get(filename), StandardOpenOption.READ); | ||
final long fileSize = fc.size(); | ||
final long startAddress = fc.map(FileChannel.MapMode.READ_ONLY, 0, fileSize, Arena.global()).address(); | ||
final long endAddress = startAddress + fileSize; | ||
final long[][] segments = findSegments(startAddress, endAddress, fileSize, fileSize > 1024 * 1024 * 1024 ? 8 : 1); | ||
final List<Table> collect = Arrays.stream(segments).parallel().map(s -> readFile(s[0], s[1])).toList(); | ||
Map<String, Row> finalMap = new TreeMap<>(); | ||
for (final Table t : collect) { | ||
for (int j = 0; j < Table.TABLE_SIZE; j++) { | ||
final Row row = t.table[j]; | ||
if (row == null) { | ||
continue; | ||
} | ||
finalMap.compute(row.name, (_, v) -> v == null ? row : v.update(row)); | ||
} | ||
} | ||
System.out.println(finalMap); | ||
} | ||
|
||
private static long[][] findSegments(long startAddress, long endAddress, long size, int segmentCount) { | ||
if (segmentCount == 1) { | ||
return new long[][]{ { startAddress, endAddress } }; | ||
} | ||
long[][] segments = new long[segmentCount][2]; | ||
long segmentSize = size / segmentCount + 1; | ||
int i = 0; | ||
long currentOffset = startAddress; | ||
while (currentOffset < endAddress) { | ||
segments[i][0] = currentOffset; | ||
currentOffset += segmentSize; | ||
currentOffset = Math.min(currentOffset, endAddress); | ||
if (currentOffset >= endAddress) { | ||
segments[i][1] = endAddress; | ||
break; | ||
} | ||
while (UNSAFE.getByte(currentOffset) != '\n') { | ||
currentOffset++; | ||
// align to newline boundary | ||
} | ||
segments[i++][1] = currentOffset++; | ||
} | ||
return segments; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need to handle collisions, otherwise you'd aggregate the values from several stations with the same hash.