Building Efficient Software

Node.js Madrid, 2018-02-20

Your Host Tonight


@pinchito
Desarrollador senior en LogTrust

What We Will Cover


  • Slow and Fast Software

  • Finding Hot Spots

  • Load Testing

  • Micro Profiling

  • Benchmarking

  • Conclusions

Slow and Fast Software

The day comes when some client tells you:


Your Software Is Slow


Well, it happens to everyone


Web developers

Backend developers

Chuck Norris


MY SOFTWARE IS NOT SLOW, COLONEL!
(BONES CRUNCHING)

But is it slow?


What is it doing?


Can it be made faster?


Can you distract the user?


Can you avoid doing some things?

What Is Your Software Doing...


...with all those CPU cycles?


Each second has ~4·109 cycles


Each toolchain has a max efficiency


Node.js: interpreter + compiler


Algorithmic efficiency

Premature Optimization


The real problem is that programmers

have spent far too much time

worrying about efficiency

in the wrong places and at the wrong times;

premature optimization is the root of all evil

(or at least most of it) in programming.


Donald Knuth, computer scientist


Fast Software


... is not written fast

Which is faster?


  • 2016: Lenovo X1 Carbon 4g (Windows)
  • 2016: Samsung Galaxy S7
  • 2002: Apple iMac G4
  • 1993: Mac Color Classic
  • 2017: Apple iPhone X
  • 1998: Nintendo Gameboy Color
  • 2017: Lenovo ThinkPad 13 (ChromeOS)
  • 2017: Apple iPad Pro 10.5"
  • 1977: Commodore Pet 4016
  • 2017: Apple iPad Pro 10.5" pencil
  • 1983: Apple 2e

Computer Latency

Source: Dan Luu

Finding Hot Spots

Pareto Rule


80% of the effects come

from 20% of the causes


Vilfredo Pareto, Italian Sociologist*


*Not known for his many catchy quotes

Pareto Principle


80% of the effects come


from 20% of the causes

Used in many engineering areas


Can be used recursively!


64% of the effects come
from 4% of the causes

Power Laws



... and the long tail

Use Sampling


It’s this simple: if you don’t sample,
you don’t scale.

If you think this is even a controversial statement,
you have never dealt with observability at scale OR you have done it wastefully and poorly.


Charity Majors, computer engineer

Load Testing

Design Load Tests


Bring the problem to the laboratory


Reproducible environment


Iterate faster

Load Test Demo

Micro Profiling

Nanoseconds




Grace Hopper, computer engineer

Profiler Granularity


Time to measure matters


Max resolution depends on measure


Measurement noise can kill you!

Microprofiler Demo

Benchmarking


Lies, damn lies

and benchmarks


There are three gradations of inveracity:

there are lies, there are damned lies

and there are statistics


Arthur James Balfour, UK Prime Minister

( probably)

Precision


Usually can get any precision desired


Your mileage may vary — wildly!


Handle with care

Iterate


Once the hot spot is replicated you can optimize it


It is usually harder than solving a regular bug


Work through the layers: interpreter, optimizer...

Benchmarking Demo

Conclusions

Don't Micro-optimize


Until you need to


And even then...


Don't micro-optimize blindly

Keep your target in sight

Persist


There are multiple obstacles on any path


Performance work requires perseverance


There are no magic bullets

Embrace Change


Results are likely to change


Every V8 version brings changes


Performance varies wildly across versions

Embrace Uncertainty


Religion is a culture of faith;
science is a culture of doubt.


— Richard Feynman?, physicist.


True Science teaches, above all,
to doubt and to be ignorant.


Miguel de Unamuno, philosopher.

Thanks!

@pinchito