107 points · by ksec · 2y ago · 73 comments
Based on the trends of "<something something> so we rewrote our entire production stack in rust!" I am surprised there is not already a comment here!
I'd also be interested in how fast JavaScript/V8 executes by comparison.
What you mention from Rust is this: https://github.com/RagnarGrootKoerkamp/1brc/ The author says himself that his solution is not compliant in the readme.
So far the top place is not clear. .NET with noahfalk, C/C++ from lehuyduc, dzaima and austindonisan require more tests. The last looks the fastest so far on the default dataset, but has issues.
Here is my blog post with details: https://hotforknowledge.com/2024/01/13/1brc-in-dotnet-among-...
CLR performance can be considered nondeterministic given it JITs and performs profile-guided-optimizations - unless you go with AOT, but there's still going to be an initialization delay, and differences in cold vs. warm. vs hot-start scenarios. In order to be fair to all languages/platforms/runtimes involved, how is that managed?
1: https://www.reddit.com/r/rust/comments/18ws370/optimizing_a_...
Why would you use Javascript for that?
Maybe 2 hours of coding, tops
https://rmoff.net/2024/01/03/1%EF%B8%8F%E2%83%A3%EF%B8%8F-1b...
https://github.com/gunnarmorling/1brc/discussions/categories...
I like this discussion because while the original is about "what can you do in 2024 stock java", I'm more interested in "what can you do if you use naive, obvious solutions". AWK does this in 6 minutes but, as pointed in this comment:
https://github.com/gunnarmorling/1brc/discussions/171#discus...
> It's either 6 minutes with 20 lines of AWK written in five minutes. Or 6 seconds in 300 lines of java written in five days. Classic "XKCD: Is It Worth the Time?".
I think it's also more readable than the awk for people who don't already know the language whilst being of a similar length, but that might be just me. More usefully you can get type checking, autocompletion, use any libraries you want and so on.
class Result(var lo: Double = Double.MAX_VALUE, var hi: Double = Double.MIN_VALUE, var total: Double = 0.0, var samples: Int = 0) {
val mean get() = total / samples
}
val results = HashMap<String, Result>()
Path("measurements.txt").useLines { it
.map { it.substringBefore(";") to it.substringAfter(";").toDouble() }
.forEach { (name, measurement) ->
with(results.getOrPut(name) { Result() }) {
lo = min(lo, measurement)
hi = max(hi, measurement)
total += measurement
samples++
}
}
}
results.toSortedMap().mapValues { (_, m) -> "%.1f/%.1f/%.1f".format(m.lo, m.mean, m.hi) }.also(::println)
You can do this sort of thing with jbang, although for some reason Gunnar's baseline is slower. Maybe I'm doing something wrong.I suspect it's because the baseline code is adding each line to a TreeMap before processing, effectively sorting 1B rows and then processing them. Your code reduces to the 413 unique stations and then sorts those.
I feel like Gunnar purposefully wrote the baseline with lots of room for improvement to encourage participation.
Your Kotlin code feels more imperative to me even though I'm used to that kind of things; I like that awk still feels more like describing stuff, but that's only me.
Path("measurements.txt")
.readLines()
.groupBy({ it.substringBefore(';') }, { it.substringAfter(';').toDouble() })
.forEach { (name, temps) ->
println("%s=%.1f/%.1f/%.1f".format(name, temps.min(), temps.average(), temps.max()))
}
I didn't do it that way because with one billion rows, it runs out of memory. The imperative version that mutates in place doesn't.You can probably write a functional version that also doesn't run out of memory using .groupingBy{}, because it all applies to lazy sequences. I just didn't bother working out how. The Java streams framework can do the same things and has the advantage that it can sometimes make it easy to parallelize.
Or use the awk one in production if you want. Nothing wrong with awk ;)
Not quite, it runs in 3 minutes written in java in 5 minutes. The official baseline is quite simple and runs in about ~3min https://github.com/gunnarmorling/1brc/blob/main/src/main/jav...
It's exactly this. Is it going to be run many times or just once or a few times? Is it going to be run in production where it impacts customers or runs up the AWS bill? If it's a throwaway script, the Linux terminal and some Awk is hard to beat. There are always tradeoffs.
¯\_(ツ)_/¯The current fastest safe version: https://github.com/gunnarmorling/1brc/blob/main/src/main/jav...
Reminds me of the code that Microsoft produced when they were trying to prove their new .NET Core is one of the fastest web runtimes - it was quite fast, but had nothing to do with the way C# code is written out there.
It probably won't make much difference on this problem for various reasons, but normally that's the case.
See Wikipedia:
> This is now the most common sense of the word in all varieties of English; it has long been established in American English and has since become common in Britain and other English-speaking countries as well.
https://en.wikipedia.org/wiki/Billion
There is both a long- and short- scale. Neither is more correct: https://en.wikipedia.org/wiki/Long_and_short_scales
And as much as I hate it, when communicating in public what matters is the commonly used definition of the word, even if it's different that what that word meant a few years ago.
That being said, there is no official word but two globally used scales.
https://en.wikipedia.org/wiki/Long_and_short_scales#Current_...
Who cares of Java can read 1bil "rows"? Why not describe it in terms of MB/s or something more reasonable?
Well, you should've - it's very clearly explained:
The text file has a simple structure with one measurement value per row:
Hamburg;12.0
Bulawayo;8.9
Palembang;38.8
St. John's;15.2
Cracow;12.6
...
The program should print out the min, mean, and max values per station, alphabetically ordered like so:
{
Abha=5.0/18.0/27.4,
Abidjan=15.7/26.0/34.1,
Abéché=12.1/29.4/35.6,
Accra=14.7/26.4/33.1,
Addis Ababa=2.1/16.0/24.3,
Adelaide=4.1/17.3/29.7,
...
}