Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Helping Your Compiler Help You

Quil
October 08, 2019

Helping Your Compiler Help You

A short overview of how monomorphism and polymorphism affects how compilers optimise JavaScript code.

Links:
- Turbolizer: https://github.com/v8/v8/tree/master/tools/turbolizer
- Franziska's "JavaScript Engines: How Do They Even?" talk: https://www.youtube.com/watch?v=p-iiEDtpy6I
- Matthias' "JavaScript Engine Fundamentals: Shapes & Inline Classes" blogpost: https://mathiasbynens.be/notes/shapes-ics
- Paul's "Debugging Node.js with Chrome DevTools" blogpost: https://medium.com/@paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27

Quil

October 08, 2019
Tweet

More Decks by Quil

Other Decks in Programming

Transcript

  1. When should you care? CPU-HEAVY Your bottleneck is the CPU

    cycles. OPTIMAL There’s no better algorithm complexity for the problem. UNCACHEABLE Caching is not a solution*.
  2. When should you care? a non-exhaustive list • Game development

    • Animation • Data processing • Generic libraries • etc
  3. function add(point1, point2): V1 = Project Field (point1, “x”); V2

    = Project Field (point2, “x”); V3 = Plus Operator (V1, V2); (...) V7 = New (Point2d, V3, V6) Return (V7)
  4. Project Field (point1, “x”): • Find the location of “x”

    within “point1” • Load the value from such memory location
  5. Plus Operator (V1, V2): • if both numbers, use float

    addition; • if both integers, use bigint addition; • if both strings, use string concatenation; • if number and object, convert RHS to number, retry; • (...)
  6. What are types? • number (64-bit floating points) • bigint

    (arbitrary-precision integers) • boolean • string • null / undefined • function • object
  7. Programs as graphs (with types) function add(p1, p2) (object, object)

    -> object New Point2d ($0, $1) (object) p1.x (any) p2.x (any) p1.y (any) p2.y (any) + (string | number) + (string | number)
  8. Programs as graphs (with types) function add(p1, p2) (Point2d, Point2d)

    -> Point2d New Point2d ($0, $1) (Point2d) p1.x (number) p2.x (number) p1.y (number) p2.y (number) float + (number) float + (number)
  9. Programs as graphs (with types) function add(p1, p2) (Point2d, Point2d)

    -> Point2d p1.x (number) p2.x (number) p1.y (number) p2.y (number) float + (number) float + (number) { x: 10, y: 15, z: 0 }
  10. Programs as graphs (with types) function add(p1, p2) (Point2d, Point2d)

    -> Point2d p1.x (number) p2.x (number) p1.y (number) p2.y (number) float + (number) float + (number) { x: 10, y: 15, z: 0 }
  11. Programs as graphs (with types) function add(p1, p2) add(p1, p2)

    (Point2d, Point2d) add(p1, p2) ({x,y,z}, {x,y,z}) { x: 10, y: 15, z: 0 }
  12. Don’t worry about cold paths. Use your time efficiently by

    focusing on hot and cpu-heavy operations.
  13. TOOLS YOU CAN USE • Your browser’s CPU profiler •

    node --inspect / --inspect-brk • node --trace-* • Turbolizer
  14. ADDITIONAL MATERIAL JavaScript Engines - How do They Even? JS

    Engine Fundamentals: Shapes and Inline Classes Debugging Node.js with Chrome DevTools