shraddhaag / 1brc Goto Github PK
View Code? Open in Web Editor NEWSolution to One Billion Rows Challenge in Golang.
Home Page: https://www.bytesizego.com/blog/one-billion-row-challenge-go
Solution to One Billion Rows Challenge in Golang.
Home Page: https://www.bytesizego.com/blog/one-billion-row-challenge-go
the unit tests didn't pass for me on go 1.22amd. They are all failing heres the first two:
--- FAIL: TestMain (0.22s)
--- FAIL: TestMain/test_cases\measurements-1 (0.02s)
billionlinechallenge\main_test.go:18:
Error Trace: billionlinechallenge/main_test.go:18
Error: Not equal:
expected: "{Kunming=19.8/19.8/19.8}\r\n"
actual : "{Kunming=0.0/0.0/0.0}\n"
Diff:
--- Expected
+++ Actual
@@ -1,2 +1,2 @@
-{Kunming=19.8/19.8/19.8}
+{Kunming=0.0/0.0/0.0}
Test: TestMain/test_cases\measurements-1
--- FAIL: TestMain/test_cases\measurements-10 (0.01s)
billionlinechallenge\main_test.go:18:
Error Trace: billionlinechallenge/main_test.go:18
Error: Not equal:
expected: "{Adelaide=15.0/15.0/15.0, Cabo San Lucas=14.9/14.9/14.9, Dodoma=22.2/22.2/22.2, Halifax=12.9/12.9/12.9, Karachi=15.4/15.4/15.4, Pittsburgh=9.7/9.7/9.7, Ségou=25.7/25.7/25.7, Tauranga=38.2/38.2/38.2, Xi'an=24.2/24.2/24.2, Zagreb=12.2/12.2/12.2}\r\n"
actual : "{Adelaide=0.0/0.0/0.0, Cabo San Lucas=0.0/0.0/0.0, Dodoma=0.0/0.0/0.0, Halifax=0.0/0.0/0.0, Karachi=0.0/0.0/0.0, Pittsburgh=84.5/84.5/84.5, Ségou=0.0/0.0/0.0, Tauranga=0.0/0.0/0.0, Xi'an=0.0/0.0/0.0, Zagreb=0.0/0.0/0.0}\n"
Diff:
--- Expected
+++ Actual
@@ -1,2 +1,2 @@
-{Adelaide=15.0/15.0/15.0, Cabo San Lucas=14.9/14.9/14.9, Dodoma=22.2/22.2/22.2, Halifax=12.9/12.9/12.9, Karachi=15.4/15.4/15.4, Pittsburgh=9.7/9.7/9.7, Ségou=25.7/25.7/25.7, Tauranga=38.2/38.2/38.2, Xi'an=24.2/24.2/24.2, Zagreb=12.2/12.2/12.2}
+{Adelaide=0.0/0.0/0.0, Cabo San Lucas=0.0/0.0/0.0, Dodoma=0.0/0.0/0.0, Halifax=0.0/0.0/0.0, Karachi=0.0/0.0/0.0, Pittsburgh=84.5/84.5/84.5, Ségou=0.0/0.0/0.0, Tauranga=0.0/0.0/0.0, Xi'an=0.0/0.0/0.0, Zagreb=0.0/0.0/0.0}
I would not be surprised if you could speed up https://github.com/shraddhaag/1brc/blob/8513d5e70a1bfabbf46ab86a9cb6558bc9805154/main.go#L187C6-L187C22 by alternating calling bytes.IndexByte(buf[lastTokenEnd:], ';')
, followed by bytes.IndexByte(buf[lastTokenEnd:], '\n')
. bytes.IndexByte
function is highly optimized for fast searching in a byte slice. Since it is the inner loop it can have a big impact.
You could probably also skip the looking at the first 1 byte for station name and temperature (since it's at least one character). From my experience not inspecting bytes can be a very efficient way of speeding up parsing.
Also, I would convert from []byte
to string
only when I need to to avoid any kind of unicode handling.
It took a while to find where to get the data from: https://github.com/gunnarmorling/1brc?tab=readme-ov-file#running-the-challenge
I had a look at https://github.com/shraddhaag/1brc/blob/8513d5e70a1bfabbf46ab86a9cb6558bc9805154/main.go#L240C6-L240C29. Given that the temperatures are always within [-99.9, 99.9] and you know that it always has a tenth of accuracy, you can build up a string -> int
lookup table on startup of your application. The lookup table will hold 1999 items. That way, parsing will be looking up the string in the lookup table. My gut feeling is this will be faster.
Caveat: I have not looked at the raw data file, nor have a ran the program.
@shraddhaag Can you please help in running the program I'm trying to run go run main.go and also tried passing input file name it is not working can you please mention the commands.. Thank You..
Hi, first of all thank you for sharing your code!
I was quite impressed by the reading speed, and I think that I found a way to make this part a bit faster:
Lines 131 to 155 in 8513d5e
Currently each loop calls 2 times make([]byte)
and copy
. This could be reduced to 1, by storing the leftover as a length, instead of a byte slice:
buf := make([]byte, chunkSize)
leftover := 0
for {
n, err := file.Read(buf[leftover:]) // append to the leftover
if err != nil {
if errors.Is(err, io.EOF) {
break
}
panic(err)
}
toSend := buf[:leftover+n]
lastNewLineIndex := bytes.LastIndexByte(toSend, '\n')
buf = make([]byte, chunkSize) // prepare a new buffer for next read
leftover = copy(buf, toSend[lastNewLineIndex+1:])
chunkStream <- toSend[:lastNewLineIndex+1]
}
On a sample file, this code is about 10% faster than the current version
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.