Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Faster than C?
Search
Felix Geisendörfer
October 18, 2012
Programming
0
380
Faster than C?
Presentation given at nodedublin.
Felix Geisendörfer
October 18, 2012
Tweet
Share
More Decks by Felix Geisendörfer
See All by Felix Geisendörfer
tus.io - Resumable File Uploads (Lightning Talk)
felixge
2
750
Programming flying robots with JavaScript
felixge
2
920
Programming flying robots with JavaScript
felixge
0
560
Programming an AR Drone Firmware with JS (de)
felixge
1
590
Faster than C?
felixge
1
1.2k
Flying robots over a 10.000 mile distance with JavaScript.
felixge
0
450
Faster than C?
felixge
1
600
The power of node.js (with quadcopters)
felixge
0
470
Faster than C? Parsing binary data in JavaScript.
felixge
3
3.8k
Other Decks in Programming
See All in Programming
GAEログのコスト削減
mot_techtalk
0
120
ファインディLT_ポケモン対戦の定量的分析
fufufukakaka
0
710
法律の脱レガシーに学ぶフロントエンド刷新
oguemon
5
740
Spring gRPC について / About Spring gRPC
mackey0225
0
220
コミュニティ駆動 AWS CDK ライブラリ「Open Constructs Library」 / community-cdk-library
gotok365
2
120
ソフトウェアエンジニアの成長
masuda220
PRO
10
1.1k
Grafana Cloudとソラカメ
devoc
0
170
Conform を推す - Advocating for Conform
mizoguchicoji
3
690
なぜイベント駆動が必要なのか - CQRS/ESで解く複雑系システムの課題 -
j5ik2o
10
3.6k
dbt Pythonモデルで実現するSnowflake活用術
trsnium
0
150
バックエンドのためのアプリ内課金入門 (サブスク編)
qnighy
8
1.8k
Kubernetes History Inspector(KHI)を触ってみた
bells17
0
230
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
9
440
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
VelocityConf: Rendering Performance Case Studies
addyosmani
328
24k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.2k
Why Our Code Smells
bkeepers
PRO
336
57k
How to train your dragon (web standard)
notwaldorf
91
5.8k
Six Lessons from altMBA
skipperchong
27
3.6k
Code Reviewing Like a Champion
maltzj
521
39k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.6k
Testing 201, or: Great Expectations
jmmastey
42
7.2k
Transcript
Faster than C? Parsing binary data in JavaScript Felix Geisendörfer
18. Oct, 2012 - NodeDublin
@felixge transloadit.com nodecopter.com
My Battle
JavaScript vs C
Good vs Evil
Good Parts vs Evil
Bad Parts vs Evil
Faster than C?
Sorry for the “title bait”
JavaScript vs C-Addons (for parsing binary protocols)
The Story
early 2010
No MySQL module for node.js early 2010
All we had was NoSQL Libraries early 2010
None
Pure JS / No C/C++
Before Buffers became usable
The Parser was using JavaScript Strings
Node.js Trivia
“ Buffers” used to be called “Blobs”
For 3 min and 15 sec
RIP Blobs ✞
Sun Dec 13 08:39:20 2009 - Sun Dec 13 08:42:45
2009
Anyway
mysql can be done without libmysql
None
No good deed goes unpunished
Sir Isaac Newton
Third Law of Motion
“When a first body exerts a force F1 on a
second body, the second body simultaneously exerts a force F2 = −F1 on the first body. This means that F1 and F2 are equal in magnitude and opposite in direction.”
Third Law of Github
“When a first person pushes a library L1 into a
remote repository, a second person simultaneously starts working on a second library L2 which will be equally awesome, but in a different way.”
<3 Github!
None
Benchmark • Parse ~180 MB / 100.000 rows of MySQL
result data • 5 Columns: id, title, text, created, updated • -> create 100k objects with 500k keys + 500k values
0 500 1,000 1,500 mysql−0.9.6 mysql−libmysqlclient−1.5.1 benchmark mbit benchmark mysql−0.9.6
mysql−libmysqlclient−1.5.1
Of course.
libmysql = C
my library = JavaScript
C > JS, right?
But V8!
And Crankshaft!!
Node.js !!!1!
Was I living a lie?
Kind of
V8 / Node = Tools
Performance is not a tool
Performance is hard work & data analysis
0 500 1,000 1,500 mysql−0.9.6 mysql−libmysqlclient−1.5.1mysql−2.0.0−alpha3 benchmark mbit benchmark mysql−0.9.6
mysql−libmysqlclient−1.5.1 mysql−2.0.0−alpha3
Third Law of Github
None
0 1,000 2,000 3,000 4,000 mysql−0.9.6 mysql−libmysqlclient−1.5.1 mysql−2.0.0−alpha3 mariadb−0.1.7 benchmark
mbit benchmark mysql−0.9.6 mysql−libmysqlclient−1.5.1 mysql−2.0.0−alpha3 mariadb−0.1.7
Time to give up?
NEVER!
New Parser
0 2,000 4,000 6,000 mysql2 new−parser benchmark mbit benchmark mysql2
new−parser
Third law of Github?
Endgame
Last bottleneck: Creating JS Objects
Also: MySQL Server saturated
Anyway
How to write fast JS
Does not work
Profiling • Good for spotting small functions with stupid algorithms
performing many iterations • Bad for complex functions with many primitive operations
Taking performance advice from strangers • Good for ideas &
inspiration • But useless when applied cargo-cult style
Does Work
Benchmark Driven Development
Benchmark Driven Development • Similar to test driven development •
Use it when performance is an explicit design goal • Benchmark first > benchmark after !
1 function benchmark() { 2 // intentionally empty 3 }
1 while (true) { 2 var start = Date.now(); 3
benchmark(); 4 var duration = Date.now() - start; 5 console.log(duration); 6 }
Benchmark Driven Development • Next step: Implement a tiny part
of your function • Example: Parse headers of MySQL packets • Look at impact, tweak code, repeat
Example Results • try...catch is ok • big switch statement
= bad • function calls = very cheap • buffering is ok
Favorite
1 function parseRow(columns, parser) { 2 var row = {};
3 for (var i = 0; i < columns.length; i++) { 4 row[columns[i].name] = parser.readColumnValue(); 5 } 6 return row; 7 }
Make it faster!
1 var code = 'return {\n'; 2 3 columns.forEach(function(column) {
4 code += '"' + column.name + '":' + 'parser.readColumnValue(),\n'; 5 }); 6 7 code += '};\n'; 8 9 var parseRow = new Function('columns', 'parser', code);
->
1 function parseRow(columns, parser) { 2 return { 3 id
: parser.readColumnValue(), 4 title : parser.readColumnValue(), 5 body : parser.readColumnValue(), 6 created : parser.readColumnValue(), 7 updated : parser.readColumnValue(), 8 }; 9 }
eval = awesome
eval = awesome
new Function = awesome
Data Analysis
Data analysis • Produce data points as tab separated values
• Add as many VM/OS metrics as you can get to every line • Do not mix data and analysis !!
Recommended Tools • node benchmark.js | tee results.tsv • R
Programming language (with ggplot2) ! • Makefiles, Image Magick, Skitch
Why?
0 2,000 4,000 6,000 mysql2 new−parser benchmark mbit benchmark mysql2
new−parser
• • • • • • • • • •
• • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • •• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 3,000 4,000 5,000 6,000 mysql2 new−parser benchmark mbit benchmark • • mysql2 new−parser
• • • • • • • • • •
• • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • •• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 3,000 4,000 5,000 6,000 mysql2 new−parser benchmark mbit benchmark • • mysql2 new−parser Dafuq? Dafuq?
3,000 4,000 5,000 6,000 0 100 200 300 number mbit
benchmark mysql2 new−parser
10 20 30 0 100 200 300 number Heap Total
(MB) benchmark mysql2 new−parser
5 10 15 0 100 200 300 number Heap Used
(MB) benchmark mysql2 new−parser
tl;dr
1. Write a benchmark 2. Write/change a little code 3.
Collect data 4. Find problems 5. Goto 2
Thank you
github.com/felixge/faster-than-c All benchmarks, results and analysis scripts Thank you !
None
None
nodecopter.com Dublin, Oct 20 (Saturday) Brighton, Nov 10 http://tinyurl.com/brcopter