Pas2js optimizations

From Free Pascal wiki
Jump to navigationJump to search

Overview

This page describes existing optimizations for the pas2js transpiler as well as feature requests.

Generated JavaScript

Existing optimizations

  • -O-: Disable all optimizations
  • -O1 : Level 1 optimizations (quick and debugger friendly)
  • -OoEnumNumbers[-]: write enum value as number instead of name. Default in -O1.
  • -OoRemoveNotUsedPrivates[-]: Default is enabled
  • -OoRemoveNotUsedDeclarations[-]: Default enabled for programs with -Jc
  • omit unneeded brackets in associative operations (a||b)||(c||d), (a&&b)&&(c&&d), (a|b)|c, (a&b)&c, (a^b)^c, (a+b)+c, (a-b)-c, (a*b)*c

Feature requests

  • -O2 : Level 2 optimizations, slow and not debugger friendly
  • -O3 : Level 3 optimizations, slow and might break special code
  • move rtl.js functions to system.pp and let the WPO decide if they are needed
  • add $mod only if needed
  • add Self only if needed
  • use a number for small sets instead of JS objects
  • -O1: put set literals into constants, e.g. if enum in ['a'..'z'] then, then ['a'..'z'] should be stored in an autogenerated const
  • shortcut for test if a set is empty a=[] a<>[]
  • -O1: set operators on literals without temporary arrays, a in [b], [a]*b<>[]
  • -O1: combine multiple var a=0,b=0
  • ECMAScript6: use const for local const
  • -O1: init a local var with the first assignment
  • -O1: skip clone array for new array and arraysetlength
  • SetLength(scope.a,l) -> read scope only once, same for Include, Exclude, Inc, Dec, +=, -=, *=, /=
  • inline -Si
  • -O1/-O2: autoinline
  • -O1: replace constant expression with result, e.g. b:=1+2 -> b:=3
  • -O1: create const for complex const expressions
  • -O1: no function Result var when assigned only once, e.g. Result = 3; return Result; -> return 3;
  • -O1: pass array element by reference: when index is constant, use that directly (i.e. no temporary variable)
  • -O1: var/out argument: create accessor as var outside loops and combine multiple to one
  • -O2: case-of with 6+ elements as binary tree
  • -O2: insert local/unit vars for global type references:
    • at start of intf var $r1=null;
    • at end of impl: $r1=path;
  • -O2 removeemptyprocs
  • -O2 skip dead code If(false){...}
  • -O2 CSE
  • -O3 DFA

Compiler itself

The compiler was deliberately written

  • in a modular fashion. Each part has its own test suite and is used by other projects.
  • can be used in a library, i.e. no restart required to compile another program.
  • can be used without a filesystem, i.e. in a browser
  • threadsafe, i.e. the compilation can run in a thread. Compiling a program with multiple threads is planned.
  • easy maintenance has higher priority than high speed and low memory