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
November 29, 2012
Programming
1
1.2k
Faster than C?
Presentation given at the Prague JS Usergroup.
Felix Geisendörfer
November 29, 2012
Tweet
Share
More Decks by Felix Geisendörfer
See All by Felix Geisendörfer
tus.io - Resumable File Uploads (Lightning Talk)
felixge
2
710
Programming flying robots with JavaScript
felixge
2
890
Programming flying robots with JavaScript
felixge
0
530
Programming an AR Drone Firmware with JS (de)
felixge
1
570
Flying robots over a 10.000 mile distance with JavaScript.
felixge
0
430
Faster than C?
felixge
1
560
The power of node.js (with quadcopters)
felixge
0
450
Faster than C?
felixge
0
360
Faster than C? Parsing binary data in JavaScript.
felixge
3
3.7k
Other Decks in Programming
See All in Programming
LangChainでWebサイトの内容取得やGitHubソースコード取得
shukob
0
160
Jakarta EE meets AI
ivargrimstad
1
520
Debugging: All you need to know (for simultaneous interpreting)
jmatsu
2
830
Shinjuku.rb#95:心の技術書紹介
free_world21
1
110
watsonx.ai Dojo #2 生成AIを使ったアプリ開発入門編
oniak3ibm
PRO
0
220
Google Sign-inの移行から始めるCredential Manager活用
clockvoid
0
310
RAGの回答精度評価用のQAデータセットを生成AIに作らせた話
kurahara
0
250
Jakarta EE meets AI
ivargrimstad
0
390
KSPの導入・移行を前向きに検討しよう!
shxun6934
PRO
0
280
XStateでReactに秩序を与えたい
gizm000
0
730
Developer Joy == Developer Productivity (really!)
hollycummins
1
220
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
490
Featured
See All Featured
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
25
3.9k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
227
52k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
363
22k
Build your cross-platform service in a week with App Engine
jlugia
228
18k
Design by the Numbers
sachag
277
19k
Product Roadmaps are Hard
iamctodd
PRO
48
10k
Making the Leap to Tech Lead
cromwellryan
128
8.8k
Writing Fast Ruby
sferik
623
60k
Mobile First: as difficult as doing things right
swwweet
221
8.8k
Speed Design
sergeychernyshev
22
430
What the flash - Photography Introduction
edds
67
11k
Automating Front-end Workflow
addyosmani
1365
200k
Transcript
felixge Faster than C? Parsing binary data in JavaScript Felix
Geisendörfer
@felixge felixge Background 2005 - 2008 2008 - now 2009
- now
@felixge felixge The NodeCopter nodecopter.com
felixge This Talk
felixge JavaScript vs C
felixge Good vs Evil
felixge Good Parts vs Evil
felixge Bad Parts vs Evil
felixge Faster than C?
felixge No Sorry for the “title bait”
felixge As fast as C bindings For parsing binary protocols
/ database drivers
felixge The Story
felixge early 2010
felixge No MySQL module for node.js early 2010
felixge
felixge Pure JS / No C/C++
felixge Before Buffers became usable
felixge The Parser was using JavaScript Strings
felixge Node.js Trivia
felixge “ Buffers” used to be called “Blobs”
felixge For 3 min and 15 sec
felixge RIP Blobs ✞
felixge Sun Dec 13 08:39:20 2009 - Sun Dec 13
08:42:45 2009
felixge Anyway
felixge mysql can be done without libmysql
felixge
felixge No good deed goes unpunished
felixge Sir Isaac Newton
felixge 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.”
felixge 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.”
felixge <3 Github!
felixge
@felixge felixge 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
felixge 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
felixge Of course.
felixge libmysql = C
felixge my library = JavaScript
felixge C > JS, right?
felixge But V8!
felixge And Crankshaft!!
felixge Node.js !!!1!
felixge Was I living a lie?
felixge Kind of
felixge V8 / Node = Tools
felixge Performance is not a tool
felixge Performance is hard work & data analysis
felixge 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
felixge Third Law of Github
felixge
felixge 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
felixge Time to give up?
felixge NEVER!
felixge New Parser
@felixge felixge 0 2,000 4,000 6,000 mysql2 new−parser benchmark mbit
benchmark mysql2 new−parser
felixge Third law of Github?
felixge Endgame
felixge Last bottleneck: V8 / Creating JS Objects
felixge Also: MySQL Server saturated
felixge Anyway
felixge How to write fast JS
felixge Does not work
@felixge felixge Profiling • Good at telling you which functions
are slow • Bad at telling you how to fix it
felixge Taking performance advice from strangers
@felixge felixge Taking performance advice from strangers • Good for
ideas & inspiration • But useless when applied cargo-cult style
@felixge felixge for (var i = 0; i < array.length;
i++) { // do some work with array[i] }
@felixge felixge for (var i = 0, length = array.length;
i < length; i++) { // do some work with array[i] }
felixge What Does Work?
felixge Benchmark Driven Development
@felixge felixge Benchmark Driven Development • Similar to test driven
development • Use it when performance is an explicit design goal • Benchmark first > benchmark after !
felixge 1 function benchmark() { 2 // intentionally empty 3
}
felixge 1 while (true) { 2 var start = Date.now();
3 benchmark(); 4 var duration = Date.now() - start; 5 console.log(duration); 6 }
@felixge felixge Benchmark Driven Development • Next step: Implement a
tiny part of your function • Example: Parse headers of MySQL packets • Look at impact, tweak code, repeat
@felixge felixge Example Results • try...catch is ok • big
switch statement = bad • function calls = very cheap • buffering is ok
@felixge felixge Favorite Optimization: Loop unrolling with eval()’s good twin
new Function()
@felixge felixge 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 }
@felixge felixge 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 }
@felixge felixge How can we unroll this loop at runtime?
@felixge felixge 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);
PLEASE DO NOT REMEMBER THIS - DO YOUR OWN BENCHMARKS!!!
@felixge felixge Data Analysis
@felixge felixge 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 !!
@felixge felixge Recommended Tools • node benchmark.js | tee results.tsv
• R Programming language (with ggplot2) ! • Makefiles, Image Magick, Skitch
@felixge felixge Why?
@felixge felixge 0 2,000 4,000 6,000 mysql2 new−parser benchmark mbit
benchmark mysql2 new−parser
@felixge felixge • • • • • • • •
• • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • •• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 3,000 4,000 5,000 6,000 mysql2 new−parser benchmark mbit benchmark • • mysql2 new−parser
@felixge felixge • • • • • • • •
• • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • •• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 3,000 4,000 5,000 6,000 mysql2 new−parser benchmark mbit benchmark • • mysql2 new−parser Dafuq? Dafuq?
@felixge felixge 3,000 4,000 5,000 6,000 0 100 200 300
number mbit benchmark mysql2 new−parser
@felixge felixge 10 20 30 0 100 200 300 number
Heap Total (MB) benchmark mysql2 new−parser
@felixge felixge 5 10 15 0 100 200 300 number
Heap Used (MB) benchmark mysql2 new−parser
@felixge felixge tl;dr
@felixge felixge 1. Write a benchmark 2. Write/change a little
code 3. Collect data 4. Find problems 5. Goto 2
@felixge felixge Thank you Felix Geisendörfer
@felixge felixge github.com/felixge/faster-than-c All benchmarks, results and analysis scripts Felix
Geisendörfer