Awesome
1️⃣🐝🏎️ The One Billion Row Challenge
Status Feb 4: The final leaderboards have been published. Congrats to all the winners, and a big thank you to everyone participating in this challenge as well as to everyone helping to organize it!
Status Feb 3: All entries have been evaluated and I am in the process of finalizing the leaderboards.
Status Feb 1: The challenge has been closed for new submissions. No new pull requests for adding submissions are accepted at this time. Pending PRs will be evaluated over the next few days.
Status Jan 31: The challenge will close today at midnight UTC.
Status Jan 12: As there has been such a large number of entries to this challenge so far (100+), and this is becoming hard to manage, please only create new submissions if you expect them to run in 10 seconds or less on the evaluation machine.
Status Jan 1: This challenge is open for submissions!
Sponsorship
A big thank you to my employer Decodable for funding the evaluation environment and supporting this challenge!
The One Billion Row Challenge (1BRC) is a fun exploration of how far modern Java can be pushed for aggregating one billion rows from a text file. Grab all your (virtual) threads, reach out to SIMD, optimize your GC, or pull any other trick, and create the fastest implementation for solving this task!
<img src="1brc.png" alt="1BRC" style="display: block; margin-left: auto; margin-right: auto; margin-bottom:1em; width: 50%;">The text file contains temperature values for a range of weather stations.
Each row is one measurement in the format <string: station name>;<double: measurement>
, with the measurement value having exactly one fractional digit.
The following shows ten rows as an example:
Hamburg;12.0
Bulawayo;8.9
Palembang;38.8
St. John's;15.2
Cracow;12.6
Bridgetown;26.9
Istanbul;6.2
Roseau;34.4
Conakry;31.2
Istanbul;23.0
The task is to write a Java program which reads the file, calculates the min, mean, and max temperature value per weather station, and emits the results on stdout like this
(i.e. sorted alphabetically by station name, and the result values per station in the format <min>/<mean>/<max>
, rounded to one fractional digit):
{Abha=-23.0/18.0/59.2, Abidjan=-16.2/26.0/67.3, Abéché=-10.0/29.4/69.0, Accra=-10.1/26.4/66.4, Addis Ababa=-23.7/16.0/67.0, Adelaide=-27.8/17.3/58.5, ...}
Submit your implementation by Jan 31 2024 and become part of the leaderboard!
Results
These are the results from running all entries into the challenge on eight cores of a Hetzner AX161 dedicated server (32 core AMD EPYC™ 7502P (Zen2), 128 GB RAM).
Note that I am not super-scientific in the way I'm running the contenders (see Evaluating Results for the details). This is not a high-fidelity micro-benchmark and there can be variations of up to +-3% between runs. So don't be too hung up on the exact ordering of your entry compared to others in close proximity. The primary purpose of this challenge is to learn something new, have fun along the way, and inspire others to do the same. The leaderboard is only means to an end for achieving this goal. If you observe drastically different results though, please open an issue.
See Entering the Challenge for instructions how to enter the challenge with your own implementation. The Show & Tell features a wide range of 1BRC entries built using other languages, databases, and tools.
Bonus Results
This section lists results from running the fastest N entries with different configurations. As entries have been optimized towards the specific conditions of the original challenge description and set-up (such as size of the key set), challenge entries may perform very differently across different configurations. These bonus results are provided here for informational purposes only. For the 1BRC challenge, only the results in the previous section are of importance.
32 Cores / 64 Threads
For officially evaluating entries into the challenge, each contender is run on eight cores of the evaluation machine (AMD EPYC™ 7502P). Here are the results from running the top 50 entries (as of commit e1fb378a, Feb 2) on all 32 cores / 64 threads (i.e. SMT is enabled) of the machine:
# | Result (m:s.ms) | Implementation | JDK | Submitter | Notes |
---|---|---|---|---|---|
1 | 00:00.323 | link | 21.0.2-graal | Jaromir Hamala | GraalVM native binary, uses Unsafe |
2 | 00:00.326 | link | 21.0.2-graal | Thomas Wuerthinger, Quan Anh Mai, Alfonso² Peterssen | GraalVM native binary, uses Unsafe |
3 | 00:00.349 | link | 21.0.2-graal | Artsiom Korzun | GraalVM native binary, uses Unsafe |
00:00.351 | link | 21.0.2-graal | Van Phu DO | GraalVM native binary, uses Unsafe | |
00:00.389 | link | 21.0.2-graal | Stephen Von Worley | GraalVM native binary, uses Unsafe | |
00:00.408 | link | 21.0.2-graal | Yavuz Tas | GraalVM native binary, uses Unsafe | |
00:00.415 | link | 21.0.2-graal | Roy van Rijn | GraalVM native binary, uses Unsafe | |
00:00.499 | link | 21.0.2-graal | Marko Topolnik | GraalVM native binary, uses Unsafe | |
00:00.602 | link | 21.0.1-graal | Roman Musin | GraalVM native binary, uses Unsafe | |
00:00.623 | link | 21.0.1-open | gonix | uses Unsafe | |
00:00.710 | link | 21.0.2-graal | Jamal Mulla | GraalVM native binary, uses Unsafe | |
00:00.727 | link | 21.0.2-graal | tivrfoa | GraalVM native binary, uses Unsafe | |
00:00.774 | link | 21.0.1-open | Serkan ÖZAL | uses Unsafe | |
00:00.788 | link | 21.0.2-graal | Elliot Barlas | GraalVM native binary, uses Unsafe | |
00:00.832 | link | 21.0.2-graal | zerninv | GraalVM native binary, uses Unsafe | |
00:00.840 | link | 21.0.1-open | gonix | ||
00:00.857 | link | 21.0.2-graal | Jaime Polidura | GraalVM native binary, uses Unsafe | |
00:00.880 | link | 21.0.2-graal | John Ziamos | GraalVM native binary, uses Unsafe | |
00:00.939 | link | 21.0.1-open | Aleksey Shipilëv | ||
00:01.026 | link | 21.0.1-open | JesseVanRooy | uses Unsafe | |
00:01.118 | link | 21.0.2-graal | Jonathan Wright | GraalVM native binary | |
00:01.140 | link | 21.0.2-graal | Arman Sharif | GraalVM native binary, uses Unsafe | |
00:01.143 | link | 21.0.1-open | Cliff Click | uses Unsafe | |
00:01.169 | link | 21.0.2-open | Yevhenii Melnyk | ||
00:01.188 | link | 21.0.1-graal | Subrahmanyam | uses Unsafe | |
00:01.193 | link | 21.0.1-open | Florin Blanaru | uses Unsafe | |
00:01.234 | link | 21.0.1-open | Olivier Bourgain | uses Unsafe | |
00:01.242 | link | 21.0.1-open | Quan Anh Mai | uses Unsafe | |
00:01.252 | link | 21.0.1-open | Jin Cong Ho | uses Unsafe | |
00:01.267 | link | 22.ea.32-open | Li Lin | uses Unsafe | |
00:01.363 | link | 21.0.2-tem | Peter Levart | ||
00:01.380 | link | 21.0.1-graal | Jason Nochlin | ||
00:01.391 | link | 21.0.1-open | Quan Anh Mai | ||
00:01.439 | link | 21.0.1-open | Arjen Wisse | ||
00:01.446 | link | 21.0.1-open | Dr Ian Preston | ||
00:01.504 | link | 21.0.1-open | Jamie Stansfield | ||
00:01.514 | link | 21.0.1-graal | Subrahmanyam | ||
00:01.516 | link | 21.0.1-graal | Vaidhy Mayilrangam | uses Unsafe | |
00:01.586 | link | 21.0.1-open | yourwass | uses Unsafe | |
00:01.647 | link | 21.0.2-graal | Diego Parra | ||
00:01.694 | link | 21.0.1-open | Parker Timmins | ||
00:01.694 | link | 21.0.1-graal | Charlie Evans | uses Unsafe | |
00:01.702 | link | 21.0.1-graal | Sam Pullara | ||
00:01.733 | link | java | Sumit Chaudhary | uses Unsafe | |
00:01.742 | link | 21.0.1-open | unbounded | ||
00:02.241 | link | 21.0.1-graal | Stefan Sprenger | uses Unsafe | |
00:02.294 | link | 21.0.1-open | Giovanni Cuccu | ||
00:02.990 | link | 21.0.1-graal | Panagiotis Drakatos | GraalVM native binary | |
00:03.205 | link | 21.0.1-open | Juan Parera | ||
00:10.929 | link | 21.0.1-open | Roman Stoffel |
10K Key Set
The 1BRC challenge data set contains 413 distinct weather stations, whereas the rules allow for 10,000 different station names to occur. Here are the results from running the top 40 entries (as of commit e1fb378a, Feb 2) against 1,000,000,000 measurement values across 10K stations (created via ./create_measurements3.sh 1000000000), using eight cores on the evaluation machine:
# | Result (m:s.ms) | Implementation | JDK | Submitter | Notes |
---|---|---|---|---|---|
1 | 00:02.957 | link | 21.0.2-graal | Artsiom Korzun | GraalVM native binary, uses Unsafe |
2 | 00:03.058 | link | 21.0.2-graal | Marko Topolnik | GraalVM native binary, uses Unsafe |
3 | 00:03.186 | link | 21.0.2-graal | Stephen Von Worley | GraalVM native binary, uses Unsafe |
00:03.998 | link | 21.0.2-graal | Roy van Rijn | GraalVM native binary, uses Unsafe | |
00:04.042 | link | 21.0.2-graal | Jaromir Hamala | GraalVM native binary, uses Unsafe | |
00:04.289 | link | 21.0.1-open | gonix | uses Unsafe | |
00:04.522 | link | 21.0.2-graal | tivrfoa | GraalVM native binary, uses Unsafe | |
00:04.653 | link | 21.0.2-graal | Jamal Mulla | GraalVM native binary, uses Unsafe | |
00:04.733 | link | 21.0.1-open | gonix | ||
00:04.836 | link | 21.0.1-graal | Subrahmanyam | uses Unsafe | |
00:04.870 | link | 21.0.2-graal | Thomas Wuerthinger, Quan Anh Mai, Alfonso² Peterssen | GraalVM native binary, uses Unsafe | |
00:05.240 | link | 21.0.2-graal | zerninv | GraalVM native binary, uses Unsafe | |
00:05.394 | link | 21.0.2-graal | Yavuz Tas | GraalVM native binary, uses Unsafe | |
00:05.906 | link | 21.0.2-graal | Elliot Barlas | GraalVM native binary, uses Unsafe | |
00:06.086 | link | 21.0.2-graal | Van Phu DO | GraalVM native binary, uses Unsafe | |
00:06.379 | link | 21.0.2-graal | John Ziamos | GraalVM native binary, uses Unsafe | |
00:07.113 | link | 21.0.2-open | Yevhenii Melnyk | ||
00:07.542 | link | 21.0.2-graal | Jonathan Wright | GraalVM native binary | |
00:07.889 | link | 21.0.1-open | Florin Blanaru | uses Unsafe | |
00:07.970 | link | 21.0.1-open | Cliff Click | uses Unsafe | |
00:08.857 | link | 21.0.1-open | Serkan ÖZAL | ||
00:09.333 | link | 21.0.1-open | yourwass | uses Unsafe | |
00:09.722 | link | 21.0.1-open | Aleksey Shipilëv | ||
00:09.777 | link | 21.0.1-graal | Vaidhy Mayilrangam | uses Unsafe | |
00:10.263 | link | 21.0.1-open | Quan Anh Mai | uses Unsafe | |
00:11.154 | link | 21.0.1-open | Parker Timmins | ||
00:13.175 | link | 21.0.1-open | Quan Anh Mai | ||
00:13.245 | link | 21.0.1-open | Dr Ian Preston | ||
00:13.377 | link | 21.0.1-open | Giovanni Cuccu | ||
00:13.761 | link | 21.0.1-open | Juan Parera | ||
00:14.441 | link | 21.0.2-tem | Peter Levart | ||
00:15.548 | link | 21.0.1-open | Jin Cong Ho | uses Unsafe | |
00:17.906 | link | 21.0.1-graal | Jason Nochlin | ||
00:18.770 | link | 22.ea.32-open | Li Lin | uses Unsafe | |
00:19.106 | link | 21.0.1-open | Roman Stoffel | ||
00:20.151 | link | 21.0.1-graal | Roman Musin | GraalVM native binary, uses Unsafe; seg-faults occassionally | |
00:22.953 | link | 21.0.2-graal | Jaime Polidura | GraalVM native binary, uses Unsafe | |
--- | |||||
DNF | link | 21.0.1-open | JesseVanRooy | Incorrect output | |
DNF | link | 21.0.1-graal | Subrahmanyam | Doesn't complete in 60 sec | |
DNF | link | 21.0.1-open | Arjen Wisse | Incorrect output |
Prerequisites
Java 21 must be installed on your system.
Running the Challenge
This repository contains two programs:
dev.morling.onebrc.CreateMeasurements
(invoked via create_measurements.sh): Creates the file measurements.txt in the root directory of this project with a configurable number of random measurement valuesdev.morling.onebrc.CalculateAverage
(invoked via calculate_average_baseline.sh): Calculates the average values for the file measurements.txt
Execute the following steps to run the challenge:
-
Build the project using Apache Maven:
./mvnw clean verify
-
Create the measurements file with 1B rows (just once):
./create_measurements.sh 1000000000
This will take a few minutes. Attention: the generated file has a size of approx. 12 GB, so make sure to have enough diskspace.
If you're running the challenge with a non-Java language, there's a non-authoritative Python script to generate the measurements file at
src/main/python/create_measurements.py
. The authoritative method for generating the measurements is the Java programdev.morling.onebrc.CreateMeasurements
. -
Calculate the average measurement values:
./calculate_average_baseline.sh
The provided naive example implementation uses the Java streams API for processing the file and completes the task in ~2 min on environment used for result evaluation. It serves as the base line for comparing your own implementation.
-
Optimize the heck out of it:
Adjust the
CalculateAverage
program to speed it up, in any way you see fit (just sticking to a few rules described below). Options include parallelizing the computation, using the (incubating) Vector API, memory-mapping different sections of the file concurrently, using AppCDS, GraalVM, CRaC, etc. for speeding up the application start-up, choosing and tuning the garbage collector, and much more.
Flamegraph/Profiling
A tip is that if you have jbang installed, you can get a flamegraph of your program by running async-profiler via ap-loader:
jbang --javaagent=ap-loader@jvm-profiling-tools/ap-loader=start,event=cpu,file=profile.html -m dev.morling.onebrc.CalculateAverage_yourname target/average-1.0.0-SNAPSHOT.jar
or directly on the .java file:
jbang --javaagent=ap-loader@jvm-profiling-tools/ap-loader=start,event=cpu,file=profile.html src/main/java/dev/morling/onebrc/CalculateAverage_yourname
When you run this, it will generate a flamegraph in profile.html. You can then open this in a browser and see where your program is spending its time.
Rules and limits
- Any of these Java distributions may be used:
- Any builds provided by SDKMan
- Early access builds available on openjdk.net may be used (including EA builds for OpenJDK projects like Valhalla)
- Builds on builds.shipilev.net If you want to use a build not available via these channels, reach out to discuss whether it can be considered.
- No external library dependencies may be used
- Implementations must be provided as a single source file
- The computation must happen at application runtime, i.e. you cannot process the measurements file at build time (for instance, when using GraalVM) and just bake the result into the binary
- Input value ranges are as follows:
- Station name: non null UTF-8 string of min length 1 character and max length 100 bytes, containing neither
;
nor\n
characters. (i.e. this could be 100 one-byte characters, or 50 two-byte characters, etc.) - Temperature value: non null double between -99.9 (inclusive) and 99.9 (inclusive), always with one fractional digit
- Station name: non null UTF-8 string of min length 1 character and max length 100 bytes, containing neither
- There is a maximum of 10,000 unique station names
- Line endings in the file are
\n
characters on all platforms - Implementations must not rely on specifics of a given data set, e.g. any valid station name as per the constraints above and any data distribution (number of measurements per station) must be supported
- The rounding of output values must be done using the semantics of IEEE 754 rounding-direction "roundTowardPositive"
Entering the Challenge
To submit your own implementation to 1BRC, follow these steps:
- Create a fork of the onebrc GitHub repository.
- Run
./create_fork.sh <your_GH_user>
to copy the baseline implementation to your personal files, or do this manually:- Create a copy of CalculateAverage_baseline.java, named CalculateAverage_<your_GH_user>.java, e.g. CalculateAverage_doloreswilson.java.
- Create a copy of calculate_average_baseline.sh, named calculate_average_<your_GH_user>.sh, e.g. calculate_average_doloreswilson.sh.
- Adjust that script so that it references your implementation class name. If needed, provide any JVM arguments via the
JAVA_OPTS
variable in that script. Make sure that script does not write anything to standard output other than calculation results. - (Optional) OpenJDK 21 is used by default. If a custom JDK build is required, create a copy of prepare_baseline.sh, named prepare_<your_GH_user>.sh, e.g. prepare_doloreswilson.sh. Include the SDKMAN command
sdk use java [version]
in the your prepare script. - (Optional) If you'd like to use native binaries (GraalVM), add all the required build logic to your prepare_<your_GH_user>.sh script.
- Make that implementation fast. Really fast.
- Run the test suite by executing /test.sh <your_GH_user>; if any differences are reported, fix them before submitting your implementation.
- Create a pull request against the upstream repository, clearly stating
- The name of your implementation class.
- The execution time of the program on your system and specs of the same (CPU, number of cores, RAM). This is for informative purposes only, the official runtime will be determined as described below.
- I will run the program and determine its performance as described in the next section, and enter the result to the scoreboard.
Note: I reserve the right to not evaluate specific submissions if I feel doubtful about the implementation (I.e. I won't run your Bitcoin miner ;).
If you'd like to discuss any potential ideas for implementing 1BRC with the community, you can use the GitHub Discussions of this repository. Please keep it friendly and civil.
The challenge runs until Jan 31 2024. Any submissions (i.e. pull requests) created after Jan 31 2024 23:59 UTC will not be considered.
Evaluating Results
Results are determined by running the program on a Hetzner AX161 dedicated server (32 core AMD EPYC™ 7502P (Zen2), 128 GB RAM).
Programs are run from a RAM disk (i.o. the IO overhead for loading the file from disk is not relevant), using 8 cores of the machine.
Each contender must pass the 1BRC test suite (/test.sh).
The hyperfine
program is used for measuring execution times of the launch scripts of all entries, i.e. end-to-end times are measured.
Each contender is run five times in a row.
The slowest and the fastest runs are discarded.
The mean value of the remaining three runs is the result for that contender and will be added to the results table above.
The exact same measurements.txt file is used for evaluating all contenders.
See the script evaluate.sh for the exact implementation of the evaluation steps.
Prize
If you enter this challenge, you may learn something new, get to inspire others, and take pride in seeing your name listed in the scoreboard above. Rumor has it that the winner may receive a unique 1️⃣🐝🏎️ t-shirt, too!
FAQ
Q: Can I use Kotlin or other JVM languages other than Java?
A: No, this challenge is focussed on Java only. Feel free to inofficially share implementations significantly outperforming any listed results, though.
Q: Can I use non-JVM languages and/or tools?
A: No, this challenge is focussed on Java only. Feel free to inofficially share interesting implementations and results though. For instance it would be interesting to see how DuckDB fares with this task.
Q: I've got an implementation—but it's not in Java. Can I share it somewhere?
A: Whilst non-Java solutions cannot be formally submitted to the challenge, you are welcome to share them over in the Show and tell GitHub discussion area.
Q: Can I use JNI?
A: Submissions must be completely implemented in Java, i.e. you cannot write JNI glue code in C/C++. You could use AOT compilation of Java code via GraalVM though, either by AOT-compiling the entire application, or by creating a native library (see here.
Q: What is the encoding of the measurements.txt file?
A: The file is encoded with UTF-8.
Q: Can I make assumptions on the names of the weather stations showing up in the data set?
A: No, while only a fixed set of station names is used by the data set generator, any solution should work with arbitrary UTF-8 station names
(for the sake of simplicity, names are guaranteed to contain no ;
or \n
characters).
Q: Can I copy code from other submissions?
A: Yes, you can. The primary focus of the challenge is about learning something new, rather than "winning". When you do so, please give credit to the relevant source submissions. Please don't re-submit other entries with no or only trivial improvements.
Q: Which operating system is used for evaluation?
A: Fedora 39.
Q: My solution runs in 2 sec on my machine. Am I the fastest 1BRC-er in the world?
A: Probably not :) 1BRC results are reported in wallclock time, thus results of different implementations are only comparable when obtained on the same machine. If for instance an implementation is faster on a 32 core workstation than on the 8 core evaluation instance, this doesn't allow for any conclusions. When sharing 1BRC results, you should also always share the result of running the baseline implementation on the same hardware.
Q: Why 1️⃣🐝🏎️ ?
A: It's the abbreviation of the project name: One Billion Row Challenge.
1BRC on the Web
A list of external resources such as blog posts and videos, discussing 1BRC and specific implementations:
- The One Billion Row Challenge Shows That Java Can Process a One Billion Rows File in Two Seconds , by Olimpiu Pop (interview)
- Cliff Click discussing his 1BRC solution on the Coffee Compiler Club (video)
- 1️⃣🐝🏎️🦆 (1BRC in SQL with DuckDB), by Robin Moffatt (blog post)
- 1 billion rows challenge in PostgreSQL and ClickHouse, by Francesco Tisiot (blog post)
- The One Billion Row Challenge with Snowflake, by Sean Falconer (blog post)
- One billion row challenge using base R, by David Schoch (blog post)
- 1 Billion Row Challenge with Apache Pinot, by Hubert Dulay (blog post)
- One Billion Row Challenge In C, by Danny Van Kooten (blog post)
- One Billion Row Challenge in Racket, by Bogdan Popa (blog post)
- The One Billion Row Challenge - .NET Edition, by Frank A. Krueger (podcast)
- One Billion Row Challenge, by Ragnar Groot Koerkamp (blog post)
- ClickHouse and The One Billion Row Challenge, by Dale McDiarmid (blog post)
- One Billion Row Challenge & Azure Data Explorer, by Niels Berglund (blog post)
- One Billion Row Challenge - view from sidelines, by Leo Chashnikov (blog post)
- 1 billion row challenge in SQL and Oracle Database, by Gerald Venzl (blog post)
- One Billion Row Challenge: Learned So Far, by Roman Stoffel (blog post)
- One Billion Row Challenge in Racket, by Bogdan Popa (blog post)
- The 1 Billion row challenge with Singlestore, by Anna Semjen (blog post)
- 1BRC in .NET among fastest on Linux: My Optimization Journey, by Victor Baybekov (blog post)
- One Billion Rows – Gerald’s Challenge, by Connor McDonald (blog post)
- Reading a file insanely fast in Java, by Romain Manni-Bucau (blog post)
- #1BRC Timeline, by tivrfoa (blog post)
- 1BRC - What a Journey, by Marius Staicu (blog post)
- One Billion Rows Challenge in Golang, by Shraddha Agrawal (blog post)
- The Billion Row Challenge (1BRC) - Step-by-step from 71s to 1.7s by Marko Topolnik (blog post)
- Entering The One Billion Row Challenge With GitHub Copilot by Antonio Goncalves (blog post)
- DataFrame and The One Billion Row Challenge--How to use a Java DataFrame to save developer time, produce readable code, and not win any prizes by Vladimir Zakharov (blog post)
License
This code base is available under the Apache License, version 2.
Code of Conduct
Be excellent to each other! More than winning, the purpose of this challenge is to have fun and learn something new.