1BRC – Nerd Sniping the Java Community by Gunnar Morling

ScyllaDB 648 views 44 slides Oct 15, 2024
Slide 1
Slide 1 of 44
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44

About This Presentation

Gunnar Morling dives into the tricks that the fastest 1BRC solutions used to process the challenge’s 13 GB input file within less than 2 secs -- including parallelization and efficient memory access, optimized parsing routines using SIMD and SWAR, and custom map implementations.


Slide Content

A ScyllaDB Community
1BRC – Nerd Sniping
the Java Community
Gunnar Morling
Senior Staff Software Engineer at Decodable

#1BRC | @gunnarmorling
The Goals
Learn something
new
Have some fun
along the way
Inspire others to
do the same

is slow?!

#1BRC | @gunnarmorling
Is It Really a Problem, Though?

#1BRC | @gunnarmorling
The Rules
●Java-only; Version of your choice
●No dependencies
●10K stations, -99.9°C - +99.9°C
●Copying allowed
●Five runs, slowest and fastest
discarded

#1BRC | @gunnarmorling
Baseline

04:49.679

#1BRC | @gunnarmorling
The First Submission
Roy Van Rijn
??????

Parallelization
Ozzy Delaney https://flic.kr/p/pMsvzE (CC BY-SA 2.0 DEED)

#1BRC | @gunnarmorling
Parallelization–Chunking the File
We Have
CoresMany

#1BRC | @gunnarmorling
Parallelization
⌛ 71 sec.

#1BRC | @gunnarmorling
JEP 454
Foreign Function & Memory API
https://openjdk.org/jeps/454
Introduce an API by which Java programs can
interoperate with code and data outside of the Java
runtime. By efficiently invoking foreign functions
(i.e., code outside the JVM), and by safely accessing
foreign memory (i.e., memory not managed by the
JVM), the API enables Java programs to call native
libraries and process native data without the
brittleness and danger of JNI.

#1BRC | @gunnarmorling
Parallelization–Chunking the File

#1BRC | @gunnarmorling
1BRC Mythbusters

#1BRC | @gunnarmorling
1BRC—A Trivial Task?

Parsing
jjmusgrove https://flic.kr/p/2mJbA8S (CC BY 2.0 DEED)

#1BRC | @gunnarmorling
Parsing – Byte by Byte

#1BRC | @gunnarmorling
Parsing – Byte by Byte
⌛ 20 sec.

#1BRC | @gunnarmorling
Parsing – SIMD
Single Instruction, Multiple Data
https://speakerdeck.com/gunnarmorling/to-the-moon-and-beyond-with-java-17-apis

#1BRC | @gunnarmorling
Parsing – SWAR
SIMD Within a Register

#1BRC | @gunnarmorling
Parsing – SWAR
SIMD Within a Register

#1BRC | @gunnarmorling
Parsing
Branchless Programming
https://en.wikipedia.org/wiki/Branch_predictor

#1BRC | @gunnarmorling
Don’t Try This At Home!
Hacker’s Delight

Bookkeeping
R. D. Barry https://flic.kr/p/P5RWWR (CC BY-SA 2.0 DEED)

#1BRC | @gunnarmorling
Custom Maps – Linear Probing

#1BRC | @gunnarmorling
JIT and AOT Compiler

#1BRC | @gunnarmorling
Further Tricks & Techniques
●The “spawn trick”
●Unsafe
●Super-scalar execution
●Perfect value hashing

The Results
Clint Budd https://flic.kr/p/2m6F7hw (CC BY 2.0 DEED)

#1BRC | @gunnarmorling
Results

#1BRC | @gunnarmorling
Results – 32 Cores / 64 Threads

#1BRC | @gunnarmorling
The 1BRC Community
●TCK
●Environment
●Evaluation Scripts

#1BRC | @gunnarmorling
Should You Do Any of This?
It
Depends

#1BRC | @gunnarmorling
Getting the Basics Right Gets You Very Far
CalculateAverage_MeanderingProgrammer.java

#1BRC | @gunnarmorling
If You’re Building a Database
Or Want To Get This Coffee Mug…

#1BRC | @gunnarmorling
If You’re Building a Database
Or Want to Get Hired Into the GraalVM Team...

#1BRC | @gunnarmorling
1BRC—Distracting Developers Around the World ??????

#1BRC | @gunnarmorling
Show & Tell

is slow?

is slow?
rocks!

Thank you! Let’s connect.
Gunnar Morling
[email protected]
@gunnarmorling
https://www.morling.dev
Tags