6.5 KiB
🚀 From Boilerplate to One-Liner: The simdjson Revolution
Conference Talk: JSON Parsing in Modern C++
📋 Talk Abstract
Discover how simdjson's new API combined with C++26 reflection transforms JSON parsing from a tedious, error-prone task into a single line of code. This talk showcases the dramatic evolution from manual parsing to automatic struct deserialization.
🎯 Key Talking Points
1. The Problem (2 minutes)
- JSON is everywhere: APIs, configs, data exchange
- C++ historically makes JSON parsing verbose
- Show other languages:
user = json.loads(data)in Python - "Why can't C++ be this simple?"
2. The Legacy Approach (5 minutes)
- Live demo:
github_legacy.cpp - Walk through the boilerplate:
// 😓 Manual field extraction user.login = std::string(doc["login"].get_string().value()); user.id = doc["id"].get_int64().value(); // 😰 Handle optional fields auto company_result = doc["company"]; if (!company_result.is_null()) { user.company = std::string(company_result.get_string().value()); } - Count the lines: ~30 lines just for parsing!
- Error-prone: typos, missing fields, type mismatches
3. The Magic Moment (3 minutes)
- "What if I told you it could be just ONE line?"
- Show
github_modern.cpp - The magic line:
GitHubUser user = simdjson::from(simdjson::padded_string(json_data)); - Audience reaction: 🤯
4. How It Works (5 minutes)
- C++26 static reflection
- Compile-time struct introspection
- simdjson generates parsing code automatically
- Zero runtime overhead - it's all compile-time!
5. Live Demo (5 minutes)
- Compile both examples
- Run them side-by-side
- Show identical output
- Highlight the code difference
- Add a new field to the struct - watch it "just work"
6. Deserialization Performance (3 minutes)
- "But is deserialization fast?"
- Live benchmark demonstration
- Both approaches achieve ~2.7 GB/s deserialization speed on real GitHub API data
- Reflection adds ZERO runtime overhead to deserialization
- "You get simplicity WITHOUT sacrificing speed!"
- Note: We're measuring JSON → struct deserialization performance
- Note: Performance scales with larger documents (up to 3+ GB/s)
7. The Future is Now (2 minutes)
- Bloomberg clang fork available today
- C++26 coming soon
- Start preparing your codebases
- simdjson ready for the future
💻 Live Coding Demo Script
Setup
# Show the two files
ls -la github_*.cpp
# Show line count difference
wc -l github_legacy.cpp github_modern.cpp
Demo 1: The Pain of Legacy
# Open github_legacy.cpp in editor
# Highlight the parse_github_user function
# Point out each manual field extraction
# "Look at all this code just to parse 7 fields!"
Demo 2: The Modern Magic
# Open github_modern.cpp
# Show the struct - "Just a plain struct!"
# Show the one-liner
# "That's it. That's the entire parsing code."
Demo 3: Compilation and Execution
Option 1: Use the demo script (Recommended)
# The script handles everything - building cpr, compiling, and running
./conference_demo.sh
# For the extended demo with serialization:
./conference_demo_serialization.sh
Option 2: Manual compilation
# Build cpr first (one-time setup)
mkdir -p ../deps && cd ../deps
git clone https://github.com/libcpr/cpr.git
cd cpr && git checkout 1.10.5
mkdir build && cd build
cmake .. -DCMAKE_CXX_COMPILER=clang++ -DCPR_USE_SYSTEM_CURL=ON \
-DBUILD_SHARED_LIBS=OFF -DCPR_ENABLE_SSL=OFF
make -j4
cd ../../../examples
# Compile legacy
clang++ -std=c++20 \
-I../deps/cpr/include -I../deps/cpr/build/cpr_generated_includes \
-I../include -I.. github_legacy.cpp ../singleheader/simdjson.cpp \
../deps/cpr/build/lib/libcpr.a -lcurl -pthread -o legacy_demo
# Compile modern (with reflection)
clang++ -std=c++26 -freflection -DSIMDJSON_STATIC_REFLECTION=1 \
-I../deps/cpr/include -I../deps/cpr/build/cpr_generated_includes \
-I../include -I.. github_modern.cpp ../singleheader/simdjson.cpp \
../deps/cpr/build/lib/libcpr.a -lcurl -pthread -o modern_demo
# Run both - they fetch real GitHub data!
./legacy_demo
./modern_demo
Demo 4: Adding a New Field (Crowd Pleaser!)
// Add to struct in both files:
std::string twitter_username;
// Legacy: Must add parsing code
else if (key == "twitter_username")
user.twitter_username = field.value().get_string().value();
// Modern: Nothing to add! It just works!
🎤 Speaker Notes
Opening Hook
"How many of you have written JSON parsing code in C++? Keep your hands up if you enjoyed it... Yeah, I thought so."
Transition to Modern
"But what if I told you that in 2024, with simdjson and C++26, parsing JSON can be as simple as Python?"
After showing the one-liner
"No, this isn't pseudocode. This is real, working C++ code. Let me prove it to you."
Addressing Skeptics
- "It's not magic, it's metaprogramming"
- "No runtime cost - it's all compile-time"
- "Yes, it handles errors properly"
- "Yes, it's production-ready"
Closing
"The future of C++ is here. It's fast, it's simple, and it's beautiful. Stop writing boilerplate. Start writing the code that matters."
📊 Slide Suggestions
Slide 1: Title
From 50 Lines to 1: The simdjson Revolution Your Name - Conference 2024
Slide 2: The Problem
# Python
user = json.loads(data)
# JavaScript
const user = JSON.parse(data);
# C++ ???
// 😭 50+ lines of boilerplate
Slide 3: The Solution
// C++26 with simdjson
GitHubUser user = simdjson::from(simdjson::padded_string(data));
Slide 4: Performance Graph
- Bar chart showing simdjson vs other parsers
- "Fast AND Simple"
Slide 5: Timeline
- 2018: simdjson introduced (fast but verbose)
- 2024: New API design
- 2024: C++26 reflection support
- Future: It's here!
Slide 6: Call to Action
- Try it today: github.com/simdjson/simdjson
- Bloomberg clang: github.com/bloomberg/clang-p2996
- Join the revolution!
🔥 Audience Engagement
Interactive Elements
- Live Poll: "How many lines of code for parsing JSON?"
- Challenge: "Spot the bug in this manual parsing code"
- Q&A Focus: Performance, error handling, compatibility
Memorable Moments
- The reveal of the one-liner
- Live compilation with reflection
- Adding a field without changing parsing code
- Performance numbers
Takeaway Message
"C++ doesn't have to be painful. With modern tools and modern standards, C++ can be as elegant as any language - and faster than all of them."