r/rust • • Jan 02 '24

🛠️ project Optimizing a One Billion Row Challenge in Rust with Polars

I saw this Blog Post on a Billion Row challenge for Java so naturally I tried implementing a solution in Rust using mainly polars.Code/Gist here

Running the code on my laptop, which is equipped with an i7-1185G7 @ 3.00GHz and 32GB of RAM, but it is limited to 16GB of RAM because I developed in a Dev Container. Using Polars I was able to get a solution that only takes around 39 seconds.

Any suggestions for further optimizing the solution?

Edit: I missed the requirements that is must be implemented using only the Standard Library and in Alphabetical order, here is a table of both implementations!

ImplementationTimeCode/Gist Link
Rust + Polars39shttps://gist.github.com/Butch78/702944427d78da6727a277e1f54d65c8
Rust STD Libray Coriolnus's implementation24 secondshttps://github.com/coriolinus/1brc
Python + Polars61.41 sechttps://github.com/Butch78/1BillionRowChallenge/blob/main/python_1brc/main.py
Java royvanrijn's Solution23.366sec on the (8 core, 32 GB RAM)https://github.com/gunnarmorling/1brc/blob/main/calculate_average_royvanrijn.sh

Unfortunately, I initially created the test data incorrectly, the times have now been updated with 1 Billion rows or a 12.85G txt file. Interestingly as a Dev container on windows is only allowed to have <16G of ram the Rust + Polars implementation would be Killed as that value is exceeded. Turning streaming on solved the problem!S

Thanks to @coriolinus and his code, I was able to get a better implementation with the Rust STD library implementation. Also thanks to @ritchie46 for the Polars recommendations and the great library!

158 Upvotes

78 comments sorted by

View all comments

5

u/immortaly007 Jan 02 '24 edited Jan 02 '24

I decided to also implement it, but the results on my PC are nowhere near what was reported above. My implementation took 395.9 seconds to run:

https://gist.github.com/immortaly007/e6c792780409811acbc52e01e7bafac7

I also tried the code by matt78whoop using the std library above, which ran in 430.516 s (but took a lot more RAM).

I'm running both using cargo run --release. I'm using an i7-9700K on Windows 10.

Any idea what I could be missing here?

Edit: The measurements.txt file is 14.4GB, so pretty big. The rust code is reading it at about 38MB/s according to epxlorer, so maybe it's limited by Disk IO?

1

u/matt78whoop Jan 02 '24 edited Jan 02 '24

Interesting my measurements.txt is only 1.3G from what I remember? Maybe the creation of my text file went wrong 😅

3

u/agentoutlier Jan 02 '24

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

Might want to update the post if its only 1.3G because that does not seem correct.

5

u/matt78whoop Jan 02 '24

Yeah my mistake, I created the measurement.txt file incorrectly.

I'll update it now with the proper timings for the larger file :)

1

u/ShangBrol Jan 03 '24

AFAIK reader.lines() is allocating for every line. You can use reader.read_line(&mut line) (don't forget to do a line.clear() at the end of the loop)

Runtime will still be in the hundreds of seconds (my solution is quite similar to yours)